Cheapest Energy Hours - Jinja macro for dynamic energy prices

I’ve tried your code and it works, thanks!

Yes it is the start of the moste expensive period

Can you post the template you used to get that start time?

Here is it:
expensive_hours_jinja_am:
value_template: >
{% from ‘cheapest_energy_hours.jinja’ import cheapest_energy_hours %}
{{ cheapest_energy_hours(‘sensor.nordpool_kwh_se4_sek_2_00_0’, lowest=false, hours=3, start=‘07:00’, end=‘13:00’, mode=‘start’) }}

If you use it in a binary_sensor instead of a sensor and use mode='is_now you will get a binary sensor which is on during the most expensive hours.

You can use that in a condition or trigger.

I made binary_sensor.yaml in the configuration directory and made this binary sensor.

binary_sensor:
  - platform: template
    sensors:
      expensive_hours_jinja_am:
        friendly_name: 'Expensive Hours'
        device_class: expensive_hours_am
        value_template: >
          {{ is_state('binary_sensor.expensive_hours_jinja_am', 'is_now') }}

Have ‘binary_sensor: !include binary_sensor.yaml’ in the configuration file.
Got warning: Invalid config for ‘binary_sensor’ at configuration.yaml, line 24: required key ‘platform’ not provided
:upside_down_face:

That’s not a valid device_class
But your main problem is that you are repeating the binary_sensor key in her first line. Remove that line from binary_sensor.yaml

Oh, and that value_template doesn’t make sense.

- platform: template
  sensors:
    expensive_hours_jinja_am:
      friendly_name: 'Expensive Hours'
      value_template: >
        {% from 'cheapest_energy_hours.jinja' import cheapest_energy_hours %}
        {{ cheapest_energy_hours('sensor.nordpool_kwh_se4_sek_2_00_0', lowest=false, hours=3, start='07:00', end='13:00', mode='is_now') }}

You might also want to consider moving to the modern template sensor format

Thanks so much. It was so easy, just change mode in the present template sensor. BTW which modern template format?

The one described here:

1 Like

Thank you for this great macro! I’ve already started using it for my dishwasher and washing machine. For my dryer however I’m looking for a small tweak I can’t figure out.

I want to be able to throw washed clothes/sheets/towels/etc. in the dryer whenever and I want the dryer to start no later than 6 hours after I’ve done that. I’ve been trying set up a trigger to start the automation but can’t get it to work. Would great appreciate any tips.

How can HA know you did prepare your dryer? Is there a sensor for that?

I’ve set up a boolean switch that I’ll switch on.

input_boolean.dryer_automation_switch

Make a trigger based template sensor triggering on the state of that input boolean. Then you can use start=now(), end=now() + timedelta(hours=6)

You might want to add the duration of the program to that 6 hours, depending on your exact goal

Not sure if I understood correctly but currently running this in an automation with the and if condition that the ‘automation switch’ has to be on. It seems to be working! :slight_smile:

{% set start = as_timestamp(states.input_boolean.dryer_automation_switch.last_changed) %}
{% set end = as_timestamp(states.input_boolean.dryer_automation_switch.last_changed) + (6*3600) %}
{% from 'cheapest_energy_hours.jinja' import cheapest_energy_hours %}
{{ cheapest_energy_hours(sensor='sensor.current_electricity_price_all_in', hours=2, start=start, end=end, mode='is_now') }}

I was more thinking in line of this, which will create a sensor you can use in a time trigger to start the program

template: 
  - trigger:
      - platform: state
        entity_id: input_boolean.dryer_automation_switch
    sensor: 
      - unique_id: 016219bc-a575-497e-b982-3506c1fb48b1
        name: Dryer start
        device_class: timestamp
        state: >
          {% from 'cheapest_energy_hours.jinja' import cheapest_energy_hours %}
          {{ cheapest_energy_hours(sensor='sensor.current_electricity_price_all_in', hours=2, start=now(), end=now() + timedelta(hours=6)) }}
        availability: "{{ trigger.to_state.state == 'on' }}"

