toy-example-setup.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. .. _tut_install_load_data:
  2. Toy example: Introduction and setup
  3. ===================================
  4. This page is a starting point of a series of tutorials that will help you get practical experience with FlexMeasures.
  5. Let's walk through an example from scratch! We'll ...
  6. - install FlexMeasures
  7. - create an account
  8. - load hourly prices
  9. What do you need? Your own computer, with one of two situations: either you have `Docker <https://www.docker.com/>`_ or your computer supports Python 3.8+, pip and PostgresDB. The former might be easier, see the installation step below. But you choose.
  10. Below are the ``flexmeasures`` CLI commands we'll run, and which we'll explain step by step. There are some other crucial steps for installation and setup, so this becomes a complete example from scratch, but this is the meat:
  11. .. code-block:: bash
  12. # setup an account with a user, assets for battery & solar and an energy market (ID 1)
  13. $ flexmeasures add toy-account
  14. # load prices to optimise the schedule against
  15. $ flexmeasures add beliefs --sensor 1 --source toy-user prices-tomorrow.csv --timezone Europe/Amsterdam
  16. Okay, let's get started!
  17. .. note:: You can copy the commands by hovering on the top right corner of code examples. You'll copy only the commands, not the output!
  18. Install Flexmeasures and the database
  19. ---------------------------------------
  20. .. tabs::
  21. .. tab:: Docker
  22. If `docker <https://www.docker.com/>`_ is running on your system, you're good to go. Otherwise, see `here <https://docs.docker.com/get-docker/>`_.
  23. We start by installing the FlexMeasures platform, and then use Docker to run a postgres database and tell FlexMeasures to create all tables.
  24. .. code-block:: bash
  25. $ docker pull lfenergy/flexmeasures:latest
  26. $ docker pull postgres
  27. $ docker network create flexmeasures_network
  28. .. note:: A tip on Linux/macOS ― You might have the ``docker`` command, but need `sudo` rights to execute it.
  29. ``alias docker='sudo docker'`` enables you to still run this tutorial.
  30. After running these commands, we can start the Postgres database and the FlexMeasures app with the following commands:
  31. .. code-block:: bash
  32. $ docker run --rm --name flexmeasures-tutorial-db -e POSTGRES_PASSWORD=fm-db-passwd -e POSTGRES_DB=flexmeasures-db -d --network=flexmeasures_network postgres:latest
  33. $ docker run --rm --name flexmeasures-tutorial-fm --env SQLALCHEMY_DATABASE_URI=postgresql://postgres:fm-db-passwd@flexmeasures-tutorial-db:5432/flexmeasures-db --env SECRET_KEY=notsecret --env FLEXMEASURES_ENV=development --env LOGGING_LEVEL=INFO -d --network=flexmeasures_network -p 5000:5000 lfenergy/flexmeasures
  34. When the app has started, the FlexMeasures UI should be available at http://localhost:5000 in your browser.
  35. .. include:: ../notes/macOS-docker-port-note.rst
  36. To establish the FlexMeasures database structure, execute:
  37. .. code-block:: bash
  38. $ docker exec flexmeasures-tutorial-fm bash -c "flexmeasures db upgrade"
  39. Now - what's *very important* to remember is this: The rest of this tutorial will happen *inside* the ``flexmeasures-tutorial-fm`` container! This is how you hop inside the container and run a terminal there:
  40. .. code-block:: bash
  41. $ docker exec -it flexmeasures-tutorial-fm bash
  42. To leave the container session, hold CTRL-D or type "exit".
  43. To stop the containers, you can type
  44. .. code-block:: bash
  45. $ docker stop flexmeasures-tutorial-db
  46. $ docker stop flexmeasures-tutorial-fm
  47. To start the containers again, do this (note that re-running the `docker run` commands above *deletes and re-creates* all data!):
  48. .. code-block:: bash
  49. $ docker start flexmeasures-tutorial-db
  50. $ docker start flexmeasures-tutorial-fm
  51. .. note:: Got docker-compose? You could run this tutorial with 5 containers :) ― Go to :ref:`docker-compose-tutorial`.
  52. .. tab:: On your PC
  53. This example is from scratch, so we'll assume you have nothing prepared but a (Unix) computer with Python (3.8+) and two well-known developer tools, `pip <https://pip.pypa.io>`_ and `postgres <https://www.postgresql.org/download/>`_.
  54. We'll create a database for FlexMeasures:
  55. .. code-block:: bash
  56. $ sudo -i -u postgres
  57. $ createdb -U postgres flexmeasures-db
  58. $ createuser --pwprompt -U postgres flexmeasures-user # enter your password, we'll use "fm-db-passwd"
  59. $ exit
  60. Then, we can install FlexMeasures itself, set some variables and tell FlexMeasures to create all tables:
  61. .. code-block:: bash
  62. $ pip install flexmeasures
  63. $ pip install highspy
  64. $ export SQLALCHEMY_DATABASE_URI="postgresql://flexmeasures-user:fm-db-passwd@localhost:5432/flexmeasures-db" SECRET_KEY=notsecret LOGGING_LEVEL="INFO" DEBUG=0
  65. $ export FLEXMEASURES_ENV="development"
  66. $ flexmeasures db upgrade
  67. .. note:: When installing with ``pip``, on some platforms problems might come up (e.g. macOS, Windows). One reason is that FlexMeasures requires some libraries with lots of C code support (e.g. Numpy). One way out is to use Docker, which uses a prepared Linux image, so it'll definitely work.
  68. In case you want to re-run the tutorial, then it's recommended to delete the old database and create a fresh one. Run the following command to create a clean database with a new user, where it is optional. If you don't provide the user, then the default `postgres` user will be used to create the database.
  69. .. code-block:: bash
  70. $ make clean-db db_name=flexmeasures-db [db_user=flexmeasures]
  71. To start the web application, you can run:
  72. .. code-block:: bash
  73. $ flexmeasures run
  74. When started, the FlexMeasures UI should be available at http://localhost:5000 in your browser.
  75. .. include:: ../notes/macOS-port-note.rst
  76. Add some structural data
  77. ---------------------------------------
  78. The data we need for our example is both structural (e.g. a company account, a user, an asset) and numeric (we want market prices to optimize against).
  79. Let's create the structural data first.
  80. FlexMeasures offers a command to create a toy account with a battery:
  81. .. code-block:: bash
  82. $ flexmeasures add toy-account --kind battery
  83. Generic asset type `solar` created successfully.
  84. Generic asset type `wind` created successfully.
  85. Generic asset type `one-way_evse` created successfully.
  86. Generic asset type `two-way_evse` created successfully.
  87. Generic asset type `battery` created successfully.
  88. Generic asset type `building` created successfully.
  89. Generic asset type `process` created successfully.
  90. Creating account Toy Account ...
  91. Toy account Toy Account with user toy-user@flexmeasures.io created successfully. You might want to run `flexmeasures show account --id 1`
  92. Adding transmission zone type ...
  93. Adding NL transmission zone ...
  94. Created day-ahead prices
  95. The sensor recording day-ahead prices is day-ahead prices (ID: 1).
  96. Created <GenericAsset None: 'toy-battery' (battery)>
  97. Created discharging
  98. Created <GenericAsset None: 'toy-solar' (solar)>
  99. Created production
  100. The sensor recording battery discharging is discharging (ID: 2).
  101. The sensor recording solar forecasts is production (ID: 3).
  102. And with that, we're done with the structural data for this tutorial!
  103. If you want, you can inspect what you created:
  104. .. code-block:: bash
  105. $ flexmeasures show account --id 1
  106. ===========================
  107. Account Toy Account (ID: 1)
  108. ===========================
  109. Account has no roles.
  110. All users:
  111. ID Name Email Last Login Last Seen Roles
  112. ---- -------- ------------------------ ------------ ----------- -------------
  113. 1 toy-user toy-user@flexmeasures.io None None account-admin
  114. All assets:
  115. ID Name Type Location
  116. ---- ----------- ------- -----------------
  117. 2 toy-building building (52.374, 4.88969)
  118. 3 toy-battery battery (52.374, 4.88969)
  119. 4 toy-solar solar (52.374, 4.88969)
  120. .. code-block:: bash
  121. $ flexmeasures show asset --id 2
  122. =========================
  123. Asset toy-building (ID: 2)
  124. =========================
  125. Type Location Attributes
  126. ------- ----------------- ----------------------------
  127. building (52.374, 4.88969)
  128. ====================================
  129. Child assets of toy-building (ID: 2)
  130. ====================================
  131. Id Name Type
  132. ------- ----------------- ----------------------------
  133. 3 toy-battery battery
  134. 4 toy-solar solar
  135. No sensors in asset ...
  136. $ flexmeasures show asset --id 3
  137. ===================================
  138. Asset toy-battery (ID: 3)
  139. Child of asset toy-building (ID: 2)
  140. ===================================
  141. Type Location Flex-Context Sensors to show Attributes
  142. ------- ----------------- -------------------------------- ------------------- -----------------------
  143. battery (52.374, 4.88969) consumption-price: {'sensor': 1} Prices: [1] capacity_in_mw: 500 kVA
  144. Power flows: [3, 2] min_soc_in_mwh: 0.05
  145. max_soc_in_mwh: 0.45
  146. ====================================
  147. Child assets of toy-battery (ID: 3)
  148. ====================================
  149. No children assets ...
  150. All sensors in asset:
  151. ID Name Unit Resolution Timezone Attributes
  152. ---- ----------- ------ ------------ ---------------- ------------
  153. 2 discharging MW 15 minutes Europe/Amsterdam
  154. Yes, that is quite a large battery :)
  155. You can also see that the asset has some meta information about its scheduling. Within :ref:`flex_context`, we noted where to find the relevant optimization signal for electricity consumption (Sensor 1, which stores day-ahead prices).
  156. .. note:: Obviously, you can use the ``flexmeasures`` command to create your own, custom account and assets. See :ref:`cli`. And to create, edit or read asset data via the API, see :ref:`v3_0`.
  157. We can also look at the battery asset in the UI of FlexMeasures (in Docker, the FlexMeasures web server already runs, on your PC you can start it with ``flexmeasures run``).
  158. Visit `http://localhost:5000/ <http://localhost:5000/>`_ (username is "toy-user@flexmeasures.io", password is "toy-password"):
  159. .. image:: https://github.com/FlexMeasures/screenshots/raw/main/tut/toy-schedule/asset-view-dashboard.png
  160. :align: center
  161. |
  162. .. note:: You won't see the map tiles, as we have not configured the :ref:`MAPBOX_ACCESS_TOKEN`. If you have one, you can configure it via ``flexmeasures.cfg`` (for Docker, see :ref:`docker_configuration`).
  163. .. _tut_toy_schedule_price_data:
  164. Add some price data
  165. ---------------------------------------
  166. Now to add price data. First, we'll create the CSV file with prices (EUR/MWh, see the setup for sensor 1 above) for tomorrow.
  167. .. code-block:: bash
  168. $ TOMORROW=$(date --date="next day" '+%Y-%m-%d')
  169. $ echo "Hour,Price
  170. $ ${TOMORROW}T00:00:00,10
  171. $ ${TOMORROW}T01:00:00,11
  172. $ ${TOMORROW}T02:00:00,12
  173. $ ${TOMORROW}T03:00:00,15
  174. $ ${TOMORROW}T04:00:00,18
  175. $ ${TOMORROW}T05:00:00,17
  176. $ ${TOMORROW}T06:00:00,10.5
  177. $ ${TOMORROW}T07:00:00,9
  178. $ ${TOMORROW}T08:00:00,9.5
  179. $ ${TOMORROW}T09:00:00,9
  180. $ ${TOMORROW}T10:00:00,8.5
  181. $ ${TOMORROW}T11:00:00,10
  182. $ ${TOMORROW}T12:00:00,8
  183. $ ${TOMORROW}T13:00:00,5
  184. $ ${TOMORROW}T14:00:00,4
  185. $ ${TOMORROW}T15:00:00,4
  186. $ ${TOMORROW}T16:00:00,5.5
  187. $ ${TOMORROW}T17:00:00,8
  188. $ ${TOMORROW}T18:00:00,12
  189. $ ${TOMORROW}T19:00:00,13
  190. $ ${TOMORROW}T20:00:00,14
  191. $ ${TOMORROW}T21:00:00,12.5
  192. $ ${TOMORROW}T22:00:00,10
  193. $ ${TOMORROW}T23:00:00,7" > prices-tomorrow.csv
  194. This is time series data, in FlexMeasures we call *"beliefs"*. Beliefs can also be sent to FlexMeasures via API or imported from open data hubs like `ENTSO-E <https://github.com/SeitaBV/flexmeasures-entsoe>`_ or `OpenWeatherMap <https://github.com/SeitaBV/flexmeasures-openweathermap>`_. However, in this tutorial we'll show how you can read data in from a CSV file. Sometimes that's just what you need :)
  195. .. code-block:: bash
  196. $ flexmeasures add beliefs --sensor 1 --source toy-user prices-tomorrow.csv --timezone Europe/Amsterdam
  197. Successfully created beliefs
  198. In FlexMeasures, all beliefs have a data source. Here, we use the username of the user we created earlier. We could also pass a user ID, or the name of a new data source we want to use for CLI scripts.
  199. .. note:: Attention: We created and imported prices where the times have no time zone component! That happens a lot. FlexMeasures can localize them for you to a given timezone. Here, we localized the data to the timezone of the price sensor - ``Europe/Amsterdam`` - so the start time for the first price is `2022-03-03 00:00:00+01:00` (midnight in Amsterdam).
  200. Let's look at the price data we just loaded:
  201. .. code-block:: bash
  202. $ flexmeasures show beliefs --sensor 1 --start ${TOMORROW}T00:00:00+01:00 --duration PT24H
  203. Beliefs for Sensor 'day-ahead prices' (ID 1).
  204. Data spans a day and starts at 2022-03-03 00:00:00+01:00.
  205. The time resolution (x-axis) is an hour.
  206. ┌────────────────────────────────────────────────────────────┐
  207. │ ▗▀▚▖ │
  208. │ ▗▘ ▝▖ │
  209. │ ▞ ▌ │
  210. │ ▟ ▐ │ 15EUR/MWh
  211. │ ▗▘ ▝▖ ▗ │
  212. │ ▗▘ ▚ ▄▞▘▚▖ │
  213. │ ▞ ▐ ▄▀▘ ▝▄ │
  214. │ ▄▞ ▌ ▛ ▖ │
  215. │▀ ▚ ▐ ▝▖ │
  216. │ ▝▚ ▖ ▗▘ ▝▖ │ 10EUR/MWh
  217. │ ▀▄▄▞▀▄▄ ▗▀▝▖ ▞ ▐ │
  218. │ ▀▀▜▘ ▝▚ ▗▘ ▚ │
  219. │ ▌ ▞ ▌│
  220. │ ▝▖ ▞ ▝│
  221. │ ▐ ▞ │
  222. │ ▚ ▗▞ │ 5EUR/MWh
  223. │ ▀▚▄▄▄▄▘ │
  224. └────────────────────────────────────────────────────────────┘
  225. 5 10 15 20
  226. ██ day-ahead prices
  227. Again, we can also view these prices in the `FlexMeasures UI <http://localhost:5000/sensors/1>`_:
  228. .. image:: https://github.com/FlexMeasures/screenshots/raw/main/tut/toy-schedule/sensor-data-prices.png
  229. :align: center
  230. |
  231. .. note:: Technically, these prices for tomorrow may be forecasts (depending on whether you are running through this tutorial before or after the day-ahead market's gate closure). You can also use FlexMeasures to compute forecasts yourself. See :ref:`tut_forecasting_scheduling`.