Cheapest Energy Hours - Jinja macro for dynamic energy prices

hacs_badge
Version
Buy me a coffe
Paypal

A jinja macro to easily find the cheapest block of hours to know when to turn on your dryer

Why this macro

If your energy provider uses dynamic pricing, you might want to know when to turn on devices which consume a lot of power. It is relatively easy to find the lowest price on the day, but if it takes 3 hours for your dryer to complete, you might want to find the cheapest block of hours. It could be that the cheapest hour is not even in that block.
This macro comes to the rescue!

Funny note :smile: I don’t have a dynamic price contract myself. It started as a challenge to myself to get it working, and gradually expanded to what it is now. I do have the Nordpool and ENTSOE integrations installed to test, but I don’t have real life scenario’s implemented, so I’m also depending on your feedback to improve the macro even further. So please keep sending in issues/questions/other feedback.

In case you are able to reduce your energy bill by using this macro, please consider to buy me a cup of coffee :coffee:

How to install

You need to have Home Assistant 2023.11 or higher installed to use this custom template.

This custom template is compatible with HACS, which means that you can easily download and manage updates for it. Custom templates are currently only available in HACS when you enable experimental features. Make sure to enable it in the HACS settings, which you can access from Settings > Devices & Services > HACS When experimental features are enabled you click the button below to add it to your HACS installation,
Open your Home Assistant instance and open a repository inside the Home Assistant Community Store.

For a manual install you can copy the contents of cheapest_energy_hours.jinja to a jinja file in your custom_templates folder.
Run the homeassistant.reload_custom_templates service call to load the file.

How to use

See the separate documentation section on how to use the macro

Thanks to

  • @basbruss for providing the output mode and a lot of other additions!
  • @hmmbob for reviewing the 5.0.0 documentation
  • All users of the macro, especially those who provide valuable feedback in questions, issue reports or feature requests
8 Likes

Update

Version v3.0.0

:rotating_light: BREAKING

  • The template no longer checks if data plots are complete. I’ve amended the template sensor so it will create a temporary plot while it’s running, so the complete plots are not overwritten while a new plot is created. As the plots not marked as incomplete are per definition complete, there is no need to check on that. There is also no longer a complete key to check on in the new data plots. It might be wise to check if there are incomplete plots in the sensor, and remove them.
  • The data plot sensor is not automatically updated through HACS, you need to do that manually!

:star2: NEW

  • The template now also works with energy providers with dynamic prices which update more than once per hour.
  • It is now possible to remove data plots from the sensor.

:sparkles: IMPROVEMENTS

  • More error messages are added
  • Checks on correct input for the parameters which expect a boolean are added (including error messages if not)

:bug: BUG FIXES

  • Version 2.1.0 introduces a whitespace issue, where the output would have some additional whitespace. This meant you could not convert it to a datetime without using trim first. This is fixed now.

What’s Changed

Full Changelog: Comparing v2.1.0...v3.0.0 · TheFes/cheapest-energy-hours · GitHub

3 Likes

Hello TheFes,

I am trying to get your template sensor going.

I installed it thru HACS (Experimental Features enabled) and can see the jinja file in /config/custom_templates/cheapest_energy_hours.jinja.

I reloaded the Home Assistant Core Integration: Reload custom Jinja2 templates service call.

The integration I am using to pull the energy data from is EntsoE;
I use sensor “sensor.dynamische_prijzen_elektriciteit_average_electricity_price_today”; attribute is “prices_today”.

Attributes for above sensor look like this:
state_class: measurement
prices_today:

  • time: ‘2023-10-17 00:00:00+02:00’
    price: 0.12758
  • time: ‘2023-10-17 01:00:00+02:00’
    price: 0.11404
  • time: ‘2023-10-17 02:00:00+02:00’
    price: 0.1084
  • time: ‘2023-10-17 03:00:00+02:00’
    price: 0.10636
  • time: ‘2023-10-17 04:00:00+02:00’
    price: 0.1021
  • time: ‘2023-10-17 05:00:00+02:00’
    price: 0.10899
  • time: ‘2023-10-17 06:00:00+02:00’
    price: 0.11876
  • time: ‘2023-10-17 07:00:00+02:00’
    price: 0.14877
  • time: ‘2023-10-17 08:00:00+02:00’
    price: 0.17582
  • time: ‘2023-10-17 09:00:00+02:00’
    price: 0.15948
  • time: ‘2023-10-17 10:00:00+02:00’
    price: 0.135
  • time: ‘2023-10-17 11:00:00+02:00’
    price: 0.11376
  • time: ‘2023-10-17 12:00:00+02:00’
    price: 0.10552
  • time: ‘2023-10-17 13:00:00+02:00’
    price: 0.08933
  • time: ‘2023-10-17 14:00:00+02:00’
    price: 0.07948
  • time: ‘2023-10-17 15:00:00+02:00’
    price: 0.09
  • time: ‘2023-10-17 16:00:00+02:00’
    price: 0.10203
  • time: ‘2023-10-17 17:00:00+02:00’
    price: 0.11396
  • time: ‘2023-10-17 18:00:00+02:00’
    price: 0.13865
  • time: ‘2023-10-17 19:00:00+02:00’
    price: 0.15225
  • time: ‘2023-10-17 20:00:00+02:00’
    price: 0.12848
  • time: ‘2023-10-17 21:00:00+02:00’
    price: 0.10776
  • time: ‘2023-10-17 22:00:00+02:00’
    price: 0.09651
  • time: ‘2023-10-17 23:00:00+02:00’
    price: 0.08731

