Random Light Schedule

Your issue is because you have quotes inside quotes. You cannot encapsolate single quotes inside single quotes. You need to use double quotes for the entire template and single quotes inside the template. Also, you are missing the colon after the word delay. Remember, yaml is space and punctuation driven, without that, yaml doesn’t know how to parse properly.

alias: TEST
trigger:
  platform: state
  entity_id: input_boolean.trigger
  to: 'on'
action:
  - delay: "00:{{ '{:02}'.format(range(1,10) | random | int) }}:00"
  - service: homeassistant.toggle
    data_template:
      entity_id: '{{ states.group.away_lights.attributes.entity_id | random }}'
1 Like

Thanks for the lesson. Learned a lot today. Thank you :smiley::smiley::tada::tada:

The code example I provided did not work for you?

Here is the code cleaned up… note conditions are by default AND so that part wasn’t needed.

################################################################
## Turn on/off indoor lights randomly while night and armed away
################################################################
- alias: Random Away Lights
  initial_state: True
  hide_entity: False

  trigger:
    - platform: time
      minutes: '/20'
      seconds: 00

  condition:
    - condition: state
      entity_id: alarm_control_panel.ha_alarm
      state: 'armed_away'

    - condition: sun
      after: sunset
  
  action:
    - delay: '00:{{ range(1,19) | random | int }}:00'

    - service: homeassistant.toggle
      data_template:
        entity_id: "{{ state_attr('group.indoor_random_away_lights','entity_id') | random }}"

It did, but there is change that it won’t work because home assistant will have the time delay in this two formats.
HH:MM:SS or HH:MM

So if the random delay is 8 min home assistant will not fire the action.

So I made some changes after some days. It’s pretty annoying that lights is turn off or on when i come home because the random delay. So i did a script with the conditions that it’s only execute when the alarmpanel is disarmed after that a random lamp is turned off or on.

But one question. Is it possible to send brightness level in the action section in the scripts. when the lamp is turned of and then turned on is comes back on 100 %.

alias: Belysning - inte hemma
trigger:
  platform: time
  minutes: '/45'
  seconds: 00
condition:
 condition: and
 conditions:
  - condition: state
    entity_id: alarm_control_panel.ha_alarm
    state: 'armed_away'
  - condition: sun
    after: sunset
action:
  - delay: "00:{{ '{:02}'.format(range(1,30) | random | int) }}:00"
  - service: script.turn_on
    entity_id: script.away

Script

  away:
    alias: Away lights
    sequence:
      - condition: state
        entity_id: alarm_control_panel.ha_alarm
        state: 'armed_away'
      - service: homeassistant.toggle
        data_template:
          entity_id: '{{ states.group.away_lights.attributes.entity_id | random }}'

You’d have to turn the group into a light group if you want to pass brightness to the lights in the group. Also, they’ll all have to be lights.

Thanks for asking this question as it’s given me inspiration to set this up myself!

For those interested here is my complete “random light” code.

It also includes automations for sunrise and alarm disarm to turn off the lights it’s randomly turned on.

As always suggestions and improvements welcome! I learn so much from this community – it truly is fantastic.

################################################################
## Random lights on while sunset and armed away
################################################################
- alias: Random Away Lights
  initial_state: True
  hide_entity: False

  trigger:
    - platform: time
      minutes: '/30'
      seconds: 00

  condition:
    - condition: state
      entity_id: alarm_control_panel.ha_alarm
      state: 'armed_away'

    - condition: sun
      after: sunset
  
  action:
    - delay: "00:{{ '{:02}'.format(range(1, 29) | random | int) }}:00"

    - condition: state
      entity_id: alarm_control_panel.ha_alarm
      state: 'armed_away'

    - service: homeassistant.toggle
      data_template:
        entity_id: "{{ state_attr('group.indoor_random_away_lights','entity_id') | random }}"
 
##########################################################
## Sunrise Random Lights Off
##########################################################
- alias: Random Lights Off Sunrise
  initial_state: True
  hide_entity: False

  trigger:
    - platform: sun
      event: sunrise
  
  condition:
    - condition: state
      entity_id: alarm_control_panel.ha_alarm
      state: 'armed_away'

  action:
    - service: homeassistant.turn_off
      entity_id: group.indoor_random_away_lights

##########################################################
## Alarm Disarmed Random lights Off
##########################################################
- alias: Random Lights Off Disarmed
  initial_state: True
  hide_entity: False

  trigger:
    - platform: state
      entity_id: alarm_control_panel.ha_alarm
      from: 'armed_away'
      to: 'disarmed'

  action:
    - service: homeassistant.turn_off
      entity_id: group.indoor_random_away_lights
3 Likes

Does this work? I thought you needed to ensure that hh, mm and ss were always two digits.
I do this:

- delay: "00:{{ '{:02}'.format(range(5, 11) | random | int) }}:00"

Apparently that’s up for debate because many people claim they are using it without the leading zero.

Maybe their random number has never been one digit long :rofl::sunglasses:

1 Like

murphy’s law right there!

My guess is that when HA see’s a time, it parses the time based on colons. If it parsed that way, it would work fine.

It seems to work and is used a couple times a day in various automations; and they are all working.

But I do agree yours is the proper solution to ensure it’s 2 digits for each field and I’ll happily update.

