I’m looking for constructive feedback for HA best-practices and templating tricks, based on the following rather common use-case - alert if it’s raining and some (roof) windows are open. I decided to invest in learning HA and in some equipment after a very expensive summer storm…
The automation works but I’m 300% sure it can be much more elegant and condensed, so it will be easier to maintain (or just look at…)
The big blocks
My automation works in the following simple way:
- Trigger if it’s raining. Until the rain sensor arrives I use the weather forecast integration, testing if rain chance is above X or if the description changes to something “wet”.
- Make sure at least one roof window is open, so we don’t send fake alerts
- Send a mobile app notification
- Flash a light and alert over Sonos, that windows should be closed
- Terminate when both windows are closed.
What could be improved IMHO? (this is where I’m looking for input and advices)
- You’ll notice the same message repeats itself 3 times in the automation. Ideally I would have liked to use something like a variable that holds the message’s template, and only edit it once so it applies to all.
- You’ll notice I have a pair of Sonos speakers (
media_player.living_room
andmedia_player.kitchen
). Right now I’m sending a message to each of them in parallel. I’m certain there’s a way to send a single message with either two targets, or to the Sonos’ grouped speakers attribute (group_members
). What’s the best approach here? - The way I’m flashing my light is by toggling it in a loop with 1 second delay. This feels awfully dirty, and indeed it litters the logs with endless on/off messages, depending on how much time it took me to close the windows… What is a more elegant way to create a flashing light? Alternatively - can I suppress log messages in some sophisticated way?
- Is there a better structure to the entire automation that would make it more efficient or simply cooler ?
Thanks!
The full automation
alias: Roof windows rain alert
description: Alert when a roof window is open and the weather is rainy
triggers:
- entity_id:
- weather.MY_LOCATION
to: >-
'Lightning, rainy', Lightning, Rainy, Pouring, 'Snowy, rainy', Snowy,
Exceptional
trigger: state
- entity_id:
- sensor.MY_LOCATION_rain_chance
above: 30
trigger: numeric_state
conditions:
- condition: and
conditions:
- condition: or
conditions:
- type: is_open
condition: device
device_id: 8...b
entity_id: e...1
domain: binary_sensor
- type: is_open
condition: device
device_id: 6...f
entity_id: 9...b
domain: binary_sensor
actions:
- action: notify.mobile_app_MY_PHONE
data:
message: >
{%- set areas_list = namespace(areas=[]) -%}
Rain is coming. Windows in the follwoing areas are open:
{%- for entity in label_entities('window_sensors') -%}
{%- if is_state(entity, 'on') -%}
{%- set areas_list.areas = areas_list.areas + [area_name(entity)] -%}
{%- endif -%}
{%- endfor -%}
{{ areas_list.areas | join (', ') }}
title: Rain alert. Close some windows!
- action: media_player.volume_set
target:
entity_id:
- media_player.living_room
- media_player.kitchen
device_id: 6...7
data:
volume_level: 0.35
- repeat:
sequence:
- parallel:
- sequence:
- action: light.toggle
target:
entity_id: light.lidl_led_light
data:
rgb_color:
- 19
- 23
- 255
brightness: 254
- delay:
hours: 0
minutes: 0
seconds: 1
milliseconds: 0
alias: Flash lights
- alias: Sonos announcement
sequence:
- parallel:
- action: tts.speak
metadata: {}
data:
cache: true
media_player_entity_id: media_player.living_room
message: >
{%- set areas_list = namespace(areas=[]) -%}
Rain is coming. Windows in the follwoing areas are open:
{%- for entity in label_entities('window_sensors') -%}
{%- if is_state(entity, 'on') -%}
{%- set areas_list.areas = areas_list.areas + [area_name(entity)] -%}
{%- endif -%}
{%- endfor -%}
{{ areas_list.areas | join (', ') }}
target:
entity_id: tts.google_en_com
- action: tts.speak
metadata: {}
data:
cache: true
media_player_entity_id: media_player.kitchen
message: >
{%- set areas_list = namespace(areas=[]) -%}
Rain is coming. Windows in the follwoing areas are open:
{%- for entity in label_entities('window_sensors') -%}
{%- if is_state(entity, 'on') -%}
{%- set areas_list.areas = areas_list.areas + [area_name(entity)] -%}
{%- endif -%}
{%- endfor -%}
{{ areas_list.areas | join (', ') }}
target:
entity_id: tts.google_en_com
enabled: true
- delay:
hours: 0
minutes: 0
seconds: 20
milliseconds: 0
enabled: true
until:
- condition: and
conditions:
- type: is_not_open
condition: device
device_id: 8...b
entity_id: e...1
domain: binary_sensor
- type: is_open
condition: device
device_id: 6...f
entity_id: 9...b
domain: binary_sensor
- action: light.turn_off
metadata: {}
data: {}
target:
entity_id: light.lidl_led_light
mode: single