Set same brightness level on many light at once

I’d like to set the brightness level on all the lights by using a code that is more simple than the code I use below. I am actually repeating the same code for each light so there have to be a better way to do this.

As far as I know, there is no way to set the brightness level on a group, just on individual lights.

  - alias: "Turn lights on / of for the cleaning lady"
    trigger:
      platform: state
      entity_id: sensor.cleaning_lady_home
    action:
      - service: homeassistant.turn_on
        entity_id: 
          - light.taklampa_vardagsrum_soffan_level
        data_template:
          brightness: >
            {% if is_state('sensor.cleaning_lady_home' , 'true') %} 255
            {% else %} 0
            {% endif %}  
      - service: homeassistant.turn_on
        entity_id: 
          - light.taklampa_vardagsrum_bokhyllan_level
        data_template:
          brightness: >
            {% if is_state('sensor.cleaning_lady_home' , 'true') %} 255
            {% else %} 0
            {% endif %}        
      - service: homeassistant.turn_on
        entity_id: 
          - light.taklampa_ovre_trapphall_level
        data_template:
          brightness: >
            {% if is_state('sensor.cleaning_lady_home' , 'true') %} 255
            {% else %} 0
            {% endif %} 

…and so on and on for all the lights in the house.

You should use the light.turn_on service, and you should just list all the lights in the entity_id parameter:

  - alias: "Turn lights on / of for the cleaning lady"
    trigger:
      platform: state
      entity_id: sensor.cleaning_lady_home
    action:
      service: light.turn_on
      entity_id: 
        - light.taklampa_vardagsrum_soffan_level
        - light.taklampa_vardagsrum_bokhyllan_level
        - light.taklampa_ovre_trapphall_level
      data_template:
        brightness: >
          {% if is_state('sensor.cleaning_lady_home' , 'true') %} 255
          {% else %} 0
          {% endif %}  

I’m not sure that setting brightness to zero will work. Are you trying to turn the lights off that way? I think it might work on some types of lights, but not all.

1 Like

You could also set up a light group for all the lights you want to include in the automation and call the service light.turn_on for the light group entity instead of all individual lights.

1 Like

These are mostly Fibaro Dimmer 2 so setting the brightness level to 0 will most probably work.