I have set up 3 template sensors. From 1 triggered sensor I get the correct time of the cheapest hour, from the other sensor I get the average electricity rate of that day, but I cannot get the third triggered binary sensor to work properly.
My intention for this sensor is that it gives the cheapest hours, per hour, between 08:00 and 20:00 and between 20:00 and 08:00 the next day.
Suppose hours 8 and 11 to 13 and 23 to 2 are cheap, then the binary_sensor should be true at these hours, but that does not happen.
What have I done wrong or is this simply not possible what I want?
This one works well.
template:
# Determine the cheapest 2-hour block between 8:00 - 19:00 and between 20:00 - 7:00.
- trigger:
- platform: time
at:
- "07:05"
- "19:05"
sensor:
- unique_id: starting_time_cheapest_hours
name: Starttijd
state: >
{% set id = iif(now().strftime("%H:%M") == "07:05", 0, 1) %}
{% set start = ["08:00", "20:00"][id] %}
{% set end = ["19:00", "07:00"][id] %}
{% set sensor = "sensor.electricity_price" %}
{% from "cheapest_energy_hours.jinja" import cheapest_energy_hours %}
{{ cheapest_energy_hours (
sensor = sensor, attr_all = "Prices", time_key = "readingDate",
hours = 2, start = start, end = end, include_tomorrow = id )
}}
device_class: timestamp
This one works well.
template:
sensor:
# Gemiddelde stroomprijs incl. de opslagkosten
- name: Gemiddelde stroomprijs
unique_id: allin_electricity_average_price
unit_of_measurement: "EUR/kWh"
state: >
{% set sensor = "sensor.electricity_price" %}
{% from "cheapest_energy_hours.jinja" import cheapest_energy_hours %}
{% set output = cheapest_energy_hours (sensor = sensor, attr_all = "Prices", time_key = "readingDate",
hours = 24, include_tomorrow = false, mode = "all") | from_json %}
{{ (float(output.average) + float(states("sensor.e_opslag"),0)) | round(2) }}
state_class: measurement
This one doesnât work.
template:
# Determine the cheapest hour between 8:00 - 20:00 and between 20:00 - 8:00.
- trigger:
- platform: time
at:
- "08:00"
- "20:00"
binary_sensor:
- unique_id: cheap_hour
name: Cheap Hour
state: >
{% set id = iif(now().hour == 8, 0, 1) %}
{% set start = ["08:00", "20:00"][id] %}
{% set end = ["20:00", "08:00"][id] %}
{% set sensor = "sensor.electricity_price" %}
{% from "cheapest_energy_hours.jinja" import cheapest_energy_hours %}
{{ cheapest_energy_hours (sensor = sensor, attr_all = "Prices", time_key = "readingDate",
hours = 12, start = start, end = end, include_tomorrow = id, mode = "is_now")
}}