Posting data

The platform FlexMeasures strives on the data you feed it. Let’s demonstrate how you can get data into FlexMeasures using the API. This is where FlexMeasures gets connected to your system as a smart backend and helps you build smart energy services.

We will show how to use the API endpoints for POSTing data. You can call these at regular intervals (through scheduled scripts in your system, for example), so that FlexMeasures always has recent data to work with. Of course, these endpoints can also be used to load historic data into FlexMeasures, so that the forecasting models have access to enough data history.

Note

For the purposes of forecasting and scheduling, it is often advisable to use a less fine-grained resolution than most metering services keep. For example, while such services might measure every ten seconds, FlexMeasures will usually do its job no less effective if you feed it data with a resolution of five minutes. This will also make the data integration much easier. Keep in mind that many data sources like weather forecasting or markets can have data resolutions of an hour, anyway.

Prerequisites

  • FlexMeasures needs some structural meta data for data to be understood. For example, for adding weather data we need to define a weather sensor, and what kind of weather sensors there are. You also need a user account. If you host FlexMeasures yourself, you need to add this info first. Head over to Getting started, where these steps are covered, study our CLI Commands or look into plugins which do this like flexmeasures-entsoe or flexmeasures-openweathermap.

  • You should be familiar with where to find your API endpoints (see Main endpoint and API versions) and how to authenticate against the API (see Authentication).

Note

For deeper explanations of the data and the meta fields we’ll send here, You can always read the API Introduction, to the FlexMeasures API, e.g. Signs of power values, Frequency and resolution, Setting the recording time and Units.

Note

To address assets and sensors, these tutorials assume entity addresses valid in the namespace fm1. See API Introduction for more explanations.

Posting sensor data

Sensor data (both observations and forecasts) can be posted to POST /sensors/data. This endpoint represents the basic method of getting time series data into FlexMeasures via API. It is agnostic to the type of sensor and can be used to POST data for both physical and economical events that have happened in the past or will happen in the future. Some examples:

  • readings from electricity and gas meters

  • readings from temperature and pressure sensors

  • state of charge of a battery

  • estimated availability of parking spots

  • price forecasts

The exact URL will depend on your domain name, and will look approximately like this:

[POST] https://company.flexmeasures.io/api/<version>/sensors/data

This example “PostSensorDataRequest” message posts prices for hourly intervals between midnight and midnight the next day for the Korean Power Exchange (KPX) day-ahead auction, registered under sensor 16. The prior indicates that the prices were published at 3pm on December 31st 2014 (i.e. the clearing time of the KPX day-ahead market, which is at 3 PM on the previous day ― see below for a deeper explanation).

{
    "type": "PostSensorDataRequest",
    "sensor": "ea1.2021-01.io.flexmeasures.company:fm1.16",
    "values": [
        52.37,
        51.14,
        49.09,
        48.35,
        48.47,
        49.98,
        58.7,
        67.76,
        69.21,
        70.26,
        70.46,
        70,
        70.7,
        70.41,
        70,
        64.53,
        65.92,
        69.72,
        70.51,
        75.49,
        70.35,
        70.01,
        66.98,
        58.61
    ],
    "start": "2015-01-01T00:00:00+09:00",
    "duration": "PT24H",
    "prior": "2014-12-31T15:00:00+09:00",
    "unit": "KRW/kWh"
}

Note how the resolution of the data comes out at 60 minutes when you divide the duration by the number of data points. If this resolution does not match the sensor’s resolution, FlexMeasures will try to upsample the data to make the match or, if that is not possible, complain. Likewise, if the data unit does not match the sensor’s unit, FlexMeasures will attempt to convert the data or, if that is not possible, complain.

Being explicit when posting power data

For power data, USEF specifies separate message types for observations and forecasts. Correspondingly, we allow the following message types to be used with the POST /sensors/data endpoint:

{
    "type": "PostMeterDataRequest"
}
{
    "type": "PostPrognosisRequest"
}

