How to add a delay before executing an action?

The automation I have is switching lights on/off based on motion sensor and espresense.

What I find is that no matter where I put a delay, if the automation chooses to switch off the lights, it will do it as soon as the clear occupancy is set on the motion sensor.

How do I add a 2 minutes delay to it so it won’t switch it off immediately?

alias: Office light motion
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.0xa4c13856fb9322ff_occupancy
    from:
      - "off"
      - "on"
    to:
      - "on"
      - "off"
condition: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ trigger.to_state.state == 'off' }}"
            alias: No motion detection
          - condition: and
            conditions:
              - condition: state
                entity_id: sensor.galaxy_s23
                state: not_home
                for:
                  hours: 0
                  minutes: 5
                  seconds: 0
        sequence:
          - delay:
              hours: 0
              minutes: 5
              seconds: 0
              milliseconds: 0
          - service: light.turn_off
            data: {}
            target:
              entity_id:
                - light.0xa4c13805cd342859
                - light.0xa4c13820c8f952c4
                - light.yeelink_color1_cb8d_light
      - conditions:
          - condition: template
            value_template: "{{ trigger.to_state.state == 'on' }}"
          - condition: and
            conditions:
              - condition: sun
                before: sunrise
                after: sunset
                after_offset: "-02:00:00"
        sequence:
          - service: scene.turn_on
            target:
              entity_id: scene.evning_light_color
            metadata: {}
      - conditions:
          - condition: template
            value_template: "{{ trigger.to_state.state == 'on' }}"
          - condition: and
            conditions:
              - condition: sun
                before: sunset
                after: sunrise
                before_offset: "-01:00:00"
        sequence:
          - service: scene.turn_on
            target:
              entity_id: scene.daytime_light_color
            metadata: {}
mode: queued
max_exceeded: silent
max: 10

Add a duration to the “off” side of the trigger.

alias: Office light motion
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.0xa4c13856fb9322ff_occupancy
    from: "off"
    to: "on"
  - platform: state
    entity_id:
      - binary_sensor.0xa4c13856fb9322ff_occupancy
    from: "on"
    to: "off"
    for: "00:02:00"

FWIW, your 3 And conditions are unnecessary and not actually doing anything.

1 Like

I’m getting Message malformed: required key not provided @ data['trigger'][0]['platform']
when saving that yaml

Change the for part to:

for:
      hours: 0
      minutes: 2
      seconds: 0

All of these are equivalent:

for: '00:02:00'
for:
  minutes: 2
for:
  hours: 0
  minutes: 2
  seconds: 0

It’s complaining about an error in the first trigger.

Post your automation containing the changes that have caused the error.

1 Like

That’s before the change:

alias: Office light motion
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.0xa4c13856fb9322ff_occupancy
    from:
      - "off"
      - "on"
    to:
      - "on"
      - "off"
condition: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ trigger.to_state.state == 'off' }}"
            alias: No motion detection
          - condition: and
            conditions:
              - condition: state
                entity_id: sensor.galaxy_s23
                state: not_home
                for:
                  hours: 0
                  minutes: 5
                  seconds: 0
        sequence:
          - delay:
              hours: 0
              minutes: 5
              seconds: 0
              milliseconds: 0
          - service: light.turn_off
            data: {}
            target:
              entity_id:
                - light.0xa4c13805cd342859
                - light.0xa4c13820c8f952c4
                - light.yeelink_color1_cb8d_light
      - conditions:
          - condition: template
            value_template: "{{ trigger.to_state.state == 'on' }}"
          - condition: and
            conditions:
              - condition: sun
                before: sunrise
                after: sunset
                after_offset: "-02:00:00"
        sequence:
          - service: scene.turn_on
            target:
              entity_id: scene.evning_light_color
            metadata: {}
      - conditions:
          - condition: template
            value_template: "{{ trigger.to_state.state == 'on' }}"
          - condition: and
            conditions:
              - condition: sun
                before: sunset
                after: sunrise
                before_offset: "-01:00:00"
        sequence:
          - service: scene.turn_on
            target:
              entity_id: scene.daytime_light_color
            metadata: {}
mode: queued
max_exceeded: silent
max: 10

That’s after the change with the error:

alias: Office light motion
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.0xa4c13856fb9322ff_occupancy
    from: "off"
    to: "on"
  - platform: state
    entity_id:
      - binary_sensor.0xa4c13856fb9322ff_occupancy
    from: "on"
    to: "off"
    for: "00:02:0
condition: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ trigger.to_state.state == 'off' }}"
            alias: No motion detection
          - condition: and
            conditions:
              - condition: state
                entity_id: sensor.galaxy_s23
                state: not_home
                for:
                  hours: 0
                  minutes: 5
                  seconds: 0
        sequence:
          - delay:
              hours: 0
              minutes: 5
              seconds: 0
              milliseconds: 0
          - service: light.turn_off
            data: {}
            target:
              entity_id:
                - light.0xa4c13805cd342859
                - light.0xa4c13820c8f952c4
                - light.yeelink_color1_cb8d_light
      - conditions:
          - condition: template
            value_template: "{{ trigger.to_state.state == 'on' }}"
          - condition: and
            conditions:
              - condition: sun
                before: sunrise
                after: sunset
                after_offset: "-02:00:00"
        sequence:
          - service: scene.turn_on
            target:
              entity_id: scene.evning_light_color
            metadata: {}
      - conditions:
          - condition: template
            value_template: "{{ trigger.to_state.state == 'on' }}"
          - condition: and
            conditions:
              - condition: sun
                before: sunset
                after: sunrise
                before_offset: "-01:00:00"
        sequence:
          - service: scene.turn_on
            target:
              entity_id: scene.daytime_light_color
            metadata: {}
