Is this automation even possible?

The documentation for ‘random’ is the most sparse of anything in the Home Assistant documentation:

" The random integration simply creates random values or state. This can be useful if you want to test automation rules or run an interactive demo. It generates a new state every time it is polled."

That’s it? How do I use it in an automation? Can I set the range of the random results?

What I want to do is to turn off my porch light at random times of sunset plus of 3 to 4 hours.

alias: Porch Light Off at sunset+4
description: ''
trigger:
  - platform: sun
    event: sunset
    offset: '+04:00:00'
condition: []
action:
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.porchlight
mode: single

You mean Home Assistant or ESP-Home?

If you want Home Assistant as your yaml seems to indicate the. Use a delay of {{ range(0, 60) | random }}
minutes

EDIT:
Actually… now that I think about it, that is a shitty solution that I would never normally suggest.
Perhaps as action set a datetime of now() + the range above minutes and trigger on that datetime.
So at sunset + 3 hours you trigger to set a datetime helper of a value.
This value is then the trigger to switch off.

Delaying automations with that many minutes could mean it won’t trigger due to a restart or reload of the automations

Have you read the page?

minimum string (optional, default: 0)

Lower limit for the values.

maximum integer (optional, default: 20)

Upper limit for the values.

What page?
I am looking for a random time object, not just a number

The documentation page for the random integration. Here, I’ll point to it, but I believe you a have read it as you quoted part of it. Random - Home Assistant

Yes, I read that- I was wondering if you had another page in mind. I was asking how to use it as a trigger in an automation.

If you read it I cannot see why you ask the following, which I was answering:

Back to the whole problem, there are such a wide variety of automation triggers, but when I scanned them the Template Trigger and the Input Date Time trigger struck a chord. There is also a random function in jinja which may help in templating. I am going to be a little naughty and tag @123 in here, as he will probably know the best way, rather than you and me guessing :slight_smile:

I have my front porch light turn off at a random time +/- 15 minutes from 10pm. The basic method I use is similar to @Hellis81’s suggestion above to set an Input Datetime helper. The script is called as part of an automation that handles resetting a few different helpers after midnight every night. The reason I do it this way instead of using a template trigger directly in the automation is as follows: A template using now() would be re-rendered every minute with a new random time and there is an edge case where the random time never matches now().

The random time script is as follows:

alias: Helper - Set Random Porch Time
sequence:
  - service: input_datetime.set_datetime
    data:
      datetime: '{{ time_calc }}'
    target:
      entity_id: input_datetime.porch_light_time
mode: single
variables:
  time_calc: >
    {{ (today_at("21:45:00") + timedelta(minutes = (range(30) | random))).strftime("%H:%M:%S") }}

Then I use input_datetime.porch_light_time as a trigger in my light control automation.

For your sunset +3-4hr time you can use the following template for time_calc:

{{ state_attr('sun.sun', 'next_setting')|as_datetime|as_local
+ timedelta(minutes= range(180, 240)|random) }}
1 Like

Looks good, I would have taken hours to get that right. It’ll join the rest of my copy & paste setup :slight_smile:

Someone else had a similar question last December. They wanted to offset both sunrise and sunset by a random value ranging from plus or minus 25 minutes. The solution was to create two Trigger-based Template Sensors that computed the times using random:

The part that performs the random selection of values between -25 and +25 minutes is:

timedelta(minutes=range(-25, 26) | random)

In your case, where you want +3 to +4 hours (180 to 240 minutes), it would be:

timedelta(minutes=range(180, 241) | random)

If you want it to randomly select +3 or +4 hours:

timedelta(hours=[3,4] | random)

random is documented here and simply selects a value from a supplied sequence. So if you want it to randomly select either 5, 8, 11, or 23 you can create a list containing the values and feed it to random.

[5, 8, 11, 23] | random

If you want it to select a value from a long sequential sequence of integer values, instead of typing all of them in a list, you can use the range function to generate it for you. The documentation for range indicates the second argument’s value must be one higher than the value you want. For example, if want from 1 to 100 you would do this: range(1, 101)

1 Like

DidgeridrewDrew
Thanks. this looks pretty straightforward. Can you help me identify my problem. When I run the script I get this error:

Failed to call service script/set_porchlight_off_time. Invalid time specified: 2022-03-31 22:23:02.242724 for dictionary value @ data[‘time’]

My script:

alias: set porchlight off time
sequence:
  - service: input_datetime.set_datetime
    data:
      time: '{{ time_calc }}'
    target:
      entity_id: input_datetime.porchlight_off_time
mode: single
variables:
  time_calc: >
    {{ state_attr('sun.sun', 'next_setting') | as_datetime | as_local + timedelta(minutes = 240 -(range(60) | random)) }}

The data variable time: should be datetime:… I cleaned up the template math a little as well:

alias: set porchlight off time
sequence:
  - service: input_datetime.set_datetime
    data:
      datetime: '{{ time_calc }}'
    target:
      entity_id: input_datetime.porchlight_off_time
mode: single
variables:
  time_calc: >
    {{ state_attr('sun.sun', 'next_setting') | as_datetime | as_local 
    + timedelta(minutes = range(180, 240) | random) }}

The clarification is much appreciated. This is the first time I used variables: in a script; I learned something new today. Thanks

Many thanks. I wish I could set two solutions because this and Didgeridrew’s method both work. I did learn a lot here.