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…
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.