Make failed condition part of message

good day,

I have this automation that checks if any windows, doors or gates are open when my security is armed and when we leave home. However, there’s about 18 windows, doors or gates so when the automation fires, it would be a bit ambiguous as to which sensor is open. Would it be possible to include the friendly name (or tag/id) of the sensor that failed the conditions in the tts notify? thanks

alias: Gate, door or window is opened warning when leaving or arming the house
description: >-
  Let you know if a gate, door or window is opened when you leave the house or
  arm the system.
trigger:
  - platform: state
    entity_id:
      - group.somebody_home
    for:
      hours: 0
      minutes: 3
      seconds: 0
    from: home
    to: not_home
    id: left the house
  - platform: state
    entity_id:
      - alarm_control_panel.partition1
    from: disarmed
    to: armed
    id: armed the house
condition:
  - condition: state
    entity_id:
      - binary_sensor.back_door_2
      - binary_sensor.basement_bedroom_windowl_2
      - binary_sensor.basement_bedroom_windowr_2
      - binary_sensor.basement_fam_room_winl_2
      - binary_sensor.basement_fam_room_winr_2
      - binary_sensor.front_door_2
      - binary_sensor.garage_door_2
      - binary_sensor.dining_room_window
      - binary_sensor.leonardo_s_bedroomn_window_2
      - binary_sensor.living_room_window_2
      - binary_sensor.master_bedroom_window_2
      - binary_sensor.valentina_s_bedroom_window_2
      - binary_sensor.lumi_lumi_sensor_magnet_aq2_opening_2
      - binary_sensor.lumi_lumi_sensor_magnet_aq2_opening_3
      - binary_sensor.lumi_lumi_sensor_magnet_aq2_opening_4
      - binary_sensor.gate_front_door_side_2
      - binary_sensor.gate_garage_side_2
    state: "on"
action:
  - choose:
      - conditions:
          - condition: trigger
            id: armed the house
        sequence:
          - service: notify.mobile_app_sony_xperia_zx1
            data:
              title: >-
                You {{trigger.id}}, but the outside gate, door or window is
                opened.
              message: Something Left Open
          - service: notify.mobile_app_iphone
            data:
              title: >-
                You {{trigger.id}}, but the outside gate, door or window is
                opened.
              message: Something Left Open
          - service: tts.cloud_say
            data:
              entity_id:
                - media_player.nesthub9c87
                - media_player.kitchen_display
                - media_player.family_room_display
                - media_player.basement_family_room
                - media_player.bonus_room_display
              message: >-
                You {{trigger.id}}, but the outside gate, door or window is
                opened.
      - conditions:
          - condition: trigger
            id: left the house
        sequence:
          - service: notify.mobile_app_sony_xperia_zx1
            data:
              title: >-
                You {{trigger.id}}, but the outside gate, door or window is
                opened.
              message: Something Left Open
          - service: notify.mobile_app_iphone
            data:
              title: >-
                You {{trigger.id}}, but the outside gate, door or window is
                opened.
              message: Something Left Open
    default: []
mode: single

It won’t work if they are a condition.
But you could move them to the action part and it will work.

Add a choose and have the condition bena template that iterates the states of these sensors, then use the sequence and default as either all that you have now or the message which sensor is open.

So kind of.
Pseudo

action:
  Choose
     Conditions
        Condition: template
         Template: loop sensors, report true if all is closed
         Sequence: 
            All of your action
         Default
           Notify phone
           Message save the sensor in the template as a variable or do the loop again

thanks! This sounds like a solution…but my templating is no where near as good as it should be, so I’ll try to do create this automation based on your skeleton :slight_smile:

here’s what I landed on, making a binary group sensor for all the contacts, and then some templating:

                {% set open = expand('binary_sensor.house_perimeter') |
                selectattr('state', 'eq', 'on') | map(attribute='name') | list
                %} You {{trigger.id}}, but {{ open | join(', ') }} {{ 'are' if
                open | count > 1 else 'is' }} open.

full automation is here:

alias: Gate, door or window is opened warning when leaving or arming the house
description: >-
  Let you know if a gate, door or window is opened when you leave the house or
  arm the system.
trigger:
  - platform: state
    entity_id:
      - group.somebody_home
    for:
      hours: 0
      minutes: 3
      seconds: 0
    from: home
    to: not_home
    id: left the house
  - platform: state
    entity_id:
      - alarm_control_panel.partition1
    id: armed the house
    to:
      - armed_home
      - armed_away
condition:
  - condition: state
    entity_id: binary_sensor.house_perimeter
    state: "on"
action:
  - choose:
      - conditions:
          - condition: trigger
            id: armed the house
        sequence:
          - service: notify.mobile_app_sony_xperia_zx1
            data:
              title: House Perimeter Check
              message: >-
                {% set open = expand('binary_sensor.house_perimeter') |
                selectattr('state', 'eq', 'on') | map(attribute='name') | list
                %} You {{trigger.id}}, but {{ open | join(', ') }} {{ 'are' if
                open | count > 1 else 'is' }} open.
          - service: notify.mobile_app_iphone
            data:
              title: House Perimeter Check
              message: >-
                {% set open = expand('binary_sensor.house_perimeter') |
                selectattr('state', 'eq', 'on') | map(attribute='name') | list
                %} You {{trigger.id}}, but {{ open | join(', ') }} {{ 'are' if
                open | count > 1 else 'is' }} open.
          - service: tts.cloud_say
            data:
              entity_id:
                - media_player.nesthub9c87
                - media_player.kitchen_display
                - media_player.family_room_display
                - media_player.basement_family_room
                - media_player.bonus_room_display
              message: >-
                {% set open = expand('binary_sensor.house_perimeter') |
                selectattr('state', 'eq', 'on') | map(attribute='name') | list
                %} You {{trigger.id}}, but {{ open | join(', ') }} {{ 'are' if
                open | count > 1 else 'is' }} open.
      - conditions:
          - condition: trigger
            id: left the house
        sequence:
          - service: notify.mobile_app_sony_xperia_zx1
            data:
              title: House Perimeter Check
              message: >-
                {% set open = expand('binary_sensor.house_perimeter') |
                selectattr('state', 'eq', 'on') | map(attribute='name') | list
                %} You {{trigger.id}}, but {{ open | join(', ') }} {{ 'are' if
                open | count > 1 else 'is' }} open.
          - service: notify.mobile_app_iphone
            data:
              title: House Perimeter Check
              message: >-
                {% set open = expand('binary_sensor.house_perimeter') |
                selectattr('state', 'eq', 'on') | map(attribute='name') | list
                %} You {{trigger.id}}, but {{ open | join(', ') }} {{ 'are' if
                open | count > 1 else 'is' }} open.
    default: []
mode: single
1 Like