Automation: Send message with all conditions which are true?

Hi all, hope you are doing well!

I want to send myself a message when everyone has left the house but there are still doors/windows open. The following works, but I would like to include a list in the message of exactly which doors/windows are still open. Any suggestions on how to achieve that? Any help is greatly appreciated!

- alias: Melding Let Op
  description: ''
  trigger:
  - platform: state
    entity_id: input_boolean.presence_anyone
    to: 'off'
  condition:
  - condition: or
    conditions:
    - type: is_open
      condition: device
      device_id: 35c79f7e483dc4f101edff12257142b4
      entity_id: binary_sensor.woonkamer_deursensor_schuifpui_contact
      domain: binary_sensor
    - type: is_open
      condition: device
      device_id: 37293db8c2d0767a739de54ba7062e0c
      entity_id: binary_sensor.hal_deursensor_voordeur_contact
      domain: binary_sensor
    - type: is_open
      condition: device
      device_id: 20e84c9298aa620d82c9d55f083a4ff8
      entity_id: binary_sensor.garage_deursensor_garage_1_contact
      domain: binary_sensor
    - type: is_open
      condition: device
      device_id: 157fbc1687147b89754c4a1bad5203ab
      entity_id: binary_sensor.garage_deursensor_garage_2_contact
      domain: binary_sensor
    - type: is_open
      condition: device
      device_id: 13f252f32f60aec9a34b4d4024accc14
      entity_id: binary_sensor.badkamer_raamsensor_badkamer_contact
      domain: binary_sensor
  action:
  - service: notify.telegram
    data:
      message: '*LET OP:* Het huis is niet helemaal afgesloten!'
  mode: single
1 Like

Create a group containing your five contact sensors:

#group
  contact_sensors:
    entity_id:
    - binary_sensor.woonkamer_deursensor_schuifpui_contact
    - binary_sensor.hal_deursensor_voordeur_contact
    - binary_sensor.garage_deursensor_garage_1_contact
    - binary_sensor.garage_deursensor_garage_2_contact
    - binary_sensor.badkamer_raamsensor_badkamer_contact

Modify your existing automation to look like this:

- alias: Melding Let Op
  description: ''
  trigger:
  - platform: state
    entity_id: input_boolean.presence_anyone
    to: 'off'
  action:
  - variables:
      active: >
        {{ expand('group.contact_sensors') 
            | selectattr('state', 'eq', 'on')
            | map(attribute='name') | list }}
  - condition: template
    value_template: "{{ active | count > 0 }}"
  - service: notify.telegram
    data:
      message: '*LET OP:* Het huis is niet helemaal afgesloten! {{ active }}'
  mode: single

It will not send a notification if all contact sensors are off.

To test it, open a door and then manually execute the automation: Configuration > Automations > Melding Let op > Execute

OK, that sounds like a possible solution, thanks! :slight_smile: I will try this!

I’m trying to do the exact same thing but it’s not working like I’d hoped.

- alias: Open Door Nobody Home
  trigger:
    - platform: zone
      entity_id: device_tracker.a_s_iphone_ios, device_tracker.brads_iphone_ios
      zone: zone.home
      event: leave
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: group.millers_home
        state: "not_home"
      - condition: or
        conditions:
        - condition: state
          entity_id: binary_sensor.l_garage_opened
          state: 'on'
        - condition: state
          entity_id: binary_sensor.r_garage_opened
          state: 'on'
        - condition: state
          entity_id: binary_sensor.front_door
          state: 'on'
        - condition: state
          entity_id: binary_sensor.side_door
          state: 'on'
        - condition: state
          entity_id: binary_sensor.deck_slider
          state: 'on'
        - condition: state
          entity_id: binary_sensor.patio_slider
          state: 'on'
  action:     
    service: notify.mobile_app_brads_iphone 
    data_template:
      message: '{{ trigger.to_state.friendly_name }} is open and nobody is home!'


This will send a message when one of us leaves and the group is not home BUT it doesn’t include what is exactly open. I’ve used the “trigger.to_state.friendly_name” to push out what caused the automation to run and yes I know, the condition is not the trigger.

For example, this works to tell me when a garage door is opened:

- alias: Garage Door Opened
  trigger:
  - platform: state
    entity_id: binary_sensor.l_garage_opened, binary_sensor.r_garage_opened
    to: 'on'

  action:     
    service: notify.mobile_app_brads_iphone 
    data_template:
      message: 'The {{ trigger.to_state.name }} was opened! - {{now().strftime("%H:%M:%S on %m/%d/%y")}}'

I guess I needed something like a “condition.on_state.friendly_name”. I’ve tried to find something like that but have come up blank. I’ll try out the post above as well to see if I can get it to work.

@123 Just wanted to report back that this works, thanks! :smiley:
Quick question: besides binary sensors, I’d also like to include some covers, which have “open” as the state. So I woul like to select all the entities in the group that have the state “on” OR “open”. Can you help me with the proper syntax for that?

If the group contains binary_sensors and covers, and you want to know which ones are either on or open, you only need to make a small change to the active variable’s template:

- variables:
      active: >
        {{ expand('group.contact_sensors') 
            | selectattr('state', 'in', ['on', 'open'] )
            | map(attribute='name') | list }}

1 Like

Great, thanks!