Issues with random automation

Hi

I am trying to have my light turn on at a random time but I keep getting an error and can’t figure out what’s wrong. I keep getting this error:

  • Foyer Light On: Error rendering Foyer Light On delay template: offset ‘00:14’ should be format ‘HH:MM’ or ‘HH:MM:SS’

Here is my config:

- id: '1592526460258'
  alias: Foyer Light On
  description: ''
  trigger:
  - at: '17:00:00'
    platform: time
  condition: []
  action:
  - device_id: bfd6b640bdaa4d30ab0545aa1f707f0f
    domain: switch
    entity_id: switch.06570786ecfabcb57b38
    type: turn_on
  - delay: '''00:{{ range(01,20) | random | int }}'''

Any help would be appreciated.

too many quotes.

  - delay: "00:{{ range(01,20) | random | int }}"

Also, it breaks for minutes less than 10 — it would return “00:9” for example.

na, they fixed that a few years ago.

maybe this is of help:

    action:
      - delay: 00:{{'{:02}'.format(range(2,27)|random)}}:00

the simplest way by far to really avoid that (if it were a problem) would be to just template minutes

- delay:
    minutes: "{{ range(01,20) | random | int }}"

But it’s not a problem, so why bother?

guess you’re right. I copied this from my mockupancy script from way ago…

what I don’t get in hindsight is why my template works, without using any outside quoting or multiline operator.

this has been the module since forever and it uses a second one for the seconds
the docs show the outer quotes on 1 line, but apparently delay: accepts these without further ado

    action:
      - delay: 00:{{'{:02}'.format(range(2,27)|random)}}:00
#      - delay: 00:{{range(0,2)|random|int}}{{range(2,7)|random|int}}:00
      - service: script.turn_on
        entity_id: script.home_mode_locked_lights
        data_template:
          random_group: group.home_mode_locked_lights_1
          random_entity_id: >
            {{state_attr('group.home_mode_locked_lights_1','entity_id')|random}}
          light_count_limit: 4
      - delay: 00:00:{{'{:02}'.format(range(9,45)|random)}}

also note the filter |int isn’t used in my template (as I did before, in the commented template)

because it’s being treated as a string. Typically you don’t have to add quotes to a template if you start it with characters outside a template. I.e. switch.{{ xxxx }}. Yaml see’s it as a string and treats it that way. It’s not talked about on the forums because it would confuse most newcomers.

Yeah, it’s not needed. I just lazily copied and pasted OPs original template.

o but that is great! Not exactly a newcomer myself, but I didn’t know that :wink: thanks!

Tried this but still just turns on at the scheduled time and not adding the random minutes in. Here is my code:

  alias: Turn Foyer Light On
  description: ''
  trigger:
  - at: '13:51'
    platform: time
  condition: []
  action:
  - device_id: 946d6e2633a843c88d7bd0e5ccaac192
    domain: switch
    entity_id: switch.06570786ecfabcb57b38
    type: turn_on
  - delay: 'minutes: "{{ range(01,20) | random | int }}"'```

You have to go into yaml mode to put the values in. You can’t use the UI to add the minutes field

Finally got it working. Thanks everyone for your help!!

1 Like