Skip to main content

Async client to connect to the FlexMeasures API

Project description

Project generated with PyScaffold

FlexMeasures Client

The FlexMeasures Client provides a Python package to connect to a FlexMeasures server to manage flexible assets.

The Flexmeasures Client package provides functionality for authentication, asset and sensor management, posting sensor data, and triggering and retrieving schedules from a FlexMeasures instance through the API.

As the Flexmeasures Client is still in active development and on version 0.x it should be considered in beta.

Getting Started

To get started with the FlexMeasures Client package, first an account needs to be registered with a FlexMeasures instance. To create a local instance of FlexMeasures, follow the FlexMeasures documentation. Registering to a hosted FlexMeasures instance instead can be done through Seita BV.

Install using pip:

pip install flexmeasures-client

Initialization and authentication:

from flexmeasures_client import FlexMeasuresClient
client = FlexMeasuresClient(host="localhost:5000", ssl=False, email="email@email.com", password="pw")
client = FlexMeasuresClient(host="seita.energy", ssl=True, email="email@email.com", password="pw")

Retrieve available assets and sensors:

assets = await client.get_assets()
sensors = await client.get_sensors()

Post a measurement from a sensor:

await client.post_measurements(
        sensor_id=<sensor_id>, # integer
        start="2023-03-26T10:00+02:00", #iso datetime
        duration="PT6H", # iso timedelta
        values=[1,2,3,4], # list
        unit="kWh",
        entity_address=<sensor_entity_address>, # string
    )

With FlexMeasures a schedule can be requested to optimize at what time the flexible assets can be activated to optimize for price of energy or emissions.

The calculation of the schedule can take some time depending on the complexity of the calculations. A polling function is used to check if a schedule is available after triggering the schedule.

Trigger and retrieve a schedule:

schedule = await flexmeasures_client.trigger_and_get_schedule(
        sensor_id=<sensor_id>, # int
        start="2023-03-26T10:00+02:00", # iso datetime
        duration="PT12H", # iso timedelta
        flex_context= {"consumption-price-sensor": <consumption_price_sensor_id>}, # int
        flex-model= {
                "soc-unit": "kWh",
                "soc-at-start": 50, # in soc_units (kWh)
                "soc-max": 400,
                "soc-min": 20,
                "soc-targets": [
                    {"value": 100, "datetime": "2023-03-03T11:00+02:00"}
                ],
           }
    )

The trigger and get schedule function can also be separated to trigger the schedule first and later retrieve the schedule using the schedule_uuid.

Trigger a schedule:

schedule_uuid = await flexmeasures_client.trigger_storage_schedule(
        sensor_id=<sensor_id>, # int
        start="2023-03-26T10:00+02:00", # iso datetime
        duration="PT12H", # iso timedelta
        flex_context= {"consumption-price-sensor": <consumption_price_sensor_id>}, # int
        flex-model= {
                "soc-unit": "kWh",
                "soc-at-start": 50, # soc_units (kWh)
                "soc-max": 400,
                "soc-min": 20,
                "soc-targets": [
                    {"value": 100, "datetime": "2023-03-03T11:00+02:00"}
                ],
           }
    )

The trigger_storage_schedule return a schedule_uuid. This can be used to retrieve the schedule. The client will re-try if until the schedule is available or the MAX_POLLING_STEPS of 10 is reached. Retrieve schedule:

schedule = await flexmeasures_client.get_schedule(
            sensor_id=<sensor_id>, #int
            schedule_id="<schedule_uuid>", # uuid
            duration="PT45M" # iso timedelta
        )

The schedule returns a Pandas DataFrame that can be used to regulate the flexible assets.

Making Changes & Contributing

Install the project locally (in a virtual environment of your choice):

pip install -e

Running tests locally is crucial as well. Staying close to the CI workflow:

pip install tox
tox -e clean,build
tox -- -rFEx --durations 10 --color yes

This project uses pre-commit, please make sure to install it before making any changes:

pip install pre-commit
cd flexmeasures-client
pre-commit install

It is a good idea to update the hooks to the latest version:

pre-commit autoupdate

Don’t forget to tell your contributors to also install and use pre-commit.

New releases on Pypi are made by adding a tag and pushing it:

git tag -s -a vX.Y.Z -m "Short summary"
git push --tags

(of course you need the permissions to do so)

See releases in GitHub Actions at https://github.com/FlexMeasures/flexmeasures-client/deployments/release

S2 Protocol

Disclaimer

The S2 Protocol integration is still under active development. Please, beware that the logic and interfaces can change.

Run Demo

Run the following commands in the flexmeasures folder to create a toy-account and an admin user:

flexmeasures add toy-account
flexmeasures add user --username admin --account-id 1 --email admin@mycompany.io --roles admin

Launch server:

flexmeasures run

To load the data, run the following command in the flexmeasures-client repository:

python src/flexmeasures_client/s2/script/demo_setup.py

Start the S2 server:

python src/flexmeasures_client/s2/script/websockets_server.py

In a separate window, start the S2 Client:

python src/flexmeasures_client/s2/script/websockets_client.py

Note

This project has been set up using PyScaffold 4.4. For details and usage information on PyScaffold see https://pyscaffold.org/.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flexmeasures_client-0.2.5.tar.gz (50.1 kB view details)

Uploaded Source

Built Distribution

flexmeasures_client-0.2.5-py3-none-any.whl (30.5 kB view details)

Uploaded Python 3

File details

Details for the file flexmeasures_client-0.2.5.tar.gz.

File metadata

  • Download URL: flexmeasures_client-0.2.5.tar.gz
  • Upload date:
  • Size: 50.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for flexmeasures_client-0.2.5.tar.gz
Algorithm Hash digest
SHA256 eadff48f5e00e8637662cc927e2da539bd805c69df96be0eab608d5062944528
MD5 854a3c496f30c9e03b0b77d3b8fe1348
BLAKE2b-256 cd0307dea08e24c83d53ee77e63b7caa69c9eb6644eaf37735381c46eb2df6b7

See more details on using hashes here.

Provenance

The following attestation bundles were made for flexmeasures_client-0.2.5.tar.gz:

Publisher: ci.yml on FlexMeasures/flexmeasures-client

Attestations:

File details

Details for the file flexmeasures_client-0.2.5-py3-none-any.whl.

File metadata

File hashes

Hashes for flexmeasures_client-0.2.5-py3-none-any.whl
Algorithm Hash digest
SHA256 4b0b52d1006ddb45e813700c64778491a0e8b82cdd3e843e73f29506a08c404a
MD5 8ad10358186fcd17a9c39261e6554ebc
BLAKE2b-256 d92c655e40b38e544f99fc78ef3238b0588fc37c5b727b1045b32a15ac01a9eb

See more details on using hashes here.

Provenance

The following attestation bundles were made for flexmeasures_client-0.2.5-py3-none-any.whl:

Publisher: ci.yml on FlexMeasures/flexmeasures-client

Attestations:

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page