Automation to alert specific open windows when it is raining?

Hi,

I would like to create an automation to alert me through Google TTS and Telegram when there is/are windows open when it is raining.

I have 3 windows so at, the first time, I created 3 automations for 3 windows to do the alerting but the Google TTS only play 1 message at 1 one and all 3 automations could be triggerred at the same time if dark_sky_icon turn to ‘rain’ and 3 windows are open.

So I am thinking about some “IF … THEN” clause inside the YAML code as below. I know it is not correct codes but I would like to know is there any similar solution like this for my automation? Thank you.

  - alias: 'Alert for open windows when raining'
    trigger:
      - platform: state
        entity_id: sensor.dark_sky_icon
        to: 'rain'
    condition:
      - condition: time
        after: '6:00'
        before: '23:00'
    
    action:
 
{% if is_state('binary_sensor.door_window_sensor_158d00042455e0', 'on') %}
      - service: tts.google_translate_say
        entity_id: media_player.loa_home_mini, media_player.loa_mini_phong_khach
        data:
          message: 'It is raining and the master room window is opened.'
      - service: notify.galaxynote
        data:
          message: It is raining and the master room window is opened.
{% endif %}

{% if is_state('binary_sensor.door_window_sensor_158d00045dc863', 'on') %}
      - service: tts.google_translate_say
        entity_id: media_player.loa_home_mini, media_player.loa_mini_phong_khach
        data:
          message: 'It is raining and the kid room window is opened.'
      - service: notify.galaxynote
        data:
          message: It is raining and the kid room window is opened.
{% endif %}

{% if is_state('binary_sensor.door_window_sensor_158d0004508d1e', 'on') %}
      - service: tts.google_translate_say
        entity_id: media_player.loa_home_mini, media_player.loa_mini_phong_khach
        data:
          message: 'It is raining and the living room window is opened.'
      - service: notify.galaxynote
        data:
          message: It is raining and the living room window is opened.
{% endif %}

Create a template that produces a comma-separated list of all open windows. Then the notification message can contain all open windows:

The following windows are open: living room, kid room, master room

I have an old automation similar to this, here’s the way I did it. First create a group that contains all of your window sensors:

group:
  windows:
    name: Windows
    entities:
      - binary_sensor.door_window_sensor_158d00042455e0
      - binary_sensor.door_window_sensor_158d00045dc863
      - binary_sensor.door_window_sensor_158d0004508d1e

And now here would be your updated automation:

automation:
  - alias: 'Alert for open windows when raining'
    trigger:
      - platform: state
        entity_id: sensor.dark_sky_icon
        to: 'rain'
    condition:
      - condition: time
        after: '6:00'
        before: '23:00'
    action:
      - service: tts.google_translate_say
        data_template:
          entity_id: media_player.loa_home_mini, media_player.loa_mini_phong_khach
          message: "It is raining and the {{ dict((states|selectattr('entity_id', 'in', state_attr('group.windows', 'entity_id'))|list)|groupby('state'))['on']|map(attribute='name')|list|join(', ') }} is opened."

      - service: notify.galaxynote
        data_template:
          message: "It is raining and the {{ dict((states|selectattr('entity_id', 'in', state_attr('group.windows', 'entity_id'))|list)|groupby('state'))['on']|map(attribute='name')|list|join(', ') }} is opened."

This gives you one TTS / notification listing all of your open windows. And if you get more window sensors in the future, you’d just add them to the group rather than having to touch the automation.

4 Likes

Use the force expand Luke!

This:

{{ expand('group.windows') | selectattr('state', 'eq', 'on') | map(attribute='name') | list | join(', ') }}

produces the same result as this:

{{ dict((states | selectattr('entity_id', 'in', state_attr('group.windows', 'entity_id')) | list) | groupby('state'))['on'] | map(attribute='name') | list | join(', ') }}

In addition, you can ensure the message’s grammar remains correct by adapting to the number of windows (‘window is’ or ‘windows are’ open):

    action:
      - service: tts.google_translate_say
        data_template:
          entity_id: media_player.loa_home_mini, media_player.loa_mini_phong_khach
          message: >
            {% set w = expand('group.windows') | selectattr('state', 'eq', 'on') | map(attribute='name') | list %}
            It is raining and the {{ w | join(', ') }} window{{ 's are' if w | count > 1 else ' is' }} open.

      - service: notify.galaxynote
        data_template:
          message: >
            {% set w = expand('group.windows') | selectattr('state', 'eq', 'on') | map(attribute='name') | list %}
            It is raining and the {{ w | join(', ') }} window{{ 's are' if w | count > 1 else ' is' }} open.
4 Likes

A lot cleaner than mine and it works like a charm - thanks!

1 Like

Thank you very much. I tried and it worked like a charm :smiley: I also added a state condition to make sure the automation only triggered when at least 1 window is open.

Hi Guys,

does anybody know how i would do this in node red? i want a tts notification to tell me only the open windows and doors at a set time.

Cheers

{{ dict((states | selectattr('entity_id', 'in', state_attr('group.windows', 'entity_id')) | list) | groupby('state'))['on'] | map(attribute='name') | list | join(', ') }}

This works great!! thx.
is it possible to crop the friendly names of the entities without changing customizing them in HA?

My windows sensors are all named like “contact sensor bathroom” or “contact sensor living room” etc.
Is it possible in the message above to remove the part “contact sensor” and only display the name of the room?

thx and best

For something like that you could use a simple replace filter at the end of your template.

{{ dict((states | selectattr('entity_id', 'in', state_attr('group.windows', 'entity_id')) | list) | groupby('state'))['on'] | map(attribute='name') | list | join(', ') | replace("contact sensor ", "") }}
2 Likes

I use a template for getting a message on my mobile via the home assistant app. Does anyone know how to do a line break. Can’t find any hints for solving this.

Best

EDIT: I realized this is not a Jinja2 topic but rather how the app handles messages. Therefore using HTML code does the trick. :slight_smile:

For a little further on this, as dark sky is going away, anyone using or could instruct me on using ambient weather?
I’m thinking event rain would be what I want, as a notifier when it starts raining…
code:
unit_of_measurement: in
friendly_name: NC Weather station_Event Rain

and the state with no rain is 0.

I guess I’m asking, how would i modify this window notify automation to kick off if the state is greater than 1? I’m guessing just greater than 0… >0 ??

I think I may have to go and pour some water to get an idea of what increments thats measured, unless someone already knows what ambientweather PWS returns that number as… integer, etc…
Thanks
edit: got the measurement… the gauge reports back at the beginning of rain to be 0.###, so I guess it would start about >0.2

Sorry to revive this old thread.

I would like to achieve something similar as OP, but for humidity sensors. Get notified with the name (or entity ID) of the humidity sensor that reports a greater value than the threshold.

Unfortunately I can’t group non-binary sensors, so I don’t know how to get this working. Is there an analogous way?

Thank you!

I got help in another thread, if anyone is looking for it:

- alias: "Alert for open windows when raining"
  trigger:
    - platform: numeric_state
      entity_id: sensor.grover_nc_pws_event_rain
      above: 0
  condition:
    - condition: time
      after: "6:00"
      before: "23:00"
  action:
    - service: notify.mobile_app_pixel_8_pro
      data_template:
        message: >
          {% set w = expand('group.windows') | selectattr('state', 'eq', 'on') | map(attribute='name') | list %}
          It is raining and the {{ w | join(', ') }} window{{ 's are' if w | count > 1 else ' is' }} open.

This works perfectly for me, telling me what window is open, and the windows are in a group. It will list one or multiple windows in a push notification.