Automation, how to know which condition was verified?

Hello all. :slight_smile:

Let’s say I have an automation telling me “at least a window was left open” when I left home:

 - alias: "Home: something was left unsecured before leaving"
  hide_entity: true
  trigger:
    - platform: state
      entity_id: group.family
      to: not_home
  condition:
    condition: or
    conditions:
      - condition: state
        entity_id: binary_sensor.door_window_sensor_kitchen
        state: 'on'
      - condition: state
        entity_id: binary_sensor.door_window_sensor_bathroom
        state: 'on'
      - condition: state
        entity_id: binary_sensor.door_window_sensor_office
        state: 'on'
      - condition: state
        entity_id: binary_sensor.door_window_sensor_bedroom
        state: 'on'
  action:
    - service: notify.chris_tts
      data:
        message: "Warning, at least one window was left open with nobody at home!"

My phone will use Google TTS service to tell me the given message just there. However, I’d like it to tell me WHICH windows were left open. The “dumb” way to do that would be to put as much automations as windows I have, but that would be redundant. Is there a way to intercept, in the action block, which conditions were on TRUE when the event triggered?

Any advice will be appreciated. Thank you very much! :slight_smile:

if it helps, this is what I have as part of my alarm system:

- id: alarm_triggered
  alias: '[Alarm] Triggered'
  hide_entity: true
  trigger:
  - platform: state
    entity_id: alarm_control_panel.house
    to: 'triggered'
  action:
  - service: switch.turn_on
    entity_id: switch.alarm_siren1_switch
  - service: notify.Pushbullet
    data:
      message: 'ALARM TRIGGERED!!! {{ states[states.alarm_control_panel.house.attributes.changed_by.split(".")[0]][ states.alarm_control_panel.house.attributes.changed_by.split(".")[1]].name }}'
  - service: script.turn_on
    entity_id: script.alarm_lights_on

You should be able to use a similar config to achieve what you want. The above tells me which sensor caused the alarm to trigger

1 Like

Oh, thanks @sparkydave! Your snippet helped a lot.

Here is what I got:

{% for item in states.group.doors_windows.attributes.entity_id -%}
  {%- if states(item) == 'on' %}
    {{ state_attr(item, 'friendly_name') }}
  {% endif -%}
{%- endfor %}.

This will give me the friendly name of each item from my windows group having the “on” state.

Thank you very much! :slight_smile:

it will do this, but only on first read, or on group state change. I fear it wont work when several of the entities change state, but the group state change doesn’t change because of that. Be interested to hear from @vanclec if it does in his setting?

maybe you could use this:

- alias: 'Activate Map sensors actueel'
  id: 'Activate Map sensors actueel'
#  hide_entity: True
  initial_state: 'off'
  trigger:
    platform: event
    event_type: state_changed
    event_data:
      domain: sensor
  condition:
    condition: template
    value_template: >
      {{ trigger.event.data.entity_id.endswith('actueel')}}
  action:

which would give you the option of using the trigger data to show in the message

Hi @vanclec ,

I’ve posted my notification in a different thread as an example - take a look if you like:

with a little ‘smart’ naming for the locks or windows, you could use this:

 {%-for state in states.binary_sensor 
        if 'lock' in state.entity_id and state.state == 'on' %}
        {{state.name}}: {{state.state}}
 {%endfor%} 

 {%-for state in states.sensor 
        if 'window' in state.entity_id and state.state == 'on' %}
        {{state.name}}: {{state.state}}
 {%endfor%}
1 Like