Pull cheapest electricity price for template sensor - Solar

So you installed the custom template in HACS right?

Does that sensor also provide a raw_today attribute? Can you show the attribute data from devtools > states?

Are you using this integration? In that case you should have all the data needed for the macro

Assuming you installed the macro from HACS, try this in developer tools > templates

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

@celodnb
I had a closer look at the code you provided, and read your post a bit better.

This should give the same 7:00 timestamp (as a full datetime isoformat string) as you already had. Note that I have it end at “10:00”. My macro will take the end provided as the end of the 3 hour period, so if you provide end='07:00' the start of the 3 hour period will be 04:00 latest

{% from 'cheapest_energy_hours.jinja' import cheapest_energy_hours %}
{{ cheapest_energy_hours('sensor.energi_data_service', hours=3, end='10:00') }}

Then this (with the same import above it) will give the lowest price in that 3 hour period:

{{ cheapest_energy_hours('sensor.energi_data_service', hours=3, end='10:00', mode='min') }}

And this will return the highest price

{{ cheapest_energy_hours('sensor.energi_data_service', hours=3, end='10:00', mode='max') }}

@celodnb did you get it working?

Hi, really sorry for the late reply! Thanks for your efforts, but so far I’ve not got it working (but I also haven’t spent much time on it so far). Time’s limited these days, so right now I’m prioritising a couple of other HA projects when time between kids allows :rofl:

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

This has been solved, thanks to TheFes.

See this thread:
Cheapest Energy Hours - Jinja macro for dynamic energy prices - Share your Projects! - Home Assistant Community (home-assistant.io)

1 Like

I am using the energy provider Tibber that provides the prices like this (see picture).
Is there a way to use the macro for this sensor format as well? If I try to apply the macro it always complains “UndefinedError: ‘h’ is undefined”. Any thoughts here that could help?

The macro expects a datetime (string) with the time the price is valid. I could add support for the list you now have, assuming the first item is the price at midnight.

Don’t know when I will find some time for that. You could also create a template sensor to transform your data to the expected format

You can try with this

template:
  - sensor:
      - unique_id: 9706c04c-7f21-492f-851f-4f854eaaa7ae
        state: "{{ states('sensor.tibber_prices') }}"
        name: Tibber prices for macro
        attributes:
          today: >
            {% set source = state_attr('sensor.tibber_prices', 'today') | default([], true)%}
            {% set ns = namespace(new=[]) %}
            {% for i in source %}
              {% set td = timedelta(hours=24/source|count) %}
              {% set ns.new = ns.new + [dict(datetime=(today_at()+loop.index0*td).isoformat(), value=i.total)] %}
            {% endfor %}
            {{ ns.new }}
          tomorrow: >
            {% set source = state_attr('sensor.tibber_prices', 'tomorrow') | default([], true)%}
            {% set ns = namespace(new=[]) %}
            {% for i in source %}
              {% set td = timedelta(hours=24/source|count) %}
              {% set ns.new = ns.new + [dict(datetime=(today_at()+loop.index0*td).isoformat(), value=i.total)] %}
            {% endfor %}
            {{ ns.new }}

That should give you a new sensor with entity_id sensor.tibber_prices_for_macro which you can use in the macro

1 Like

many thanks for the quick solution idea. I will test it soon :slight_smile:

The new sensor is created like the attached picture.


Unfortunately, if I tested it, it still creates an error message:

It seems that I used a wrong macro logic. With
“{{ cheapest_energy_hours(‘sensor.tibber_prices_for_macro’, attr_today=‘today’, hours=1, lowest=True, mode=‘max’) }}”
it worked now. Many Thanks :slight_smile:

Yes, I wanted to say you probably need to provide the attributes or keys. But I’ll look into it to make prevent this error and return a more informative error message

1 Like

thanks for your work, this is a really helpful macro for those new hourly price power suppliers

I have added a new version, 5.1.0 which now supports the data as provided by Tibber without the need for a template sensor.

v5.1.0

:sparkles: IMPROVEMENTS

  • This version adds support for data sources which don’t provide the date and time in the list with prices. 2 new parameters are added: datetime_in_data (defaults to true) and data_minutes (defaults to 60, only used when datetime_in_data is set to false). More information can be found in the documentation

What’s Changed

Full Changelog: Comparing v5.0.3...v5.1.0 · TheFes/cheapest-energy-hours · GitHub

Hi TheFes and thanks a lot for a great job. I have both “energi_data_service” and “nordpool” installed. I need a solution for battery charge when low price. How can I make sensors for cheapest and most expensive hours for night and day (i.e. 22:00 - 06:00 and 06:00 - 22:00). Period should be 3 or 4 hours?

There are examples in the documentation.
Can you show what you already tried?

I have tested this template:


I have got only the value of the chepest energy hour not a perion when these hourse are. And I have maked a sensor:
image

use mode='start' (which is the default) to get the start date and time of that period

Got this:


The cheapest hours start 02:00 and 1 houre ahead?