Delay in automation not working - what did I do wrong?

I am quite certain that there is en error in my automation, but I cannot find. Here are the details: First, I created the automation (via the web GUI) with a trigger time of 12:54 and the light turned on at 12:54, as instructed. No issue.

  alias: test auto
  description: ''
  trigger:
  - platform: time
    at: '12:54:00'
  condition: []
  action:
  - type: turn_on
    device_id: 70f138e671054d4465583069b38ce0c2
    entity_id: switch.testswitch
    domain: switch

Then, I changed the trigger time to 12:56, added the following delay line, and the light turned on at 12:57, as instructed. No issue.

- delay:
      minutes: 1

Then, I changed the trigger time to 12:58, changed the delay line as shown, but the light turned on at 12:58. This means that the delay was ignored.

- delay:
      minutes: '{{ range(1, 3) | random }}'

Please point out what I did wrong. Thanks.

Perhaps the difference between a string and a numeric. Try '{{ range(1, 3) | random | int }}'

Second thoughts, take out the enclosing quotes, they turn the expression into a string. ie

{{ range(1, 3) | random }}

I think this topic is already discussed here:

Okay, I am now getting REALLY frustrated! Here is the relevant section from my automations.yaml file:

- id: '1647445797149'
  alias: testdevauto
  description: ''
  trigger:
  - platform: time
    at: <time>
  condition: []
  action:
  - type: turn_on
    device_id: 026ac5d909bd3006b5ae853be7813ce3
    entity_id: switch.testdev_basement_switch
    domain: switch
  <see below>
  mode: single

using file editor add-on, I:

  • changed <time> to 23:08 and <see below> to:
  - delay:
      minutes: '1'

and the device turned on at 23:08

  • changed <time> to 23:11 and <see below> to:
  - delay:
      minutes: 1

and the device turned at 23:11

  • changed <time> to 23:13 and <see below> to
delay: '00:01:00'

and the device turned on at 23:13

  • changed <time> to 23:14 and <see below> to
delay: 00:01:00

and the device turned on at 23:14

At this point, I have no idea what to do because i have tried every syntax I have seen and the delay never works. What should I try??? Note that after each change, I went to Server Controls and clicked ‘Automation’ to reread the file and then Check Configuration’ to make sure that there were no problems found.

Why are you putting the delay after the Device Action?

Because I am a novice at yaml files and did not realize that the factors were order dependent.

1 Like

Most of the time they aren’t. the major keys don’t matter in which order they are listed. and triggers and conditions don’t matter because they are all checked.

However actions are taken in the order they are written. And if you think about it that is the only way it makes any sense.

Otherwise you couldn’t have a “switch on”, “delay”, “switch off” sequence of events and be able to define it in that order.