I’ve got an issue with a script I’m currently working on and maybe someone here knows an answer. I’ve got 17 windows with window contacts in my house for which I want to get notified if I leave anyone of them open for too long, depending on the outside temperature. In order to not repeat the same automation 17 times I decided to use a script with fields. I’m also using persistent notifications (phones in my House are running Android) which are supposed to be cleared only when an opened window is closed again.
My issue is that I when I test the script by opening a window, the persistent notification is created, but it isn’t removed when the window is closed again. What am I doing wrong here?
alias: Fenster offen gelassen
description: >-
Script which notifies the user via app sticky app notification and tts message
when a window was left open for a specified amount of time. The notifying only
occurs when the outside temperature strays too far from the inside
temperature.
icon: mdi:window-open
mode: parallel
max: 16
fields:
window_contact:
name: Window contact
description: Window contact which triggers the script
required: true
selector:
entity:
domain: binary_sensor
device_class: window
min_outside_temp:
name: Minimum outside temperature
description: Minimum allowed temperature for window to stay open
required: true
default: 10
selector:
number:
min: -20
max: 20
step: 0.5
unit_of_measurement: °C
mode: slider
max_outside_temp:
name: Maximum outside temperature
description: Maximum allowed temperature for window to stay open
required: true
default: 25
selector:
number:
min: 25
max: 100
step: 0.5
unit_of_measurement: °C
mode: slider
tts_message:
name: TTS message
description: >-
TTS message to play back on media player after expired time. message is
also used for notification.
required: true
default: Achtung! Ein Fenster wurde offen gelassen. Bitte schliessen
selector:
text:
multiline: true
sequence:
- condition: template
value_template: |
{{ (states('sensor.openweathermap_temperature')|float) < min_outside_temp
or
(states('sensor.openweathermap_temperature')|float) >= max_outside_temp }}
- service: notify.all_smartphones
data_template:
title: Fenster offen gelassen!
message: '{{tts_message}}'
data:
persistent: true
tag: '{{window_contact}}'
- service: script.tts_transmit_message
data_template:
tts_message: '{{tts_message}}'
- wait_template: '{{ is_state(window_contact,''off'') }}'
- service: notify.all_smartphones
data:
message: clear_notification
tag: '{{window_contact}}'
Ok, I modified my Script to use a template trigger like so:
alias: Fenster offen gelassen
description: >-
Script which notifies the user via app sticky app notification and tts message
when a window was left open for a specified amount of time. The notifying only
occurs when the outside temperature strays too far from the inside
temperature.
icon: mdi:window-open
mode: parallel
max: 16
fields:
window_contact:
name: Window contact
description: Window contact which triggers the script
required: true
selector:
entity:
domain: binary_sensor
device_class: window
min_outside_temp:
name: Minimum outside temperature
description: Minimum allowed temperature for window to stay open
required: true
default: 10
selector:
number:
min: -20
max: 20
step: 0.5
unit_of_measurement: °C
mode: slider
max_outside_temp:
name: Maximum outside temperature
description: Maximum allowed temperature for window to stay open
required: true
default: 25
selector:
number:
min: 25
max: 100
step: 0.5
unit_of_measurement: °C
mode: slider
tts_message:
name: TTS message
description: >-
TTS message to play back on media player after expired time. message is
also used for notification.
required: true
default: Achtung! Ein Fenster wurde offen gelassen. Bitte schliessen
selector:
text:
multiline: true
sequence:
- condition: template
value_template: |
{{ (states('sensor.openweathermap_temperature')|float) < min_outside_temp
or
(states('sensor.openweathermap_temperature')|float) >= max_outside_temp }}
- service: notify.all_smartphones
data_template:
title: Fenster offen gelassen!
message: '{{tts_message}}'
data:
persistent: true
tag: '{{window_contact}}'
- service: script.tts_transmit_message
data_template:
tts_message: '{{tts_message}}'
- wait_for_trigger:
- platform: template
value_template: "{{ is_state(window_contact}}','off') }}"
- service: notify.all_smartphones
data:
message: clear_notification
tag: '{{window_contact}}'
However, the trigger is not firing when the window is closed. I guess
"{{ is_state(window_contact}}','off') }}"
inside the value_template of the wait_for_trigger is wrongly formatted by me. What would be the right way to do it?
Thank you @petro, it is working now. I had to clean up a few other errors. I will post the working version in case someone else runs into the same troubles:
alias: Fenster offen gelassen
description: >-
Script which notifies the user via app sticky app notification and tts message
when a window was left open for a specified amount of time. The notifying only
occurs when the outside temperature strays too far from the inside
temperature.
icon: mdi:window-open
mode: parallel
max: 16
fields:
window_contact:
name: Window contact
description: Window contact which triggers the script
required: true
selector:
entity:
domain: binary_sensor
device_class: window
min_outside_temp:
name: Minimum outside temperature
description: Minimum allowed temperature for window to stay open
required: true
default: 10
selector:
number:
min: -20
max: 20
step: 0.5
unit_of_measurement: °C
mode: slider
max_outside_temp:
name: Maximum outside temperature
description: Maximum allowed temperature for window to stay open
required: true
default: 25
selector:
number:
min: 25
max: 100
step: 0.5
unit_of_measurement: °C
mode: slider
tts_message:
name: TTS message
description: >-
TTS message to play back on media player after expired time. message is
also used for notification.
required: true
default: Achtung! Ein Fenster wurde offen gelassen. Bitte schliessen
selector:
text:
multiline: true
sequence:
- condition: template
value_template: |
{{ (states('sensor.openweathermap_temperature')|float) < min_outside_temp
or
(states('sensor.openweathermap_temperature')|float) >= max_outside_temp }}
- service: notify.all_smartphones
data:
title: Fenster offen gelassen!
message: '{{tts_message}}'
data:
persistent: true
tag: '{{window_contact}}'
- service: script.tts_transmit_message
data_template:
tts_message: '{{tts_message}}'
- wait_for_trigger:
- platform: template
value_template: '{{ is_state(window_contact,''off'') }}'
- service: notify.all_smartphones
data:
message: clear_notification
data:
tag: '{{window_contact}}'