PSA: Turn on/off all lights in Home Assistant 0.104+ (group.all_* changes)

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_*.

  1. Create a folder named python_scripts inside your config folder.
  2. 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)
  1. 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
108 Likes

Thoughts on conditions?

condition: state
entity_id: group.all_lights
state: 'off' 

Becomes what?

I use that one in combination with a time condition to work out when everyone who is home has gone to bed. I’ve had to resort to listing all the lights for now but it’s a bit messy.

A hot mess.

condition: template
value_template: >
  {{ states.light | count == states.light | selectattr('state','eq','off') | list | count }}
3 Likes

It’s a truck load prettier than what I typed out earlier! Cheers :sunglasses:

Yeah, nobody turns off all_lights anyways, right? :crazy_face:

hello, does anyone know how to replace the group.all_devices as trigger and condition in an automation?

  trigger:
  - entity_id: group.all_devices
    platform: state
    to: not_home


  - condition: state
    entity_id: group.all_devices
    state: home

The trigger is tricky.

Probably the easiest without listing all the devices:

trigger:
- platform: event
  event_type: state_changed
condition:
- condition: template
  value_template: "{{ trigger.event.entity_id.split('.')[0] == 'device_tracker' }}"

The condition is also tricky, but easier

- condition: template
  value_template: "{{ states.device_tracker | count == states.device_tracker | selectattr('state','eq','home') | list | count }}"
1 Like

thanks a lot,ill try it and change all them

the problem is that with group.all_devices when one of the device_tracker entities has the value “home”, the status of the group also changes to “home”, the condition does not work, the trigger has not yet been able to test it.
thanks

Ah ok, use this

- condition: template
  value_template: "{{ states.device_tracker | selectattr('state','eq','home') | list | count > 0 }}"

Hey Google… turn off all the lights…

5 Likes

TOP,thanks a lot again,thats its working,
I just need to try the trigger

Maybe is a silly question, but if I create a group with all device_tracker I have, wouldn’t it work just like the old group.all_devices?
EDIT: thats working,i think is the best fix for me
:grinning:

I though I understood how piping worked in Home Assistant but the use of the piping here really confuses me. Anybody cares to explain to me what’s going on?

The way I see it, states[domain] returns a list of states the entities of type domains are and piping that to count will result in the number of devices returned. So what piping that number to an equality test with the list again do?

Bit disappointed with this update.

How can I now easily check if all door are locked. Now i need to maintain the group myself. A pitty they did not foresee an option to make the groups available again.

I have a Node-red flow to turn off ALL lights except a few (using a function node), guess I know have to iterate over all the entities …

1 Like

No you don’t. I posted a solution here:

Change domain to ‘lock’ and state to ‘locked’.

- condition: template
  value_template: >
    {% set domain = 'lock' %}
    {% set state = 'locked' %}
    {{ states[domain] | count == states[domain] | selectattr('state','eq', state) | list | count }}
2 Likes

Again, no you don’t. Node red calls a service. Instead of using entity_id: ‘group.all_lights’, use entity_id: ‘all’.

I get that, but previously I iterated over the entities in the group. Using all only works when trying to turn off the lights, not for getting the list of entities.

Luckily I’ve put all that code in a subflow, so it’s a change on 1 place, but still. This change kinda sucks.

dude, i posted that solution as well…

Use this as a list of entity_id’s to return to entity_id’s

{{ states.switch | map(attribute='entity_id') | list | join(', ') }}

Use this to find all items in a domain

{{ states.switch | map(attribute='entity_id') | list }}

EDIT: In the words of southpark “Rabble rabble rabble rabble”

What about some tiny python_script to get your group.all_* back?

automation:
  - alias: HA Start automation
    trigger:
      platform: homeassistant
      event: start
    action:
      - service: python_script.create_all_group
        data:
          domain: light
          group: all_lights

python_scripts/create_all_group.py

domain = data.get('domain')
group = data.get('group')

service_data = {"object_id": group, "entities": hass.states.entity_ids(domain)}
hass.services.call("group", "set", service_data, False)

I have no clue if this is safe, but it works for me. :sunglasses: It is!

EDIT: optimized create_all_group.py

5 Likes