Automation - random turn off time within time window?

I’ve got some pretty simple automation set up currently. A few scenes for watching movies (which aren’t technically automation) and an automation of some outside lights that turn on just before sunset and then turn off just after sunrise. This has been working quite well.

I’d like to set up another automation that turns a few lights on somewhere around sunset (that part is easy - the ‘Turn On’ would be essentially the same as in my existing automation), but then shut them off at a random time within a time window, say between 9:30 and 11 pm. I think I have setting a specific time to turn off, but I don’t want it to be a static time - I’d like it to be random within a window.

Is something like this possible, or would it be beyond HA capabilities?

I’m sure @CCOSTAN will check in on this now that I tagged him, but I found this in his configuration.yaml example in github:

###  Future Ideas

# - alias: 'Get Random Time'
  # trigger:
    # platform: time
    # after: '21:00:00'
  # action:
    # - service: input_slider.select_value
      # data_template:
        # entity_id: input_slider.hour
        # value: '{{ (range(22, 23) | random) }}'
    # - service: input_slider.select_value
      # data_template:
        # entity_id: input_slider.random_minute
        # value: '{{ (range(30, 45) | random) }}'
# Then simply use that in your light turn off automation:

# - alias: 'Turn lights off'
  # trigger:
    # platform: template
    # value_template: '{{ now.hour == (states.input_slider.random_hour.state | int) and now.minute == (states.input_slider.random_minute.state | int) }}'
  # action:
    # - service: light.turn_off
      # data: 
        # entity_id: light.hue_color_lamp_1 

Here’s an unrelated tip: I have a bookmarked search setup as:

https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=site%3A%20https%3A%2F%2Fhomeassistant.io

This allows me to search the entire HA site by clicking the bookmark and then just putting my search criteria in after the site name. That’s how I found the item above. :wink:

I know the ‘Turn Lights Off’ belongs in Automation - I assume Get Random Time’ does as well?

I’m not quite sure how to interpret the ‘Get Random Time’ function. The ‘After: ‘21:00:00’’ is self explanatory, but how does the ‘input_slider.hour’ fit in? does that make the function ‘after 21:00, and before 22:00 and 23:00’? Or is it ‘After 21:00’ fo the main value, but then the random makes a selection between 22:00 and 23:00?

With the minute value, does the ‘(range(30, 45)’ mean somewhere between xx:30 and xx:45, so if I were to change it to, say, ‘(range(01, 59)’, would that mean the possible random value would be between 22:01 and 23:59?

From what I can see, yes. The GRT automation acts like a function and that value is used in the Lights Off automation.

Keeping in mind that this isn’t my automation - so get a second opinion from the author - it looks like the sliders are used to store the hour and minute values that are generated by the random function. Actually pretty cool, but he had it commented out so I’m not sure if it was just a thought experiment or he actually got it to work and then just put it aside to use it later.

Again, you’d have to ask @CCOSTAN… LOL

I haven’t used this one yet. :slight_smile: Future Ideas. I have just broken out the lights into interior and exterior lights so the random times will be the next step for the outdoor lights. Random colors might be next after that! :slight_smile: I will update my github of course and try to revisit this thread when I have it working.

I know @aimc did an app like this in AppDaemon; you may want to look into it.

Just look at this: Set random time for random automatic turning off lights (mimic someone is home)

That script is old and not working anymore (BTW I put that script initially online :)).
Here is an up to date working script: Set random time for random automatic turning off lights (mimic someone is home)

Thanks, I’ll take a look at that.