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

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.

So doubled checked. Control panel is disarmed
pasting {{ states(‘alarm_control_panel.alarm’) != ‘disarmed’ }} returns true
pasting “{{ states.alarm_control_panel.alarm.state != ‘disarmed’ }}” returns true

entity_id is alarm_control_panel.home_alarm

{{ states('alarm_control_panel.home_alarm') != 'disarmed' }}

Thats works. Now I’ll study the small difference in the code to actually try to understand what you just did :slight_smile:

Thanks mate

His shortened message works as I have it throughout my config. I have a feeling it is a case mismatch on the group.

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

It works for me so you must have done something wrong. :man_shrugging:

Your previous message quoted the Template Condition I posted and then proceeded to discuss a completely different Template Condition. Was there a question and was it meant for me?

An entity_id is a slug (meaning a string that has no whitespace or non-ASCII characters and is in lower-case). Given that format, I don’t see how the group’s entity_id could contain upper-case letters.

@Marcweemer
The entity’s entity_id is shown in Developer Tools > States and that’s what should be used when referencing it in templates, scripts, automations, etc.