Automation wait for trigger malformed

when i try the following wait for trigger config, I get malformed error.

  - wait_for_trigger:
      - platform: state
        entity_id: '{{ trigger.to_state.entity_id }}'
        to: 'off'
        for:
          hours: 0
          minutes: 0
          seconds: 30
          milliseconds: 0

I want to wait for the triggered entity to be off for 30s before proceeding further. How can i do this?

As far as your main problem goes, I don’t notice anything wrong. Since you didn’t include the error, it’s hard to be of too much help.

However, a bigger problem is that I don’t think this is the correct way to do what you want, as it is going to wait for the state to change, and, since it is your triggering entity, I’m assuming the state already has.

If you want to wait 30 seconds before performing any action, just put the for on the trigger.

If you want to perform some actions right when the state changes and then more if the state stays changed for 30 seconds, you’ll either need two different automations, one with the for on the trigger and one without or you’ll need a sequence like the following:

actions:
  [ ... ]
  - delay: 30
  - condition: state
    entity_id: '{{ trigger.entity_id }}'
    state: 'off'
  [ ... ]

For a State Trigger, entity_id does not support a template. That’s why you are getting a ‘malformed’ error.

Consider trying a Template Trigger.

1 Like

I have tried this for checking if the triggerred entity has changed to off state for at least 30s but it doesnot work

{{ trigger.entity_id.state=="off" and (as_timestamp(now()) - as_timestamp(trigger.to_state.last_changed))|int >30 }}

The problem with this is that after delay if condition is not satisfied, the automation will be exited, that wont be ideal in this case.

There’s no state property for trigger.entity_id. The structure is described here: State Objects.

Post the entire automation so we can understand what the wait_for_trigger is trying to do.

Oh, based on what you originally posted, I assumed that was the behavior you wanted.

If you just want a delay of thirty seconds before proceeding, then just use delay. If that still isn’t right, then post the entire automation like Taras requested.

The following is the automation yaml.

alias: Binary Auto
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.1,binary_sensor.2
    to: 'on'
    for:
      hours: 0
      minutes: 1
      seconds: 0
      milliseconds: 0
condition:
  - condition: time
    after: '07:00:00'
    before: '20:00:00'
action:
  - service: switch.turn_off
    target:
      entity_id: >-
        {% if trigger.entity_id =="binary_sensor.1" %}switch.switch1
        {% elif trigger.entity_id =="binary_sensor.2" %}switch.switch2
        {%endif%}
  - wait_for_trigger:
      - platform: template
        value_template: >-
          {{ states(trigger.to_state.entity_id)==  "off" and
          (as_timestamp(now()) -
          as_timestamp(trigger.to_state.last_changed))|int >20 }}
    continue_on_timeout: false
  - service: switch.turn_of
    target:
      entity_id: >-
        {% if trigger.entity_id =="binary_sensor.1" %}switch.switch1
        {% elif trigger.entity_id =="binary_sensor.2" %}switch.switch2
        {%endif%}
mode: parallel
max: 10

In the wait template I need to wait for the trigger entity to be turned of for 30s. I am able to check the off state but time is not working.

Try this (and remove the continue_on_timeout).

  - wait_for_trigger:
      - platform: template
        value_template: '{{ states(trigger.entity_id) ==  "off" }}'
        for: '00:00:20'
1 Like

Thanks mate