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

Sorry for the basic question, but could I please get some help with the installation?
I’m just not sure what ...then add the configuration to enable the hacs module. means.
I’ve downloaded and copied the Python script into my custom_components folder and that’s pretty much as far as I got.

Thanks

Hacs isn’t required for the python script. Follow the main post under the python script section

Brilliant, thanks.
I followed the instractions and got the following error:

2021-04-25 23:10:57 ERROR (MainThread) [homeassistant.components.automation.create_all_lights_group_on_startup] Create All Lights Group on Startup: Error executing script. Service not found for call_service at pos 1: Unable to find service python_scripts.create_all_group
2021-04-25 23:10:57 ERROR (MainThread) [homeassistant.components.automation.create_all_lights_group_on_startup] Error while executing automation automation.create_all_lights_group_on_startup: Unable to find service python_scripts.create_all_group

I’ve tried:

  1. Changing the service tag to - service: config.python_scripts.create_all_group
  2. Adding an __init__.py file to the python_scripts folder

None of these worked

did you restart home assistant after adding the script?

Of course :slight_smile:

Do you have python_script: in your configuration.yaml?

Nope. What keys and values does it take?

none

1 Like

Problem is sorted. Many thanks :slight_smile:

I have this code for getting the count of lights that are on. And the result is written in a sentence:

{% set domain = 'light' %}
{% set state = 'on' %}
{% set count = states[domain] | selectattr('state','eq', state) | map(attribute='entity_id') | list | count %}
There {{ 'are' if count > 1 else 'is' }} {{ count }} {{ 'lights' if count > 1 else 'light' }} on.

How can I change this for using with windows / doors binary_sensors? I have group.doors_windows with all entities. Thanks

{% set count = expand('group.doors_windows') | selectattr('state','eq', 'on') | list | count %}
1 Like

thanks, for your help :slight_smile:

On more thing with the light counting :slight_smile: I have created few groups for (Ikea Tradfri) lights. Is it possible to count not individual lights but groups for grouped lights?
Currently lights in groups are numbered as this:

  • light.kitchen_1
  • light.kitchen_2
  • light.kitchen_3

I have max 5 lights in groups. If it helps I can rename these individual lights so I can filter them not by number at end but something like light.kitchen_1_grouped.

If you’re asking if you count the number of groups in your system then the answer is yes.

{{ states.group | list | count }}

If it’s not that then I don’t understand your question.

Ok I will try to explain myself better:

currently that light count above is countig all light entities. If kitchen lights and living room light are on, it will count 5 lights (4 individual lights + 1 group). Is it possible to count only group light (when applicable) and not the individual grouped lights? So that in mentioned example only 2 lights are counted (1 group + 1 individual light = living room).

  • light.kitchen_1
  • light.kitchen_2
  • light.kitchen_3
  • light.kitchen (light group)
  • light.bedroom_1
  • light.bedroom_2
  • light.bedroom_3
  • light.bedroom (light group)
  • light.livingroom (individual light)
  • light.tv_lamp (individual light)

Correct, because a Light Group entity belongs to the light domain.

Probably.

First thing you will need is to distinguish a Light Group entity from a standard Light entity. A Light Group entity has an attribute called entity_id containing a list of its members (a standard Light entity doesn’t have this attribute). You can test for the presence of the entity_id attribute to determine if the entity is a Light Group. Here’s a test that will include Light Group entities and exclude standard Light entities:

selectattr('attributes.entity_id', 'defined')

For example, this will count all the Light Group entities you currently have:

{{ states.light | selectattr('attributes.entity_id', 'defined') | list | count }}

I leave it to you to figure out how to use that in a template to meet your stated requirement.

I got the part to count the group lights that are on.

{% set count = states | selectattr(‚attributes.entity_id‘, ‚defined‘)
                      | selectattr(„domain“, ‚eq‘, ‚light‘)
                      | selectattr(‚state‘, ‚eq‘, ‚on‘)
                      | map(attribute=‚entity_id‘)
                      | list
                      | length %}
{{ count }}

But I can’t figure out, how can I count the lights that don’t have attributes.entity_id defined. Or am I going the wrong way?

Update: After posting I’ve got an idea, how to do this other way. I’ve added custom attribute to lights that are grouped and then it’s easy calculation (all lights - grouped lights).

{% set count = states   | selectattr('attributes.grouped', 'eq', 'on')
                        | selectattr('domain', 'eq', 'light')
                        | selectattr('state', 'eq', 'on')
                        | map(attribute='entity_id')
                        | list
                        | count %}

I went through the full tread, just to make sure I understand… In order to get a constant update, we need to add all relevant entity_ids. Correct ?
If so, the code should something like

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

correct ?

no, the entity_ids is deprecated. The template is smart enough to not need it.

1 Like

@petro
Hey. Hello. Is there any way can auto create the group that contain certain string?

light.kitchen_1

light.kitchen_2

light.kitchen_3

light.bedroom_1

light.bedroom_2

Then auto Create light.kitchen_group or group.kitchen_group include(light.kitchen_1+ light.kitchen_2+ light.kitchen_3)

Thanks in advance