Request for Help: Creating Template Lights for Multiple Rooms and Setups

Hello guys!

I am looking for guidance on how to create template lights that function like a “scene” in Home Assistant. The goal is to control a group of lights with parameters such as brightness, effects, and colors, and have the ability to turn them all off simultaneously.

Ideally, I would like to be able to easily replicate this code for multiple rooms and specific setups using variables to set the lights and get their states (if any) to determine the template light’s current status.

Here’s a non-functional example of what I have in mind:

template_lights_suite:
  unique_id: template_lights_suite
  friendly_name: "Template - Suite Lights"
  variables:
    lights:
      - light.painel_tv_suite_ceiling_led
      - light.painel_tv_suite_tv_led
      - light.painel_tv_suite_floor_led
      - light.roberta_dresser_led
      - light.antonio_dresser_led
      - light.roberta_bedside_led
  value_template: "{{ is_state('**lights**', 'on') }}"
  turn_on:
    - service: light.turn_on
      data:
        entity_id: {{ lights }}
        rgb_color: [0, 75, 245]
        transition: 1
  turn_off:
    service: light.turn_off
    data:
      entity_id: {{ lights }}

I would appreciate any suggestions on how to approach this issue. Perhaps @123 or other experienced members could provide some guidance?

Thank you in advance for your help!

Light Group

Hi, I appreciate your response. I think template light is more suitable for my needs because it allows me to control individual lights with different settings, such as brightness, color, effect and so on. Light groups are more limited and can only turn a group of lights on and off, right?

That wasn’t clear to me from the requirements mentioned in your first post. The example you posted is using the same settings for all lights.

A Light Group is effectively a virtual light entity so you can control its brightness, color, etc.

My apologies, I was too simplistic in my explanation. You are right :slight_smile:

My idea is to turn on a few different lights with their own specific settings, have their state as template (if any = true) and turn them off with a simple light.turn_off command.

Personally I wouldn’t. There are better controls for this. You are basically replicating scenes and scripts in a way that won’t be apparent to end users. And as such will become hard if not impossible to maintain.

Light groups for groups of lights that you want to control together. Scenes for sets of settings. Then you can use other controls to automate those.

Why. What happens when you want to change settings? Are you going to manually code each lamp setting into the template? (id strongly suggest not…). Who updates them?

In my setup I have an input select for each ‘room’ which is one of a bunch of states. (vacant, occupied, cleaning, locked, etc.) just image each as a program with a set of conditions to trigger it. When the conditions trigger a state, the automation then goes and looks up what scene is appropriate for the room at that time with the current condition…

Occupied midday with one person home is different than night with multiple people home…

Ive not built anything non-standard so anyone who knows HA can figure it out and more importantly my wife can edit the scenes without my help. The code is the code and the settings are the settings.

Hey Nathan,

Thanks for taking the time to share your thoughts on this. I totally get your concerns about maintainability and ease of use for other users. But, as the only one managing the code in my setup, I’m not too worried about the complexity.

What I’m trying to do is create a template light that uses variables, so I can easily replicate it for different groups of lights. I need it to reflect the current state of all the selected lights, and I want to be able to control their individual turn-on properties (like brightness, effects, color, etc.) as a group, then turn them all off at once.

I’m aware of how to achieve this using light groups and scenes, but I’d like to create a more streamlined solution by using template lights with variables. This way, I can quickly set up as many template lights as needed by simply changing the variables.

I appreciate your input and the ideas you’ve shared, but I’m still hoping to find a solution that fits my specific needs. If you or anyone else in the Home Assistant community has any suggestions on how to accomplish this, I’d be super grateful for the help.

Thanks again for your response, and I look forward to any additional insights you might have.

Cheers!

1 Like

This may not be exactly what you’re looking for but I create light groups dynamically using areas. Here’s the automation I use

alias: Group - Create interior lights
description: ""
trigger:
  - platform: homeassistant
    event: start
condition: []
action:
  - repeat:
      for_each:
        - area_name: Kitchen
        - area_name: Master bedroom
        - area_name: Living room
        - area_name: Family room
        - area_name: Gym
        - area_name: Hallway
        - area_name: Guest bathroom
        - area_name: Master bathroom
      sequence:
        - variables:
            area_name: "{{ repeat.item.area_name }}"
            members: >
              {{ area_entities(area_name) | reject('is_hidden_entity') | expand
              | selectattr('entity_id', 'match', 'light') | rejectattr("state",
              "eq", "unavailable") | map(attribute='entity_id') | list }}
        - if:
            - condition: template
              value_template: "{{ members |length > 0 }}"
          then:
            - service: group.set
              data:
                object_id: interior_lights
                add_entities: |
                  {{members}}
mode: single

What this does is on each home assistant start is it gets a list of all the lights in each area I defined in the for_each and appends them to a group called group.interior_lights.

I did this because I tinker a lot, adding and removing lights to areas pretty frequently, but don’t want to always have to rebuild my light groups.

This also uses the new is_hidden_entity feature to filter out hidden lights.

1 Like