Looking for best practices and template efficiency advices (simple example: alert on rain when windows are open)

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… :expressionless:

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:

  1. 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”.
  2. Make sure at least one roof window is open, so we don’t send fake alerts
  3. Send a mobile app notification
  4. Flash a light and alert over Sonos, that windows should be closed
  5. Terminate when both windows are closed.

What could be improved IMHO? (this is where I’m looking for input and advices)

  1. :repeat: 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.
  2. :loud_sound: You’ll notice I have a pair of Sonos speakers (media_player.living_room and media_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?
  3. :rotating_light: 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?
  4. Is there a better structure to the entire automation that would make it more efficient or simply cooler :sunglasses: ?

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
1 Like

Advice 1: Rather than using a repeat keyword in your automation, use an actual alert

use an actual alert

Oh nice! This makes a lot of sense. This means however that I’ll be taking care of the flashing light with a repeat automation like I do now.
So here are some follow up questions:

  1. For my scenario, would it mean I need to configure the voice alert in one place (configuration.yaml), and the visual alert in another (automations.yaml)?
  2. Can I control the automation from within the alert? (maybe the question is - can an action/automation be considered as a notifier)
  3. Assuming I want all my alerts to behave in the same way in terms of delay and targets, is there a way to combine repeated alert patterns together?

I’ll give alert a shot anyway, as it seems very very right.

Hey @Didgeridrew my automation is given at the bottom of my post, you may have missed it :slight_smile:
I’ll try to figure out your approach and try it.

@koying so I’m trying alert and the first thing I see is that I need to edit configuration.yaml and restart HA for each alert.
I understand configuring alerts may simply not be available through the UI yet. But having to restart HA on each modification to alerts doesn’t seem right in terms of management and overall stability.

Am I missing anything?

I basically don’t do anything in the UI if I can do it in YAML, so I’m not the one to ask :wink:
Yes, there are downtimes, but I can definitely live with that.

1 Like

No, you’re not missing anything. That was just the way everything was done prior to a few years ago… and Alert is an old integration (introduced in Home Assistant 0.38) that hasn’t been significantly updated in a while.

Based on comments from various devs over the last year or so, it seems unlikely that the Alert integration will ever be moved to the UI. There is currently work being done to move notification from services to entities. This change should make notifications much easier, allowing creating notification groups in the Helpers menu, and making the functionality of Alerts much easier to accomplish in an automation.

1 Like