I have seen and read a lot of post people trying to get this working and Im SO CLOSE.
I am able to trigger on group and with the use of a template able to send the specific sensor in the group that triggers it. Now I only want to sent the notification when it goes from “off” to “on” (when one of the doors opens and it now sends a notifications on whatever state change.
Probably simple but i have no experience with templates.
How do i rewrite this to only sent when the state goes from “off” to “on”
###########################################################################################
# NOTIFY WHEN DOORS ARE OPEN
###########################################################################################
- alias: Notify if door opens
trigger:
platform: event
event_type: state_changed
condition:
condition: template
value_template: >
{{ trigger.event.data.entity_id in
state_attr('group.Door_sensors','entity_id') }}
action:
service: notify.mobile_app_XXXXX
data:
title: "Alarm"
message: "{{ trigger.event.data.new_state.name }} opened"
How do i trace/debug an automation that i configured manually in yaml? I know i can do that when you create one from the ui but not when you just code an automation.
Add an id to it, reload automations, and then, whenever it’s triggered, a trace will be created.
- alias: Notify if door opens
id: notify_if_door_opens
trigger:
platform: event
event_type: state_changed
... etc ...
However, you may find it difficult to use a trace to debug this automation. Why? Because you have chosen to design it with an Event Trigger that monitors everystate_changed event produced by your system.
That means every time any entity changes state (temperature sensor changes value, or a weather entity updates, or some other automation executes), this automation is triggered and a trace will be generated.
Fortunately, by default, only the last 5 traces are recorded for any automation so you won’t be filling your storage drive with traces.
Unfortunately, if you have a busy system, it may be challenging to find the trace you want (related to door sensors) because traces will be produced for other state-changes (and only the last 5 are kept).
tl;dr
An Event Trigger listening to state-changes is not a good design choice.
Damn than im in a pickle. I used this as this was the only way i could get a trigger on a group (that lists the entity that triggers it it in a notification) to work.
Is there anyway to have this trigger more granular for example just when the state of a specific group changes to fire of the event?
If you use a State Trigger to monitor a group entity, it will trigger only when the group’s state changes.
That means only when the first member of the group changes from on to off (but not when subsequent members change from on to off) and when the last group member changes from off to on. That behavior makes it unusable for determining when individual group members undergo a state-change.
The most efficient method is to use a State Trigger to monitor individual entities. That means you have to list all of the entities individually (i.e. specify each door sensor). Unfortunately, it foregoes the convenience of having a group containing door sensors.
Well for my use case having 10 sensors on doors and windowsand wanting to start an alarm it is fine if it goes of if the first one goes off. A burglar opening 2 doors in short concession is fine as long as the siren goes when the first door opens
I have used the following for a while now. I have a group of door sensors, window sensors, and their respective tampers. It uses the behavior of the group to give me the first item that opened. Edited to your used entities.
trigger:
- platform: state
entity_id: group.Door_sensors
to: 'on'
condition:
- condition: template
value_template: "{{ states.alarm_control_panel.alarm.state != 'disarmed' }}"
action:
- service: notify.mobile_app_xxx
data:
title: 'Alarm'
message: >
{% for entity in trigger.to_state.attributes.entity_id %}
{% if states(entity) == 'on' %}
{{ state_attr(entity, 'friendly_name') | upper }} opened
{% endif %}
{% endfor %}
If you use a State Trigger with a group entity like this:
- alias: Notify if door opens
trigger:
platform: state
entity_id: group.door_sensors
from: 'off'
to: 'on'
it will trigger when the group’s state changes from off to on (when the first member in the group changes state from off to on) but it won’t tell you which group member did it. You would have to use a template to determine it.
EDIT
while composing my reply, walrus_parka already posted the gist of the idea.
Here’s something that reports when any member of a group changes state to on. It combines the convenience of a Group with the efficiency of a State Trigger.
Create a State-based Template Sensor that report the latest open door (i.e. it reports the door’s entity_id).
template:
- sensor:
- name: Latest Door Open
state: >
{% set x = expand('group.door_sensors') | selectattr('state', 'eq', 'on') | sort(attribute='last_changed', reverse=true) | list %}
{{ (x[0].entity_id if now() - x[0].last_changed < timedelta(seconds=3) else '') if x | count > 0 else '' }}
Create an automation with a State Trigger that monitors the sensor.
If i paste {{ states(‘alarm_control_panel.alarm’) }} returns unknown
if i paste {{ states.alarm_control_panel.alarm.state != ‘disarmed’ }} returns true.
Should that change to false in the developer tools if i actually change the state of the panel from disarmed to armed? Because it doesn’t it stays true