For these message types, FlexMeasures validates whether the data unit is suitable for communicating power data. Additionally, we validate whether meter data lies in the past, and prognoses lie in the future.

Single value, single sensor

A single average power value for a 15-minute time interval for a single sensor, posted 5 minutes after realisation.

{
    "type": "PostSensorDataRequest",
    "sensor": "ea1.2021-01.io.flexmeasures.company:fm1.1",
    "value": 220,
    "start": "2015-01-01T00:00:00+00:00",
    "duration": "PT0H15M",
    "horizon": "-PT5M",
    "unit": "MW"
}

Multiple values, single sensor

Multiple values (indicating a univariate timeseries) for 15-minute time intervals for a single sensor, posted 5 minutes after each realisation.

{
    "type": "PostSensorDataRequest",
    "sensor": "ea1.2021-01.io.flexmeasures.company:fm1.1",
    "values": [
        220,
        210,
        200
    ],
    "start": "2015-01-01T00:00:00+00:00",
    "duration": "PT0H45M",
    "horizon": "-PT5M",
    "unit": "MW"
}

Observations vs forecasts: The time of knowledge

To correctly tell FlexMeasures when a meter reading or forecast was known is crucial, as it determines which data is being used to compute schedules or to make other forecasts.

Usually, the time of posting is assumed to be the time when the data was known. But you can also explicitly tell FlexMeasures what these times are. This either works with one fixed time (for the whole set of data being sent) or with a horizon (which applies to each data point separately).

E.g. to post a forecast rather than an observation after the fact, simply set the prior to the moment at which the forecasts were made, e.g. at “2015-01-01T16:30:00+09:00”. Assuming your data starts at 5.00pm, this denotes that the data are forecasts, made half an hour before realisation.

Alternatively, to indicate that each individual observation was made directly after the end of its 15-minute interval (i.e. at 3.15pm, 3.30pm and so on), set a horizon to “PT0H” instead of a prior.

Finally, delays in reading out sensor data can be simulated by setting the horizon field to a negative value. For example, a horizon of “-PT1H” would denote that each temperature reading was observed one hour after the fact (i.e. at 4.15pm, 4.30pm and so on).

See Setting the recording time for more information regarding the prior and horizon fields.

A good example for the use of the prior field are markets, which have clearing times. For example, at the KPX day-ahead auction this is every day at 3pm. This point in time (i.e. when contracts are signed) determines the difference between an ex-post observation and an ex-ante forecast.

Another example for the prior field is running simulations with FlexMeasures. It gives you control over the timing so that you could run a month in the past as if it happened right now.

Posting flexibility states

There is one more crucial kind of data that FlexMeasures needs to know about: What are the current states of flexible devices? For example, a battery has a certain state of charge, which is relevant to describe the flexibility that the battery currently has. In our terminology, this is called the “flex model” and you can read more at Describing flexibility.

Owners of such devices can post the flex model along with triggering the creation of a new schedule, to [POST] /schedules/trigger. The URL might look like this:

https://company.flexmeasures.io/api/<version>/sensors/10/schedules/trigger

The following example triggers a schedule for a power sensor (with ID 10) of a battery asset, asking to take into account the battery’s current state of charge. From this, FlexMeasures derives the energy flexibility this battery has in the next 48 hours and computes an optimal charging schedule. The endpoint also allows to limit the flexibility range and also to set target values.

{
    "start": "2015-06-02T10:00:00+00:00",
    "flex-model": {
        "soc-at-start": "12.1 kWh"
    }
}

Note

More details on supported flex models can be found in The flex-models & corresponding schedulers.

Note

Flexibility states are persisted on sensor attributes. To record a more complete history of the state of charge, set up a separate sensor and post data to it using [POST] /sensors/data (see Posting sensor data).

In How scheduling jobs are queued, we’ll cover what happens when FlexMeasures is triggered to create a new schedule, and how those schedules can be retrieved via the API, so they can be used to steer assets.