installation.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. .. _installation:
  2. Installation & First steps
  3. =================================
  4. This section walks you through the basics of installing FlexMeasures on a computer and running it continuously.
  5. We'll cover the most crucial settings you need to run FlexMeasures step-by-step, both for `pip`-based installation, as well as running via Docker.
  6. In addition, we'll explain some basics that you'll need:
  7. .. contents:: Table of contents
  8. :local:
  9. :depth: 1
  10. Installing and running FlexMeasures
  11. ------------------------------------
  12. In a nutshell, what does installation and running look like?
  13. Well, there are two major ways:
  14. .. tabs::
  15. .. tab:: via `pip`
  16. .. code-block:: bash
  17. $ pip install flexmeasures
  18. $ flexmeasures run # this won't work just yet
  19. .. note:: Installation might cause some issues with latest Python versions and Windows, for some pip-dependencies (e.g. ``rq-win``). You might overcome this with a little research, e.g. by `installing from the repo <https://github.com/michaelbrooks/rq-win#installation-and-use>`_.
  20. .. tab:: via `docker`
  21. .. code-block:: bash
  22. $ docker pull lfenergy/flexmeasures
  23. $ docker run -d lfenergy/flexmeasures # this won't work just yet
  24. The ``-d`` option keeps FlexMeasures running in the background ("detached"), as it should.
  25. .. note:: For more information, see :ref:`docker-image` and :ref:`docker-compose`.
  26. However, FlexMeasures is not a simple tool - it's a web-app, with bells and whistles, like user access and databases.
  27. We'll need to add a few minimal preparations before running will work, see below.
  28. Make a secret key for sessions and password salts
  29. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  30. Set a secret key, which is used to sign user sessions and re-salt their passwords.
  31. The quickest way is with an environment variable, like this:
  32. .. tabs::
  33. .. tab:: via `pip`
  34. .. code-block:: bash
  35. $ export SECRET_KEY=something-secret
  36. (on Windows, use ``set`` instead of ``export``\ )
  37. .. tab:: via `docker`
  38. Add the `SECRET_KEY` as an environment variable:
  39. .. code-block:: bash
  40. $ docker run -d --env SECRET_KEY=something-secret lfenergy/flexmeasures
  41. This suffices for a quick start. For an actually secure secret, here is a Pythonic way to generate a good secret key:
  42. .. code-block:: bash
  43. $ python -c "import secrets; print(secrets.token_urlsafe())"
  44. Choose the environment
  45. ^^^^^^^^^^^^^^^^^^^^^^^
  46. Set an environment variable to indicate in which environment you are operating (one out of `development|testing|documentation|production`).
  47. We'll go with ``development`` here:
  48. .. tabs::
  49. .. tab:: via `pip`
  50. .. code-block:: bash
  51. $ export FLEXMEASURES_ENV=development
  52. (on Windows, use ``set`` instead of ``export``\ )
  53. .. tab:: via `docker`
  54. .. code-block:: bash
  55. $ docker run -d --env FLEXMEASURES_ENV=development lfenergy/flexmeasures
  56. The default environment setting is ``production``\ , which will probably not work well on your localhost, as FlexMeasures then expects SSL-encrypted communication.
  57. Tell FlexMeasures where the time series database is
  58. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  59. * Make sure you have a Postgres (Version 9+) database for FlexMeasures to use. See :ref:`host-data` (section "Getting ready to use") for deeper instructions on this.
  60. *
  61. Tell ``flexmeasures`` about it:
  62. .. tabs::
  63. .. tab:: via `pip`
  64. .. code-block:: bash
  65. $ export SQLALCHEMY_DATABASE_URI="postgresql://<user>:<password>@<host-address>[:<port>]/<db-name>"
  66. (on Windows, use ``set`` instead of ``export``\ )
  67. .. tab:: via `docker`
  68. .. code-block:: bash
  69. $ docker run -d --env SQLALCHEMY_DATABASE_URI=postgresql://<user>:<password>@<host-address>:<port>/<db-name> lfenergy/flexmeasures
  70. If you install this on localhost, ``host-address`` is ``127.0.0.1`` and the port can be left out.
  71. *
  72. On a fresh database, you can create the data structure for FlexMeasures like this:
  73. .. tabs::
  74. .. tab:: via `pip`
  75. .. code-block:: bash
  76. $ flexmeasures db upgrade
  77. .. tab:: via `docker`
  78. Go into the container to create the structure:
  79. .. code-block:: bash
  80. $ docker exec -it <your-container-id> -c "flexmeasures db upgrade"
  81. Use a config file
  82. ^^^^^^^^^^^^^^^^^^^
  83. If you want to consistently use FlexMeasures, we recommend you add the settings we introduced above into a FlexMeasures config file.
  84. See :ref:`configuration` for a full explanation where that file can live and all the settings.
  85. So far, our config file would look like this:
  86. .. code-block:: python
  87. SECRET_KEY = "something-secret"
  88. FLEXMEASURES_ENV = "development"
  89. SQLALCHEMY_DATABASE_URI = "postgresql://<user>:<password>@<host-address>[:<port>]/<db>"
  90. .. tabs::
  91. .. tab:: via `pip`
  92. Place the file at ``~/.flexmeasures.cfg``. FlexMeasures will look for it there.
  93. .. tab:: via `docker`
  94. Save the file as ``flexmeasures-instance/flexmeasures.cfg`` and load it into the container like this (more at :ref:`docker_configuration`):
  95. .. code-block:: bash
  96. $ docker run -v $(pwd)/flexmeasures-instance:/app/instance:ro lfenergy/flexmeasures
  97. Adding data
  98. ---------------
  99. Let's add some data.
  100. From here on, we will not differentiate between `pip` and `docker` installation. When using docker, here are two ways to run these commands:
  101. .. code-block:: bash
  102. $ docker exec -it <your-container-name> -c "<command>"
  103. $ docker exec -it <your-container-name> bash # then issue the data-generating commands in the container
  104. Add an account & user
  105. ^^^^^^^^^^^^^^^^^^^^^^^
  106. FlexMeasures is a tenant-based platform ― multiple clients can enjoy its services on one server. Let's create a tenant account first:
  107. .. code-block:: bash
  108. $ flexmeasures add account --name "Some company"
  109. This command will tell us the ID of this account. Let's assume it was ``2``.
  110. FlexMeasures is also a web-based platform, so we need to create a user to authenticate:
  111. .. code-block:: bash
  112. $ flexmeasures add user --username <your-username> --email <your-email-address> --account-id 2 --roles=admin
  113. * This will ask you to set a password for the user.
  114. * Giving the first user the ``admin`` role is probably what you want.
  115. Add initial structure
  116. ^^^^^^^^^^^^^^^^^^^^^^^
  117. Populate the database with some standard asset types, user roles etc.:
  118. .. code-block:: bash
  119. $ flexmeasures add initial-structure
  120. Add your first asset
  121. ^^^^^^^^^^^^^^^^^^^^^^^
  122. There are three ways to add assets:
  123. First, you can use the ``flexmeasures`` :ref:`cli`:
  124. .. code-block:: bash
  125. $ flexmeasures add asset --name "my basement battery pack" --asset-type-id 3 --latitude 65 --longitude 123.76 --account-id 2
  126. For the asset type ID, I consult ``flexmeasures show asset-types``.
  127. For the account ID, I looked at the output of ``flexmeasures add account`` (the command we issued above) ― I could also have consulted ``flexmeasures show accounts``.
  128. The second way to add an asset is the UI ― head over to ``https://localhost:5000/assets`` (after you started FlexMeasures, see step "Run FlexMeasures" further down) and add a new asset there in a web form.
  129. Finally, you can also use the `POST /api/v3_0/assets <../api/v3_0.html#post--api-v3_0-assets>`_ endpoint in the FlexMeasures API to create an asset.
  130. Add your first sensor
  131. ^^^^^^^^^^^^^^^^^^^^^^^
  132. Usually, we are here because we want to measure something with respect to our assets. Each assets can have sensors for that, so let's add a power sensor to our new battery asset, using the ``flexmeasures`` :ref:`cli`:
  133. .. code-block:: bash
  134. $ flexmeasures add sensor --name power --unit MW --event-resolution 5 --timezone Europe/Amsterdam --asset-id 1 --attributes '{"capacity_in_mw": 7}'
  135. The asset ID I got from the last CLI command, or I could consult ``flexmeasures show account --account-id <my-account-id>``.
  136. .. note: The event resolution is given in minutes. Capacity is something unique to power sensors, so it is added as an attribute.
  137. Seeing it work and next steps
  138. --------------------------------------
  139. It's finally time to start running FlexMeasures. This here is the direct form you can use to see if it's working:
  140. .. tabs::
  141. .. tab:: via `pip`
  142. .. code-block:: bash
  143. $ flexmeasures run
  144. .. tab:: via `docker`
  145. .. code-block:: bash
  146. # assuming you loaded flexmeasures.cfg (see above)
  147. $ docker run lfenergy/flexmeasures
  148. .. code-block:: bash
  149. # or everything on the terminal
  150. $ docker run -d --env FLEXMEASURES_ENV=development --env SECRET_KEY=something-secret --env SQLALCHEMY_DATABASE_URI=postgresql://<user>:<password>@<host-address>:<port>/<db-name> lfenergy/flexmeasures
  151. This might print some warnings, see the next section where we go into more detail. For instance, when you see the dashboard, the map will not work. For that, you'll need to get your :ref:`mapbox_access_token` and add it to your config file.
  152. You can visit ``http://localhost:5000`` now to see if the app's UI works. You should be asked to log in (here you can use the admin user created above) and then see the dashboard.
  153. We achieved the main goal of this page, to get FlexMeasures to run.
  154. Below are some additional steps you might consider.
  155. Add time series data (beliefs)
  156. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  157. There are three ways to add data:
  158. First, you can load in data from a file (CSV or Excel) via the ``flexmeasures`` :ref:`cli`:
  159. .. code-block:: bash
  160. $ flexmeasures add beliefs --file my-data.csv --skiprows 2 --delimiter ";" --source OurLegacyDatabase --sensor-id 1
  161. This assumes you have a file `my-data.csv` with measurements, which was exported from some legacy database, and that the data is about our sensor with ID 1. This command has many options, so do use its ``--help`` function.
  162. For instance, to add data as forecasts, use the ``--beliefcol`` parameter, to say precisely when these forecasts were made. Or add ``--horizon`` for rolling forecasts if they all share the same horizon.
  163. Second, you can use the `POST /api/v3_0/sensors/data <../api/v3_0.html#post--api-v3_0-sensors-data>`_ endpoint in the FlexMeasures API to send meter data.
  164. You can also use the API to send forecast data. Similar to the ``add beliefs`` commands, you would use here the fields ``prior`` (to denote time of knowledge of data) or ``horizon`` (for rolling forecast data with equal horizon). Consult the documentation at :ref:`posting_sensor_data`.
  165. Finally, you can tell FlexMeasures to compute forecasts based on existing meter data with the ``flexmeasures add forecasts`` command, here is an example:
  166. .. code-block:: bash
  167. $ flexmeasures add forecasts --from-date 2020-03-08 --to-date 2020-04-08 --asset-type Asset --asset my-solar-panel
  168. This obviously depends on some conditions (like the right underlying data) being right, consult :ref:`tut_forecasting_scheduling`.
  169. Set mail settings
  170. ^^^^^^^^^^^^^^^^^
  171. For FlexMeasures to be able to send email to users (e.g. for resetting passwords), you need an email service that can do that (e.g. GMail). Set the MAIL_* settings in your configuration, see :ref:`mail-config`.
  172. .. _install-lp-solver:
  173. Install an LP solver
  174. ^^^^^^^^^^^^^^^^^^^^
  175. For computing schedules, the FlexMeasures platform uses a linear program solver. Currently that is the HiGHS or CBC solvers.
  176. It's already installed in the Docker image. For yourself, you can simply install it like this:
  177. .. code-block:: bash
  178. $ pip install highspy
  179. Read more on solvers (e.g. how to install a different one) at :ref:`installing-a-solver`.
  180. Install and configure Redis
  181. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  182. To let FlexMeasures queue forecasting and scheduling jobs, install a `Redis <https://redis.io/>`_ server (or rent one) and configure access to it within FlexMeasures' config file (see above). You can find the necessary settings in :ref:`redis-config`.
  183. Then, start workers in a console (or some other method to keep a long-running process going):
  184. .. code-block:: bash
  185. $ flexmeasures jobs run-worker --queue forecasting
  186. $ flexmeasures jobs run-worker --queue scheduling
  187. Where to go from here?
  188. ------------------------
  189. If your data structure is good, you should think about (continually) adding measurement data. This tutorial mentioned how to add data, but :ref:`tut_posting_data` goes deeper with examples and terms & definitions.
  190. Then, you probably want to use FlexMeasures to generate forecasts and schedules! For this, read further in :ref:`tut_forecasting_scheduling`.
  191. One more consideration is to run FlexMeasures in a more professional ways as a we service. Head on to :ref:`deployment`.