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.
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’
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