Change brightness on lights turned on

Is it possible to change brightness of lights that are turned on? If I use the the lights.turn_on service on an area, and some of the lights are turned off based on a scene, and I change the brightness, the lights that are off are turned on.

I tried different things:

  • Script that creates a group of lights when any of the lights change status (sensor that counts lights that are on)
  • Script that increase the brightness of the lights that are turned on

But I’m running into limitations (or lack of understanding) for both approaches.

create_group_living_room_lights_on:
  mode: queued
  alias: Create 'living room lights' group for every turned on lights
  icon: mdi:lightbulb
  sequence:
    - service: group.set
      data_template:
        name: "Living room lights on"
        object_id: "living_room_lights_on"
        entities: >
          {% for light in 
            expand(states.light) 
            |selectattr('state', 'eq', 'on')
            |selectattr(('state', 'eq', 'on') and 'entity_id', 'in', area_entities('Living room'))
            |map(attribute='entity_id')
            |list
          -%}
            - {{ light }}
          {% endfor %}
increase_living_room_lights:
  sequence:
    - service: light.turn_on
      entity_id: |
        {% for light in 
          expand(states.light) 
          |selectattr('state', 'eq', 'on')
          |selectattr(('state', 'eq', 'on') and 'entity_id', 'in', area_entities('Living room'))
          |map(attribute='entity_id')
          |list
        -%}
          - {{ light }}
        {% endfor %}
      data_template:
        brightness_step_pct: 10

I am not sure how to create groups dynamically, but in the mean time this will work:

action:
  - choose:
    - conditions:
        - condition: state
          entity_id: light.livingroom_1
          state: 'on'
      sequence:
        - service: light.turn_on
          data:
            brightness_step_pct: 20
          target:
            entity_id: light.livingroom_1

    - conditions:
        - condition: state
          entity_id: light.livingroom_2
          state: 'on'
      sequence:
        - service: light.turn_on
          data:
            brightness_step_pct: 20
          target:
            entity_id: light.livingroom_2

    - conditions:
        - condition: state
          entity_id: light.livingroom_3
          state: 'on'
      sequence:
        - service: light.turn_on
          data:
            brightness_step_pct: 20
          target:
            entity_id: light.livingroom_3
    default: []
mode: single

Just add all the light you have in this fashion and it will get the job done. You can turn this automation into a lot of different things with adding Trigger ID’s and expanding it. Hope this helps.

increase_living_room_lights:
  sequence:
    - variables:
        lights_on: >
          {{ expand(area_entities('Living room') | select('match', 'light'))
            | selectattr('state', 'eq', 'on') | map(attribute='entity_id') | list }}
    - condition: "{{ lights_on | count > 0 }}"
    - service: light.turn_on
      target:
        entity_id: '{{ lights_on }}'
      data:
        brightness_step_pct: 10
3 Likes

I believe it depends largely on what brand/model of light you are using - my LIFX bulbs only recognise some of the options for colour and brightness. You will probably need to do a web search or three, and some experimenting.

This works perfect. I had a light group for the living room (created by Philips Hue integration), that I had to disable. It was messing things up.

1 Like

This is quite wonderful! I’m puzzled as how to add multiple areas, i.e. my Living Room and Dining Room, to the template though.

This does not result in any light entities at all:

{{ expand(area_entities('Living Room' + 'Dining Room') | select('match', 'light'))
            | selectattr('state', 'eq', 'on') | map(attribute='entity_id') | list }}

Templating is definitely not my strongpoint… Am I missing something, yet again?

If you refer to the documentation for area_entities you will see that it can only accept one area name. Therefore this invention of yours, to “add” area names together, can’t work.

area_entities('Living Room' + 'Dining Room')

The result of area_entities is a list and you can combine lists so I suggest you provide expand with a combined list like this:

{{ expand((area_entities('Living Room') + area_entities('Dining Room')) | select('match', 'light'))
  | selectattr('state', 'eq', 'on')
  | map(attribute='entity_id') | list }}
1 Like