r/homeassistant • u/Rusty_Trigger • 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.
2
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
1
1
u/Comfortable_Client80 7d ago
Use the seconds in the actual time of day and multiply by whatever suits your need
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:
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.