Your description is fairly ambiguous. I can interpret it at least three different ways. Maybe it would help if you explain what you’re trying to achieve rather than how you tried to implement it. E.g., what entity or entities are involved? What do they do that should cause the actions in the automation to run?
I have a automation that trigger on state change based a dynamic entities list.
So far so good.
But for a couple of devices I get too much notifications. So I would like to improve the code in order to be notified only if the status persist for more than 2 minutes; basically discarding flipping devices.
Let me know if it’s more clear now.
thank you for your help,
So the reasons it doesn’t work as you have it written are:
The state condition requires a state to be specified, which you didn’t do, so it’s invalid. Of course, given what you’re trying to do there’s no way to know what that state should be.
The logic is wrong. Using a state condition with a for option means the state can not have changed for that much time, but you’re triggering when the state changes. So it’s not possible to trigger when the state changes and for the state not to have changed for some amount of time.
What I think you want to do – only run the actions two minutes after one of the entities changes, but only if it does not change again during that two minutes – is not easy. It’s possible, but not easy given the current trigger, condition & automation capabilities. But, something close may be more doable. I.e., only run the actions when one of the entities changes, but only if it has not changed in the last two minutes. Not exactly the same, but might be a reasonable compromise.
alias: Shelly cloud status
trigger:
platform: event
event_type: state_changed
condition:
- condition: template
value_template: >
{% set list = namespace(id=[]) %}
{% for s in states.sensor %}
{% if s.object_id.startswith('shelly_sh') and s.object_id.endswith('_cloud_status') %}
{% set list.id = list.id + [ s.entity_id ] %}
{% endif %}
{% endfor %}
{{ trigger.event.data.entity_id in list.id and
states('sensor.ha_uptime') | float > 2 and
trigger.event.data.new_state.state != trigger.event.data.old_state.state and
(now() - trigger.event.data.old_state.last_changed).total_seconds() > 120 }}
action:
- data_template:
message: Shelly {{trigger.event.data.new_state.name}} moved to state {{trigger.event.data.new_state.state}}
title: HA Notification - Shelly cloud status
service: notify.mail_send
- data_template:
message: Shelly {{trigger.event.data.new_state.name}} moved to state {{trigger.event.data.new_state.state}}
service: system_log.write
First of all, thx for the answers and the time dedicated to my question.
So the reasons it doesn’t work as you have it written are:
The state condition requires a state to be specified, which you didn’t do, so it’s invalid. Of course, >given what you’re trying to do there’s no way to know what that state should be.
Indeed, I pasted one of the many tests, was already fixed but not enough of course.
The logic is wrong. Using a state condition with a for option means the state can not have changed >for that much time, but you’re triggering when the state changes. So it’s not possible to trigger >when the state changes and for the state not to have changed for some amount of time.
Good to know, thx.
What I think you want to do – only run the actions two minutes after one of the entities changes, but >only if it does not change again during that two minutes – is not easy. It’s possible, but not easy given >the current trigger, condition & automation capabilities. But , something close may be more doable. >I.e., only run the actions when one of the entities changes, but only if it has not changed in the last >two minutes . Not exactly the same, but might be a reasonable compromise.
Seems it cover my requirement anyway, it’s not the same but is very very close. Should be fine.
I’ll try and report back