When you make use of dynamic energy prices, it’s a good idea to start your appliances at the lowest energy price. I will explain in this video how you can do this automatically using a custom template and an automation. Let’s do this!
โญโญโญ NOTE: โญโญโญ
This article accompanies a YouTube video. I wrote it for people who would rather read than watch a video. To keep doing this, I want to ask you to check out the video, leave a comment under the video, give the video a thumbs up, and subscribe to my YouTube channel. This means that the video is offered more often to new visitors so that they also stay informed of the latest Home Assistant tutorials.
Thank you for your support!
Ed
Introduction
Today I am going to explain how you can create a template sensor that returns the lowest energy price and cheapest time within the next couple of hours if you make use of dynamic energy prices. This explanation depends heavily on the knowledge that I shared in this video where I explained how you can monitor your dynamic energy prices. Check that video out first if you didn’t already and then come back to this video. Please note, today’s tutorial can be quite challenging for Home Assistant beginners!
The main thing that you need to be able to work with today’s tutorial is that you installed the Nordpool integration using HACS already. This is explained in the previous video, so if you need help with that, go to that video first. I will use the Nordpool integration to explain how to set up the lowest price and cheapest time template sensor, but this works for the ENTSO-e and Zonneplan integration with a couple of small changes as well. If you haven’t got HACS installed yet, please watch this easy-to-follow video on how to install HACS in Home Assistant. After that, search for the Nordpool integration in HACS and install it. Again, if you have issues with installing the Nordpool integration, go to this video to get help.
Create the custom template sensor to get the lowest energy price and cheapest hour
So, what are we going to create today? First I am going to create a custom template sensor that determines the lowest energy price within a certain timeframe. Then I am going to show the lowest energy price and cheapest hour on a dashboard. And after that, I will create an automation that will start an appliance at the cheapest time or will send you a message to warn you when the price is low so that you can start appliances manually if they are not smart.
Create the Helpers
I want to be able to configure the hours that I want to look ahead for the lowest price. For that, I will create a helper and I will use the value of that helper in my template.
- Go to Settings.
- Go to Devices & Services.
- Click on the Helpers tab.
- Click create Helper.
- Select a Number Helper.
- Give the helper a name. In my case, it’s Nordpool Hours Ahead.
- Select an icon. I will choose mdi:clock for that.
- The minimum value is 0 and the maximum value is 36 in my case.
- I set the display mode to Input Field.
I also created a helper that stores the additional costs of the energy provider in my previous video. This is a number helper as well.
Install Studio Code Server
To create the template, you need to use Studio Code Server.
- You can install this by going to Settings->Add-ons.
- Click on Add-on store.
- Search for Studio Code Server and install studio code server.
- Make sure that Show in sidebar is selected and start the add-on.
Create a templates.yaml file
- Now go to Studio Code server.
- Here you see the list of all the files on your Home Assistant server.
- If you do not make use of a templates.yaml file yet, then open the Configuration.yaml file and add the following line: template: !include templates.yaml
- Now create a templates.yaml file in the config folder by clicking on the “New File” icon.
- Now reboot Home Assistant
Add the template code for the lowest energy price
- Open the templates.yaml file after Home Assistant is restarted.
- Now add the following code to the templates.yaml file.
- I will explain the code line by line to show you how this template works. If you want to know more about how to create your own template sensors, then watch this video where I explain this in more detail.
- I created two template sensors. The first one determines the lowest price in the upcoming hours and the second one determines the cheapest hour in the upcoming hours. I could also create one template sensor and put the values for the price and time in the attributes of that sensor, but I wanted to make this tutorial as simple as possible. I mean, it’s already complicated enough.
You can type the code from the screen, or you can download the code by sponsoring me. This will save you time and frustration. The download code contains everything for the Nordpool integration plus it contains all the code for the ENTSO-e and Zonneplan integration if you use one of those two integrations.
The Norpool Lowest Energy Price Template explained
- The template starts with a name, followed by a unique ID. You can generate the unique_id by clicking on your right mouse button and selecting generate UUID at cursor. It’s always wise to add a unique id to your template so that you can change the template config in the UI of Home Assistant. You cannot do that if a template does not have a unique ID.
- In my case, the icon is a euro icon and the unit of measurement is a euro sign.
- Now the most important part of the template is the state. This is the eventual value of the template.
- We start with defining some variables. The first variable is called hours_ahead which contains the value of our helper nordpool_hours_ahead.
- The second variable contains the current time.
- Then I define a namespace that contains the times, prices, lowest price, and an hour counter. The default value for the lowest price is 100 and the hour counter starts at zero.
What is a namespace?
A namespace is needed if you want to store data in variables in a for loop and use that data also outside the for loop.
- After that, I fill the today variable with a list of all the values of today. These values are attributes of the nordpool sensor as you can see in the developer tools. If you are wondering how that exactly works, I explain this more in detail in the previous video about dynamic energy prices.
- Now I am going to loop through the values of today.
- First I check whether I have already checked all the hours that I want to check in advance. If this is not the case, I retrieve the time from the list.
- Then I check whether the time from the list is at least within the current hour.
- If so, I store the price from the list including the additional costs in a local variable.
- Then I check if the stored price is lower than the lowest price I’ve found in the list so far.
- If the retrieved price is lower, then I store that new lowest price in the namespace variable.
- Then I increase the hour counter and go to the next list item in the list until all the list items have been checked.
- I do the same for tomorrow’s hours in this part of the code. It’s essentially the same code as the one I just explained, but it runs through tomorrow’s hours if they are available and if we still have hours to check within our hours_ahead variable.
- In the end, I return the value of the lowest price and round it to two decimals.
I need your help!
You will be doing me a huge favor if you subscribe to my channel if you haven’t already. And, you will help me a lot if you also give this video a thumbs up and leave a comment. This way, YouTube will present this video to new people, making the channel grow! In the video description, you will also find information about how you can sponsor me so that I can continue to make these tutorials for you.
Thank you!
The Norpool Cheapest Time Template explained
The second template calculates the cheapest time. This code looks a lot like the code for the lowest-price template. It just has an extra variable called cheapest_time and returns that variable as a string that contains the Hour and Minute of the cheapest time. In practice, this is always exactly on the hour.
Now save the template and reload the template by going to Developer Tools, clicking the YAML tab, and clicking on template entities.
Let’s test the values of our templates.
- Go to Developer Tools.
- Click on the States tab.
- Search for Nordpool Lowest Price.
- You see that the lowest price is now returned.
- Search for Nordpool Cheapest hour.
- The cheapest hour is also returned.
Nice, our template is working!
Create the dashboard card for the lowest energy price
Now let’s create a dashboard card that shows these values. I use a mushroom template card for that. If you haven’t installed the mushroom cards yet, do so via HACS. Just search for Mushroom in HACS and you’ll be fine.
- Open one of your Dashboards or create a new one.
- Click on the three dots in the right upper corner and click on Edit Dashboard.
- Click on Add Card.
- Select the Mushroom Template Card.
- For the icon enter mdi:currency-eur or usd.
- You can choose any color you like.
- Then at primary information enter the code to retrieve the lowest price.
- Enter the code to retrieve the cheapest time in the secondary information field.
- Now save the card.
Now you’ve created a card on your dashboard that shows the lowest price. That’s awesome! The only thing that we need to add now is an automation that starts an appliance and or sends a message at the cheapest time.
Create the automation
- For this, go to Settings.
- Go to Automations and Scenes.
- Click Create automation.
- Click Start with an empty automation.
- We are first going to add a template trigger.
- Click on Add trigger.
- Select Template.
- Add the following value template.
- This template checks if the cheapest time is equal to the current time. If so, the trigger will be true and the actions can be performed.
- Go to Actions.
- Here we can start an appliance or send a message. I will just send a message here, but you can also start one of your appliances if you have a smart washer or dryer for instance.
- Click add Action.
- Select Call Service.
- Select a notification service with your phone’s name in it.
- The message can be something like The energy price is low. Start appliances now!
- and the Title can be something like Energy price is low!
- If you want to start an appliance automatically you can click on add action again and select a device.
- In the action of the device, you can then select the action to start the device.
I am showing this on my test environment, so I do not have any devices connected that I can start automatically here. But, a message is already valuable so that you get a signal when to start the appliances that use a lot of energy like your washer, dryer, and dishwasher for instance.
Conclusion
NOTE: There is a caveat in setting the look-ahead time very high. The prices are being retrieved by the integrations at 1PM. If the cheapest hour was for instance 2:00 PM and after the new prices have been retrieved, the cheapest hour changes to 8PM, it might be that you will get surprised that your appliance did not start at 2PM.
I was able to make this tutorial thanks to these wonderful people who are my sponsors. I do this job full time and I am completely dependent on sponsors so that I can pay for my groceries and make these videos for you. Please sponsor me too if you find the work I do valuable. This ensures that I can continue to make these video tutorials for you.
And don’t forget to give this video a thumbs up, subscribe to my channel, and hit the notification bell.
I’ll see you in my next video.
Bye bye.