Cycling through sensors

Hello,

could some help me please with my task?
I have a number of (door/window) sensors that could be in “on” or “off” state.
I need to cycle through them to get a name of a sensor which is in “on” state to fire a message to a user.
Currently I have this setup:

# Called before alarm goes to ARM state to make sure all doors are shut.
- alias: Doors pre-check
  trigger:
    platform: state
    entity_id: input_boolean.alarm_arm_state
    to: 'on'
  condition:
    condition: or
    conditions:
      - condition: state
        entity_id: binary_sensor.door_window_sensor_158d0002b6d532 #Rear Sliding Door
        state: 'on'
      - condition: state
        entity_id: binary_sensor.door_window_sensor_158d0002b7d039 #Living Sliding Door
        state: 'on'
...
  action:
    - service: tts.google_say
      entity_id: media_player.googlehome8032
      data:
        message: "Attention! Some doors still open!"

Is it possible, instead of one message “Attention, some doors opened” create individual messages so I would get something like “Attention, kitchen door is open”, “Attention, garage door is opened” etc?

Each sensor has its own meaningful description of course.

Thank you.

How about one message that lists all that are open?

First, I’d recommend creating a group that contains all the doors & windows you want to check with this automation. Let’s call it group.alarm_doors_windows. Then:

# Called before alarm goes to ARM state to make sure all doors are shut.
- alias: Doors pre-check
  trigger:
    platform: state
    entity_id: input_boolean.alarm_arm_state
    to: 'on'
  condition:
    condition: state
    entity_id: group.alarm_doors_windows
    state: 'on'
  action:
    service: tts.google_say
    entity_id: media_player.googlehome8032
    data_template:
      message: >
        Attention! The following are still open: {{
          expand('group.alarm_doors_windows')|selectattr('state','eq','on')
          |map(attribute='attributes.friendly_name')|join(', ') }}

Thank you pnbruckner,. I’ll give it a try when I have time and report my success here. Cheers.

@pnbruckner thank you very much for your help, it works like a charm!!!

1 Like