This is an old thread, but I liked to share a clean solution.
E.g. the last proposal to trigger on any state change could induce a lot of template processing:
With all the new template options I created a nice “group” template suitable to trigger automations ( template grouping was already mentioned earlier in this thread, but it was old style based).
For those looking how to triggering an automation by group members; my new style template example:
STEP 1: Create a group
alike template. It is not exactly the same as official groups:
- you can’t expand the group, but have enumerate the
entity_id
attribute instead.
expand( 'binary_sensor.myalarms')
will fail!
- use
set entities = state_attr('binary_sensor.myalarms','entity_id')
instead
- hurrah: the
auto-entities
card does accept this as a group sensor!
- The template is setup in such a way that is does minimal processing and “declare your entities once” moto:
YAML anchors
and variables in the trigger
section to achieve declare once
- Templates evaluate triggers, then the state and finaly the attribute. So try to evaluate the group state in only once the trigger section and share the results via variables
This is the example of the templated group binary_sensor, that monitors alarm sensors (like water, smoke):
- trigger:
- platform: state
variables: &my_variables_alarms
entities: &triggered_by_alarms
- binary_sensor.keuken_smoke
- binary_sensor.zolder_smoke
- binary_sensor.water_leak_keuken
- binary_sensor.water_leak_garden
alarms: >
{% set ns = namespace(alarms = []) %}
{% for entity in entities %}
{% if is_state(entity,'on') %}
{% set ns.alarms = ns.alarms + [{
'name':entity,
'since':as_timestamp(states[entity].last_changed),
}]
%}
{% endif %}
{% endfor %}
{{ ns.alarms | sort(reverse=true,attribute='since') }}
entity_id: *triggered_by_alarms
- platform: homeassistant
event: start
variables: *my_variables_alarms
binary_sensor:
- unique_id: myalarms
name: MyAlarms
state: >
{{ alarms | count > 0 }}
attributes:
entity_id: "{{ entities }}"
alarms: "{{ alarms }}"
device_class: problem
Some explanation on the template structure:
my_variables_alarms
and triggered_by_alarms
are yaml anchors
.
- Keep their names unique per sensor!
- include the variable anchor
my_variables_alarms
in every extra trigger you add!
alarms
in the trigger section is an rather advanced group evaluation example!
- it creates a list of alarms triggers and when they occured (in seconds since). That is stored in the attributes for easy creation a notification text.
- You can also simplify and just count them:
{{ expand(entities) | rejectattr('state', 'eq', 'idle') | list | count }}
, and change the state
part into {{ alarms > 0 }}
- the triggered alarms are reversed sorted on time stamp, so the last one is on the top of the list
- The entities are only declared once in the template
- The template group evaluation is only done for each trigger
The result will be this sensor:
entity_id:
- binary_sensor.keuken_smoke
- binary_sensor.zolder_smoke
- binary_sensor.water_leak_keuken
- binary_sensor.water_leak_garden
alarms:
- name: binary_sensor.keuken_smoke
since: 1697618037.804547
device_class: problem
icon: mdi:alarm-light
friendly_name: MyAlarms
note: only the first sensor was triggered in this example. So just one sensor listed in the attribute alarms
.
STEP 2: the automation example
alias: Alarm group handling
description: Notify when e.g. water leakage or smoke alarms got activated
trigger:
- platform: state
entity_id:
- binary_sensor.myalarms
condition:
- condition: state
entity_id: binary_sensor.myalarms
state: "on"
action:
- variables:
msgtxt: >
{% set alarms = state_attr('binary_sensor.myalarms','alarms') or [] %}
{%- for alarm in alarms %}
{{ state_attr(alarm.name, 'friendly_name') }} ({{ relative_time(as_datetime(float(alarm.since))) }})
{%- endfor %}
- service: notify.mobile_app_iOS
data:
title: Alarm!
message: "{{msgtxt}}"
data:
push:
sound: default
interruption-level: critical
volume: 1
- service: notify.mobile_app_Android
data:
title: Alarm!
message: "{{msgtxt}}"
data:
ttl: 0
priority: high
importance: high
channel: alarm_stream_max
enabled: false
mode: single
Some explanation on the automation part:
- Important that you monitor the group state without any other constrains (like to or from). That ensures that changes in the
alarms
attribute will also trigger this automation. So even if the state is already on
, and an extra alarm emerges, the list will be extended.
- Then the automation
condition
ensures the actions (e.g. notify) are only performed when the state is on
- As a bonus, I left in two notification examples, that demonstrate how to prioritize them on the iOS and android platforms (as it is an alarm example)
I hope this example inspires
Suggestions to further improve the concept are welcome
Even better if it inspires home assistant contributors to natively support template groups as a helper