Random Light Schedule

Hi MarkR,

That’s great but if the family come back that evening, the delay method will not be tested further against the “not_home” condition and wouldn’t that make the rest of your evening pretty erratic?

Just asking.

Regards,

1 Like

you have a homeassistant.turn_off service on the random automation when someone is back home, which stops it running

Sorry for the delay in my answer as I didn’t have much time and I had to test it but no, turning off automations doesn’t stop what has been delayed in that automation from being run. It just won’t trigger anymore.

1 Like

Can somebody explain me the values please? What they are standing for? Thanks

It appears to be randomizing the delay from 00:20:00 to 00:59:00 or 01:20:00 to 01:59:00 (20 to 59 minutes or between an hour and 20 minutes to almost 2 hours)

1 Like

This is how I do my random lights when I am away from home. The below automation calls the script every 15 minutes plus 1 to 10 minutes random. The script checks if the light is on/off and trips it the other way. There is a group of inside lights that it chooses from.

Automation:

  • id: away_lights
    alias: Away Lights
    trigger:
    platform: time
    minutes: ‘/15’
    condition:
    • condition: and
      conditions:
      • condition: state
        entity_id: alarm_control_panel.alarm_system
        state: ‘armed_away’
      • condition: state
        entity_id: sun.sun
        state: below_horizon
        action:
    • delay: ‘00:{{ range(1,10) | random | int }}:00’
    • service: script.turn_on
      entity_id: script.away_lights
      data_template:
      variables:
      eid: ‘{{ states.group.inside_lights.attributes.entity_id | random }}’

Script:
away_lights:
alias: “Away Lights Loop”
sequence:
- service_template: “{% if is_state(eid, ‘off’) %}homeassistant.turn_on{% else %}homeassistant.turn_off{% endif %}”
data_template:
entity_id: ‘{{ eid }}’

2 Likes

Hi! This looks interesting, but i can’t get it to work. Could you please post you config with </>. And could you please explain this part?

eid: ‘{{ states.group.inside_lights.attributes.entity_id | random }}’

It i have a group with the name away_lights that has light inside that should i replace?

This is pulling the entity_id attribute from the group.inside_lights state object.

Lets break down the object: states.group.inside_lights.attributes.entity_id

states is the state machine. This object contains all the objects/domains for home assistant.
group is the domain. Domains are organizational areas for home assistant. I.E. Light, switch, group, input_boolean, climate are all domains of home assistant.
inside_lights is the object_id. The domain + object_id equals the entity_id. I.E. group.inside_lights.
attributes is the property where all ‘extra’ attributes are stored, things like brightness for a light.
entity_id is the attribute that is inside the attributes property.

Now on to the filter. The | random filter chooses a random item in a collection. The collection can be a string or a list or tuple. If you use | random on a string, it will choose a random letter in the string. If you use | random it will choose a random item in the collection.

2 Likes

First, I have improved the automation, first it checks every 15 minutes, if the alarm is armed away and it is after sunset: then it adds an additional randomness to the lights and then toggles a random light on or off.

This is the automation:

  • id: away_lights
    alias: Away Lights
    trigger:
    platform: time
    minutes: ‘/15’
    seconds: 00
    condition:
    • condition: and
      conditions:
      • condition: state
        entity_id: alarm_control_panel.alarm_system
        state: ‘armed_away’
      • condition: sun
        after: sunset
        action:
    • delay: ‘00:{{ range(1,10) | random | int }}:00’
    • service: homeassistant.toggle
      data_template:
      entity_id: ‘{{ states.group.inside_lights.attributes.entity_id | random }}’

This is the group:

inside_lights:
name: Inside Lights
entities:
- light.bedroom_light_level
- switch.zwave_outlet_switch
- switch.kitchen_light_switch
- switch.stairway_light_switch
- switch.playroom_light_switch
- light.family_room_light_level
- switch.living_room_light_switch
- switch.dining_room_light_switch
- light.family_room_dining_light_level

Nice! Not sure how to write the last part in action. Should I have any spaces or should entity_id be in line with data_template.

Below is my config. I have no conditions in this part to see if the automation is workning. And i trigger i with a boolean. Looks ok?

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

away_lights:
  control: hidden
  entities:
#   - scene.kvallsbelysning            ###Visar relevant scene###
#   - scene.slack_lampor               ###Visar släck alla lampor###
#   - input_number.silder1             ###Visar slider för ljusstyrka###
   - light.grovkok                    #Grovköket fönster [IKEA]
   - light.kok_tak                    #Köket taklampa [HUE]
   - switch.fosterbelysning_kok       #Köket fönsterbelysning [TELLSTICK]
   - light.hall_2                     #Hall skänken [HUE]
   - light.hall
   - switch.trappfonster              #Trappen fönster [TELLSTICK]
   - light.kontoret_stalampa          #Kontoret stålampa [HUE]
   - light.vardagsrum_fonster_uterum  #Vardagsrum fönster mot uterum [HUE]
   - switch.slinga2                   #Uterum fönster [TELLSTICK]
   - light.vardagsrum_fonster_vagen   #Vardagsrum fönster mot vägen [HUE]
   - light.vardagsrum_fonster_vagen_2 #Vardagsrum fönster mot vägen [HUE]
   - switch.tv_lampa                  #Belysing på TV-bänk [TELLSTICK]
   - light.vardagsrum_taklampa        #Vardagsrum taklampa TV [HUE]
   - light.uterum_1                   #Uterum vägglampa [IKEA]
   - light.uterum_2                   #Uterum vägglampa [IKEA]
   - light.trappen                    #Trappen taklampa [HUE]
   - light.allrum_ovanvaning          #Allrum ovanvåning fönster [HUE]
   - switch.allrum_ovanvaning          #Slingan ovanvåningen [TELLSTICK]
   - light.sovrum_1_taklampa          #H&M sovrum tak [HUE]
   - switch.fonsterbelysning_sovrum   #H&M sovrum fönster [TELLSTICK]
   - light.sovrum_1_fonster           #Axel tak [HUE]
   - switch.fosterbelysning_axel      #Axel fönster [TELLSTICK]
   - light.sovrum_3_tak               #Sovrum 3 tak [HUE]
   - switch.fonsterbelysning_sovrum_3 #Sovrum 3 fönster [TELLSTICK]
   - switch.julgransbelysning         #Julgran belysning [TELLSTICK]

It’s because @rebelnme isn’t formatting his code properly and you are copying from that. The byproduct is that you end up using invalid string indication characters.

This should work for you:

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

Sorry about that. Evidently when I copied the text from my editor to the web page editor, it changed the characters. Those are not the characters in my automation. And, yes you need to indent the entity_id under the data_template.

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.