Automation trigger on group of sensors AND sent specific triggering sensor in notification SO CLOSE

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"

You should be able to just add the event_data to the trigger to limit it to what you want.

- alias: Notify if door opens
  trigger:
    platform: event
    event_type: state_changed
    event_data:
      old_state: "off"
      new_state: "on"
  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"

Digeridrew thanks but that didn’t seem to work for me. I managed to get it working using the code bellow:

- 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') }}
    - condition: template
      value_template: "{{ trigger.event.data.new_state.state == 'on' }}"
    - condition: template
      value_template: "{{ trigger.event.data.old_state.state == 'off' }}"
        
  action:
    service: notify.mobile_app_xxxx
    data:
      title: "Alarm"
      message: "{{ trigger.event.data.new_state.name }} opened"

Now im trying to include to only do this when the control panel is armed_away but that seems not to be working :frowning:

- alias: Notify if door opens
  trigger:
    platform: event
    event_type: state_changed
  condition:
    - condition: state
      entity_id: alarm_control_panel.alarm
      state: armed_away
    - condition: template
      value_template: >
        {{ trigger.event.data.entity_id in
        state_attr('group.Door_sensors','entity_id') }}
    - condition: template
      value_template: "{{ trigger.event.data.new_state.state == 'on' }}"
    - condition: template
      value_template: "{{ trigger.event.data.old_state.state == 'off' }}"

  action:
    service: notify.mobile_app_xxx
    data:
      title: "Alarm"
      message: "{{ trigger.event.data.new_state.name }} opened"

Anyone know how to incorporate the state of the alarm panel?

Have ich checked the state of the alarm in the dev-tools?

I have the same condition in one of my automations and that is working without a problem.

  - alias: "Alarm - Ausgelöst"
    id: "alarm_ausgeloest"
    trigger:
      - platform: state
        entity_id: binary_sensor.tuersensor
        to: 'on'
    condition:
      - condition: state
        entity_id: alarm_control_panel.alarm
        state: armed_away
    action:
      service: alarm_control_panel.alarm_trigger
      entity_id: alarm_control_panel.alarm

yes i have difference is you have a platform: state and i use a platform: event

Have you checked the trace of the automation? You should see which condition prevents the automation from executing the action.

Since the automation has worked before I don’t believe it has something to do with the trigger.

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 every state_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 :slight_smile:

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 %}
2 Likes

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.

If you wish, you can reduce the template to this:

      message: "{{ expand('group.door_sensors') | selectattr('state', 'eq', 'on) | map(attribute='name') | list | join(', ') | upper }} opened"
3 Likes

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.

- alias: Notify if door opens
  trigger:
    platform: state
    entity_id: sensor.latest_door_open
  condition:     
    condition: template
    value_template: "{{ trigger.to_state.state != '' }}"
  action:
    service: notify.mobile_app_XXXXX
    data:
      title: "Alarm"
      message: "{{ state_attr(trigger.to_state.state, 'friendly_name') }} opened"
2 Likes

Sorted Walrus’ config worked.

@123 Taras Putting in your shortened message did not fire a notification for me even though it passed the syntax test.

Anyway appreciate your help

Bit too fast looks like the condition is not working either as it also triggers when the state is disarmed

Paste the following into the Template section of the Developer Tools:

{{ states('alarm_control_panel.alarm') }}

The case needs to match.

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

What is the entity_id of the alarm control panel? It should return false if the system is disarmed.