Help need with automation : defining dynamic entity_id for the state condition

Hi,

I would like my automation to wait 2 minutes for a state change before processing.
This is the extract of the relevant code:

  trigger:
    platform: event
    event_type: state_changed
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: {{ trigger.event.data.entity_id }}
        for:
          minutes: 2

I tried several syntax for the entity_id , but unfortunately none works.
Any suggestion ?

Thank you,

Simone

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,

Simone

I think I see what you’re trying to do. Can you post your existing automation?

  alias: Shelly cloud status
  trigger:
    platform: event
    event_type: state_changed
  condition:
    condition: and
    conditions:
      - 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 }}"
      - condition: template
        value_template: "{{ trigger.event.data.new_state.state != trigger.event.data.old_state.state }}"
      - condition: state
        entity_id: {{ trigger.event.data.entity_id }}
        for:
          minutes: 2
      - condition: template
        value_template: "{{ states('sensor.ha_uptime') | float > 2 }}"
  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

Simone

So the reasons it doesn’t work as you have it written are:

  1. 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.
  2. 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
2 Likes

Hmmm, I’m sure Phil will be back to help but the first thing is that you don’t write multiline templates like that.

    value_template: >
      {{ template goes here, note the multiline indicator and NO quotes outside }} 

Edit: Ah, beaten by Phil !

1 Like

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:

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

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

Simone

1 Like

Hmmm, I’m sure Phil will be back to help but the first thing is that you don’t write multiline templates like >that.

Edit: Ah, beaten by Phil !

Thx so much for the answer.

Simone