mode: queued
max_exceeded: silent
max: 10

Your duration in the second trigger is missing it’s end… Open-ended quotes can have weird effects on how the yaml is interpreted.

It’s missing a closing double-quote.

Change this:

    for: "00:02:0

to this:

    for: "00:02:00"

besides the delay, can you attest the trigger behaves correctly like that?

    from:
      - "off"
      - "on"
    to:
      - "on"
      - "off"

I’ve never tried to put 2 pairs of on/off triggers and expect the HA logic to understand that, as it’s not documented to work like that.

had a side discussion on the matter the other day with Petro, but never finalized that. My automation failed, probably because of other reasons. Would still love to hear some confirmation.

what I quoted earlier was an error in the copy&paste. it actually has the " " on both ends.

The error is still there.

Here’s the full yaml:

alias: Office light motion
description: ""
trigger:
  - alias: Office light motion
    description: ""
    trigger:
      - platform: state
        entity_id:
          - binary_sensor.0xa4c13856fb9322ff_occupancy
        from: "off"
        to: "on"
      - platform: state
        entity_id:
          - binary_sensor.0xa4c13856fb9322ff_occupancy
        from: "on"
        to: "off"
        for: "00:02:00"
condition: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ trigger.to_state.state == 'off' }}"
            alias: No motion detection
          - condition: and
            conditions:
              - condition: state
                entity_id: sensor.galaxy_s23
                state: not_home
                for:
                  hours: 0
                  minutes: 5
                  seconds: 0
        sequence:
          - delay:
              hours: 0
              minutes: 5
              seconds: 0
              milliseconds: 0
          - service: light.turn_off
            data: {}
            target:
              entity_id:
                - light.0xa4c13805cd342859
                - light.0xa4c13820c8f952c4
                - light.yeelink_color1_cb8d_light
      - conditions:
          - condition: template
            value_template: "{{ trigger.to_state.state == 'on' }}"
          - condition: and
            conditions:
              - condition: sun
                before: sunrise
                after: sunset
                after_offset: "-02:00:00"
        sequence:
          - service: scene.turn_on
            target:
              entity_id: scene.evning_light_color
            metadata: {}
      - conditions:
          - condition: template
            value_template: "{{ trigger.to_state.state == 'on' }}"
          - condition: and
            conditions:
              - condition: sun
                before: sunset
                after: sunrise
                before_offset: "-01:00:00"
        sequence:
          - service: scene.turn_on
            target:
              entity_id: scene.daytime_light_color
            metadata: {}
mode: queued
max_exceeded: silent
max: 10

right, so you changed that trigger pair to what I always use indeed… :wink:

You have doubled the alias, description, and trigger keys in the trigger…

Delete that trigger, then just set up the triggers using the normal editor. You can just as easily add the “for” there:

If I follow the UI editor, I have from clear to detected
Do I need to change the template value to detected as well?

No.

cool, thanks.

so here’s the full yaml file with the removal of the 3 AND conditions.
I hope this one will work :slight_smile:

alias: Office light motion
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.0xa4c13856fb9322ff_occupancy
    from:
      - "off"
      - "on"
    to:
      - "on"
      - "off"
    enabled: false
  - platform: state
    entity_id:
      - binary_sensor.0xa4c13856fb9322ff_occupancy
    from: "on"
    to: "off"
    for:
      hours: 0
      minutes: 2
      seconds: 0
  - platform: state
    entity_id:
      - binary_sensor.0xa4c13856fb9322ff_occupancy
    from: "off"
    to: "on"
condition: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ trigger.to_state.state == 'off' }}"
            alias: No motion detection
          - condition: state
            entity_id: sensor.galaxy_s23
            state: not_home
            for:
              hours: 0
              minutes: 0
              seconds: 0
        sequence:
          - service: light.turn_off
            data: {}
            target:
              entity_id:
                - light.0xa4c13805cd342859
                - light.0xa4c13820c8f952c4
                - light.yeelink_color1_cb8d_light
      - conditions:
          - condition: template
            value_template: "{{ trigger.to_state.state == 'on' }}"
          - condition: sun
            before: sunrise
            after: sunset
            after_offset: "-02:00:00"
        sequence:
          - service: scene.turn_on
            target:
              entity_id: scene.evning_light_color
            metadata: {}
      - conditions:
          - condition: template
            value_template: "{{ trigger.to_state.state == 'on' }}"
          - condition: sun
            before: sunset
            after: sunrise
            before_offset: "-01:00:00"
        sequence:
          - service: scene.turn_on
            target:
              entity_id: scene.daytime_light_color
            metadata: {}
mode: queued
max_exceeded: silent
max: 10

Why is your automation’s mode set to queued?

copied from another automation. no real reason

In that case, I suggest you familiarize yourself with the mode option. It determines an automation’s behavior if it’s triggered while it’s still busy executing its actions.

I read about that and since I use a knob that rotates for dimming, I figured I need a queued option, so I copied it across.

What does that have to do with your automation posted above (which is triggered by a binary_sensor)?