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

Hi to all,
I need a sensor to check “all light off”.
I created these two, but they don’t work.
Someone can help me?
Thanks too much.

- platform: template
  sensors:
    luci_spente:
      value_template:  >
        {% set domain = 'light' %}
        {% set state = 'on' %}
        {{states[domain] | selectattr('state','eq', state) | list | count == 0}}

- platform: template
  sensors:
    luci_spente_2:
      value_template:  >
        {% set domain = 'light' %}
        {% set state = 'off' %}
        {{ states[domain] | count == states[domain] | selectattr('state','eq', state) | list | count }}

that template will work but you need to list all lights inside the entity_id field so the system knows when to update the sensor. See entity_id field in the docs. And here is the explanation why.

Thanks @petro for you fast response.
Sorry but I don’t understand.
I tried also with this but nothing, so I suppose that something wrong:

{% set domain = 'light' %}
{% set state = 'on' %}
{% set count = states[domain] | selectattr('state','eq', state) | map(attribute='entity_id') | list | count == 0%}

I have ti put every single ID? In this case, where I have to put?
Please, can you give me an example with two lights (ie: light_1, light_2).
Thanks for your patience

Did you read the documentation I linked? There are 2 links there. You list them in the entity_id field for the template.

entity_id:
- a
- b

Ok @petro, sorry.
I clicked only second link.
Now it works.

So I understand that for every single light that I add, I have to add manualy to the sensor.
It’s right?
There isn’t a way to do it automatically? (ie: like the olda group.all_lights)
Thansk for your support

Unfortunately no, not when you use it in a template sensor. This is a restriction of the template sensor. It requires a list of entities to detect changes on. If you provide it a group, it will only use the group. Not the entities inside the group.

Ok @petro , Thank you again :+1:

maybe do it like this:

  - alias: 'State changed Light'
    id: 'State changed Light'
    trigger:
      platform: event
      event_type: state_changed
    condition:
      condition: template
      value_template: >
        {{trigger.event.data.entity_id in state_attr('group.all_lights_only','entity_id')}}
    action:
      service: homeassistant.update_entity
      entity_id: sensor.lights_on

Would the OP examples result in a turn_off command to all lights or would HA automatically ignore those that are already switched off?

Yes, the entity list template will work. Makes a list of entitles that are on

{% set domain = 'light' %}
{% set state = 'on' %}
{{ states[domain] | selectattr('state','eq', state) | map(attribute='entity_id') | list | join(', ') }}
1 Like

Is it possible to find one or more lights in the group are turning on to turn OFF it/them?

I’ve been following this post carefully and still have a problem. I would like to create a group with several entities and not the entire domain. I have five entities from the light domain from which I want to form a group when they are switched off. That should be mapped in a script. I use the following script for the domain light which also works. But how do I get the entities built in without the complete domain?

  • light_1
  • light_2
  • light_3
  • light_4
  • light_5
alias: Group.set
sequence:
  - service: group.set
    data_template:
      object_id: test_lights
      entities: >-
        {% set domain = 'light' %} {% set state = 'off' %} {{ states[domain] |
        selectattr('state','eq', state) | map(attribute='entity_id') | list |
        join(', ') }}
mode: single

what do you mean by “without the complete domain”? can you explain what you want as awhole picture?

I want to create a group with some (five) entities with the state off. By using the above example I receive the group test_lights with the complete domain light.

So make a group that contains your 5 entities. Then use expand on that group with your state filter.

{{ expand('group.my_5_lights') | selectattr('state','eq', 'off') | map(attribute='entity_id') | list | join(', ') }}
1 Like

Yes, many thanks for the fast solution. This is how it works perfectly. :+1:

My goal is to switch ON the light which has the status OFF. After three minutes these lights should be switched OFF with the help of a script and a delay or timer.
Light that was switched ON beforehand remains unaffected by the delay / timer. This works very well with this script. In this script I create a temporary group.test_lights which is then operated via the delay / timer. Now I asked myself whether this could be simplified and the temporary group group.test_lights could be dispensed with? Maybe it could be easier?

alias: Group.set
sequence:
  - service: group.set
    data_template:
      object_id: test_lights
      entities: >-
        {{ expand('group.living') | selectattr('state','eq','off') |
        map(attribute='entity_id') | list | join(', ') }}
  - service: light.turn_on
    data: {}
    entity_id: group.test_lights
  - delay: '00:03:00'
  - service: light.turn_off
    data: {}
    entity_id: group.test_lights
mode: single

You can try using the new variables feature introduced in version 0.115.

Here’s an untested example:

alias: example 1
variables:
  lights_off: >
    {{ expand('group.living') | selectattr('state','eq','off')
       | map(attribute='entity_id') | list | join(', ') }}
sequence:
  - service: light.turn_on
    data:
      entity_id: "{{ lights_off }}"
  - delay: '00:03:00'
  - service: light.turn_off
    data:
      entity_id: "{{ lights_off }}"
mode: single

Very well! It works with using variables.
Now there is a little problem. If all lights of the group are ON, there is an error message:

- Group.set: Error executing script. Invalid data for call_service at pos 1: Entity ID is an invalid entity id for dictionary value @ data['entities']
- not a valid value for dictionary value @ data['entity_id']

If at least one lamp of the group is switched OFF, it works properly.

alias: example 1
variables:
  lights_off: >
    {% set x = expand('group.living') | selectattr('state','eq','off')
       | map(attribute='entity_id') | list %}
    {{ x | join(', ') if x | length > 0 else 'none' }}
sequence:
  - service: light.turn_on
    data:
      entity_id: "{{ lights_off }}"
  - delay: '00:03:00'
  - service: light.turn_off
    data:
      entity_id: "{{ lights_off }}"
mode: single

When all lights were already on, the template produced an empty string which is unacceptable for entity_id. The revised version produces none which is understood to mean no entities. The services will simply ‘do nothing’.