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 %}
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.
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.
Thank you very much. I tried and it worked like a charm I also added a state condition to make sure the automation only triggered when at least 1 window is open.
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?
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.
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
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?
- 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.