Automations not working with random Delay

Hey i recently setup a random delay but after this random delay may automations would not trigger

  - action:
    - data:
        entity_id: cover.fibaro_system_fgrm222_roller_shutter_controller_2_level
      service: cover.open_cover
    - delay: '00:{{ range(00,20) | random | int }}:00'
    alias: Open shutter
    condition:
    - condition: state
      entity_id: input_boolean.shutter
      state: 'on'
    trigger:
    - platform: time
      at: '08:00:00'

#######################################################################################
  - action:
    - data:
        entity_id: cover.fibaro_system_fgrm222_roller_shutter_controller_2_level
      service: cover.close_cover
    - delay: '00:{{ range(01,20) | random | int }}:00'
    alias: Close shutter
    condition:
    - condition: state
      entity_id: input_boolean.shutter
      state: 'on'
    trigger:
    - at: '10:00:00 '
      platform: time

Can you please format the code section of your post so that the indentations are displayed properly.
Makes it much easier for anybody to help and takes a lot of guesswork out of possible suggestions.

1 Like

Sorry I forget, thanks for reminding

Any errors in the log?

So I’m pretty sure the example in the documents is 90% complete and avoids an issue that may or may not be required.

The typicaly format for time based entries is xx:xx:xx. I’m not sure if it will accept xx:x:xx, I don’t believe it does but I could be wrong. Anyways, to account for that:

- delay:  "{{ '00:%02i:00'%range(01,20) | random }}"

will return

Notice how the 4 has a leading zero.

This could not be related to your issue, and it could not be a problem at all but it may be worth a shot to update.

Also, for testing purposes, you may want to decrease your time range because you could be waiting upwards of 20 minutes for the action to fire based on the random delay being between 1 minute and 20 minutes.

2 Likes

Or I’ve had luck with this sort of construct:

- delay: '{{ range(0,20*60+1)|random|timestamp_custom("%H:%M:%S",False) }}'

This allows random delays from seconds to hours. Just replace the first parameter to range with the lowest number of seconds, and the second parameter with the highest number of seconds + 1.

EDIT: Well, I’ve learned a lot since I posted the above! :laughing: Currently I’d recommend:

- delay: "{{ range(0, 20*60+1)|random }}"

The delay action accepts a time period specified in any of these formats:

- delay: SECONDS
- delay: "HH:MM"
- delay: "HH:MM:SS"
- delay:
    days: DAYS
    hours: HOURS
    minutes: MINUTES
    seconds: SECONDS
    milliseconds: MILLISECONDS

The last form allows any or all of those components, and all forms accept templates. And, FWIW, I’m actually right now working on a PR that makes it accept fractional values (such as “delay: 0.5”) as well (except for HH & MM.)

2 Likes

My delay is formatted like this - seems to work reliably:

    - delay: '00:{{ (range(1, 35)|random|int) }}:{{ (range(1, 59)|random|int) }}'
1 Like

It workes great, thanks for helping :smiley:

1 Like

Noob here having a similar issue. Anyone spot the config error? My kitchen lights and study switch come on just fine, but anything after the first delay never happens. Have set to 1-2 mins for testing purposes.

  alias: Away Lights On
  trigger:
  - event: sunset
    platform: sun
    offset: -00:30:00
  action:
  - data:
      brightness_pct: 80
      entity_id: light.kitchen_lights
    service: light.turn_on
  - service: switch.turn_on
    entity_id: switch.study
  - delay: ‘00:{{ (range(1, 2)|random|int) }}:00’
  - data:
      brightness_pct: 100
      entity_id: light.den_lights
    service: light.turn_on
  - delay: ‘00:{{ (range(1, 2)|random|int) }}:00’
  - data:
      brightness_pct: 70
      entity_id: light.bedroom_lights
    service: light.turn_on

You’re using the wrong kind of quote characters. Also, range(1, 2) always returns 1. (The range starts with the first argument, and ends with one less than the second argument.) Oh, and the |int and outside parentheses aren’t needed in this case. Try:

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

EDIT: I should also add, this will delay either 1 or 2 minutes. If you want it to range between 1 and 2 minutes, try this instead:

  - delay: "{{ range(60, 121)|random }}"
1 Like

@pnbruckner thank you sir. Didn’t realize there was a difference in quotes. That’s what I get for copy-pasting from the innerwebs. Working now. Cheers!

1 Like

Am I right assuming you mean:

 - delay: '00:00:{{ range(60, 121)|random) }}'

EDIT:
But my UI generated format lloks more like:

delay:
      hours: 0
      minutes: 0
      seconds: 0
      milliseconds: 0

Does this mean it should be (for range between 1 and 2 hours):

delay:
      hours: 0
      minutes: {{ range(60, 121)|random) }}
      seconds: 0
      milliseconds: 0

Either is fine. Either will work.

See:

Script Syntax - Wait for time to pass (delay)

It accepts three different basic forms – seconds, a time string, or a dictionary.

Just because the UI uses one of the three forms doesn’t mean that’s the only one. But if you did use the dictionary form, then it would be:

delay:
      hours: 0
      minutes: 0
      seconds: "{{ range(60, 121)|random) }}"
      milliseconds: 0

Thank you very much, Phil. :+1:
I will play around with the different versions.

EDIT:
There is a typo in your last example:
The parenthesis after random must be removed.

Ah, sorry, I just copy & pasted your code and modified it, missing that there was an extra parenthesis.

Don’t worry. I am grateful for your help and got it solved.
Much appreciated.

I am successfully using the random delay but would like to know what the actual delay amount is each time. Is there a way of seeing what the random delay value is with each run of the automation?

The automation trace will show that.

Thanks. I see that after the automation has finished but is it possible to know the delay value at the time it is run? For example, could I get a notification that says it was delayed by x minutes each time?

Yes but you’d have to create the value in a variable then use the variable in both the delay and the message