Script with fields not working

Hi everyone,

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}}'

Make a second automation or use a wait_for_trigger not a wait_template.

@petro: I tried with a wait_for_trigger before, however it always throws a message malformed error and I cannot save the script:

Yeah, you can’t template entity_id in triggers. You need to make a template trigger with the template you used for the wait_template.

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?

Your template is wrong, all you had to do is copy/paste your template from your original post’s wait_template and use that and it would work.

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}}'

1 Like