Strugling to get an automation to pass validation

Hi,

I have the following automation defined:

automation:
  - id: create_lounge_room_light_group
    alias: "Create Lounge Room Light Group"
    mode: restart
    trigger:
      - platform: homeassistant
        event: start
      - platform: event
        event_type: "call_service"
        event_data:
          domain: "group"
          service: "reload"
    action:
      - service: group.set
        data:
          name: "Lounge Room Lights"
          object_id: lr_lights
          entities: >
            {{ area_entities('lounge room') | expand
              | selectattr('domain', 'eq', 'light')
              | map(attribute='entity_id')
              | list }}

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.

Any advice?

Seems that there is still a problem with the use of expand as a filter. Use this instead:


            {{ expand(area_entities('lounge room'))
              | selectattr('domain', 'eq', 'light')
              | map(attribute='entity_id')
              | list }}

or the shorter one:


            {{ area_entities('lounge room')
              | select('match', 'light')
              | list }}

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

I’ve tried that. It won’t work. :weary:

Thank you very much! That worked perfectly.

I had been playing around with filtering by other attributes, which is why I had the more complicated filter.

Yep that shorter one is ideal in this case.

Haha I was just trying your suggestion too!

I appreciate the assistance from both of you.

My immediate problem of getting the group.set automation to validate has been solved.

I am still hunting a good solution for dynamically creating/updating groups with the newer group types, but this will get me going for the moment.

Thanks!

I don’t think that this is currently possible because group.set creates a group under the domain group as opposed to the new groups.