How can I create a light group programmatically?

Do the groups need to be dynamic?

If not, you can use the Template Editor to write the light group configuration for you, then copy the rendered value into your actual configuration…

# For use in Template Editor ONLY
# DO NOT USE DIRECTLY AS CONFIGURATION, IT WILL NOT WORK

light:
{%- for a in areas() if area_entities(a)|select('match', 'light.')| list | count > 0 %}
  - platform: group
    name: "{{ area_name(a) }} Lights"
    unique_id: {{ area_name(a)|slugify }}_lights_unique_id
    entities:
      {%- for e in area_entities(a)|select('match', 'light.')| list %}
      - {{e}}
      {%- endfor %}
{% endfor %}

# For use in Template Editor ONLY
# DO NOT USE DIRECTLY AS CONFIGURATION, IT WILL NOT WORK

If they need to be dynamic, you need to use a Template light as mentioned previously by dominic. A template light won’t have exactly the same properties as a light group, but it will still be a light entity that could be added to any dashboard card and would have the interface, functions, etc.

1 Like