3.x ‐ Usage example: Fetch spot prices from Entso‐E API - masipila/openhab-spot-price-optimizer GitHub Wiki

Important note

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 and it might be that there will be an Entso-E Binding developed at some point. When that happens, the optimizer scripts can still be used but you can replace the Rule described on this page with the binding.

Pre-requisites

Create an Item SpotPrice

First create an Item called SpotPrice so that you will be able to use the SpotPrice values in the openHAB charts.
image

Optionally, you can create Items DistributionPrice and TotalPrice in addition to SpotPrice if you want to include the tariffs in your optimizations.

Create a Rule FetchSpotPrices

  • Create a new Rule called FetchSpotPrices which will fetch the spot prices from Entso-E API and save them 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 at 13.15 CET/CEST and 14.15 CET/CEST
  • Copy-paste the code below as the Script Action (ECMAScript 262 Edition 11).
  • The script will write the spot prices as SpotPrice time series to your InfluxDB

Entso-E publishes the spot prices for next day in the afternoon. The example script below requests spot prices for yesterday, today and tomorrow so some prices should always be included in the response even if you would execute it in the morning and tomorrow's prices are not yet available. The script can be executed multiple times, possible previous database points are overwritten.

Notes:

Inline script action for fetching the spot prices

// Influx database connection parameters must be configured in config.js
// Entso-E bidding zone and authentication token must be configured in config.js

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

// Create services.
var entsoe = new Entsoe();
var influx = new Influx();

// Multiplier for VAT. Adjust this to your country or leave as 1.0.
var vat = 1.255; 

// Fetch spot prices from yesterday 00:00 to day after tomorrow 00:00. Save them as 'SpotPrice'.
var start = time.toZDT('00:00').minusDays(1);
var end = time.toZDT('00:00').plusDays(2);
var spotPrices = entsoe.getSpotPrices(start, end, vat);
influx.writePoints('SpotPrice', spotPrices);

Validate the results by checking the data in Influx Data Explorer

Run the Rule manually and check from your InfluxDB Data Explorer that you can see the spot prices. If you run the rule in the afternoon or evening after the day-ahead prices have been published, the prices will include tomorrow. Remember to choose a date range in Influx data explorer which includes the day you just fetched the prices for.

image

If you do not see the SpotPrice 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 instructions from the main README file

If the logs do not reveal the reason, increase the openHAB log level to DEBUG. The log level can be modified using openHAB console with a command log:set DEBUG org.openhab.automation.script.ui.FetchSpotPrices. Once you've done with debugging, it is recommended to return the log level to INFO to avoid polluting your logs with debug level information.

Optional: Calculate distribution price and total price

The total price of electricity usually consists of the spot price, local network operator's distribution fee and taxes. If you want openhab-spot-price-optimizer to optimize against the total price instead of the spot price, you can add two more Items the same ways as above (make sure their type is Number):

  • DistributionPrice
  • TotalPrice

Add a second inline script action to your FetchSpotPrices Rule with the following code:

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

// Create services.
var influx = new Influx();
var tariffCalculator = new TariffCalculator(); 

// Calculate distribution fees from yesterday 00:00 to day after tomorrow 00:00.
var start = time.toZDT('00:00').minusDays(1);
var end = time.toZDT('00:00').plusDays(2);

// Define your tariffs here.
var priceParams = {
  'price1': 3.60,
  'price2': 1.31,
  'tax': 2.83
};

// Calculate distribution price and save them to the database.
var distributionPrices = tariffCalculator.getPrices(start, end, 'seasonal', priceParams);
influx.writePoints('DistributionPrice', distributionPrices);

// Calculate TotalPrices and save them to the database.
var spotPrices = influx.getPoints('SpotPrice', start, end);
var distributionPrices = influx.getPoints('DistributionPrice', start, end);
var totalPrices = tariffCalculator.getTotalPrices(spotPrices, distributionPrices);
influx.writePoints('TotalPrice', totalPrices);

The TariffCalclator class currently supports two distribution pricing logics:

  • Caruna Night Distribution: Day price between 07-22, night price between 22-07.
  • Caruna Seasonal Distribution:
    • Cheaper price from the beginning of April until the end of October
    • Same cheaper price during winter Sundays
    • Same cheaepr price Mon-Sat 22-07
    • More expensive day price during winter daytime 7-22 except Sundays

If your operator has some other pricing logic, you can suggest adding support for it.

Optional: Create a Rule UpdateSpotPriceItem

If you want to render the current spot price in the openHAB user interface, you need to create a Rule that runs every full hour. The Script Action needs to read the spot price for the current hour from the database and update the value of the SpotPrice item so that openHAB knows about the changed price. This is needed because we saved the spot prices to the Influx DB bypassing the openHAB persitence layers.

Note: Running this rule every hour might cause duplicate SpotPrice entries in your database; there is the originally written point by the FetchSpotPrices rule which bypassed normal openHAB persistence layer. When you refresh / update the value of the Item every hour with this optional rule, openHAB may persist the value to your database again.

Inline script action to refresh the value of SpotPrice Item every full hour

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

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

// Update the spot price Item with the price of the current hour
var currentPrice = influx.getCurrentControl('SpotPrice');
var item = items.getItem('SpotPrice');
item.sendCommand(currentPrice);