My aim is to create “dynamic” (I know, not truly dynamic) groups of light entities per room.
I’d like to create dynamic light groups (as opposed to old groups of entities), but I cant find information on how to do that.
When I restart Home Assistant this automation throws the following error:
Automation with alias 'Create Lounge Room Light Group' could not be validated and has been disabled: template value should be a string for dictionary value @ data['action'][1]['data']. Got OrderedDict([('name', 'Lounge Room Lights'), ('object_id', 'lr_lights'), ('entities', "{{ area_entities('lounge room') | expand \n | selectattr('domain', 'eq', 'light') \n | map(attribute='entity_id') \n | list }}\n")])
When I run the automation with the light entities manually defined then the automation passes validation and works fine, so I am assuming it is something to do with how the template is written.
I have a script to dynamically create groups for my power sensors per area. That will be quite easy to adapt to lights (it’s even easier,as you don’t have to check for specific integrations as I have done).
But this is creating using the old fashioned groups, as I’m pretty sure there’s no service like group.set for the new types.
It still possible to create the new types thru yaml. Have you ever tried to use a template for the entities in that case? That would have the downside of updating only during startup, but maybe this is good enough for your case.
script:
home_groups_update:
alias: Home - Groups - Update
icon: mdi:group
mode: single
variables:
core_power_entities: "{{ ['sensor.kitchen_cooker_hood_power'] + expand(integration_entities('shelly')) | selectattr('domain', 'eq', 'sensor') | selectattr('attributes.device_class', 'defined') | selectattr('attributes.device_class', 'eq', 'power') | map(attribute='entity_id') | list }}"
core_switch_entities: "{{ ['switch.kitchen_cooker_hood'] + expand(integration_entities('shelly')) | selectattr('domain', 'eq', 'switch') | map(attribute='entity_id') | list }}"
all_areas: >-
{% set areas = namespace(area=[]) %}
{% for state in expand(core_power_entities) %}
{% set areas.area = areas.area + [area_name(state.entity_id)] %}
{% endfor %}
{{ areas.area | unique | list }}
sequence:
- alias: 'Group: Core power switches'
continue_on_error: true
service: group.set
data:
object_id: home_core_power_switches
name: Home - Core power switches
icon: mdi:flash
entities: "{{ core_switch_entities | join(',') }}"
## Deprecated - Now using sensor groups ##
- alias: 'Group: Core power sensor'
enabled: false
continue_on_error: true
service: group.set
data:
object_id: home_core_power_sensors
name: Home - Core power sensors
icon: mdi:flash
entities: "{{ core_power_entities | join(',') }}"
- alias: 'Group: Power sensors by areas'
enabled: false
continue_on_error: true
repeat:
count: '{{ all_areas | count }}'
sequence:
- service: logbook.log
data:
name: Create group
entity_id: '{{ this.entity_id }}'
message: "Creating group {{ all_areas[repeat.index - 1] | lower | replace(' ', '_') | replace('-', '') }}_power_sensors."
continue_on_error: true
- service: group.set
data:
object_id: "{{ all_areas[repeat.index - 1] | lower | replace(' ', '_') | replace('-', '') }}_power_sensors"
name: "{{ all_areas[repeat.index - 1] | capitalize }} - Power sensors"
icon: mdi:flash
entities: >-
{{ expand(core_power_entities)
| selectattr('entity_id', 'in', area_entities(all_areas[repeat.index-1]))
| map(attribute='entity_id')
| list
}}
continue_on_error: true