Seeing that group.all_* was removed from states, here are some useful canned items that will get you the same result as before.
Using Services to perform an action on all entities
Using all
inside the entity_id field will allow you to use turn everything on or off inside a domain. You just need to pair it with the correct command. This should work for pretty much every service that has entity_id.
Replaces
# Do not use, this is out of date.
- service: homeassistant.turn_on
entity_id: group.all_lights
e.g.
Turn On All Lights
- service: light.turn_on
entity_id: all
Turn Off All Lights
- service: light.turn_off
entity_id: all
Useful Conditions
Are all entities inside a domain equal to a state?
Simply change the domain inside the quotes, and the state inside the quotes.
# All lights on?
- condition: template
value_template: >
{% set domain = 'light' %}
{% set state = 'on' %}
{{ states[domain] | count == states[domain] | selectattr('state','eq', state) | list | count }}
# All lights off?
- condition: template
value_template: >
{% set domain = 'light' %}
{% set state = 'off' %}
{{ states[domain] | count == states[domain] | selectattr('state','eq', state) | list | count }}
# All locks locked?
- condition: template
value_template: >
{% set domain = 'lock' %}
{% set state = 'locked' %}
{{ states[domain] | count == states[domain] | selectattr('state','eq', state) | list | count }}
Is any entity inside the domain equal to a state?
# Is any light on?
- condition: template
value_template: >
{% set domain = 'light' %}
{% set state = 'on' %}
{{ states[domain] | selectattr('state','eq', state) | list | count > 0 }}
# Is any light off?
- condition: template
value_template: >
{% set domain = 'light' %}
{% set state = 'off' %}
{{ states[domain] | selectattr('state','eq', state) | list | count > 0 }}
# Is anything locked?
- condition: template
value_template: >
{% set domain = 'lock' %}
{% set state = 'locked' %}
{{ states[domain] | selectattr('state','eq', state) | list | count > 0 }}
Useful Templates
Template to get a list of entities
This template returns a list of entity_ids that can be used for conditions. The list is useful for performing operations like finding out if an entity is on.
{% set domain = 'light' %}
{% set entities = states[domain] | map(attribute='entity_id') | list %}
Template to count all entities that are a state
This template counts all entities that have a specific state. e.g. count all lights that are on.
{% set domain = 'light' %}
{% set state = 'on' %}
{% set count = states[domain] | selectattr('state','eq', state) | map(attribute='entity_id') | list | count %}
Template comma separates string of entity_ids
This template returns a list of all lights that are ‘off’ and separates the entity_id’s with a comma. Typically used for the entity_id field for a <domain>.turn_on/off
service.
{% set domain = 'light' %}
{% set state = 'off' %}
{{ states[domain] | selectattr('state','eq', state) | map(attribute='entity_id') | list | join(', ') }}
Template to combine 2 domains into a SUPER group.all
Thanks to @123
{% set state = 'on' %}
{{ expand(states.light, states.switch) | selectattr('state','eq', state) | map(attribute='entity_id') | list | join(', ') }}
Appdaemon Automation
If you want these groups back and you’re keen on appdaemon. Use this app. (Provided in HACS).
Python Script Startup Solution
Thanks to @VDRainer
Use a python script to get your group.all_*.
- Create a folder named
python_scripts
inside your config folder. - Create the python file below
create_all_group.py
and place it into the<config>/python_scripts/
directory.
python_scripts/create_all_group.py
domain = data.get('domain', '')
group = data.get('group', '')
if not isinstance(domain, str) or not isinstance(group, str) or not domain or not group:
logger.warning("bad domain or group! Not executing.")
else:
service_data = {"object_id": group, "entities": hass.states.entity_ids(domain)}
hass.services.call("group", "set", service_data, False)
- create an automation that fires at startup and creates the group using the script.
Automation
automation:
- alias: Create All Lights Group on Startup
trigger:
platform: homeassistant
event: start
action:
- service: python_script.create_all_group
data:
domain: light
group: all_lights