Cheapest Energy Hours - Jinja macro for dynamic energy prices

UPDATE

v3.1.0

:sparkles: IMPROVEMENTS

  • The macro will try to find the right time_key and value_key itself if it doesn’t match the default, no need to provide it anymore
  • Added an option to provide attr_all in case the source senor provides an attribute with both the prices for today and tomorrow combined. This defaults to prices which is the attribute provided by the ENTSO-e integration

This should work in v3.1.0

{% from 'cheapest_energy_hours.jinja' import cheapest_energy_hours %}
{% set sensor = 'sensor.steven_average_electricity_price_today' %}
{{ cheapest_energy_hours(sensor, hours=1, lowest=True, mode='max') }}
{{ cheapest_energy_hours(sensor, hours=1, lowest=true, mode='time_min', time_format='time24') }}

Update

v3.2.0

:sparkles: IMPROVEMENTS

  • The macro will now also find the right attributes in the source sensor, so no need to provide attr_today, attr_tomorrow and attr_all anymore.

:bug: BUG FIXES

  • the template to find time_key was not working when the time values were datetimes, and not datetime strings.

v3.2.1

:sparkles: IMPROVEMENTS

  • Created a separate section in the readme on the output modes

:bug: BUG FIXES

  • time_key finder was not working in case include_today was set to false

Hey Martijn, can you give an example, how to use this?

are we supposed to create a template sensor for every “load” we want to know the starting hours, and then use this sensor in an automation with time trigger?

@paschdan What do you mean with "load"?

The macro can provide you with datetimes and prices (and combinations of them). It’s up to your imagination how to use it :slight_smile:

As you can see a few posts above you can use it on your dashboard, but you can also use it to find the optimal time to start your washing machine, or when to charge your car.

you are incredible… :+1:

BTW, is your sensor now showing today’s data again for the prices_today attribute? I have an API key now, and for me the ENTSO-e integration is showing the correct data.

I’m running v3.2.1 now, but I still need to setup the time_key finder.

{% from 'cheapest_energy_hours.jinja' import cheapest_energy_hours %}
{% set sensor = 'sensor.electricity_price_today' %}
{{ (cheapest_energy_hours(sensor, hours=1, lowest='true', mode='max')) }}
{{ (cheapest_energy_hours(sensor, time_key='readingDate', hours=1, lowest='true', mode='max')) }}

1 error: Time key “start” not found in data
0.11

Which integration are you using?

can you provide me the attribute data from sensor.electricity_price_today in YAML format from developer tools > states

So please copy the attribute info from the section as shown in my screenshot below.

Prices:
  - price: 0.15
    readingDate: '2023-10-22T22:00:00Z'
  - price: 0.12
    readingDate: '2023-10-22T23:00:00Z'
  - price: 0.12
    readingDate: '2023-10-23T00:00:00Z'
  - price: 0.12
    readingDate: '2023-10-23T01:00:00Z'
  - price: 0.12
    readingDate: '2023-10-23T02:00:00Z'
  - price: 0.13
    readingDate: '2023-10-23T03:00:00Z'
  - price: 0.17
    readingDate: '2023-10-23T04:00:00Z'
  - price: 0.21
    readingDate: '2023-10-23T05:00:00Z'
  - price: 0.24
    readingDate: '2023-10-23T06:00:00Z'
  - price: 0.18
    readingDate: '2023-10-23T07:00:00Z'
  - price: 0.16
    readingDate: '2023-10-23T08:00:00Z'
  - price: 0.13
    readingDate: '2023-10-23T09:00:00Z'
  - price: 0.12
    readingDate: '2023-10-23T10:00:00Z'
  - price: 0.12
    readingDate: '2023-10-23T11:00:00Z'
  - price: 0.13
    readingDate: '2023-10-23T12:00:00Z'
  - price: 0.16
    readingDate: '2023-10-23T13:00:00Z'
  - price: 0.16
    readingDate: '2023-10-23T14:00:00Z'
  - price: 0.18
    readingDate: '2023-10-23T15:00:00Z'
  - price: 0.19
    readingDate: '2023-10-23T16:00:00Z'
  - price: 0.18
    readingDate: '2023-10-23T17:00:00Z'
  - price: 0.15
    readingDate: '2023-10-23T18:00:00Z'
  - price: 0.12
    readingDate: '2023-10-23T19:00:00Z'
  - price: 0.13
    readingDate: '2023-10-23T20:00:00Z'
  - price: 0.11
    readingDate: '2023-10-23T21:00:00Z'
average: 0.15
unit_of_measurement: EUR/kWh
friendly_name: Electricity price today

BTW: I get this info from EnergyZero.

Is that a custom integration? Because the core integration doesn’t give that attribute

Yep, the prices_today attribute is showing the correct data again.
The data for today is no longer in the future :wink:

BTW, this is working

{% from 'cheapest_energy_hours.jinja' import cheapest_energy_hours %}
{% set sensor = 'sensor.steven_average_electricity_price_today' %}
{{ cheapest_energy_hours(sensor, hours=1, lowest=True, mode='max') }}
{{ cheapest_energy_hours(sensor, hours=1, lowest=true, mode='time_min', time_format='time24') }}

1 Like

I use platform: rest to pull data from EnergyZero
https://api.energyzero.nl/v1/energyprices?fromDate=2023-10-22T22%3A00%3A00.000Z&tillDate=2023-10-23T21%3A59%3A59.999Z&interval=4&usageType=1&inclBtw=true

Update

v3.4.0

:sparkles: IMPROVEMENTS

  • Create new output mode to output all other modes in one go

:bug: BUG FIXES

  • fix time_key finder if datetime values are not in local time

fixed in 3.4.0

v3.4.0 fixed the “issue”

1 Like

Great, thanks for the confitmation

BTW lowest defaults to true and hours defaults to 1 and if you only output one hour, the mode time_min will display the same as the default mode start. So this will output the same as your current settings:

{% from 'cheapest_energy_hours.jinja' import cheapest_energy_hours %}
{% set sensor = 'sensor.steven_average_electricity_price_today' %}
{{ cheapest_energy_hours(sensor, mode='max') }}
{{ cheapest_energy_hours(sensor, time_format='time24') }}

Nice, got it !
Another question, I hope you don’t curse me :slight_smile:
Would it also be possible to round the price to 3 decimals in the jinja macro? For now I have created a template sensor with

| round(3) }}"

How many decimals do you get? All values should be rounded to 5 decimals now.
But I can add a parameter to override that with a custom setting

I have 4 decimals now, reducing it to 2 or 3 would be fantastic