Cheapest Energy Hours - Jinja macro for dynamic energy prices

{%- set sensor = ‘sensor.energyzero_prijzen’ -%}
{% set hours = 4 %}
{% from ‘cheapest_energy_hours.jinja’ import cheapest_energy_hours %}
{% set output = cheapest_energy_hours(sensor=sensor, hours=hours, include_tomorrow=true, value_key=‘price’, time_key=‘time’) %}

Just continue ti search for the issue. Using nearly all examples in this topic continues to deliver the ZeroDivisionError: float division by zero whilst using sensor.energyzero_prijzen
sensor.energyzero_prijzen continues to deliver the correct price/hour. Has EnergyZero rendered incompatible with your macro?

How does that sensor look like? What kind of attributes and what is the value of those attributes?

Prices:

  • price: 0.1
    readingDate: “2024-10-27T00:00:00Z”
  • price: 0.1
    readingDate: “2024-10-27T01:00:00Z”
  • price: 0.1
    readingDate: “2024-10-27T02:00:00Z”
  • price: 0.1
    readingDate: “2024-10-27T03:00:00Z”
  • price: 0.1
    readingDate: “2024-10-27T04:00:00Z”
  • price: 0.11
    readingDate: “2024-10-27T05:00:00Z”
  • price: 0.11
    readingDate: “2024-10-27T06:00:00Z”
  • price: 0.1
    readingDate: “2024-10-27T07:00:00Z”
  • price: 0.08
    readingDate: “2024-10-27T08:00:00Z”
    runs through to
  • price: 0.12
    readingDate: “2024-10-28T22:00:00Z”
    friendly_name: Energyzero prijzen

Has been working flawlessly over the past 8 month. No idea whether energyzero has made any changes.

Your time_key is not time
Try time_key='readingDate'


Unfortunately no luck

Try attr_all = 'Prices'

Is this a price sensor coming from a custom integration, or a template sensor based on the core integration using the action?

Is derived from the following UI integration:

But that integration doesn’t provide the price forecast in a sensor (attribute). So you created a trigger based template sensor using the action?

I’m lost, probably struckling already for too long. Indeed, the prices are retrieved using the following : Hourly electricity prices Netherlands

- platform: rest
  unique_id: energyzero_prices_rest
  name: Energyzero prijzen 
  resource: https://api.energyzero.nl/v1/energyprices
  scan_interval: 900
  value_template: 'OK'
  params:
    fromDate: >
      {{(now().strftime('%Y-%m-%d')|as_datetime).isoformat()}}Z
    tillDate: >
      {{((now()+ timedelta(days=2)).strftime('%Y-%m-%d')|as_datetime).isoformat()}}Z
    interval: 4
    usageType: 1
    inclBtw: true
  json_attributes:
    - Prices

Okay, that has nothing to do with the integration, this will also work workout the Energy Zero integration installed, as it uses the rest integration.

As you have the integration installed, I would suggest to use the trigger based template sensor as described in the documentation

https://github.com/TheFes/cheapest-energy-hours/blob/a7c184e011507a4a928fde1a6e15211ff0f642ff/documentation/1-source_sensor.md#energyzero-and-easyenergy

Then use the parameters as given in the table
https://github.com/TheFes/cheapest-energy-hours/blob/a7c184e011507a4a928fde1a6e15211ff0f642ff/documentation/1-source_sensor.md#data-provider-settings

You will also have yesterday’s prices then, which will make it easier to use the macro in a time period which crosses midnight

Thanks a lot for the guidance. Enough to digest and try-out. Will revert as soon as I make progress. Again, thanks for your help to get me going again.

Thanks a lot! All set again thanks to the perfect guidance. Brrrrr I’m glad its restored.

@Bergerie
I did some debugging, and found the issue.
It was caused by a combination of factors

  1. An error in your start and end criteria for the rest sensor
  2. The change from Daylight Savings Time to normal time

Your selection started at midnight UTC, but that’s 2:00 local time, so You actually didn’t had all data of the first day. So the first time of your selection was 2:00, and the second time was 3:00. However, due to the time change, that was also 2:00 last night.

I use the first 2 items of the selected data to determine how much data there is between the datapoints. In this particular case this resulted in 0 seconds, and when I then tried to divide by that value, it resulted in the error.
So it would have been automatically fixed tomorrow :wink:

Thanks a lot for diving deeper into the issue and identified the cause. All is running fine now. Downloaded your latest release v5.5.3. However, am I correct that the file cheapest_energy_hours.jinja still refers to {%- set _version = ‘v5.5.2’-%}?

Yes, forgot to update the version number

Hi, I love this marco. It works great for my home battery.

But is it possible to get the lowest price that is before the highest price even if there is a lower price after the highest price of the day?

Exampel: highest price today is at 17:00 (5pm) and lowest price today is at 23:00(11pm)

Time now is 08:00 (8am) and I want to find the lowest price between now and highest price at 17:00(5pm). Let’s say that is at 13:00(1pm).

Is that possible with this macro? Read through the documents and either I can’t find it or i don’t understand what use to accomplish this.