Random Light Schedule

Hello.

Is there a way to create a random light schedule for say, when you are on holiday. I have so far 5 Philips hue lights named
- light.dining_room_light
- light.bedroom_light
- light.living_room_light
- light.hall_light
- light.landing_light
and was wondering if it was possible to have say 2 or 3 on for 60-90 minutes then switch to another random set etc. Or am I just trying to take things a tad too far ?? :smiley:

Cheers
Mark

Here is one way:

You will need to install AppDaemon.

Here is how I do it https://github.com/Danielhiversen/home-assistant_config/blob/master/automation/lighting.yaml#L229

4 Likes

wow this place is amazing
 thanks guy! time to make my head hurt again!! :smiley:

Sorry dragging this up again as I am wanting to finally implement it, as I have a rather static setup and never got time to do this.

How does this work Daniel please?

The condition runs that script that does what exactly? and how does this work?

 delay: '0{{ range(0,3) | random | int }}:{{ range(20,59) | random | int }}:00'

what times are they turned on between and same goes for off

  - delay: '00:0{{ range(4,10) | random | int }}:00'

Thanks
 it is probably dead easy but my brain won’t work right now


I think i have worked it out regarding the delays, I keep forgetting I can test using the templates section

I don’t understand the condition script though :slight_smile:

It looks like @Danielhiversen is using a series of automations and scripts to track whether or not he’s been home in the past 48 hours. So, his automation triggers once the sun is below -4 degrees, but only if he’s not home.

You could just as easily get rid of this condition if you plan on enabling your “away mode” manually, or use some other condition.

1 Like

yeah cheers, I already do that myself to a lesser degree (well not now :smiley: ) it was the 48 hours bit I couldn’t work out how it was ran, i.e. is it a manual switch on the frontend? thanks

I have kind of butchered it and have come up with this
 it is used along with 2 other I have on most of the time during the evening anyway and used a similar idea to turn them all off after a certain now random time
 loving it thanks again

- alias: 'Switch on Random Lights when we are out'
  hide_entity: False
  trigger:
    platform: numeric_state
    entity_id: sensor.solar_angle
    below: -20.0
  condition:
    condition: state
    entity_id: group.family
    state: 'not_home'
  action:
     - delay: '0{{ range(0,1) | random | int }}:{{ range(20,59) | random | int }}:00'
     - service: light.turn_on
       entity_id: light.dining_room,light.kitchen
       data:
         rgb_color: [255, 255, 255]
         brightness: 150
     - delay: '0{{ range(0,1) | random | int }}:{{ range(20,59) | random | int }}:00'
     - service: light.turn_off
       entity_id: light.dining_room,light.kitchen
     - delay: '0{{ range(0,2) | random | int }}:{{ range(10,59) | random | int }}:00'
     - service: light.turn_on
       entity_id: light.bedroom,light.bathroom
       data:
         rgb_color: [255, 215, 255]
         brightness: 150
     - delay: '0{{ range(0,1) | random | int }}:{{ range(20,59) | random | int }}:00'
     - service: light.turn_off
       entity_id: light.bathroom

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.