Random Light Schedule

Thanx!
When writing 00:{{ range(1,10) | random | int }}:00' in templates I get ex 00:8:00 that home assistant dont like. It should be in hh:mm:ss. What changes should i do?

"00:{{ '{:02}'.format(range(1,10) | random | int) }}:00"
1 Like

After adding that i get this error in my log

2018-11-28 21:24:54 ERROR (SyncWorker_0) [homeassistant.util.yaml] while parsing a block mapping
  in "/config/automations/test.yaml", line 7, column 5
expected <block end>, but found '{'
  in "/config/automations/test.yaml", line 7, column 20
2018-11-28 21:24:54 ERROR (MainThread) [homeassistant.bootstrap] Error loading /config/configuration.yaml: while parsing a block mapping
  in "/config/automations/test.yaml", line 7, column 5
expected <block end>, but found '{'
  in "/config/automations/test.yaml", line 7, column 20

gotta post your whole config, can’t tell what the error is

This works fine for me:

  action:
    - delay: '00:{{ (range(1, 59)|random|int) }}:{{ (range(1, 59)|random|int) }}'
2 Likes

I don’t personally use delays so I’m not certian if the numbers need to have leading zeros when under 10. If that works, then that is what @Henrik1986 should use.

Sorry. This is the config that gave me the errors. And yes home assistant will have this format HH:MM:SS or HH:MM

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 }}'

Home assistant will have two numbers in minutes also.

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!