Is it possible to add or remove entities from a group with a script or automation?

Currently I’m using the Alexa Media Player component to send notifications to a group of Echo devices.

All the the Echo devices are in a group, and I can simply send a notification to group.echo_talk and this is great if I want to message the same devices each and every time.

What I’d like is an input_boolean or switch for each Echo device, which will add or remove it from the group, as I don’t want all messages going to all devices at all times, is this possible?

Not sure why this doesn’t seem to be documented, but go to the Services page and you’ll find the group.set service.

1 Like

Here is a simple example script creating a group of every light. You cannot remove items from an existing group… but you CAN overwrite the entire group! :slight_smile:

############################################
## Creates group.every_light
############################################
create_every_light_group:
  sequence:
    - service: group.set
      data_template:
        object_id: "every_light"
        entities: >
          {{ states.light |  map(attribute='entity_id') | join(',') }}
1 Like

Thanks, I did check the docs for the docs

But it does not mention the group.set service at all.

I’ll look into making some sort of automation that re-creates the group whenever the switches are flipped.

Here’s how I do it. First I create a group with all lights, minus those on the “ignore list”, that I call group.actionable_lights.

create_actionable_lights_group:
  sequence:
    - service: group.set
      data_template:
        object_id: "actionable_lights"
        entities: >
          {%- set ignore_list = ['light.garage_entry','light.kitchen_cabinet','light.network_closet','light.upstairs_bathroom_nightlight','light.entertainment_area_1','light.dining1','light.dining2','light.dining3','light.dining4','light.dining5','light.dining6','light.dining7','light.dining8','light.dining9','light.xiaomi_hub_light'] -%}
          {{ states.light | rejectattr('entity_id','in',ignore_list) | map(attribute='entity_id') | join(',') }}

Then I query group.actionable_lights for lights that are on to create group.lights_on.

lights_on_update:
  sequence:
    # Create both a group and a variable list.
    - service: group.set
      data_template:
        object_id: "lights_on"
        entities: >
          {%- set actionable_lights = 'group.actionable_lights' -%}
          {%- set on_states = ['on'] -%}
          {%- set entities = states | selectattr('entity_id','in', state_attr(actionable_lights, 'entity_id')) | selectattr('state','in',on_states) | map(attribute='entity_id') | join(', ') -%}
          {%- if "light." not in entities -%}
            {{ "light.none" }}
          {%- else -%}
            {{ entities }}
          {%- endif -%}

Thanks for the example, it looks like jinja2 allows for quite a bit of control while creating groups.

Right now I only have 5 echo devices but I may be more in future, and it will be handy to turn off certain echo devices at certain times. :slight_smile:

1 Like