This plugin currently supports two Weather API services: OpenWeatherMap One Call API and Weather API. The configuration is controlled via your FlexMeasures config file.
To collect forecasts in the database, first register the weather sensors you need:
flexmeasures weather register-weather-sensor --name "wind speed" --latitude 30 --longitude 40
flexmeasures weather register-weather-sensor --name "cloud cover" --latitude 30 --longitude 40Supported sensor names are temperature (°C), wind speed (m/s), cloud cover (%) and irradiance (W/m²). Irradiance is calculated from the location, time and forecast cloud cover. All four sensors have an hourly resolution. The optional --timezone for registration defaults to UTC.
Sensor registration by coordinates looks for an asset at exactly those coordinates. It reuses a single matching asset or, when several match, the weather station if there is exactly one. Otherwise it lists the matches and asks you to select one with --asset-id. If no asset is found, it creates a public weather station. It does not search nearby locations. WEATHER_STATION_NAME controls the name used for new stations. The sensors it creates get the attribute "weather_sensor": true. Running it again for a sensor that already exists adds this attribute if it is missing.
There is no separate weather-plugin command to create a station, but FlexMeasures provides general asset commands. If the weather station asset type does not exist yet, create it with flexmeasures add asset-type --name "weather station". Then use its ID to create a public asset with a unique name:
flexmeasures add asset --name "Antwerp weather station" --asset-type-id <type-id> --latitude 51.260197 --longitude 4.402771Replace <type-id> with the ID of the weather station asset type, and register sensors using its coordinates or --asset-id. You can also register a sensor on another existing asset with coordinates by passing its ID.
To collect forecasts for one location:
flexmeasures weather get-weather-forecasts --location 30,40Forecast collection fetches a forecast once; schedule it to run repeatedly if you need updates. For each weather quantity present in the provider response, it first looks for a matching sensor at the exact coordinates, either on a weather station or with the weather_sensor attribute on any other asset. Sensors on other assets without this attribute are skipped, so your own measurements never receive forecasts. If multiple sensors match, it warns and uses one on a weather station, or else the one with the lowest sensor ID. If none matches exactly, it uses the nearest sensor on an asset of type weather station, within WEATHER_MAXIMAL_DEGREE_LOCATION_DISTANCE degrees in both latitude and longitude (default: 0.3). Different quantities may use different assets. If no suitable sensor exists, that quantity is skipped with a warning. The command does not create sensors or stations.
You can also pass --asset-id to use an asset's coordinates. The command still selects sensors by location, so the ID does not restrict the selected sensors to that asset.
The same import can run as a recurring task. Register the weather sensors on a weather-station asset, put its ID
in a YAML file such as weather-automation.yml:
weather-station: 123Then create an hourly automation:
flexmeasures add automation \
--asset 123 \
--name "Hourly weather forecasts" \
--cron "5 * * * *" \
--timezone Europe/Amsterdam \
--type weather-forecasts \
--config weather-automation.ymlWeather imports run on the ingestion queue. The automation runner and an ingestion
worker must both load this plugin. The provider and API key remain server settings and
are not stored in the automation.
Moving a station from the CLI to an automation changes the data source its new forecasts are recorded under.
The CLI records them under the weather provider's source (named after WEATHER_DATA_SOURCE_NAME), and irradiance under "FlexMeasures ".
The automation records all of them, irradiance included, under a source of its own: it has the same name, but it is a different source, with the WeatherForecastImporter model and the station in its configuration.
Anything reading forecasts regardless of source keeps working.
Anything that selects forecasts by the CLI's source (for instance a forecaster regressor or a report with a source filter) needs to select the automation's source instead,
which the automation's page shows.
The --location option can also describe a bounding box and generate a grid; see flexmeasures weather get-weather-forecasts --help. The current database-saving implementation saves only the final grid location, so use separate single-location calls when saving multiple locations to the database.
Alternatively, save the provider's hourly forecast entries as JSON files:
flexmeasures weather get-weather-forecasts --location 30,40 --store-as-json-files --region somewhereThis does not require registered sensors. The entries are taken from the provider's response; the file is not the complete API response. The optional region becomes a subfolder. Check your provider's current subscription and request limits when scheduling calls.
To add as plugin to an existing FlexMeasures system, add "/path/to/flexmeasures-weather-repo/flexmeasures_weather" to your FlexMeasures config file, using the FLEXMEASURES_PLUGINS setting (a list).
Alternatively, if you installed this plugin as a package (e.g. via pip install -e . from the repository), then "flexmeasures_weather" suffices.
To enable weather forecast functionality, two PostgreSQL extensions must be installed. Run the following SQL commands in your database:
CREATE EXTENSION IF NOT EXISTS cube;
CREATE EXTENSION IF NOT EXISTS earthdistance;
These extensions provide support for geographical calculations such as ll_to_earth and earth_distance, which we use to find the nearest weather station asset.
Add the following entries to your config:
# Select the weather provider to use: "OWM" (OpenWeatherMap) or "WAPI" (Weather API)
WEATHER_PROVIDER = "OWM"
# API key for the selected weather provider
WEATHERAPI_KEY = "your-api-key-here"
# Name to register the weather data source in FlexMeasures. The default is 'Weather'.
# Examples: "OpenWeatherMap" (for backwards compatibility with the OWM plugin).
WEATHER_DATA_SOURCE_NAME = "OpenWeatherMap"
# Directory in which to store weather forecasts as JSON files
WEATHER_FILE_PATH_LOCATION = "/path/to/weather_output"
# Maximum latitude and longitude difference from a sensor, in degrees (default: 0.3)
WEATHER_MAXIMAL_DEGREE_LOCATION_DISTANCE = 0.3To expand the plugin's coverage to additional weather API services:
-
Update the configuration
Change theWEATHER_PROVIDERsetting in your config to the identifier for the new API service (e.g.,NEWAPI), and provide the necessary credentials inWEATHERAPI_KEY. -
Implement a new API function
Create a function named in the format:def call_NEWAPI_api(...): # Your logic to call the API and return data in the expected format
This function should return the API call time and a list of hourly forecast entries. Its field names must match the provider-specific mapping, and the forecast processing code must recognize the provider’s timestamp and any units requiring conversion.
You also need a provider-specific mapping entry in
flexmeasures_weather/sensor_specs.py. Each supported sensor should include the new provider's response field name, for example:dict( fm_sensor_name="temperature", OWM_sensor_name="temp", WAPI_sensor_name="temp_c", NEWAPI_sensor_name="temperatureC", unit="°C", event_resolution=timedelta(minutes=60), attributes=weather_attributes, )
-
Integrate into the plugin Update both
call_apiandsave_forecasts_in_dbinweather.pyto accept the provider. Add a dispatch branch and handle its timestamp field and any unit conversions. Also update sensor registration to remove the new provider mapping field before constructing a sensor. -
Finalize and contribute
Once you've implemented and tested the plugin with your chosen API service:- Update this README to reflect the new configuration and usage details.
- Submit a pull request with your changes for review.
This modular structure allows for seamless integration of additional services while maintaining consistency and clarity in data handling.
We use pre-commit to keep code quality up.
Install necessary tools with:
pip install pre-commit
pre-commit install
or:
make install-for-dev
Try it:
pre-commit run --all-files --show-diff-on-failure