Skip to content

Instantly share code, notes, and snippets.

@asaf400
Forked from mwolter805/create_group.yaml
Last active September 15, 2025 18:58
Show Gist options
  • Select an option

  • Save asaf400/940f793970b8f8d99c04a7d32c8f0bcc to your computer and use it in GitHub Desktop.

Select an option

Save asaf400/940f793970b8f8d99c04a7d32c8f0bcc to your computer and use it in GitHub Desktop.
Home Assistant Blueprint: Create a group with either dynamic or with static members.
blueprint:
name: Create Group
description: |
# Create Group Blueprint
This blueprint creates a group with either static or dynamic members.
(extended with ability to filter by Area)
domain: automation
source_url: https://gist.github.com/mwolter805/965fb5c3b4d6bf8e9aeba8acd82fd224
input:
group_name:
name: "Name to assign to the group"
description: "Example: All Lights"
default: Group Name
group_object_id:
name: "object_id to assign to the group"
description: "Example: all_lights"
default: group_id
search_domain:
name: "Dynamic Domain List"
description: "Domains to search. Format: List (-) of strings."
default: []
selector:
object: {}
search_includes:
name: "Dynamic Include List"
description: "Entity object ID's to include. Format: List (-) of strings."
default: []
selector:
object: {}
search_excludes:
name: "Dynamic Exclude List"
description: "Entity object ID's to exclude. Format: List (-) of strings."
default: []
selector:
object: {}
search_areas:
name: "Dynamic Area List"
description: "Areas to include. Format: List (-) of area IDs or area names."
default: []
selector:
area: {}
entities_to_add:
name: "Static Entities To Add"
description: "Select the entities to add to the group."
default:
entity_id: []
selector:
target:
entity: {}
entities_to_remove:
name: "Static Entities To Remove"
description: "Select the entities to remove from the group."
default:
entity_id: []
selector:
target:
entity: {}
mode: single
max_exceeded: silent
trigger:
- platform: homeassistant
event: start
- platform: event
event_type: call_service
event_data:
domain: group
service: reload
variables:
input_group_name: !input group_name
input_group_group_object_id: !input group_object_id
domains: !input search_domain
includes: !input search_includes
excludes: !input search_excludes
areas: !input search_areas
add: !input entities_to_add
rem: !input entities_to_remove
action:
- service: group.set
data_template:
name: "{{ input_group_name }}"
object_id: "{{ input_group_group_object_id }}"
entities: >
{# --- Start with candidate entities --- #}
{%- set entities = states
| selectattr('domain','in', domains) | list if domains
else states | list if (includes or excludes or areas) %}
{%- set ns = namespace(include=[], exclude=[], area_filtered=[]) %}
{# --- Filter includes --- #}
{%- for item in includes %}
{%- set ns.include = ns.include + entities
| selectattr('object_id', 'search', item) | list %}
{%- endfor %}
{# --- Filter excludes --- #}
{%- for item in excludes %}
{%- set ns.exclude = ns.exclude + entities
| selectattr('object_id', 'search', item) | list %}
{%- endfor %}
{# --- Filter by area if requested --- #}
{%- if areas %}
{%- for e in entities %}
{%- set area = area_id(e.entity_id) %}
{%- if area in areas %}
{%- set ns.area_filtered = ns.area_filtered + [e] %}
{%- endif %}
{%- endfor %}
{%- set entities = ns.area_filtered %}
{%- endif %}
{# --- Apply include/exclude --- #}
{%- set entities = entities
| select('in', ns.include if includes else entities)
| reject('in', ns.exclude)
| map(attribute="entity_id") | list %}
{# --- Add/Remove static entities --- #}
{%- set entities = (entities + add.entity_id | list)
| reject('in', rem.entity_id) | list %}
{{ entities | unique | sort }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment