To answer your last question:
Template triggers are not limited to limited triggers, so that shouldn’t be an issue.
Your first trigger should work. Did the attribute change from false to true? A trigger won’t trigger if it’s already true, it has to become true.
So in case of a template trigger, the result of the template has to change from false to true
Sorry for the late response.
I can only test in real time.
I have created a test automation and see that the automation is only started when the cheap hour starts.
I also see that the “from_state” state goes from “off” to the “to_state” state “off” and does not get the expected “on” state.
When the cheap hour is over, nothing happens.
Below is the test automation with associated Traces:
Is there a particular reason that you want to use attributes inside a binary sensor?
I’m doing something similar but with multiple binary sensors, one for each block of hours, that seems to work fine. I’ve also created a binary sensor linked to a HA input number. If I want to use the cheapest block of 2.5 hours, I simply move the slider in HA to 2.5.
- binary_sensor:
- unique_id: xyz
name: Boiler Active
state: >
{% set sensor = 'sensor.nordpool_kwh' %}
{% set hours = states('input_number.boiler_hours') | float(0) %}
{% from 'cheapest_energy_hours.jinja' import cheapest_energy_hours %}
{{ cheapest_energy_hours(sensor=sensor, hours=hours, mode='is_now', split=true, use_hourly_avg=true) }}
You fixed the state of the binary sensor to off by using state: false
So your binary sensor will never change state, only the attribute values will change.
A trigger should be generated just when the attribute value changes. This is what I want. Not the state of the binary sensor.
There will be cheap hours again tonight, so hopefully the automation below will be triggered.
I create the sensors the way you do, and it works well.
However, I don’t want six separate sensors, but just one sensor with six different hours (attributes: 1h to 6h).
One sensor, one entity_id; this keeps everything organized within HA.
So the Nordpool core integration? Not the custom one from HACS? In that case you need the blueprint. You can place the example code in your configuration.yaml.
sensor:
- unique_id: f8853830-bf81-46d4-85a9-379c25c37c23
name: Dishwasher Start Time
device_class: timestamp
state: >
{%- set sensor = 'sensor.nordpool_ceh_prices' -%}
{% from "cheapest_energy_hours.jinja" import cheapest_energy_hours %}
{{ cheapest_energy_hours(sensor=sensor, hours=2.5, start='22:00', end='08:00', look_ahead=true, include_tomorrow=true) }}
availability: "{{ is_state('binary_sensor.dishwasher_door', 'on') and is_state('binary_sensor.dishwasher_remote_active', 'on') }}"
# automation
# the door sensor is covered in the sensor now, no need to check on it in the automation
automation:
- id: 3f61bd8e-81b8-4d62-8d5d-96d8596e29b1
alias: Start Dishwasher During Night
trigger:
- platform: time
at: sensor.dishwasher_start_time
action:
- service: swith.turn_on
target:
It fires the template to update the scheduled times (sensor.dishwasher_start_time)? But why only at these three moments? And isn’t the sensor updated anyway as soon as availability changes (the door of the dishwasher is closed)?
Anyway, the template sensor became unavailable at 00:00. I’m now seeing the following in the template editor (developer tools)
{%- set sensor = 'sensor.nordpool_ceh_prices' -%}
{% from "cheapest_energy_hours.jinja" import cheapest_energy_hours %}
{{ cheapest_energy_hours(sensor=sensor, hours=4.5, start='22:00', end='08:00', look_ahead=true, include_tomorrow=true) }}
Result:
1 error: 4 datapoints (2.0 hours) in selection, where 9 (4.5 hours) are expected
Result type: string
This template updates at the start of each minute.
This template listens for the following state changed events:
Entity: sensor.nordpool_ceh_prices
The idea is that you prepare the dishwasher before 22:00 and then the macro will determine the best time to start the dishwasher.
The automation will then use this to actually start it.
You are now getting errors because the template will evaluate every minute, and after midnight it will use only the evening of the new day. At that moment the prices for the next day aren’t available yet, so there isn’t enough information for the complete direction of the dishwasher run.
The trigger prevents that, as the template will only be evaluated at the trigger times.
I’m using a custom EnergyZero integration, and today I managed to get the energy prices using their new API, but somehow I can’t get the macro to work.
I use this macro, but I do not how to set up the value_key= key.
{% from 'cheapest_energy_hours.jinja' import cheapest_energy_hours %}
{{ cheapest_energy_hours(sensor='sensor.electricity_market_price_hour_price', attr_today='base', time_key='start', value_key='price[value]') }}
1 error: Value key 'price[value]' not found in data
There is no support for nested values like this.
You could create a template sensor which loops over the items and creates a new list without the nested value.