Automation Platform event not possible to use for: minutes:

Hi,

i am still quite new to HA and i hope this is the right section to paste my question.
I am struggeling with making my automation a bit more “handy”.

I had using 2 door sensors and created 2 automations with

platform: state

and i was able to use:

for:
  minutes: 10

to start the action after 10 minutes. Means, if the window was opened and still open after 10 minutes, i received a notification.

As I have now multiple door sensors running, i wanted to use some wildcard, which is not working for

platform: state

so I switched to “platform: event” and condition: template:

alias: Window_open_longer_10min
description: Send notification when window is open for longer than 10 minutes
trigger:
  - platform: event
    event_type: state_changed
condition:
  - condition: template
    value_template: "{{ \"binary_sensor.door_sens\" in trigger.event.data.entity_id }}"
  - condition: template
    value_template: "{{ trigger.event.data.new_state.state == \"on\" }}"

action:
  - service: telegram_bot.send_message
    data_template:
      message: since 10 minutes open
      title: "{{ trigger.event.data.new_state.attributes.friendly_name }}"
mode: single

I do not get, where and how i can still use this “for: minutes: 10”.

Thanks in advance,

argonius

Events are instantaneous… they do not have a duration so you cannot use the for: variable. Currently, for is supported in State, Numeric State, Device, and Template triggers. It is also supported in State conditions.

FWIW, the construction of your event-based automation would likely make it unreliable. By using event_type: state_changed without specifying additional event data in the trigger, your automation will trigger and try to check the conditions on every state change of every entity in your instance. When you combine that with having the automation’s mode set to single, it leads to a much higher probability that any specific state change will be missed. Additionally, it means your logs can easily get flooded with warnings.

There is no need to create multiple automations just because you have multiple entities and State triggers. You can list the all the entities you want to observe and use templates to determine the content of your notification.

alias: Window_open_longer_10min
description: Send notification when window is open for longer than 10 minutes
trigger:
  - platform: state
    to: 'on'
    from: 'off'
    for: "00:10:00"
    entity_id:
      - binary_sensor.door_sensor_1
      - binary_sensor.door_sensor_2
      - binary_sensor.door_sensor_3
condition: []
action:
  - service: telegram_bot.send_message
    data:
      message: since 10 minutes open
      title: "{{ trigger.to_state.attributes.friendly_name }}"
mode: parallel

Thank you very much for your detailed answer, which also makes total sense to me.

But at least, i wanted to keep the list of entities “dynamic”. So in case, i get a new door sensor for another door/window/… i just add it to HA and do not need to take care on the automation again…

is there any way to have something like:

entity_id:
  - binary_sensor.door_sensor*

thanks,

argonius

No, you cannot use wildcards like that in a State trigger. There are a couple options, but they will not have the exact same behavior as what I posted previously. The closest to what you seem to want would be as follows:

Template trigger

alias: Window_open_longer_10min
description: Send notification when window is open for longer than 10 minutes
trigger:
  - platform: template
    value_template: |-
      {{ states.binary_sensor
      | selectattr('state', 'eq', 'on')
      | selectattr('entity_id', 'search', 'door_sensor') 
      | list | count > 0 }}
    for: "00:10:00"
condition: []
action:
  - service: telegram_bot.send_message
    data:
      message: since 10 minutes open
      title: "{{ state_attr(trigger.entity_id, 'friendly_name') }}"
mode: single

This method allows the use of the for: variable and requires no maintanence as long as you keep the naming convention. As presented above, it will only fire a notification when the first door sensor has been open for 10 minutes. Subsequent open doors would not trigger notifications. This could be overcome by adding more copies of the trigger with increasing count comparison values and changing the mode to parallel. Because of the way this works, there is a risk of false-negative situations where, based on sequence, a door is open for more than 10 minutes and you do not receive a notification.