Thanks for sharing. Haven’t you got the state of the alarm panel under conditions in there twice unnecessarily?

  condition:
    - condition: state
      entity_id: alarm_control_panel.ha_alarm
      state: 'armed_away'

    - condition: sun
      after: sunset
  
  action:
    - delay: "00:{{ '{:02}'.format(range(1, 29) | random | int) }}:00"

    - condition: state
      entity_id: alarm_control_panel.ha_alarm
      state: 'armed_away'

    - service: homeassistant.toggle
1 Like

The second one is to ensure the light is turned on after the delay only if the alarm is still armed. During that delay if you disarm the light would still come on without that 2nd one.

Thanks a heap. Didn’t realise (after 2+ yrs of using HA), that you can have a condition after an action! Awesome. P.S If you can edit your code above, the time statement is no longer valid. It now needs to be:

trigger:
  - platform: time_pattern
    minutes: '/30'
1 Like

I thought I’s share what I have been using for a few months. I called it “Occupancy Simulator”.

occupancy_simulator.yaml (package)

input_boolean:
  enable_occupancy_simulator:
    name: Enable Occupancy Simulator

automation:
  - alias: Occupancy Simulator-Doorbell
    initial_state: 'on'
    trigger:
      - platform: event
        event_type: xiaomi_aqara.click
        event_data:
          entity_id: binary_sensor.switch_158d00018b3ede
          click_type: single
    condition:
      - condition: state
        entity_id: input_boolean.enable_occupancy_simulator
        state: 'on'
      - condition: state
        entity_id: sun.sun
        state: 'below_horizon'
      - condition: template
        value_template: "{{ is_state('device_tracker.davess7lan','not_home') and is_state('device_tracker.rowess7lan','not_home') }}"
    action:
      - delay: '00:00:{{ range(10,40) | random | int }}'
      - service: homeassistant.turn_on
        data:    
          entity_id: light.bedroom_lamp
          brightness: 200
      - delay: '00:0{{ range(9) | random | int }}:{{ range(10,58) | random | int }}'
      - service: homeassistant.turn_off
        data:    
          entity_id: light.bedroom_lamp

  - alias: Occupancy Simulator-Random Timed Lights (Sunset)
    initial_state: 'on'
    trigger:
      - platform: sun
        event: sunset
        offset: '00:15:00'
    condition:
      - condition: state
        entity_id: input_boolean.enable_occupancy_simulator
        state: 'on'
      - condition: template
        value_template: "{{ is_state('device_tracker.davess7lan','not_home') and is_state('device_tracker.rowess7lan','not_home') }}"
    action:
      - delay: '00:{{ range(10,40) | random | int }}:00'
      - service: homeassistant.turn_on
        data:    
          entity_id: light.kitchen_light_level
          brightness: 255
      - delay: '00:0{{ range(9) | random | int }}:{{ range(10,58) | random | int }}'
      - service: homeassistant.turn_on
        data:    
          entity_id: light.bedroom_lamp
          brightness: 255
      - delay: '00:0{{ range(9) | random | int }}:{{ range(10,58) | random | int }}'
      - service: homeassistant.turn_off
        data:    
          entity_id: light.kitchen_light_level
      - delay: '00:0{{ range(9) | random | int }}:{{ range(10,58) | random | int }}'
      - service: homeassistant.turn_off
        data:    
          entity_id: light.bedroom_lamp
      - delay: '00:0{{ range(9) | random | int }}:{{ range(10,58) | random | int }}'
      - service: homeassistant.turn_on
        data:    
          entity_id: light.kitchen_light_level
          brightness: 255
      - delay: '00:0{{ range(9) | random | int }}:{{ range(10,58) | random | int }}'
      - service: homeassistant.turn_off
        data:    
          entity_id: light.kitchen_light_level

  - alias: Occupancy Simulator-Random Timed Lights (Midnight)
    initial_state: 'on'
    trigger:
      - platform: time
        at: '00:00:00'
    condition:
      - condition: state
        entity_id: input_boolean.enable_occupancy_simulator
        state: 'on'
      - condition: template
        value_template: "{{ is_state('device_tracker.davess7lan','not_home') and is_state('device_tracker.rowess7lan','not_home') }}"
    action:
      - delay: '0{{ range(2) | random | int }}:{{ range(10,55) | random | int }}:00'
      - service: homeassistant.turn_on
        data:    
          entity_id: light.bedroom_lamp
          brightness: 255
      - delay: '00:0{{ range(9) | random | int }}:{{ range(10,58) | random | int }}'
      - service: homeassistant.turn_off
        data:    
          entity_id: light.bedroom_lamp
      - delay: '00:0{{ range(9) | random | int }}:{{ range(10,58) | random | int }}'
      - service: homeassistant.turn_on
        data:    
          entity_id: light.bedroom_lamp
          brightness: 255
      - delay: '00:0{{ range(9) | random | int }}:{{ range(10,58) | random | int }}'
      - service: homeassistant.turn_off
        data:    
          entity_id: light.bedroom_lamp

I have am input_boolean in the GUI to turn it on/off and it only switches lights if my girlfriend and I are both not_home. If someone presses the doorbell, it starts a light switching sequence, likewise at a random time after sunset and again after midnight.

1 Like

What type of doorbell are you using?

I’m using a Xiaomi wireless button (round type) for the doorbell button and the Xiaomi Gateway to produce the chime sound. As well as making the chime sound, automations pause any playing media and send notifications to phone’s with a camera snapshot

1 Like