Home Assistant logo

How to pick the cheapest hours of electricity using Home Assistant?

3 min read.

Time to write about Home Assistant for a change!

I’ve been using the home automation platform for a couple of years now and while it’s immensely powerful, you also run into all kinds of issues. I could rant in length about one weather station or how difficult it is to get some stuff working on Docker on Windows (and maybe I already have!), but overall it’s incredible how many complex things you can do just like that.

And one somewhat complex thing (that you CAN do with commercial solutions, but heck, Home Assistant is FREE!) is scheduling different devices on electricity cost. And this, dear reader, is what I’ll be talking about today.


With Home Assistant, you can control things like switches, lights, heating, wifi plugs, and.. Relays. And if you can control a relay, you can control pretty much anything behind it.

And in my old house, the 2 biggest electricity consumers are my water boiler and sauna heater. The latter I’ll ignore because it’s non-negotiable, but a water boiler is something that needs to be on every day, but not every hour.

According to my very scientific calculation (i.e. looking at electricity consumption charts), my water heater needs to be on 2-4 hours per day to produce enough hot water for my family. And while I can’t control the heater itself (it’s old and dumb), with Home Assistant, I have the freedom to decide when the thing is on.


So my solution makes use of a sensor (of type “template”) that I configure using YAML. I get the electricity prices from Nordpool (a readymade integration in Home Assistant) but you can use whichever price provider that’s most relevant to you.


  • You get an array of hourly electricity prices (mine is nordpool’s “raw_today” attribute)
  • Decide how many of the cheapest hours you want to pick (I’m picking 6)
  • (have a device that’s high consumption and set up behind a relay you can control with Home Assistant)


Enough chit-chat, show me the code!

And here goes:

- platform: template 
      friendly_name: Cheapest electricity price
      value_template: >
        {# You can't set a variable value inside a loop, unless it's a namespace #}
        {% set ns = namespace(isCheapHour=false) %}      
        {% for item in state_attr('sensor.nordpool_kwh_fi_eur', 'raw_today')|sort(reverse=false, attribute="value") %}
          {% if loop.index <= 6 %}
            {% if as_timestamp(item.start) < as_timestamp(now()) and as_timestamp(item.end) > as_timestamp(now()) %}
              {% set ns.isCheapHour = true %}
            {% endif %}
          {% endif %}
        {% endfor %}
        {{ ns.isCheapHour }}

This provides a sensor that’ll show right now, whether it’s the cheapest hour or not. It looks like this in Home Assistant:

And when you combine the info with the actual electricity cost (sans tariff/transfer & some taxes, mind you):

Of course, it’ll pick the cheapest within the 24 hours that forms a day. These hours might be adjacent, or distributed evenly for the duration of the day. It all depends on the costs.

And this might sometimes lead to slightly confusing and not the most convenient results:

Usage sample

And how do you use it in practice?

Well, here’s another template sensor I have – the one that controls whether my heat pumps should actively cool during the summer or just clean air:

{% if (states('binary_sensor.is_time_for_probablycoolingduringsummer')|bool and states('input_boolean.ac_heating_mode')|bool == false) and (states('binary_sensor.ha_excesselectricityavailable_low')|bool or states('sensor.cheapest_hours_of_the_day')|bool) %}
  {{ True }}
{% else %}
  {{ False }}
{% endif %}

In plain English:

  • If it’s daytime (yeah, that’s the weird binary_sensor – naming things is hard!) and
  • heating is “off” (we don’t want to cool during the heating season and I don’t trust automation) and
  • either we have at least a bit of excess electricity (I have a smart meter and I’m pulling the data of it) or
  • it’s the cheapest time of the day

We’re good to cool the house. Otherwise, nope, we’re not turning those bad boys on. (And of course, the devices will only cool to my target temperature – not indefinitely!)

Yay for the environment!

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments