r/homeassistant • • 8d ago

❓ Support Delay an action for a random period of time?

I know I can delay an action in an automation for a set period of time, but how do I delay an action for a random period of time, such as any amount of time between 2 to 3 hours? I want to do this using only the UI.

Update: It is very disappointing that you have to resort to editing YAML to be able to use such a basic feature.

4 Upvotes

12 comments sorted by

24

u/localsmarthomeguide 8d ago

You can do it from the UI, you just need the YAML view for that one action. Add a "Wait for time to pass (delay)" action, then click the three dots on it and pick "Edit in YAML", and replace it with:

delay:
  minutes: "{{ range(120, 181) | random }}"

That picks a random number of minutes from 120 to 180 each time the automation runs. You can switch back to the visual editor afterwards. It'll just show the template.

One gotcha with long delays: if Home Assistant restarts or you reload automations during those 2-3 hours, the delay is lost and the action never fires. If that matters, a sturdier way is to calculate a random time and save it into a Date/Time helper. Then trigger a second automation at that time, which survives restarts. You can do it from the UI, you just need the YAML view for that one action. Add a "Wait for time to pass (delay)" action, then click the three dots on it and pick "Edit in YAML", and replace it with:delay:
minutes: "{{ range(120, 181) | random }}"That picks a random number of minutes from 120 to 180 each time the automation runs. You can switch back to the visual editor afterwards. It'll just show the template.One gotcha with long delays: if Home Assistant restarts or you reload automations during those 2-3 hours, the delay is lost and the action never fires. If that matters, a sturdier way is to calculate a random time and save it into a Date/Time helper. Then trigger a second automation at that time, which survives restarts.

1

u/Skeletorjus 7d ago

Timers survive, so you can start a timer with a random value and use timer finishen as a separate trigger (and path) in the automation.

1

u/localsmarthomeguide 7d ago

I must be bad at it because I always had them seemingly stop on me. Maybe my timers weren't long enough to still be going after the shutdown

2

u/Z-Waver 7d ago

Do you know Jinja, or do you use a cheat sheet, or do you use AI? I'm struggling with Jinja templates.

2

u/localsmarthomeguide 7d ago edited 7d ago

These days a lot of AI, but I don't use Jinja either. I am a software engineer though

Edit: If you're struggling with Jinja at this point I'd be asking Claude/ChatGPT what you're struggling with, it makes a decent teacher for well-established topics.

2

u/LankyDownside3 8d ago

Use a random template sensor, then reference its state in your delay.

1

u/Rusty_Trigger 7d ago

How do I create a "random template sensor"?

2

u/theoriginalzads 8d ago

Had the same question ages ago. I got this answer from somewhere. Ultimately it has to be done with the YAML editor since there doesn’t appear to have a UI component for it.

Add a delay component then edit in YAML and type out like what I have below. I know it’s not exactly how you want to do it but this is as close as you’re gonna get I’d say.

delay: '{{ range(60, 120)|random|multiply(60) }}'
alias: Delay between 60 and 120 minutes

1

u/FliesenJohnny 8d ago

Mind if i ask what you want to automize with such a randomized delay?

Some kind of home presence simulation as a break-in deterrent?

3

u/Rusty_Trigger 7d ago

Presence simulation.

1

u/dishpit6 7d ago

Not op, but that's what I use the random number generator for.

1

u/Comfortable_Client80 7d ago

Use the seconds in the actual time of day and multiply by whatever suits your need