Automation Delay template convert seconds to HH:MM:SS format

For a random delay in an automation I am using the following template:

{{ range(5, 25)|random|multiply(60) }}

This gives me this error:

delay template: offset 2100.0 should be format 'HH:MM' or 'HH:MM:SS'

What function could I use to change seconds to HH:MM:SS ?

Append an int filter to the template.

It appears to be generating a float value and delay won’t accept that. I assume you are using this template with seconds: ( or minutes: ) and not directly with delay:

Example:

- delay:
    seconds: "{{ range(5, 25)|random|multiply(60)|int }}"
1 Like

Why not just create the delay in the required format (edit as @123 suggested):

'00:{{ range(5, 25)|random|int }}:00'
3 Likes

The lack of the int filter was the problem. FWIW, this also works:

- delay: "{{ range(5, 25)|random|multiply(60)|int }}"

Although your suggestion is probably clearer.

BTW, range(5, 25) returns a value between 5 and 24, not 5 and 25. If you really want the upper end to be 25, then you need to specify 26 as the second value:

- delay: "{{ range(5, 25+1)|random|multiply(60)|int }}"
1 Like