When everyone leaves I’d like to alert on the doors left open. An approach I was thinking about was a simple set of if/then checks:
In pseudocode:
msg = ''
if door_1 is open
msg += 'Door 1 is open\n'
if door_2 is open
msg += 'Door 2 is open\n'
if door_3 is open
msg += 'Door 3 is open\n'
if len(msg) > 0
send_alert(msg)
However there doesn’t appear to be a way to mutate variables from a parent scope. Is there some general approach for a situation like this?
The approach I use employs a template to query the state machine for all binary_sensors whose state is on and device_class is door. The resulting list of names is used to produce a sentence suitable for TTS such as:
Attention! The following doors are open: garage, garden, and rear doors.
It doesn’t have to be a Template Sensor. It can be part of an automation that monitors the doors and sends a notification. Or it can be a Template Sensor that is used to trigger an automation. The choice depends on the intended application.