{"id":1620,"date":"2023-04-12T14:31:24","date_gmt":"2023-04-12T14:31:24","guid":{"rendered":"https:\/\/www.creatingsmarthome.com\/?p=1620"},"modified":"2023-12-28T08:31:05","modified_gmt":"2023-12-28T08:31:05","slug":"home-assistant-advanced-nord-pool-cheapest-hours-with-local-calendar-support","status":"publish","type":"post","link":"https:\/\/www.creatingsmarthome.com\/index.php\/2023\/04\/12\/home-assistant-advanced-nord-pool-cheapest-hours-with-local-calendar-support\/","title":{"rendered":"Home Assistant: Advanced Nord Pool Cheapest Hours automation with local calendar support"},"content":{"rendered":"\n<p>In few weeks I finally have to move from constant electricity price to the spot price and I&#8217;m going to have to expand my automations a bit. In the most popular article <a rel=\"noreferrer noopener\" href=\"https:\/\/www.creatingsmarthome.com\/index.php\/2022\/09\/17\/home-assistant-nord-pool-spot-prices-and-how-to-automate-devices-for-cheapest-hours\/\" data-type=\"URL\" data-id=\"https:\/\/www.creatingsmarthome.com\/index.php\/2022\/09\/17\/home-assistant-nord-pool-spot-prices-and-how-to-automate-devices-for-cheapest-hours\/\" target=\"_blank\">how to use Nord Pool cheapest hours and automate devices<\/a> 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!<\/p>\n\n\n\n<p>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 &#8220;immediately&#8221; when next day prices are published by the Nord Pool. So that really got me thinking, that I just don&#8217;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!<\/p>\n\n\n\n<p>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 &#8211; 08).<\/p>\n\n\n\n<p>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 \ud83d\ude42<\/p>\n\n\n\n<p>If you wish to know more and see exact features that this package has to offer, continue reading next sections..<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"864\" height=\"374\" src=\"https:\/\/www.creatingsmarthome.com\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-16.02.46.png\" alt=\"\" class=\"wp-image-1791\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Features and what does this package has to REALLY offer<\/h2>\n\n\n\n<p>Here&#8217;s the list this automation package implements (<strong>feature list<\/strong>):<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Calculate wanted number of sequential cheapest hours for the next day immediately when available<\/li>\n\n\n\n<li>Support for two days (today + tomorrow)<\/li>\n\n\n\n<li>Required hour range needs to be easily configurable (e.g. during night, full day, etc.. just like in the previous version)<\/li>\n\n\n\n<li>Once prices are received from the Nord Pool, a calendar event is created for the cheapest hours<\/li>\n\n\n\n<li>Devices(s) will start when &#8216;cheapest hours&#8217; -calendar event begins<\/li>\n\n\n\n<li>Device(s) will stop when &#8216;cheapest hours&#8217; -calendar event is over<\/li>\n\n\n\n<li>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).<\/li>\n\n\n\n<li>Support to use multiple separate cheapest hours sequences for different devices (e.g. car charging, water boiler&#8230;)<\/li>\n<\/ul>\n\n\n\n<p>That&#8217;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.<br><br><strong>This automation is the sequential version of cheapest hours. For non-sequential, checkout <a href=\"https:\/\/www.creatingsmarthome.com\/index.php\/2023\/11\/12\/home-assistant-advanced-nord-pool-cheapest-hours-automation-non-sequential\/\" data-type=\"link\" data-id=\"https:\/\/www.creatingsmarthome.com\/index.php\/2023\/11\/12\/home-assistant-advanced-nord-pool-cheapest-hours-automation-non-sequential\/\">this article<\/a><\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using the local calendar<\/h2>\n\n\n\n<p>Home Assistant release <a rel=\"noreferrer noopener\" href=\"https:\/\/www.home-assistant.io\/blog\/2022\/12\/07\/release-202212\/\" data-type=\"URL\" data-id=\"https:\/\/www.home-assistant.io\/blog\/2022\/12\/07\/release-202212\/\" target=\"_blank\">2022.12<\/a> finally introduced a <a rel=\"noreferrer noopener\" href=\"https:\/\/www.home-assistant.io\/integrations\/local_calendar\/\" data-type=\"URL\" data-id=\"https:\/\/www.home-assistant.io\/integrations\/local_calendar\/\" target=\"_blank\">local calendar<\/a> 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&#8217;t even need those pesky little time helpers to hold our information anymore.<\/p>\n\n\n\n<p>Anyhow, there is still one missing features with the local calendar implementation that we need to handle or make a workaround:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We can&#8217;t check if calendar event has already been created<\/li>\n<\/ul>\n\n\n\n<p>This can luckily be worked around though with a small boolean helper utility to keep the information about calendar mark being created.<\/p>\n\n\n\n<p>That&#8217;s the necessary background information now.. time to get hands dirty set the package + configurations!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"setting-up-the-local-calendar\">Setting up the local calendar<\/h3>\n\n\n\n<p>So we need to create a calendar, great! In my package, the calendar is called &#8216;<em>electricity<\/em>&#8216; so I suggest to use the same or else you need to rename the calendar events in the package as well. <\/p>\n\n\n\n<p>Creating the calendar can be done by going to <strong>Home Assistant Settings -&gt; Devices &amp; Services -&gt; + Add Integration (bottom right corner) -&gt; Search for &#8216;Local Calendar&#8217; <\/strong>and click it. <\/p>\n\n\n\n<p>Next enter name &#8216;electricity&#8217; and press Submit, done! Now the calendar is in place and we are ready to move on to the actual automations!<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"994\" height=\"534\" data-id=\"1784\" src=\"https:\/\/www.creatingsmarthome.com\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-10-at-10.09.53.png\" alt=\"\" class=\"wp-image-1784\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"808\" height=\"502\" data-id=\"1783\" src=\"https:\/\/www.creatingsmarthome.com\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-10-at-10.10.13.png\" alt=\"\" class=\"wp-image-1783\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"804\" height=\"302\" data-id=\"1785\" src=\"https:\/\/www.creatingsmarthome.com\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-10-at-10.10.20.png\" alt=\"\" class=\"wp-image-1785\"\/><\/figure>\n<\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Setting up the package<\/h2>\n\n\n\n<p>Prerequisites for this package to work is to have Nord Pool integration installed. You can look for integration installation guide from <a rel=\"noreferrer noopener\" href=\"https:\/\/www.creatingsmarthome.com\/index.php\/2022\/09\/17\/home-assistant-nord-pool-spot-prices-and-how-to-automate-devices-for-cheapest-hours\/\" data-type=\"URL\" data-id=\"https:\/\/www.creatingsmarthome.com\/index.php\/2022\/09\/17\/home-assistant-nord-pool-spot-prices-and-how-to-automate-devices-for-cheapest-hours\/\" target=\"_blank\">the original cheapest hours article<\/a> or directly from <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/custom-components\/nordpool\" data-type=\"URL\" data-id=\"https:\/\/github.com\/custom-components\/nordpool\" target=\"_blank\">Nord Pool integration GitHub page.<\/a> Once that is done, continue reading..<\/p>\n\n\n\n<p>Advanced cheapest hours package can be found from my <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/kotope\/ha_nordpool_cheapest_hours\/blob\/main\/advanced_cheapest_hours.yaml\" data-type=\"URL\" data-id=\"https:\/\/github.com\/kotope\/ha_nordpool_cheapest_hours\/blob\/main\/advanced_cheapest_hours.yaml\" target=\"_blank\">Cheapest hours GitHub page<\/a>, so download it and copy it on your Home Assistant &#8216;<em>config<\/em>&#8216; folder.<\/p>\n\n\n\n<p>After copied, find the <em>homeassistant:<\/em> -block from your <em>configurations.yaml<\/em> file and add the package include on it. If you don&#8217;t have <em>homeassistant:<\/em> -block, just copy the whole block in any root level of <em>configuration.yaml<\/em> file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>homeassistant:\n  packages:\n    cheapest_hours: !include advanced_cheapest_hours.yaml<\/code><\/pre>\n\n\n\n<p>More details about Home Assistant packages can be found from <a href=\"https:\/\/www.home-assistant.io\/docs\/configuration\/packages\/\" data-type=\"URL\" data-id=\"https:\/\/www.home-assistant.io\/docs\/configuration\/packages\/\">here<\/a>. Or if in doubt, leave a comment on the comment field and I&#8217;ll try to help you out \ud83d\ude42<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Configurations<\/h2>\n\n\n\n<p>This version of the cheapest hours package is quite much easier to configure than the previous one. So open up the file <em>advanced_cheapest_hours.yaml<\/em> for editing. All the required lines to be edited can be found with <em>#CHANGE-ME<\/em> tag! <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mandatory configuration<\/h3>\n\n\n\n<p>First there are five sensor attributes that needs to be changed:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>attributes:\n# CHANGE-ME: Set your personal configurations in here\n     # Amount of sequantial cheapest hours in search\n     number_of_sequential_hours: 3\n     # Search starting hour\n     first_hour: 22\n     # Search ending hour\n     last_hour: 08\n     # Is the first_hour today (true \/ false). If false, first_hour needs to be before last_hour. \n     starting_today: true\n     # Nord pool sensor id. Check it ouf from your integrations page! \n     sensor: sensor.nordpool_kwh_fi_eur_3_10_01\n     # If nordpool fetch fails, starting time to make the calendar entry\n     fail_safe_starting: '00:00' <\/code><\/pre>\n\n\n\n<p><sub>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.<\/sub><\/p>\n\n\n\n<p>Next thing to do is to implement the automation actions. In the package, there&#8217;s two more <em>#CHANGE-ME<\/em> tags in the next section. Those are the actual automation actions that should be done during the cheapest hours.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># CHANGE-ME: Actions to do when cheapest hours starts\n      - service: light.turn_on\n        entity_id: light.office_work\n      else:\n# CHANGE-ME: Actions to do when cheapest hours ends\n      - service: light.turn_off\n        entity_id: light.office_work<\/code><\/pre>\n\n\n\n<p><sub>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!<\/sub><\/p>\n\n\n\n<p>With these changes made, the automation should be ready to go! Just reload yaml file using &#8216;<strong>developer tools -&gt; restart<\/strong>&#8216; -functionality. If everything is working properly, a calendar event should be created to the local calendar called &#8216;electricity&#8217; when the clock hits full hours <strong>AND<\/strong> tomorrow prices are available. Along with that, the configured actions should start running once the calendar event (cheapest hour) is hit.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating multiple sequences (optional)<\/h2>\n\n\n\n<p>If it&#8217;s not enough to have one cheapest hours sequence, with this package it&#8217;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:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Copy the templated sensor<\/strong> &#8220;Cheapest hours energy&#8221; and rename its <strong>unique_id<\/strong> and <strong>name<\/strong> something more suitable. Remember to write this unique_id down somewhere. You are going to need it on the later steps.<\/li>\n\n\n\n<li><strong>Make a copy of the first automation<\/strong> &#8216;<em>cheapest_hours_calendar_entry<\/em>&#8216; and rename it to something more suitable (also change the <strong>id<\/strong>). After copied, rename the <strong>sensorId<\/strong> of to the templated sensor <strong>unique_id<\/strong> you made previously. And of course change the actions to what every you like to do during the cheapest hours sequence.<\/li>\n\n\n\n<li>On the second automation &#8216;<em>cheapest_hours_set_sequence<\/em>&#8216;, <strong>make again a new copy<\/strong> &#8216;<em>calendar.create_event<\/em>&#8216; <strong>action<\/strong>. On this new action, rename all the <strong>sensorId<\/strong>s from this action to the templated sensor (<strong>unique_id<\/strong>) you made previously.<\/li>\n\n\n\n<li>Last, but not least, make the failsafe work with the new automation. <strong>Make once again a copy<\/strong> of another &#8216;<em>calendar.create_event<\/em>&#8216; <strong>action<\/strong> of the third automation &#8216;<em>cheapest_hours_failsafe<\/em>&#8216; and rename the all <strong>sensorId<\/strong>s of this new action to the templated sensor unique_id you made previously.<\/li>\n<\/ol>\n\n\n\n<p>That&#8217;s about it! Multiple entires should now be created once cheapest hours are received and automations should run when specific calendar mark occurs!<\/p>\n\n\n\n<p>In my GitHub page, there&#8217;s also an <a href=\"https:\/\/github.com\/kotope\/ha_nordpool_cheapest_hours\/blob\/main\/advanced_cheapest_hours_multiple.yaml\" data-type=\"URL\" data-id=\"https:\/\/github.com\/kotope\/ha_nordpool_cheapest_hours\/blob\/main\/advanced_cheapest_hours_multiple.yaml\" target=\"_blank\" rel=\"noreferrer noopener\">example with two separate time schedules<\/a> in the same package. You can look example directly from that one.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Bonus: Making the UI<\/h2>\n\n\n\n<p>To make the advanced cheapest hours visible through Home Assistant UI it&#8217;s a good idea to add a calendar card in a view.<\/p>\n\n\n\n<p>First go to a dashboard and select a view you would like to add the card. Then press <strong>&#8216;Menu&#8217; -&gt; &#8216;Edit Dashboard&#8217;<\/strong>. In edit mode, press &#8216;<strong>+ add new card<\/strong>&#8216; and select <strong>&#8216;Calendar&#8217;<\/strong>.<\/p>\n\n\n\n<p>Remove all the pre-selected calendars (if any) and select one entity <strong>&#8216;electricity&#8217;<\/strong>. I&#8217;d suggest to use <strong>&#8216;List (7 days)&#8217;<\/strong> as a display mode and it will create you a calendar card as shown below.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"455\" height=\"284\" src=\"https:\/\/www.creatingsmarthome.com\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-15.31.38.png\" alt=\"\" class=\"wp-image-1780\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion and what&#8217;s next<\/h2>\n\n\n\n<p>I&#8217;ve been running this automation for a week now and so far it has worked great! <\/p>\n\n\n\n<p>The configuration is now much easier, but I&#8217;m not still very happy when configuring for multiple sequences. For this, I&#8217;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&#8217;s and the package will be automatically generated and can be downloaded without touching the contents of the file itself at all!<\/p>\n\n\n\n<p>However, based on the time I&#8217;ve got in my hands, it might take &#8216;some time&#8217;. So keep checking the blog frequently to get the utility immediately when available \ud83d\ude42 <\/p>\n\n\n\n<p>Another thing what do next is to handle non-sequential hours. Some device doesn&#8217;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&#8217;t need to be sequential.<\/p>\n\n\n\n<p>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?<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<div style=\"border: 3px dashed #abb8c3; border-radius: 0%; background-color: inherit; \" class=\"ub-styled-box ub-bordered-box wp-block-ub-styled-box\" id=\"ub-styled-box-aef8a7bc-43a2-481d-ae01-730db23b8a15\">\n<p id=\"ub-styled-box-bordered-content-\"><h5 class=\"wp-block-heading has-text-align-center\">Did you find this guide helpful? You can keep the blog going by bidding me a coffee!<\/h5>\r\n<center>\r\n<script type=\"text\/javascript\" src=\"https:\/\/cdnjs.buymeacoffee.com\/1.0.0\/button.prod.min.js\" data-name=\"bmc-button\" data-slug=\"tokorhon\" data-color=\"#FFDD00\" data-emoji=\"\"  data-font=\"Cookie\" data-text=\"Buy me a coffee\" data-outline-color=\"#000000\" data-font-color=\"#000000\" data-coffee-color=\"#ffffff\" ><\/script>\r\n<\/center><\/p>\n\n\n<\/div>","protected":false},"excerpt":{"rendered":"<p>In few weeks I finally have to move from constant electricity price to the spot price and I&#8217;m going to have to expand my automations&hellip;<\/p>\n","protected":false},"author":1,"featured_media":1826,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[100],"tags":[105,273,269,224,267,268,106,7,270,218,266,272],"class_list":["post-1620","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-home-assistant","tag-automation","tag-car-charging","tag-cheap-prices","tag-cheapest-hours","tag-energy","tag-energy-price","tag-energy-saving","tag-home-assistant","tag-how-to","tag-nord-pool","tag-nordpool","tag-water-boiler","has-post-thumbnail-archive"],"acf":[],"featured_image_src":"https:\/\/www.creatingsmarthome.com\/wp-content\/uploads\/2023\/04\/cheapest_hours_calendar_title.png","author_info":{"display_name":"Toni","author_link":"https:\/\/www.creatingsmarthome.com\/index.php\/author\/topsy\/"},"_links":{"self":[{"href":"https:\/\/www.creatingsmarthome.com\/index.php\/wp-json\/wp\/v2\/posts\/1620","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.creatingsmarthome.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.creatingsmarthome.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.creatingsmarthome.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.creatingsmarthome.com\/index.php\/wp-json\/wp\/v2\/comments?post=1620"}],"version-history":[{"count":69,"href":"https:\/\/www.creatingsmarthome.com\/index.php\/wp-json\/wp\/v2\/posts\/1620\/revisions"}],"predecessor-version":[{"id":2593,"href":"https:\/\/www.creatingsmarthome.com\/index.php\/wp-json\/wp\/v2\/posts\/1620\/revisions\/2593"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.creatingsmarthome.com\/index.php\/wp-json\/wp\/v2\/media\/1826"}],"wp:attachment":[{"href":"https:\/\/www.creatingsmarthome.com\/index.php\/wp-json\/wp\/v2\/media?parent=1620"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.creatingsmarthome.com\/index.php\/wp-json\/wp\/v2\/categories?post=1620"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.creatingsmarthome.com\/index.php\/wp-json\/wp\/v2\/tags?post=1620"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}