I try the following code in Template:

{% from 'cheapest_energy_hours.jinja' import cheapest_energy_hours %}
{{ cheapest_energy_hours('sensor.dynamische_prijzen_elektriciteit_average_electricity_price_today', attr_today='prices_today', hours=1, lowest=False, mode='max') }}

I get error “UndefinedError: ‘dph’ is undefined”

Could you help me out?

Thanks for your work and your support!

Best regards,

Rogy

Let me look into that.

Does it work like this?

{% from 'cheapest_energy_hours.jinja' import cheapest_energy_hours %}
{{ cheapest_energy_hours('sensor.dynamische_prijzen_elektriciteit_average_electricity_price_today', attr_today='prices_today', hours=1, lowest=False, mode='max', value_key='price', time_key='time') }}

You were missing time_key and value_key in your template. The macro should have told you that, but there was an issue which should be fixed in 3.0.1 now

Yes it works like this!

Thanks much Fes!

I will now take some time to play around with it and figure out how I can use it to steer my heat pump’s electricity consumption in function of electricity prices.

I’ll probably be back with more questions soon :slight_smile:

Best regards, Rogy

Hi TheFes,

I did al the steps needed for installation and restart HA.
cheapest_energy_hours.jinja is located in in * /config/custom_templates
I also use the EntsoE integration. I use “sensor.steven_average_electricity_price_today”

Attributes for the sensor give me

state_class: measurement
prices_today:
  - time: '2023-10-19 00:00:00+02:00'
    price: 0.00544
  - time: '2023-10-19 01:00:00+02:00'
    price: -0.0032
  - time: '2023-10-19 02:00:00+02:00'
    price: -0.00393
  - time: '2023-10-19 03:00:00+02:00'
    price: 0.00031
  - time: '2023-10-19 04:00:00+02:00'
    price: -0.00214
  - time: '2023-10-19 05:00:00+02:00'
    price: 0.0065
  - time: '2023-10-19 06:00:00+02:00'
    price: 0.06553
  - time: '2023-10-19 07:00:00+02:00'
    price: 0.08791
  - time: '2023-10-19 08:00:00+02:00'
    price: 0.11521
  - time: '2023-10-19 09:00:00+02:00'
    price: 0.127
  - time: '2023-10-19 10:00:00+02:00'
    price: 0.1232
  - time: '2023-10-19 11:00:00+02:00'
    price: 0.12572
  - time: '2023-10-19 12:00:00+02:00'
    price: 0.12531
  - time: '2023-10-19 13:00:00+02:00'
    price: 0.12156
  - time: '2023-10-19 14:00:00+02:00'
    price: 0.11933
  - time: '2023-10-19 15:00:00+02:00'
    price: 0.11805
  - time: '2023-10-19 16:00:00+02:00'
    price: 0.12146
  - time: '2023-10-19 17:00:00+02:00'
    price: 0.14234
  - time: '2023-10-19 18:00:00+02:00'
    price: 0.14632
  - time: '2023-10-19 19:00:00+02:00'
    price: 0.14472
  - time: '2023-10-19 20:00:00+02:00'
    price: 0.1168
  - time: '2023-10-19 21:00:00+02:00'
    price: 0.09748
  - time: '2023-10-19 22:00:00+02:00'
    price: 0.10174
  - time: '2023-10-19 23:00:00+02:00'
    price: 0.08958
