Random time offset

I saw an earlier post in the configuration section regarding this topic: Random timer.
But thought to re-request this idea here.

It would be nice to have access to a random time generator for use with a burglar deterrent light system.
i.e. have hass be able to switch the lights off at a random time time between 22:30 and 00:00 on nights when no one is at home.


I requested this prior to the communities. Here is some additional information. https://github.com/home-assistant/home-assistant/issues/387

i think you can do that right now:

write a small py which writes a random time to a file.
in automation call the py every night, lets say at 21:00
the you can make a sensor to connect to the file and write an automation that fires at the set time.
that way you can randomize the times from any automation you like.

Thanks!, I am currently doing it using a python file, but as I find it such a useful feature, I thought it be worth requesting it as part of the main platform.

1 Like

already placed the code you made anywhere?

that way others could use it the way you are doing right now.

This is possible with templates as a work around for now.

{% set randHour = range(0,23) | rand %}
{% set randMin = range(0,59) | rand %}
{{ now.hour == randHour and now.minute == randMin}}


Apologies for necroposting but over 4 years later is this integrated somewhere yet? Tasmota has a really nice timer system that allows adding a random offset of +/- up to 15 minutes. This is such a basic feature that most fancier standalone lamp timers have offered for decades. I feel like I must be overlooking something obvious. I’m currently using the local timers in Tasmota for a lot of my timed lights for this reason, but I would love to be able to do everything in one place. It would be fantastic if a random offset could just be tacked onto any time value. I want to avoid the “Home Alone” scenario where the burglars have figured out that everyone is away and the lights are on timers because they all turn on at precisely predictable times.

Not that I am aware off, but 4 years on, I am still happily using the template work around similar to the one suggested by @Teagan42.

@Teagan42, can you please write a few more words about your work around? I have not used templates before and therefore I am not clear what to do with the code you wrote. thanks in advance :slight_smile:

If you can give us a sample of where you would like to use it and a use case, we can show you how to use templates in such a case.

e.g. do you want to have a light come on ‘somewhere’ between 20:00 and 21:00 and have it go off again 30 to 60 minutes later ?

Post what you have so far …

Edit: Oh ! and you’ll have to say which file normally stores your automations (probably automations.yaml)
You may or may not (probably not) be able to edit this automation from the GUI after it is made (I dunno, I don’t use the GUI editors)

@Mutt that’s really kind of you.
You guessed right; I’d like the light to turn on between 7:30 and 8:00 and turn off between 23:30 and 00:10.
My automations are stored in automations.yaml.

So write an automation that triggers the ‘light’ at 07:30 then we have the ‘entities’ to work with (entity_id's).
Note: To Paraphrase what you asked for (it’s the same* but makes the automation simpler) : -
Light on between 07:30 and 08:00 (so in the morning ???) and switches off again between 15:30 - 16:10 later (i.e. after it switched on.
* - This is ‘not quite’ the same, the randomness is constrained by the boundaries of the limits but (say) if the light switched on at 07:30 it would have to switch off at or before 16:10 later and the same for 08:00
This allows you to keep it all in one simple automation rather than having to make an automation to switch it on, and another to switch it off.
Regardless, we’ll write it and you can adapt it as you need to.

Edit: You don’t need to tag me if you are repling to my post rather than the thread

to be more specific, this automation is for roller blinds (i said light just to keep it simple :slight_smile: , I didn’t think it matters).

If I understood you correctly, it makes sense that the randomness is within a boundary of a time window. The task at hand has two time windows, one in the morning, and the other at night. I did not expect to trigger both from within a single automation.

p.s. tnx for the tag tip :slight_smile:

If you can’t work the code out from the above example then you are likely to have difficulties in implementation.
The reasons we ask for a basic automation are : -

  1. To get the entities
  2. To check your spacing (yaml is sensitive)
  3. To give you a fighting chance of getting an automation that works (or if it doesn’t has a limited area where it could be wrong)
  4. Then with it working you can tweak it / break it - but recover and try again

Without this we get people who think they can re-interpret it on the fly and do it using the gui

But whatever floats your boat

I figured out the code and I totally understand it (p.s. the filter rand should be spelled random).

I am not sure about a couple of things:

  1. where and in what way is this code integrated in the automation yaml?
  2. is the automation yaml processed on a regular basis? Otherwise, how will the random code change the trigger time from one instance to the next? don’t be shy to elaborate on this :slight_smile:


The guy who wrote that was probably doing it on his phone and mis-remembered the syntax

I’m just not sure I’m getting through to you, either that or you are being deliberately obtuse.

Ooo Look … a Squirel !

Okay …


Use the GUI Editor to create an automation that raises the ‘cover’ at 09:00 (or whatever time you wish), save and then CUT & PASTE that automation here.