Jump to content
🌟 NOTIFICATION/Benachrichtigung: Welcome to our New Store! - shelly.com 🌟 ×

Script: Price-based Control of Shelly Devices


Recommended Posts

I have published a script that can use variable hourly energy prices for the Austrian or German market (provided by the Awattar API) to control the power output of a Shelly device. Installation and configuration are described extensively in the link below - here is a short overview:

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 (see attached image for an example).

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.

Here is the link to the Github repository:

https://github.com/towiat/spotelly

Have fun!

KVSMessage.png

Link to comment
Share on other sites

Thank you very much for the script!
I tried it out right away, but unfortunately, I get an error message every time I start it.
Do you have any idea what could be causing this?
Thanks in advance from the woodquarter 🙂

Uncaught TypeError: Assignment to a constant 08:02:10
at const call = job.calls[0]; 08:02:10
^ 08:02:10
in function called from system 08:02:10

 

Edit:

Chatgpt told me that the Error is caused because of a false assignment to a constant variable:

Issue: Uncaught TypeError: Assignment to a constant variable

If you encounter the error Uncaught TypeError: Assignment to a constant in your JavaScript code, especially in lines similar to:

 

const call = job.calls[0];
call.params.code = "someValue"; // Error occurs here

It happens because const variables cannot have their properties reassigned. To fix this, change the declaration from const to let:

let call = job.calls[0];
call.params.code = "someValue"; // This works

The error is common when working with complex objects or configurations that are modified in loops. Just ensure you use let instead of const when you need to update object properties.

Edited by gabbagnom
Link to comment
Share on other sites

Huh, this is weird - my Shelly has no issue whatsoever with this line (and as far as I can tell, it is actually correct from a JavaScript perspective).

Anyway, I have changed the script so that this error can no longer occur - please try again with the current version and let me know if it works.

Und schöne Grüße ins Waldviertel 😁!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Erstelle neue...