Counts the lights on

Yes, this went into affect around 0.115 or so. maybe 0.116.

Also, change the count all lights template:

        value_template: "{{ states.light | list | length }}"

to

        value_template: "{{ states.light | count }}"

Provisions were made to hog less resources.

1 Like

thanks Petro, so it wasn’t me going crazy. Will update to count which is indeed much nicer, this is what you get if you look at older code.

It’s hard to avoid old code here. When in doubt, come to the forums and ask!

1 Like

Long time since you posten this but if you see this, maybe you can have a look at my code. I have simplified it but get “Unavailable” on the sensor. Looks like your but for some reason it isn’t working.

- platform: template
    sensors:
      lamptest_template:
        friendly_name: "Lampor tända"
        value_template: >
          {% set lights = [
            light.arbetsrummet,
            light.bollen,
            ] %}
          {{ lights | selectattr('state','eq','on') | list | count }}

you’re missing states. before each light

1 Like

How embarrassing… But thank you for a super fast reply to this!

If you wish, you can also do it like this:

- platform: template
    sensors:
      lamptest_template:
        friendly_name: "Lampor tända"
        value_template: >
          {% set lights = [
            'light.arbetsrummet',
            'light.bollen',
            ] %}
          {{ expand(lights) | selectattr('state','eq','on') | list | count }}

3 Likes

This is a fantastic thread and I’m trying to implement a similar count for interior motion detectors. The idea is trigger an automation when two sensor trigger in order to reduce false triggers. Unfortunately implementing this value_template, I’m running into errors.

    # Motion Detection Logic
      multiple_motion:
        unit_of_measurement: "on"
        value_template: >
            {% set motion = [
                states.binary_sensor.4_in_1_sensor_home_security_motion_detection,
                states.binary_sensor.4_in_1_sensor_home_security_motion_detection_2,
                states.binary_sensor.4_in_1_sensor_home_security_motion_detection_3,
                states.binary_sensor.4_in_1_sensor_home_security_motion_detection_4
                ] %}
            {{ motion | selectattr('state','eq','on') | list | count }}

Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: expected token ‘,’, got ‘_in_1_sensor_home_security_motion_detection’) for dictionary value @ data[‘sensors’][‘multiple_motion’][‘value_template’]. Got “{% set motion = [\n states.binary_sensor.4_in_1_sensor_home_security_motion_detection,\n states.binary_sensor.4_in_1_sensor_home_security_motion_detection_2,\n states.binary_sensor.4_in_1_sensor_home_security_motion_detection_3,\n states.binary_sensor.4_in_1_sensor_home_security_motion_detection_4\n ] %}\n{{ motion | selectattr(‘state’,‘eq’,‘on’) | list | count }}\n”. (See ?, line ?).

Could someone point out my error?

I had read under the “Templating” doc that perhaps I should be using states(‘binary_sensor.4_in_1_sensor_home_security_motion_detection’) to avoid errors, but when I did do that the errors west away, but the template count did not work and stayed stuck at 0.

You should: it’s because these sensors start with a number, the states syntax thinks you’re looking for [4] then finding the rest of the name. Post the code that you tried that didn’t work…

issue’s based on what troon said. Instead of using a list, use expand with strings.

    # Motion Detection Logic
      multiple_motion:
        unit_of_measurement: "on"
        value_template: >
            {% set motion = expand(
                'binary_sensor.4_in_1_sensor_home_security_motion_detection',
                'binary_sensor.4_in_1_sensor_home_security_motion_detection_2',
                'binary_sensor.4_in_1_sensor_home_security_motion_detection_3',
                'binary_sensor.4_in_1_sensor_home_security_motion_detection_4'
                ) %}
            {{ motion | selectattr('state','eq','on') | list | count }}

Thanks for the quick reply from you and Troon! Absolutely perfect.

That’s an interesting issue that because the sensor’s name starts with a number we need to approach this differently. Never considered it. The sensor is a Z-Wave sensor that auto populated the name. Will keep this in mind next time.

So that I fully understand, let’s say the sensor name did not start with a number, would the other way work without using ‘expand()’ ?

