Counting Bulbs and Sockets

I have this code that counts all my smart bulbs and show number of turned On Bulbs im my home, which works perfectly. It’s only counts bulbs, but i also have couple of smart sockets with normal bulbs
switch.lamp_socket_1
switch.lamp_socket_2
switch.lamp_socket_3
that i would like to count them togather with the bulbs.
This is My Code:

# lights of count
  - platform: template
    sensors:
      lights_of_count:
        friendly_name: 'lights of count 1 floor'
        unique_id: lights_of_count
        value_template: > 
          {%- set search_state = 'on' %}
          {%- set search_areas = ['My Home Area'] %}
          {%- set ns = namespace(lights=[]) %}
          {%- for light in states.light | selectattr('state','eq', search_state) %}
            {%- for area in search_areas %}
              {% if area_name(light.entity_id) == area and "1 floor" not in state_attr(light.entity_id, "friendly_name") %}
                {%- set ns.lights = ns.lights + [ light.entity_id ] %}
              {% endif%}
              {%- endfor %}
          {%- endfor %}
          {{ ns.lights| list | length }}

i’m kinda new to HA, any idea how i can achieve that.

# lights of count
  - platform: template
    sensors:
      lights_of_count:
        friendly_name: 'lights of count 1 floor'
        unique_id: lights_of_count
        value_template: >
          {{ expand(area_entities('My Home Area'))
            | selectattr('domain', 'in', ['light', 'switch'])
            | selectattr('state', 'eq', 'on')
            | rejectattr('name', 'search', '1 floor')
            | list | count
          }}
2 Likes

Thanks for replay, I’m getting this error

# lights of count
  - platform: template
    sensors:
      lights_of_count:
        friendly_name: 'lights of count 1 floor'
        unique_id: lights_of_count
        value_template: >
          0
This template does not listen for any events and will not update automatically.

Ouch! It works fine in the Template Editor but it’s true that, when used in a Template Sensor, it doesn’t contain anything that Home Assistant can monitor for state-changes.

Looks like it’ll need to be based on states.light and states.switch in order to monitor their state-changes.

1 Like

I’m testing it in the Template Editor, it always reports 0, maybe the domain is not well specified!

# lights of count
  - platform: template
    sensors:
      lights_of_count:
        friendly_name: 'lights of count 1 floor'
        unique_id: lights_of_count
        value_template: >
          {{ expand(states.light, states.switch)
            | selectattr('state', 'eq', 'on')
            | rejectattr('name', 'search', '1 floor')
            | map(attribute='entity_id')
            | select('in', area_entities('My Home Area'))
            | list | count
          }}

Both templates I’ve posted so far correctly report the number of lights and switches that are on in my “Kitchen” area. If this latest version still reports a count of zero for you, then I need to know if you are modifying anything in the template to adapt it to your specific needs (such as changing the area’s name or adding more areas to it).

1 Like

Also always reports 0 but now

# lights of count
  - platform: template
    sensors:
      lights_of_count:
        friendly_name: 'lights of count 1 floor'
        unique_id: lights_of_count
        value_template: >
          0
This template listens for the following state changed events:

Domain: light
Domain: switch
Entity: light.hombli_smart_bulb
Entity: light.hombli_smart_bulb_2
Entity: light.hombli_smart_bulb_2_2
Entity: light.hombli_smart_bulb_3
Entity: light.smart_bulb_ha2_light
Entity: light.smart_bulb_light
Entity: light.smart_led_strip
Entity: light.smart_led_strip_2
.
.
.
.
.
etc

Need to know if you’re altering it in any way because, as mentioned, it correctly reports the count for an area in my home.

If you are not altering it, as an experiment, remove the line containing select() and see what it reports.

You can optionally remove the final | count to see the list of entity_ids it produces (assuming it finds any entity that’s on).

1 Like

sorry my bad, i had a typo in the Area name :sweat_smile:
now it’s working correctly… thanks a lot for your time

1 Like

Glad to hear it ultimately worked.

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic is resolved. This helps other users find answers to similar questions. For more information, refer to guideline 21 in the FAQ.

1 Like

done :white_heart: :white_heart: :white_heart: