In few weeks I finally have to move from constant electricity price to the spot price and I’m going to have to expand my automations a bit. In the most popular article how to use Nord Pool cheapest hours and automate devices I did the automation that will search for cheapest sequential hours. However, there are some flaws in that I wanted to finally handle myself and create even better automation!
One of the biggest issue (in my opinion) was the automation time of next day being set at 23:15. I want to be able to see the next days cheapest hours “immediately” when next day prices are published by the Nord Pool. So that really got me thinking, that I just don’t want to use one variable to store the time, but I could use a calendar to actually mark the sequential hours and run the automations by the calendar entries instead!
Second issue was only able to use one single day (the next day) prices and the automation was not able expand between two days (e.g. 22 – 08).
And one more thing (not a big problem for me), I think the configurations were a bit messy, so maybe its time to try to make this package a bit more configurable 🙂
If you wish to know more and see exact features that this package has to offer, continue reading next sections..
Features and what does this package has to REALLY offer
Here’s the list this automation package implements (feature list):
- Calculate wanted number of sequential cheapest hours for the next day immediately when available
- Support for two days (today + tomorrow)
- Required hour range needs to be easily configurable (e.g. during night, full day, etc.. just like in the previous version)
- Once prices are received from the Nord Pool, a calendar event is created for the cheapest hours
- Devices(s) will start when ‘cheapest hours’ -calendar event begins
- Device(s) will stop when ‘cheapest hours’ -calendar event is over
- If prices are not fetched by 23:15, create a static cheapest hours event from 00:00 to XX:XX depending on the selected number of sequential hours. This will provide us a failsafe that no critical devices are being left out of electricity (e.g. water boiler).
- Support to use multiple separate cheapest hours sequences for different devices (e.g. car charging, water boiler…)
That’s about it.. with those features in place, it should be rather simple and error proof to run devices during the hours of cheapest electricity prices.
Using the local calendar
Home Assistant release 2022.12 finally introduced a local calendar support that can be used exactly like these situations! No longer we need to rely on a cloud based calendar, no-one wants to run critical automations based on a cloud connection, right? And we don’t even need those pesky little time helpers to hold our information anymore.
Anyhow, there is still one missing features with the local calendar implementation that we need to handle or make a workaround:
- We can’t check if calendar event has already been created
This can luckily be worked around though with a small boolean helper utility to keep the information about calendar mark being created.
That’s the necessary background information now.. time to get hands dirty set the package + configurations!
Setting up the local calendar
So we need to create a calendar, great! In my package, the calendar is called ‘electricity‘ so I suggest to use the same or else you need to rename the calendar events in the package as well.
Creating the calendar can be done by going to Home Assistant Settings -> Devices & Services -> + Add Integration (bottom right corner) -> Search for ‘Local Calendar’ and click it.
Next enter name ‘electricity’ and press Submit, done! Now the calendar is in place and we are ready to move on to the actual automations!
Setting up the package
Prerequisites for this package to work is to have Nord Pool integration installed. You can look for integration installation guide from the original cheapest hours article or directly from Nord Pool integration GitHub page. Once that is done, continue reading..
Advanced cheapest hours package can be found from my Cheapest hours GitHub page, so download it and copy it on your Home Assistant ‘config‘ folder.
After copied, find the homeassistant: -block from your configurations.yaml file and add the package include on it. If you don’t have homeassistant: -block, just copy the whole block in any root level of configuration.yaml file:
homeassistant: packages: cheapest_hours: !include advanced_cheapest_hours.yaml
More details about Home Assistant packages can be found from here. Or if in doubt, leave a comment on the comment field and I’ll try to help you out 🙂
This version of the cheapest hours package is quite much easier to configure than the previous one. So open up the file advanced_cheapest_hours.yaml for editing. All the required lines to be edited can be found with #CHANGE-ME tag!
First there are five sensor attributes that needs to be changed:
attributes: # CHANGE-ME: Set your personal configurations in here # Amount of sequantial cheapest hours in search number_of_sequential_hours: 3 # Search starting hour first_hour: 22 # Search ending hour last_hour: 08 # Is the first_hour today (true / false). If false, first_hour needs to be before last_hour. starting_today: true # Nord pool sensor id. Check it ouf from your integrations page! sensor: sensor.nordpool_kwh_fi_eur_3_10_01 # If nordpool fetch fails, starting time to make the calendar entry fail_safe_starting: '00:00'
The example configuration above will search for cheapest three sequential hours between today 22:00 and tomorrow 08:00. If failing to get the hours, it will mark the time from 00:00 to 03:00.
Next thing to do is to implement the automation actions. In the package, there’s two more #CHANGE-ME tags in the next section. Those are the actual automation actions that should be done during the cheapest hours.
# CHANGE-ME: Actions to do when cheapest hours starts - service: light.turn_on entity_id: light.office_work else: # CHANGE-ME: Actions to do when cheapest hours ends - service: light.turn_off entity_id: light.office_work
In above example, there are actions to turn on and off the light.office entity during the cheapest hours. Just change these to what every you want to do during the cheapest hours configure previosy!
With these changes made, the automation should be ready to go! Just reload yaml file using ‘developer tools -> restart‘ -functionality. If everything is working properly, a calendar event should be created to the local calendar called ‘electricity’ when the clock hits full hours AND tomorrow prices are available. Along with that, the configured actions should start running once the calendar event (cheapest hour) is hit.
Creating multiple sequences (optional)
If it’s not enough to have one cheapest hours sequence, with this package it’s also possible to make multiple sequence. For example: five hours sequence to run the water boiler between 22-06 and load the car between 00-23 for 8 hours every day. Let me explain how to achieve that:
- Copy the templated sensor “Cheapest hours energy” and rename its unique_id and name something more suitable. Remember to write this unique_id down somewhere. You are going to need it on the later steps.
- Make a copy of the first automation ‘cheapest_hours_calendar_entry‘ and rename it to something more suitable (also change the id). After copied, rename the sensorId of to the templated sensor unique_id you made previously. And of course change the actions to what every you like to do during the cheapest hours sequence.
- On the second automation ‘cheapest_hours_set_sequence‘, make again a new copy ‘calendar.create_event‘ action. On this new action, rename all the sensorIds from this action to the templated sensor (unique_id) you made previously.
- Last, but not least, make the failsafe work with the new automation. Make once again a copy of another ‘calendar.create_event‘ action of the third automation ‘cheapest_hours_failsafe‘ and rename the all sensorIds of this new action to the templated sensor unique_id you made previously.
That’s about it! Multiple entires should now be created once cheapest hours are received and automations should run when specific calendar mark occurs!
In my GitHub page, there’s also an example with two separate time schedules in the same package. You can look example directly from that one.
Bonus: Making the UI
To make the advanced cheapest hours visible through Home Assistant UI it’s a good idea to add a calendar card in a view.
First go to a dashboard and select a view you would like to add the card. Then press ‘Menu’ -> ‘Edit Dashboard’. In edit mode, press ‘+ add new card‘ and select ‘Calendar’.
Remove all the pre-selected calendars (if any) and select one entity ‘electricity’. I’d suggest to use ‘List (7 days)’ as a display mode and it will create you a calendar card as shown below.
Conclusion and what’s next
I’ve been running this automation for a week now and so far it has worked great!
The configuration is now much easier, but I’m not still very happy when configuring for multiple sequences. For this, I’m planning to implement a configuration utility to web that will construct the package dynamically based on given information. That way user just has to enter all the info he/she want’s and the package will be automatically generated and can be downloaded without touching the contents of the file itself at all!
However, based on the time I’ve got in my hands, it might take ‘some time’. So keep checking the blog frequently to get the utility immediately when available 🙂
Another thing what do next is to handle non-sequential hours. Some device doesn’t need the hours to be sequential and could be activated when ever the electricity is cheap. E.g. water boiler could run over night for five hours, but those hours don’t need to be sequential.
Anyhow, drop a comment below and say what do you think about this package? Is this better or worse than the previous version? And what would you improve or do you have any feature requests?