Condition state for {{ trigger.entity_id }} (notification for opening windows)

Hi,

I got sensors to check the open/close state on several windows in my home. If one of the windows is opened and has not been closed after 10 minutes a notification should be sent telling me to close the specific window.

I do not want to write an automation for every window, so I tried to write just one automation. But I am stuck with the template condition to check whether the state of the triggered window sensor is still “on”. Could someone give me a hint?

- id: fenster1
  alias: Notification Window opening
  trigger:
  - platform: state
    entity_id: binary_sensor.openclose_12
    to: 'on'
  - platform: state
    entity_id: binary_sensor.openclose_13
    to: 'on'
  - platform: state
    entity_id: binary_sensor.openclose_14
    to: 'on'
  action:
    - delay: '0:10'
    - condition:
      condition: template
      state_template: {% states('trigger.entity_id') = "on" %}
    - service: notify.home
      data:
        title: '*Please close window {{ trigger.entity_id }}*'
        message: 'Lorem ipsum'

Take a look at the Script Syntax docs. In the examples you’ll find everything you need.

how about adding for: '00:10:00' to every trigger instead of delay and condition?

1 Like

This, definitely.

Sounds like a much simpler way. Thank you!

Hi again,

my code evolved a bit but I have problems with my template condition.

Now, it is working so far that the first notification is sent after one window has been open for 10 minutes. The 2nd notification, however, should be sent after 5 minutes only if the window is still open. This does not work. At the moment, the 2nd notification will be sent regardless of the window state.

Do you have a hint for me?

- id: fenster1
  alias: Fenster auf
  trigger:
    - platform: state
      entity_id: binary_sensor.openclose_12, binary_sensor.openclose_13, binary_sensor.openclose_14
      to: 'on'
      for: '00:10:00'
  action:
    - service_template: >
       {% if is_state('device_tracker.iphoneveleganer', 'home') %}
       notify.inform_all 
       {% else %} 
       notify.home
       {% endif %}
      data_template:
        title: "*Bitte Fenster schließen ({{ trigger.to_state.attributes.friendly_name }})*"
        message: "Das Fenster ({{ trigger.to_state.attributes.friendly_name }}) ist seit {{ (as_timestamp(now()) - as_timestamp(trigger.to_state.last_changed)) | timestamp_custom('%M') }} Minuten offen. Bitte schließen."
    - delay: '00:05:00'
    - condition: template
      value_template: "{{ ( trigger.to_state.state ) == ( 'on' ) }}"   
    - service_template: >
       {% if is_state('device_tracker.iphoneveleganer', 'home') %}
       notify.inform_all 
       {% else %} 
       notify.home
       {% endif %}
      data_template:
        title: "*Bitte Fenster schließen ({{ trigger.to_state.attributes.friendly_name }})*"
        message: "Das Fenster ({{ trigger.to_state.attributes.friendly_name }}) ist seit {{ (as_timestamp(now()) - as_timestamp(trigger.to_state.last_changed)) | timestamp_custom('%M') }} Minuten offen. Bitte schließen."

Because you’re using the state that triggered the automation as the condition, so it will always be true. Need to use the entity_id.

Ok, i have tried it with the following code but this does not work either.

 - condition: template
      value_template: "{{ ( trigger.entity_id.state ) == ( 'on' ) }}"   

Do I have to list the entitys individually one by one?

Man I hate the trigger based templates, try this…

 - condition: template
   value_template: "{{ ( trigger.to_state.entity_id.state ) == ( 'on' ) }}"

maybe it should be just

value_template: "{{ trigger.to_state.state == 'on' }}"

Hello, I want to do the same automation,

If it works for you you can share the code please

I amended the code a bit and I am using the new repeat until function (https://www.home-assistant.io/blog/2020/07/22/release-113/).


- id: fenster1
  alias: Fenster auf
  trigger:
    - platform: state
      entity_id: binary_sensor.openclose_22, binary_sensor.openclose_25, binary_sensor.openclose_30, binary_sensor.openclose_31
      to: 'on'
      for: "00:10:00"
  mode: single
  condition:
    - condition: template
      value_template: "{{ (state_attr('weather.dark_sky', 'temperature')|int) < 17 }}"
  action:
    - repeat:
        sequence: 
          - service: script.notify_telegram_ios
            data:
              title: "*Bitte Fenster schließen ({{ trigger.to_state.attributes.friendly_name }})*"
              message: "Das Fenster ({{ trigger.to_state.attributes.friendly_name }}) ist seit {{ (as_timestamp(now()) - as_timestamp(trigger.to_state.last_changed)) | timestamp_custom('%M') }} Minuten offen. Bitte schließen."
          - delay:
              minutes: 5   
        until:
          - condition: template 
            value_template: "{{ trigger.to_state.state == 'off' }}"

1 Like

Thanks for sharing that config. I see two problems with this, which I fixed in my version.

  1. mode: single only allows for one run. I have multiple windows which I want to send notifications independently. With mode: single, opening a window for 10min would cancel all notifications for other windows. The correct mode would be parallel
  2. In your “until”-condition you are using trigger.to_state.state which, as far as I understand, should be a fixed value during the whole automation run (otherwise your time-calculation wouldn’t work). Therefore this is effectively an endless loop. I changed this to states(trigger.entity_id) which should refer to the current value.

Here is my yaml (for any future visitors stumbling upon this thread):

alias: Fenster/Garagentor offen - Notification
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.hmip_wohnzimmer_fenster
      - binary_sensor.hmip_badezimmer_fenster
      - binary_sensor.hmip_kueche_fenster
      - binary_sensor.garagentor
    to: "on"
    for:
      hours: 0
      minutes: 10
      seconds: 0
condition:
  - condition: or
    conditions:
      - condition: template # notify about garage-door no matter the temperature
        value_template: "{{ trigger.entity_id == 'binary_sensor.garagentor' }}"
      - condition: numeric_state
        entity_id: weather.home
        attribute: temperature
        below: 17
action:
  - repeat:
      sequence:
        - service: notify.mobile_app_my_smartphone
          data:
            title: "Bitte schließen: {{ trigger.to_state.attributes.friendly_name }}"
            message: >-
              {{ trigger.to_state.attributes.friendly_name }} ist seit {{
              (as_timestamp(now()) -
              as_timestamp(trigger.to_state.last_changed)) |
              timestamp_custom('%M') }} Minuten offen. Bitte schließen.
        - delay:
            minutes: 5
      until:
        - condition: template
          value_template: "{{ states(trigger.entity_id) == 'off' }}"
mode: parallel
max: 10