Combining multiple condition checks and set dynamic values accordingly

Hello,

I have added several Sonoff Zigbee door sensors for alarms when absent. For this I added a helper “AlarmArmed” that can be on or off. When it is on and a door is opened it fires several alerts, e.g. Telegram messages. This works so far.

What I would like to add now is, that if AlarmArmed is turned on, the state of every door sensor is checked and a Telegram message is sent that tells me of this, including the friendly name of every door sensor that is open. I managed to do this by creating a separate automation for each door.

My question is: can I do this in a more elegant way in yaml, by doing a “for each” loop and in case the current sensor is open it writes the friendly name along with a newline char in a buffer and then after iterating over every sensor it sends a message with the buffer as message body? I tried to find necessary functions in the documentation, but no luck. I don’t need someone to write it for me, I’d be very happy if someone could drop the necessary function names.

Thanks a lot!

you didn’t post any code, so i’m guessing, but maybe this might work for you?

{{ label_entities('doors_to_check') | select('is_state', 'on') | map('state_attr', 'friendly_name') | join('\n') }}

this looks at all entities with the label ‘doors_to_check’ whose sensors states are ‘on’ and gives a list of their friendly names.

replace the different values/labels/whatever, that are appropriate for your needs…

that help?

I didn’t post any code since the single automations I did are so trivial. Thanks a lot for your example, I’ll take that and see how far I get. :slight_smile:

note that i missed your request for a newline in my original post. i edited it to add the new line… so grab it again if the copy you grabbed had ‘list’ instead of ‘join’

But doing so gives us lots of information, like your actual entity IDs — otherwise we have to make them up in any examples.

Of course, that’s a good point.

Here’s an example for one of the automations for a single door:

alias: DoorWarning
description: ""
trigger:
  - platform: state
    entity_id:
      - input_boolean.alarmarmed
    from: "off"
    to: "on"
condition:
  - type: is_open
    condition: device
    device_id: 04f25d54d2fc2a31a9cfba61bd97c19e
    entity_id: b3a8634b4ab87f2ae50793b15634b685
    domain: binary_sensor
action:
  - service: telegram_bot.send_message
    metadata: {}
    data:
      message: Door left open
      title: WARNING
mode: single
  1. does this do what you’re looking for? i’ve set it up so that it will check on all doors you’ve labeled with ‘doors_to_check’… replace the label as you please…

it should (unless i mucked something up) trigger if and only if one or more doors are open.

alias: DoorWarning
description: ""
trigger:
  - platform: state
    entity_id:
      - input_boolean.alarmarmed
    from: "off"
    to: "on"
condition:
  - condition: template
    value_template: >-
      {{ label_entities('doors_to_check') | select('is_state', 'on') |
      map('state_attr', 'friendly_name') | list | count > 0 }}
action:
  - service: telegram_bot.send_message
    metadata: {}
    data:
      message: >
        these doors are left open

        {{ label_entities('doors_to_check') | select('is_state', 'on') |
        map('state_attr', 'friendly_name') | join('\n') }}
      title: WARNING
mode: single
  1. read this…