Wait time so the time is random

I am new to HA and moving from using an ISY. When writing a program in the ISY, I am able to set a “random” time. I can say “If” time is 1:00 PM,. “Then” wait 10 minutes (Random), turn off or on …
In other words, in the ISY I can have a random time to give a more lived-in appearance rather than something doing the action at the same time every time.
I hope I explained that clearly and hope someone can point me in the right direction to be able to add this “random” (wait) setting in HA.
THANKS!

Here’s an example:

random amount of time between 0 and 30 minutes
- delay: "{{ range(0, 31)|random|multiply(60) }}"

1 Like

Thanks I will give that a try and let you know. I assume that I put that in “When” which would influence the first setting, in my case the “ON” time. Is there a way in which I could use that code for the “OFF” time as well?

You can’t put delays such as this in the Trigger (When) part of an Automation, only the Action (Then Do).

I am not quite following how to do this.
Here is what I currently have that works … at 9:30 the lights come on set at a certain level and turn off 60 minutes later.
What I am trying to do is to have them go on or off (or both) at a random time, for example +/- 10 minutes, just to give it some variation.

Thanks!

alias: Test for roof lights
description: ''
trigger:
  - platform: time
    at: '21:30:00'
condition: []
action:
  - service: light.turn_on
    target:
      device_id:
        - 1bfee9d666c9b5f0a9565a60c52c274c
    data: {}
  - delay:
      hours: 1
      minutes: 0
      seconds: 0
      milliseconds: 0
  - service: light.turn_off
    metadata: {}
    data: {}
    target:
      device_id: 1bfee9d666c9b5f0a9565a60c52c274c
mode: single

Please correctly format your code.

Thanks have done so

In your actions you can add a delay as the first step, with a random number of minutes as the delay time. Check out this thread for options and examples.

alias: Test for roof lights
description: ''
trigger:
  - platform: time
    at: '21:30:00'
condition: []
action:
  - service: light.turn_on
    target:
      device_id:
        - 1bfee9d666c9b5f0a9565a60c52c274c
    data: {}
  - delay:
      minutes: "{{ 50 + ( range(0,20) | random ) }}"
  - service: light.turn_off
    metadata: {}
    data: {}
    target:
      device_id: 1bfee9d666c9b5f0a9565a60c52c274c
mode: single

this turns off at 1 hr +/- 10 mins.

You can do something similar before the turn_on if you want to randomize the on time.

I don’t recommend using a long delay. If Home Assistant is restarted while the delay is counting down, the automation is immediately terminated and so, in your automation, it will never turn off the light.

Someone else had a similar request to randomize the turn-off time (and the turn-on time). A more fault-tolerant way to do it is explained in the following post:

An added advantage is that the randomized times will be displayed in sensor entities so you can know in advance at what time the light will be turned on/off for the current day (it computes new times at the start of every day and whenever Template entities are reloaded).