prices_tomorrow: []
prices:
  - time: '2023-10-18 00:00:00+02:00'
    price: 0.06433
  - time: '2023-10-18 01:00:00+02:00'
    price: 0.04301
  - time: '2023-10-18 02:00:00+02:00'
    price: 0.033
  - time: '2023-10-18 03:00:00+02:00'
    price: 0.01761
  - time: '2023-10-18 04:00:00+02:00'
    price: 0.00973
  - time: '2023-10-18 05:00:00+02:00'
    price: 0.01745
  - time: '2023-10-18 06:00:00+02:00'
    price: 0.05565
  - time: '2023-10-18 07:00:00+02:00'
    price: 0.08221
  - time: '2023-10-18 08:00:00+02:00'
    price: 0.12
  - time: '2023-10-18 09:00:00+02:00'
    price: 0.10571
  - time: '2023-10-18 10:00:00+02:00'
    price: 0.09008
  - time: '2023-10-18 11:00:00+02:00'
    price: 0.07692
  - time: '2023-10-18 12:00:00+02:00'
    price: 0.07009
  - time: '2023-10-18 13:00:00+02:00'
    price: 0.064
  - time: '2023-10-18 14:00:00+02:00'
    price: 0.061
  - time: '2023-10-18 15:00:00+02:00'
    price: 0.04898
  - time: '2023-10-18 16:00:00+02:00'
    price: 0.05099
  - time: '2023-10-18 17:00:00+02:00'
    price: 0.07788
  - time: '2023-10-18 18:00:00+02:00'
    price: 0.09273
  - time: '2023-10-18 19:00:00+02:00'
    price: 0.10917
  - time: '2023-10-18 20:00:00+02:00'
    price: 0.09844
  - time: '2023-10-18 21:00:00+02:00'
    price: 0.08666
  - time: '2023-10-18 22:00:00+02:00'
    price: 0.0661
  - time: '2023-10-18 23:00:00+02:00'
    price: 0.0351
  - time: '2023-10-19 00:00:00+02:00'
    price: 0.00544
  - time: '2023-10-19 01:00:00+02:00'
    price: -0.0032
  - time: '2023-10-19 02:00:00+02:00'
    price: -0.00393
  - time: '2023-10-19 03:00:00+02:00'
    price: 0.00031
  - time: '2023-10-19 04:00:00+02:00'
    price: -0.00214
  - time: '2023-10-19 05:00:00+02:00'
    price: 0.0065
  - time: '2023-10-19 06:00:00+02:00'
    price: 0.06553
  - time: '2023-10-19 07:00:00+02:00'
    price: 0.08791
  - time: '2023-10-19 08:00:00+02:00'
    price: 0.11521
  - time: '2023-10-19 09:00:00+02:00'
    price: 0.127
  - time: '2023-10-19 10:00:00+02:00'
    price: 0.1232
  - time: '2023-10-19 11:00:00+02:00'
    price: 0.12572
  - time: '2023-10-19 12:00:00+02:00'
    price: 0.12531
  - time: '2023-10-19 13:00:00+02:00'
    price: 0.12156
  - time: '2023-10-19 14:00:00+02:00'
    price: 0.11933
  - time: '2023-10-19 15:00:00+02:00'
    price: 0.11805
  - time: '2023-10-19 16:00:00+02:00'
    price: 0.12146
  - time: '2023-10-19 17:00:00+02:00'
    price: 0.14234
  - time: '2023-10-19 18:00:00+02:00'
    price: 0.14632
  - time: '2023-10-19 19:00:00+02:00'
    price: 0.14472
  - time: '2023-10-19 20:00:00+02:00'
    price: 0.1168
  - time: '2023-10-19 21:00:00+02:00'
    price: 0.09748
  - time: '2023-10-19 22:00:00+02:00'
    price: 0.10174
  - time: '2023-10-19 23:00:00+02:00'
    price: 0.08958
unit_of_measurement: €/kWh
attribution: Data provided by ENTSO-e Transparency Platform
device_class: monetary
icon: mdi:currency-eur
friendly_name: Average electricity price today (steven)

In the template editor I throw

{% from "cheapest_energy_hours.jinja" import cheapest_energy_hours %}
{{ cheapest_energy_hours("sensor.steven_average_electricity_price_today", hours=3) }}

and give me this error :
TemplateSyntaxError: expected token ‘,’, got ‘plot_attr’

Would you like to take a look at this?
Many thanks !

looks like I might screwed something up in the last update. Let me check.

Thanks :+1:

found it, fixed in 3.0.3

Note that you also need to provide the attr_today, price_key and value_key like I did here:
https://community.home-assistant.io/t/cheapest-energy-hours-jinja-macro-for-dynamic-energy-prices/569370/5

