Turn light on/off at random time

Hi all,

I am new to HA and just trying to automate my lights to turn on or off at a random time between two time or after a cirtain hour. So for example, i want my light in my living room window to turn on around 16:00. But with a random time between 15:45 and 16:15 for example. Is this possible and how would i go about doing this? I am a complete noob when it comes to HA and it was a loong time ago since i last touched any coding.

and if it is possible to combine and autumation for a light to turn on at a random time as described and then turn off at a random time in the same event or what to say that would be awesome aswell!

trigger:
  - id: 'on'
    platform: time
    at: "15:45:00"
  - id: 'off'
    platform: time
    at: "19:00:00"
action:
  - delay:
      minutes: "{{ range(0,31)|random }}"
  - service: "light.turn_{{ trigger.id }}"
    target:
      entity_id: light.your_light_here

How it works:

It triggers at 15:45 and 19:00 then waits a random time between 0 and 30 minutes, then turns your light on or off respectively.

You did not mention when you wanted it turned off so I chose 19:00 which will mean it can turn off anywhere from 19:00 to 19:30.

2 Likes

Assuming you want to do this to have lights turn on and off while you’re away, there is a very good Presence Simulation HACS integration that might help you.

1 Like

You can also use this app with the app daemon addon, it just replays your activity from the past to make it look like you’re home. You get your normal behavior pattern this way.

Thanks for the answers!

Yes i want the lights to turn on before I get home after work and before it gets dark outside here in sweden. And then turn off during the night after I go to bed so around 02-03 or so. I will definitely test out the trigger and Chek out the two apps aswell.

when i try to add the trigger you gave me in the Automation section i get a time format error?

Remove the “-” left from minutes

  - delay:
      minutes: "{{ range(0,31)|random }}"
2 Likes
alias: "Light "
description: sätter på/ stänger av Light random tid
trigger:
  - id: "on"
    platform: time
    at: "15:45:00"
  - id: "off"
    platform: time
    at: "03:00:00"
action:
  - delay:
      minutes: "{{ range(0,31)|random }}"
  - service: light.turn_{{ trigger_id }}
    target:
      entity_id: light.buren
    data: {}

It does not seem to work, i can see in the logg and traces that it triggerd at 03:00. But the light was still on when i woke up this morning. have i missed something in the trigger or is it something else?

image
the lights dont turn on either. even thou the light named “Buren” says it is turned on at 16:00 it is not on?

Here’s how I would do it.

Create two Trigger-based Template Sensors.

template:
  - trigger:
      - platform: time
        at: '00:00:00'
      - platform: event
        event_type: event_template_reloaded
    sensor:
      - name: 'Buren On'
        unique_id: 'buren_time_on'
        state: "{{ (today_at('15:45:00') + timedelta(minutes = range(0,31) | random)).isoformat() }}"
        device_class: timestamp
      - name: 'Buren Off'
        unique_id: 'buren_time_off'
        state: "{{ (today_at('03:00:00') + timedelta(minutes = range(0,31) | random)).isoformat() }}"
        device_class: timestamp

New randomized time values will be generated at the start of every day and whenever Template entities are reloaded.

Use them in your automation like this:

alias: "Light "
description: sätter på/ stänger av Light random tid
trigger:
  - id: "on"
    platform: time
    at: sensor.buren_on
  - id: "off"
    platform: time
    at: sensor.buren_off
action:
  - service: 'light.turn_{{ trigger.id }}'
    target:
      entity_id: light.buren

The advantage of this method is that it is less vulnerable to a restart compared to a delay. If Home Assistant is restarted while the delay is counting down, the delay will be terminated and the automation will not execute the next action (in other words it wouldn’t turn the light on or off).


NOTE

There’s a mistake in your existing automation.

  - service: light.turn_{{ trigger_id }}
                                  ^
                                  |
            This should be a period not an underscore 
1 Like

Ugh. Why do I keep doing that?

Fixed in the original post. Thanks.

1 Like