3.x ‐ Fetching weather forecast from Finnish Meteorology Insititute (FMI) API - masipila/openhab-spot-price-optimizer GitHub Wiki

Note: This solution was originally built in 2022 when openHAB did not have a capability where Bindings can persist forecast time series data. This capability has been added to openHAB 4.1. The actual optimizing scripts do NOT require using this FMI API script, you can also use any of the weather forecast bindings available for openHAB.

Pre-requisites

Create a new Item FMIForecastTemperature

First create an Item called FMIForecastTemperature so that you will be able to render the time series on openHAB charts.

image

Optionally, you can create Items for other attributes available in the FMI API like wind speed or total cloud cover, see the names of these attributes in the script below.

Create a Rule FetchWeatherForecast

  • Create a new Rule called FetchWeatherForecast which will fetch the weather forecast from FMI API and save it to your InfluxDB using the Influx HTTP API. In other words, the openHAB persistence layer is NOT used to access the database, the script will call Influx HTTP API directly.
  • You can schedule the Rule to be run for example every full hour.
  • Copy-paste the code below as the Script Action (ECMAScript 262 Edition 11).
  • The script will write the forecasted temperature as FMIForecastTemperature time series to your InfluxDB

Note: If you make changes to the javascript files (inluding config.js), you must re-save the Script Action (the code snippet below) to make sure openHAB re-reads the javascript files.

image

Inline script action for fetching the weather forecast

// Load modules.
var { FMI } = require('openhab-spot-price-optimizer/fmi.js');
var { Influx } = require('openhab-spot-price-optimizer/influx.js');

// Create services.
var fmi = new FMI();
var influx = new Influx();

// Read weather forecast from the API. The place must be recognized by FMI API.
var place = 'veini';
var xml = fmi.makeApiCall(place);

// Write the Temperature points to InfluxDB
var Temperature = fmi.preparePoints(xml, 'Temperature');
influx.writePoints('FMIForecastTemperature', Temperature);

// Uncomment the additional attributes you need but remember to create Items for them first!

// var PrecipitationAmount = fmi.preparePoints(xml, 'PrecipitationAmount');
// influx.writePoints('FMIForecastPrecipitationAmount', PrecipitationAmount);

// var WindSpeedMS = fmi.preparePoints(xml, 'WindSpeedMS');
// influx.writePoints('FMIForecastWindSpeedMS', WindSpeedMS);

// var TotalCloudCover = fmi.preparePoints(xml, 'TotalCloudCover');
// influx.writePoints('FMIForecastTotalCloudCover', TotalCloudCover);

// var WindChillTemp = fmi.calculateWindChillTempPoints(Temperature, WindSpeedMS);
// influx.writePoints('FMIForecastWindChillTemp', WindChillTemp);

Validate the results by checking the data in Influx Data Explorer

Once you have run the Rule, check the results from Influx Data Explorer. You should now see a measurement FMIForecastTemperature. Remember to adjust the date range filter to a custom range so that you have data for that time range.

image

If you do not see the FMIForecastTemperature data in Influx Data Explorer:

  • check the openHAB logs
  • double check that you have configured config.js correctly
  • double check that you have followed the pre-requisite and installation instructions