After my adventure with learning to build a dashboard for my phone and wall tablet, as i have some time over i’am gonne try making this work and try to learn so bare with me :wink:

As i have limited knowledge i don’t understand the docs.

i installed the jinja template and it’s in the correct place.
As i’am using energyzero i wanted to use the template thats on the github page and put that config_entry in the template.

template:
  - trigger:
      - platform: time_pattern
        hours: "/1"
      - platform: homeassistant
        event: start
    action:
      - service: energyzero.get_energy_prices
        data:
          incl_vat: false
          config_entry: 57358b6979f0bdf2756604a1504c42f2
          start: "{{ today_at() - timedelta(days=1) }}"
          end: "{{ today_at() + timedelta(days=2) }}"
        response_variable: prices
    sensor:
      - unique_id: 77358b6979f0bdf2756604a1504c42f5
        name: Energy Zero prices
        state: "{{ prices.prices | selectattr('timestamp', '<=', utcnow().strftime('%Y-%m-%d %H:%M:%S+00:00')) | map(attribute='price') | list | last }}"
        attributes:
          prices: "{{ prices.prices }}"

but got the error: prices is undefined.

as a test i installed the nordpol intergration and with the following template i’am getting the following result.

{% from 'cheapest_energy_hours.jinja' import cheapest_energy_hours %}
{% set output = cheapest_energy_hours (
    sensor = 'sensor.energi_data_service',
    time_key = 'hour',
    value_key = 'price',
    hours = 3.5,
    include_tomorrow = true,
    look_ahead = true,
    mode='all',
) %}
{{ output | from_json }}

result:

{
  "start": "2024-04-21T11:35:00+02:00",
  "end": "2024-04-21T15:05:00+02:00",
  "min": -2.90884,
  "max": -0.45859,
  "time_min": "2024-04-21T12:00:00+02:00",
  "time_max": "2024-04-21T14:00:00+02:00",
  "average": -1.5528,
  "weighted_average": -1.5528,
  "list": [
    -2.6862,
    -2.6862,
    -2.6862,
    -2.6862,
    -2.6862,
    -2.90884,
    -2.90884,
    -2.90884,
    -2.90884,
    -2.90884,
    -2.90884,
    -2.90884,
    -2.90884,
    -2.90884,
    -2.90884,
    -2.90884,
    -2.90884,
    -0.847,
    -0.847,
    -0.847,
    -0.847,
    -0.847,
    -0.847,
    -0.847,
    -0.847,
    -0.847,
    -0.847,
    -0.847,
    -0.847,
    -0.45859,
    -0.45859,
    -0.45859,
    -0.45859,
    -0.45859,
    -0.45859,
    -0.45859,
    -0.45859,
    -0.45859,
    -0.45859,
    -0.45859,
    -0.45859,
    -1.21363
  ],
  "is_now": false,
  "extreme_now": false,
  "estimated_costs": "unknown",
  "datapoints_per_hour": 12,
  "hours": 3.5,
  "datapoints": 42
}

Can you go to developer tools > services and try if this works if you paste this (using YAML mode)

service: energyzero.get_energy_prices
data:
  incl_vat: false
  config_entry: 57358b6979f0bdf2756604a1504c42f2
  start: "{{ today_at() - timedelta(days=1) }}"
  end: "{{ today_at() + timedelta(days=2) }}"
response_variable: prices

And where did you paste the template exemple for the energy zero price sensor?
It has to be placed in your configuration.yaml. it doesn’t work in a GUI created template helper, or in Developer tools > templates

this one is working, i get the list with timestamps and prices

hmm that a bit dumb of me, i put it in my sensor.yaml where the template didn’t work.
After i put it in my configuration.yaml the sensor is actief but still get the value of ‘unknown’

edit: ok it’s working :slight_smile:

now i have to figure out how to set this up voor my dishwasher, dryer and washing machine.

i think i saw somewhere in this tread something about and binary sensor to trigger an automation, or can i just use the timestamp attr to trigger something