Global alert for opened window when raining

Hello !

I have an alert sending a notification on my phone every 10 minutes if my window window1 is opened and if the rain chance is superior to 50%.
If the window is closed or if the rain chance drops below 50%, the notification on the phone is cleared.
This is what it looks like in a package:

template:
  - binary_sensor:
      - name: "window1_open_with_rain"
        state: "{{ states.binary_sensor.window1.state == 'on' and states.sensor['rain_chance'].state | int > 50 }}"

alert:
  window1_open_with_rain:
    name: window1_open_with_raine
    entity_id: binary_sensor.window1_open_with_rain
    state: "on"
    repeat: 10
    can_acknowledge: false
    skip_first: false
    message: "Close window 1"
    done_message: clear_notification
    notifiers:
      - mobile_app_mycoolphone
    title: "It's (probably) raining inside !"
    data:
      tag: window1_open_rain
      ttl: 0
      priority: high
      channel: Notification
      notification_icon: "mdi:window-open-variant"
      persistent: false
      alert_once: true

This is working fine so I now want to do that for every windows.
I can copy/paste that for every window but i’m sure there is a better way to do that… and i can’t figure it out.
Can you help me please ?
Any suggestion to improve what i made is also welcomed !

That’s … not the best approach. This would be better:

is_state('binary_sensor.window1','on')

Similarly

would be better as

states('sensor.rain_chance')|int(0) > 50

For your question though, I’d use a binary sensor group to create a new template sensor (binary_sensor.windows_open_with_rain) and then your notification can do something like:

alert:
  window1_open_with_rain:
    name: window1_open_with_raine
    entity_id: binary_sensor.windows_open_with_rain
    state: "on"
    repeat: 10
    can_acknowledge: false
    skip_first: false
    message: >-
      Close the following windows: {{ expand('binary_sensor.my_windows')|selectattr('state', 'eq', 'on')|map(attribute='name')|list|join(', ') }}
    done_message: clear_notification
    notifiers:
      - mobile_app_mycoolphone
    title: "It's (probably) raining inside !"
    data:
      tag: window1_open_rain
      ttl: 0
      priority: high
      channel: Notification
1 Like

Thank you for the suggestions !

For the binary sensor group solution, i see that the message is constructed to contain all the opened windows. Is there a way to update this notification if the state of any window changes ?

Here is a scenario:

  • window1 is opened and it starts raining so a notification is sent with a message containing “Close the following windows: windows1”
  • One of my kids opens window2 (you never know with kids !)
  • I see the notification and close window1. At this moment, the notification is not cleared because there is still a window opened but the message is not up to date so i don’t know which window i still have to close

The message in the solution provided by Tinkerer will contain only the opened windows and not all due to this selectattr('state', 'eq', 'on')

Yes you are right. Sorry if i’m not clear enough.
The message will contain only the opened windows when HA sends the notification.
I’m now looking for a way to update this notification message if another window is opened.

you want an automation to notify as soon as a window is opened and or with other condition ?

I think an alert is better than an automatisation in this case.
Alerts can send notification every x minutes as long as the conditions are meet.
I don’t think you can achieve this easily with an automation.

Sorry if my english is not good enough.
I just want to be notified when the probability of raining is > 50% and at least one window is opened.
The message on the notification should also be updated when needed (if i close or open a window).
The notification has to disappear if all windows are closed or if the probability of rain drops below 50%.

I never used alert. So I don’t know well this.
But you can trigger an automation every 5 min for instance and check the status of windows.
Then take actions on notification. I don’t know if it’s possible to discard an notification via service.

In trigger, you can use a time_pattern with minutes: /5 for instance
Automation Trigger - Home Assistant (home-assistant.io)

In condition, you check the windows state via your code
In action, you can notify or call service to alert on / off

There is also History Stats - Home Assistant (home-assistant.io)
that may help to know previous states

This is my final solution, no alert, just an automation :

- alias: '[Notif] Pluie fenêtre ouverte'
  mode: restart
 
  trigger:
    - platform: numeric_state
      entity_id: sensor.thiais_rain_chance
      above: 50
    - platform: numeric_state
      entity_id: sensor.thiais_rain_chance
      below: 50
    - platform: state
      entity_id:
        - binary_sensor.window1
        - binary_sensor.window2
        - ...
 
  action:
    - choose:
        - conditions:
          - condition: state
            entity_id: group.all_windows
            state: 'on'
          - condition: numeric_state
            entity_id: sensor.thiais_rain_chance
            above: 50
 
          sequence:
            - repeat:
                while:
                  - condition: state
                    entity_id: group.all_windows
                    state: 'on'
                  - condition: numeric_state
                    entity_id: sensor.thiais_rain_chance
                    above: 50
 
                sequence:
                  - service: notify.allphones
                    data_template:
                      message: >
                        {{ expand('group.all_windows') | selectattr('state','eq','on') | map(attribute='entity_id') | map('area_name') | join(', ') }}
                      title: "Pluie - fenêtre ouverte !"
                      data:
                        ttl: 0
                        priority: high
                        channel: Notification
                        tag: "opened_windows_when_raining"
                  - delay:
                      minutes: 5
      default:
        - service: notify.allphones
          data:
            message: "clear_notification"
            data:
              tag: "opened_windows_when_raining"

@SuperTed , I would try to not have a loop and delay and repeat. If you restart HA or do a change in any automation, you lose.

You can trigger to notify and clear via choose per trigger id. This is much easier and no impact in case of restart or save on automation.