Like the following example? (Note: Not tested):

    # Motion Detection Logic
      multiple_motion:
        unit_of_measurement: "on"
        value_template: >
            {% set motion = [
                states('binary_sensor.motion_kitchen'),
                states('binary_sensor.motion_dining_room'),
                states('binary_sensor.motion_living_room'),
                states('binary_sensor.motion_upstairs_hall')
                ] %}
            {{ motion | selectattr('state','eq','on') | list | count }}

Paste it into the Developer Tools Template editor, and you can find out for yourself :wink:

the example you have there would also work with the numbers but it doesn’t return state objects so the lines after it would not work.

Your previous method doesn’t work because it’s accessing the objects directly, not through a method.

code works with number returns state object
states.sensor.1_something :x: :white_check_mark:
states('sensor.1_something') :white_check_mark: :x:
expand('sensor.1_something') :white_check_mark: :white_check_mark:
3 Likes

Still learning the system, thanks! I also didn’t know there was a Template “tester” under the development tools.

Always something new. Thanks Troon! :sweat_smile:

Did you solved your issue with the automatic light groups of Hue? I also have this “issue”.

I now have this, but that isn’t really pretty.
I made it this way, because I will add more light regular and not rooms, so I don’t have to adjust this template.

# Lights count
  - platform: template
    sensors:
      lightsoncount:
        friendly_name: "Lights on count"
        value_template: >
          {% set total = states.light|selectattr('state','equalto','on')|list|length %}
          {% if is_state('light.woonkamer', 'on') %}
            {% set total = total - 1 %}
          {% endif %}
          {% if is_state('light.tuin', 'on') %}
            {% set total = total - 1 %}
          {% endif %}
          {% if is_state('light.alle_lampen', 'on') %}
            {% set total = total - 1 %}
          {% endif %}
          {% if is_state('light.baby', 'on') %}
            {% set total = total - 1 %}
          {% endif %}
          {% if is_state('light.custom_group_for_lights', 'on') %}
            {% set total = total - 1 %}
          {% endif %}
          {% if is_state('light.garage', 'on') %}
            {% set total = total - 1 %}
          {% endif %}
          {% if is_state('light.hal', 'on') %}
            {% set total = total - 1 %}
          {% endif %}
          {% if is_state('light.keuken', 'on') %}
            {% set total = total - 1 %}
          {% if is_state('light.oprit', 'on') %}
            {% set total = total - 1 %}
          {% endif %}
          {% if is_state('light.slaapkamer_eline', 'on') %}
            {% set total = total - 1 %}
          {% if is_state('light.vrc_1', 'on') %}
            {% set total = total - 1 %}
          {% endif %}
          {% endif %}
          {% endif %}
          {{total}}

You can filter based on the object_id… where entity_id is equal to the domain.object_id.

  - platform: template
    sensors:
      lightsoncount:
        friendly_name: "Lights on count"
        value_template: >
          {% set reject = [ 'woonkamer', 'tuin', 'alle_lampen', 'baby', 'custom_group_for_lights', 'garage', 'hal', 'keuken', 'oprit', 'slaapkamer_eline', 'vrc_1' ] %}
          {{ states.light | rejectattr('object_id', 'in', reject) | selectattr('state','equalto','on')| list | length }}
4 Likes

Thank you Petro!
I’m getting the hang of it.

I now have the template I need!

{{ states.light | selectattr('state', 'eq', 'on') | rejectattr('attributes.is_hue_group') | list | count }}
2 Likes

that also works!

Is there a way to count the amount of lights, decvices, entities and such that are in a area? I have searched all over this forum but cannot seem to find it specific for counting in area.

Of course, I could make groups and count amount in group, but there must be a cleaner way, right?
Hope someone here can help me out!

This will return a list

{%- set search_state = 'on' %}
{%- set search_area = 'Living Room' %}
{%- set ns = namespace(lights=[]) %}
{%- for light in states.light | selectattr('state','eq', search_state) if area_name(light.entity_id) == search_area %}
  {%- set ns.lights = ns.lights + [ light.entity_id ] %}
{%- endfor %}
{{ ns.lights }}
3 Likes