This is quite an involved automation for a beginner, but if you want a single automation to do this job without hard-coding the times into the automation, tough . If you work through it slowly, it’s not that hard.
There are two trigger
s, both template triggers. The template is a bit of code that is evaluated whenever one of its elements changes, and the trigger fires if that evaluation returns a true
value.
The two triggers are functionally the same: the first is for non-workdays, the second for workdays, which is the first part of each trigger’s if
statement. The next bit of the code checks if the current time is in the list of schedule times — if it is, the trigger fires. The [0:5]
pulls the hours and minutes out of the stored times, which also have seconds: "12:34:56"[0:5] == "12:34"
.
The action is again split into a workday and non-workday part. The set idx
bit finds out which of the five timeslots matches the current time, and then uses that idx
to extract the corresponding target value, which is then used to update the target input_number
.
EDIT: Prompted by this topic, I’ve rewritten my automation to remove the hard-coded five slots (although it’s even less beginner-friendly now!). It will now handle any number of slots, but the helpers need to be customized to include a class attribute. It now reads thus:
- alias: Hot water - tank target manager
description: >-
This automation updates the value of the tank target based on the values in the
schedule. The action identifies which slot matches the current time, then sets
the target to that slots target value. Slots at midnight are considered unset,
and ignored. All schedule-related helpers must be customized with the relevant
class attribute: we (weekend) / wd (workday) plus tm (time) / tg (target).
id: 9ac52ecb-3e29-4474-b64b-9261ee9dce4e
trigger:
- platform: template
value_template: >
{% set wetm = states.input_datetime|selectattr('attributes.class','eq','wehw_time')|map(attribute='state')|map('truncate',5,False,'',0)|list %}
{% set wdtm = states.input_datetime|selectattr('attributes.class','eq','wdhw_time')|map(attribute='state')|map('truncate',5,False,'',0)|list %}
{{ ((states('binary_sensor.workday_sensor') == 'off') and (states('sensor.time') in wetm)) or
((states('binary_sensor.workday_sensor') == 'on') and (states('sensor.time') in wdtm)) }}
condition: "{{ states('sensor.time') != '00:00' }}"
action:
- service: input_number.set_value
data_template:
entity_id: input_number.tank_target
value: >
{% set wetm = states.input_datetime|selectattr('attributes.class','eq','wehw_time')|map(attribute='state')|map('truncate',5,False,'',0)|list %}
{% set wdtm = states.input_datetime|selectattr('attributes.class','eq','wdhw_time')|map(attribute='state')|map('truncate',5,False,'',0)|list %}
{% set wetg = states.input_number|selectattr('attributes.class','eq','wehw_tgt')|map(attribute='state')|list %}
{% set wdtg = states.input_number|selectattr('attributes.class','eq','wdhw_tgt')|map(attribute='state')|list %}
{{ wetg[wetm.index(states('sensor.time'))] if states('binary_sensor.workday_sensor') == 'off' else wdtg[wdtm.index(states('sensor.time'))] }}