If you want to use the data of tomorrow, you also need to provide attr_tomorrow

Wow, you’re fast. I’ll try it right away when V3.0.3 is available :+1:

You can press the Update information button in 3 dot menu in HACS to immediately get the update message for the new version

These works as expected

{% from 'cheapest_energy_hours.jinja' import cheapest_energy_hours %}
{{ cheapest_energy_hours('sensor.steven_average_electricity_price_today', attr_today='prices_today', hours=1, lowest=False, mode='max', value_key='price', time_key='time') }}

{% from 'cheapest_energy_hours.jinja' import cheapest_energy_hours %}
{{ cheapest_energy_hours('sensor.steven_average_electricity_price_today', attr_today='prices_today', hours=1, lowest=True, mode='max', value_key='price', time_key='time') }}

Result

0.14632
-0.00393

This one is not working

{% from 'cheapest_energy_hours.jinja' import cheapest_energy_hours %}
{{ cheapest_energy_hours("sensor.steven_average_electricity_price_today",attr_today='prices_today', hours=3,  lowest=True, mode='max', value_key='price', time_format="time24") }}

Result

1 error: Time key "start" not found in data

Would you like to take a look at this?
Many thanks !

That’s because you removed the time_key='time' parameter.
That makes it default to "start"

I’m also confused why you want to provide a time_format parameter there, as mode='max' will not return a datetime

Oops, my mistake
let’s say if I want to get the start datetime of an a 3 hour time block when the energy price is lowest, for today would this one be correct?

{% from 'cheapest_energy_hours.jinja' import cheapest_energy_hours %}
{{ cheapest_energy_hours("sensor.steven_average_electricity_price_today", attr_today='prices_today', hours=3, time_key='time') }}

Now you removed value_key='price'
But besides that, yes

Sorry, confused with the given nordpool examples.
I have it working.Thanks

Much respect for this fantastic jinja macro.I have it up and running. Thank you TheFes!

2 Likes

@HAstevo
Would you mind sharing the code?

@TheFes

Hello,

I use your jinja script for my energy data from EnergyZero.
This works fine, except when I request the time I get the UTC (English) time instead of the local (Dutch) time.

{% from 'cheapest_energy_hours.jinja' import cheapest_energy_hours %}
{{ cheapest_energy_hours('sensor.electricity_price_today', attr_today='Prices', value_key='price', time_key='readingDate', hours=1, lowest='true', mode='time_min', time_format='time24') }}

This results in 11:00 instead of 13:00
How can I adjust that?

unit_of_measurement: EUR/kWh
friendly_name: Electricity price today
Prices:
   -price: 0.06
     readingDate: '2023-10-20T22:00:00Z'
   -price: 0.05
     readingDate: '2023-10-20T23:00:00Z'
   -price: 0.05
     readingDate: '2023-10-21T00:00:00Z'
   -price: 0.04
     readingDate: '2023-10-21T01:00:00Z'
   -price: 0.05
     readingDate: '2023-10-21T02:00:00Z'
   -price: 0.05
     readingDate: '2023-10-21T03:00:00Z'
   -price: 0.05
     readingDate: '2023-10-21T04:00:00Z'
   -price: 0.07
     readingDate: '2023-10-21T05:00:00Z'
   -price: 0.08
     readingDate: '2023-10-21T06:00:00Z'
   -price: 0.07
     readingDate: '2023-10-21T07:00:00Z'
   -price: 0.05
     readingDate: '2023-10-21T08:00:00Z'
   -price: 0.04
     readingDate: '2023-10-21T09:00:00Z'
   -price: 0.02
     readingDate: '2023-10-21T10:00:00Z'
   -price: 0.01
     readingDate: '2023-10-21T11:00:00Z'
   -price: 0.01
     readingDate: '2023-10-21T12:00:00Z'
   -price: 0.04
     readingDate: '2023-10-21T13:00:00Z'
   -price: 0.07
     readingDate: '2023-10-21T14:00:00Z'
   -price: 0.11
     readingDate: '2023-10-21T15:00:00Z'
   -price: 0.13
     readingDate: '2023-10-21T16:00:00Z'
   -price: 0.13
     readingDate: '2023-10-21T17:00:00Z'
   -price: 0.11
     readingDate: '2023-10-21T18:00:00Z'
   -price: 0.06
     readingDate: '2023-10-21T19:00:00Z'
   -price: 0.06
     readingDate: '2023-10-21T20:00:00Z'
   -price: 0.05
     readingDate: '2023-10-21T21:00:00Z'
average: 0.06