Error Trying To group.set Entities

Hi, I’m trying to use a state trigger to remove lights from a group when they are turned off. The action and hopefully useful parts of the error message are included below. Any pointers would be appreciated. I tried several methods, which are included as comments. I also tried three different structures, since the problem seems likely related to the basic template structure, but I won’t include all of those because that would probably just be confusing.

service_template: >
  {% if expand('states.group.active_lights') | map(attribute='entity_id') | list | count == 1 %}
     {% set last=1 %}
     group.remove
   {% else %}
     {% set last=0 %}
     group.set
   {% endif %}
   object_id: active_lights
   data_template:
     {% if last == 0 %}
       entities: 'light.kitchen_sink'
       {# entities: '{{ expand(states.group.active_lights) | map(attribute='entity_id') | reject('==', 'trigger.entity_id') | join(', ') }}' #}
       {# entities: '{{ expand(states.group.active_lights) | map(attribute='entity_id') | reject('==', 'trigger.entity_id') | list }}' #}
       {# entities: {{ expand(states.group.active_lights) | map(attribute='entity_id') | reject('==', 'trigger.entity_id') | list }} #}
     {% endif %}

Error bits:

Unexpected error for call_service at pos 1: Template rendered invalid service: group.set object_id: active_lights data_template: entities: 'light.kitchen_sink'
...
entities: 'light.kitchen_sink' does not match format <domain>.<name>

The purpose of a template is to compute a value that will be assigned to a key.

key: value

For example:

service: "light.turn{{trigger.to_state.state}}"

In addition, the template for one key is completely independent of the template for another key.

Your example doesn’t follow the rules. The template attempts to do more than merely compute a value for a key, it also attempts to contain keys like object_id and data_template.

Post the entire automation and I will help you correct it.

I see. Ok, here is the full automation but what I sent before was pretty much everything. I just hard-coded the trigger entities for now. Was planning on coming back to that.

alias: Lights off (2)
description: ''
trigger:
  - platform: state
    entity_id:
      - light.family_windowwall_left
      - light.family_windowwall_center
      - light.family_windowwall_right
      - light.family_workbench_left
      - light.family_workbench_right
      - light.family_wedge
      - light.family_desk
      - light.family_arch
      - light.kitchen_open_hall
      - light.kitchen_open_familyroom
      - light.kitchen_open_windowside
      - light.kitchen_dishwasher
      - light.kitchen_sink
      - light.kitchen_microwave
      - light.kitchen_stove
      - light.kitchen_cove
      - light.kitchen_fridgefreezer
    to: 'off'
condition: null
action:
  - service_template: >
      {% if expand('states.group.active_lights') | map(attribute='entity_id') | list | count == 1 %}
        {% set last=1 %}
        group.remove
      {% else %}
        {% set last=0 %}
        group.set
      {% endif %}
        object_id: active_lights
        data_template:
          {% if last == 0 %}
            entities: 'light.kitchen_sink'
            {# entities: '{{ expand(states.group.active_lights) | map(attribute='entity_id') | reject('==', 'trigger.entity_id') | join(', ') }}' #}
            {# entities: '{{ expand(states.group.active_lights) | map(attribute='entity_id') | reject('==', 'trigger.entity_id') | list }}' #}
            {# entities: {{ expand(states.group.active_lights) | map(attribute='entity_id') | reject('==', 'trigger.entity_id') | list }} #}
          {% endif %}
mode: queued
max: 30

First question: Why?

Given a group of lights, it’s easy to report which ones are on or off without removing any of them.

I want to manually adjust the lights (e.g. brightness) that are on without turning on the ones that are off. I added a line to my “Lights on” automation that adds them to that group (active_lights) when they’re turned on.

  action:
  - service: light.turn_on
    data:
      entity_id: >
        {{ expand ('group.my_lights') | selectattr('state', 'eq', 'on')
            | map(attribute='entity_id') | list | join(', ') }}
      brightness: 125

group.my_lights contains all the lights you wish to control (effectively, it’s all the lights listed in your automation’s State Trigger).

No, I mean manually with the GUI light controls. Having a statically generated group would also make any other automations for light control more efficient. Do you think this is a bad idea?

Do you believe there’s a significant difference in processing time between evaluating the template I posted and removing members of a group?

I don’t know, but it doesn’t really matter. I would like to write this in the best way possible just on principle. More significantly, the GUI light controls only seem to operate on groups. Is there another way to configure them?

Perhaps you should explain your goal because the discussion has shifted to “GUI light controls” and that’s not mentioned in the first post.

Ok. I am trying to write a state trigger to remove lights from a group when they are turned off. Based on what you said initially, it seems like the problem with my automation is that I don’t understand the service_template action, or possibly the data_template key. I suspect the service_template, because I have seen other automations that use data_template like this (although it’s possible they didn’t work either).

If you can correct this automation, it would be very helpful for me to see that as an example to understand the logic, but I don’t mind figuring it out myself if you feel this is a waste of time. Perhaps you can show me where there is a complete list of the different action types? I haven’t been able to find any list or descriptions of these.

That part was clear but for what purpose? Give me an example of how you intend to use this group (containing only lights that are currently on).

Unless there’s some unique advantage of having such a group on hand, my point is that it’s unnecessary because it’s quick and easy to compute it with a template.

I want the lights in a group so I can adjust their settings manually with the GUI interface. It also just seems like a more elegant solution to me, since there is a good chance I will end up with several automations using a template like you proposed, but that is more a matter of my aesthetic preference than a practical necessity. I really don’t know how else to explain this. You do know what GUI interface I’m referring to, right? Sorry if I’m not explaining the reasons clearly enough but that is the kind of automation I want to write, even if it is unnecessary. I want to understand how this can be done.

Here’s another method I tried, based on another forum thread. It doesn’t seem to work though. For some reason, lights switch off (only in HA) about 1-2 seconds after I turn them on. Doesn’t make much sense to me so there must be something I’m missing here too.

action:
  - choose:
    - conditions:
      - condition: template
        value_template: "{{ expand('states.group.active_lights') | map(attribute='entity_id') | list | count == 1 }}"
      sequence:
        - service: group.remove
          data:
            object_id: active_lights
    default:
      - service: group.set
        data:
          object_id: active_lights
          entities: '{{ expand(states.group.active_lights) | map(attribute="entity_id") | reject("==", "trigger.entity_id") | list }}'