One lights automation to rule them all

Tags: #<Tag:0x00007f3268e83888>

With the new variables and repeat action in Home Assistant, I have created a single automation to control all my lights.

First I have a input_select.lights_mode defined:

input_select:
  lights_mode:
    name: Lights mode
    options:
      - 'off'
      - morning
      - day
      - evening
      - bedtime
      - night

And then this automation which controls all the default settings in the different modes.

automation:
- alias: "Lights mode automation"
  variables:
    settings:
      morning:        
        hallway_light:          [  25, 2200 ]
        wardrobe_light:         [   0, 2200 ]
        bedroom_light:          [  10, 2200 ]
        kitchen_light:          [  75, 2200 ]
        floor_lamp_light:       [  75, 2200 ]
        # more ...
      evening:
        hallway_light:          [  25, 2200 ]
        wardrobe_light:         [   0, 2200 ]
        bedroom_light:          [  25, 2200 ]               
        kitchen_light:          [ 100, 2700 ]
        floor_lamp_light:       [ 100, 2700 ]
        # more ...      
      bedtime:
        # more ...
      night:
        # more ...
  trigger:
    platform: state
    entity_id: input_select.lights_mode
  action:
    repeat:
      while:
      - condition: template
        value_template: "{{ repeat.index <= settings[ states('input_select.lights_mode') ] | length }}"
      sequence:
      - variables:
          lights_mode: "{{ states('input_select.lights_mode') }}"
          entity_id: "{{ ( settings[ lights_mode ].keys()|list )[ repeat.index - 1] }}"
          brightness_pct: "{{ ( settings[ lights_mode ].values()|list )[ repeat.index - 1][0] }}"
          kelvin: "{{ ( settings[ lights_mode ].values()|list )[ repeat.index - 1][1] }}"
      - service: light.turn_on
        data:
          entity_id: "{{ 'light.' ~ entity_id }}"
          brightness_pct: "{{ brightness_pct }}"
          kelvin: "{{ kelvin }}"
          transition: 15

The parameters hallway_light:[ 25, 2200 ] - first one is brightness_pct and the second is kelvin.

I hope this can serve as an inspiration :slight_smile:

You can shorten this:

      while:
      - condition: template
        value_template: "{{ repeat.index <= settings[ states('input_select.lights_mode') ] | length }}"

to:

      while: "{{ repeat.index <= settings[ states('input_select.lights_mode') ] | length }}"
1 Like

Why not just use Scenes?

Good question - later on, I will add additional variables controlling the lights when I have guests over. That would mean 6 scenes x 2 (one for guests, one for no guests). With 20+ lights, I would find that completely unmanageable.

I think this solution makes me have a better overview.

I actually dropped scenes a long time ago, since transitions were not supported, but I can see it has been reintroduced.

Good sugggestion - thanks. Makes it look more and more like regular programming.

How will it be more manageable using variables?

Won’t you have to create six more variables for guests?

I will probably do something along the line of

  variables:
    settings:
      morning:        
        hallway_light: [[  25, 2200 ]] # no guests / guests are the same.
        wardrobe_light: [[ 0, 2200 ], [ 25, 2200 ]] # no guests first, guests last.