This script uses EPEX spot hourly energy prices for the Austrian or German market to control the power output of a Shelly device. It runs directly on the Shelly and the only technical requirement is that the Shelly has access to the internet. The script should run on all Gen2+ Shelly switches.
The goal of the script is to activate power output when prices are at their lowest during a predefined daily time window. The script behavior can be customized with several configuration variables and it operates as follows:
You define a schedule (e.g. 15:00 every day), a switch on duration (e.g. 4 hours) and a time window (e.g. 7:00 to 19:00).
With the above settings, the script runs every day at 15:00 and fetches the hourly energy prices for the time frame from 7:00 to 19:00 of the next day. It then identifies the 4-hour-block with the lowest average price within this time frame and sets timers to activate power output at the start of the block and deactivate it at the end.
After each execution, the script writes the calculated switch times to the Key-Value Store of the Shelly where they can be reviewed:
There are two additional features:
- You can define a hard price limit (e.g. 10 cent/kWh). If the calculated lowest price is higher than this limit, the script will NOT activate power output at all for the current time window.
- Telegram integration: Optionally, the script can send you a Telegram message with schedule and price details after each execution. It can also send messages when it activates and deactivates power output.
The hourly prices are retrieved from the Awattar API. Awattar generously offers free access to the Austrian and German market prices - see their Austrian or German websites for details about their services.
Before you start the installation process, make sure that you have the latest version of the Shelly firmware installed (you can review the firmware status via Settings
/Firmware
).
- Enter the IP Address of your Shelly in the URL field of your browser.
- Select the
Scripts
Tab. - Click on the
Create Script
button. - Copy the COMPLETE source code from this link into the script window.
- (Optional): Enter a script name in the corresponding field.
- Change the configuration variables in the script to your preference (see next section for details).
- Click the
Save
button. - Click the
Start
button. The script is now running. - Go back to the
Scripts
tab and make sure that the text below the script name saysRunning
. - Activate the
Run on startup
switch to make sure that the script restarts after a reboot of the device.
The first ACTUAL calculation occurs when the first automatic execution is run (which, in the default setting, will be at 15:00 after the installation) and you will not see a calculation result on the KVS page before that time.
The behavior of the script can be customized by changing the following variables to your needs:
Defines which Awattar API will be used by the script - at
for the Austrian or de
for the German API.
This is the execution schedule for the script. With the default setting, the script will run every day at 15:00.
See this site for a description on how to set this.
Usually, you do not need to change this variable unless you want to run several instances of the script
simultaneously (for example when you want to have different schedules on work days and weekends).
Number of hours that the device will be switched on in one go. The default of four means that the script
will activate power output during the cheapest four hours of the time window.
The switchOnDuration must be a whole number in the range of 1 to 24.
These two variables define the time window within which the cheapest hours will be found. The default time
window is 7:00 to 19:00.
Both values must be whole numbers in the range of 0 to 23. If you want the time window to match the calendar day, set both values to zero.
The time window will go over midnight if the end hour is less than the start hour - e. g. a start hour of 20
and an end hour of 4 sets a window from 20:00 to 4:00 on the following day.
This variable defines a price limit which is expressed in cent per kWh. If the average price of the cheapest
period in the time window is higher than this value, the power will not be switched on at all during the time
window.
The price limit can have decimals - e.g. a value of 10.5 cent is perfectly fine.
The default value of Infinity means that there is no price limit.
Set this to true
to activate the Telegram feature. In order to use this feature, you need to have Telegram
installed. You also need a Telegram token and a Telegram ChatID. A description on how to obtain both can be
found here:
How to get
Telegram Bot Chat ID.
The following variables are only used when telegramActive is true
:
Both variables MUST be filled when telegramActive is true - otherwise, the feature will not work.
The value of this variable will be included in the Telegram message in order to identify the sender. Especially useful when you run the script on several Shellies and want to know which one send which message.
If true, the script will send a Telegram message with the calculated times and prices after each execution.
If true, the script will send a Telegram message when power output is switched on by the script.
If true, the script will send a Telegram message when power output is switched off by the script.
If there are no specific upgrade instructions in the CHANGELOG, use the following steps:
- Note down the values of your configuration variables
- Stop the script
- COMPLETELY replace the code of the script with the new version
- Reapply the values of your configuration variables from step 1
- Start the script