Is there a way to have a template trigger match multiple entity attributes? I created an automation that i use to turn entities on/off after a period of time if they changed from their desired state via an actionable notification. Right now it works by setting an input_datetime
entity for each entity being monitored and then firing when that datetime is reached. it works fine, but i was hoping to be able to make it more dynamic.
I do have an entity that i use to set/get global variables in attributes and was thinking of using that as the trigger, something like this works for a single attribute:
trigger: template
value_template: |-
{{
now().strftime("%H:%M") ==
(state_attr('sensor.global_variables', 'variables')['test_timestamp'].value | float(0.0) | as_datetime | as_local).strftime("%H:%M")
}}
but since i want to check multiple entities, i want to be able to wildcard the template, but i could not figure a way to do it. Worst case, i can continue with the automation as is, but it would be nice to not have to create an helper or an individual template trigger for every entity i want to manage.
Here is the current automation to re-set the state of entities if anyone wants to check it out
alias: Re-enable or Re-disable Devices Automatically
description: >-
Automatically re-enable or re-disable devices after a timeout with
notifications
triggers:
- alias: When the monitored entities trigger
entity_id:
- switch.modem_outlet
- switch.pi_hole
- input_boolean.testing
trigger: state
- at:
- input_datetime.timer_timestamp_modem_outlet
- input_datetime.timer_timestamp_pihole_disabled
- input_datetime.testing
trigger: time
id: timer_fired
alias: When the timers trigger
conditions: []
actions:
- variables:
local_debug: false
entities_config:
switch.modem_outlet:
timer_entity: input_datetime.timer_timestamp_modem_outlet
timer_offset: 301
desired_state: "on"
switch.pi_hole:
timer_entity: input_datetime.timer_timestamp_pihole_disabled
timer_offset: 1800
desired_state: "on"
input_boolean.testing:
timer_entity: input_datetime.testing
timer_offset: 30
desired_state: "on"
action_timeout: 40
- choose:
- conditions:
- condition: template
value_template: >-
{{ trigger.entity_id in entities_config.keys() and
trigger.to_state.state ==
entities_config[trigger.entity_id].desired_state }}
sequence:
- data:
timestamp: "1"
target:
entity_id: "{{ entities_config[trigger.entity_id].timer_entity }}"
action: input_datetime.set_datetime
alias: Handle entity being manually changed
- conditions:
- condition: template
value_template: >-
{{ trigger.entity_id in entities_config.keys() and
trigger.to_state.state !=
entities_config[trigger.entity_id].desired_state }}
sequence:
- data:
timestamp: |
{{
now().timestamp() +
entities_config[trigger.entity_id].timer_offset | float(300)
}}
target:
entity_id: "{{ entities_config[trigger.entity_id].timer_entity }}"
action: input_datetime.set_datetime
alias: Switch changed from desired mode
- conditions:
- condition: trigger
id:
- timer_fired
sequence:
- variables:
timer_trigger: "{{ trigger.entity_id }}"
triggering_switch: |-
{%- for key, value in entities_config.items() %}
{%- if value.timer_entity == timer_trigger %}
{{ key }}
{%- endif %}
{%- endfor %}
triggering_switch_desired_state: "{{ entities_config[triggering_switch].desired_state }}"
triggering_switch_timer_offset: "{{ entities_config[triggering_switch].timer_offset }}"
triggering_switch_current_state: "{{ states(triggering_switch) }}"
- action: script.turn_on
data:
variables:
severity: INFO
group: ACTIONABLE NOTIFICATION
method: >-
{{ 'all' if is_state('binary_sensor.people_only_seb_home',
'on') else 'mobile' }}
who: seb
message: >-
{{ triggering_switch }} has been {{
triggering_switch_current_state }}. Do you want to change it
to {{ triggering_switch_desired_state }}?
confirm_text: Yes, turn it {{ triggering_switch_desired_state }}
confirm_action:
- action: homeassistant.turn_{{ triggering_switch_desired_state }}
target:
entity_id: "{{ triggering_switch }}"
dismiss_text: No, check again later
dismiss_action:
- action: input_datetime.set_datetime
data:
timestamp: >-
{{ now().timestamp() + triggering_switch_timer_offset |
default(1800) }}
target:
entity_id: "{{ timer_trigger }}"
enable_timeout: true
timeout:
hours: "{{ (action_timeout // 3600) | int }}"
minutes: "{{ ((action_timeout % 3600) // 60) | int }}"
seconds: "{{ (action_timeout % 60) | int }}"
timeout_actions:
- action: homeassistant.turn_{{ triggering_switch_desired_state }}
target:
entity_id: "{{ triggering_switch }}"
target:
entity_id: script.unified_notifications
alias: TIMER FOR SWITCH EXPIRED
mode: queued
max: 10