Help with a template to count all the lights on

Hello Everyone,

Thank you in advance to everyone who keeps on helping others here.

Currently, i have two types of templates:

Template-1: (This counts all the lights that are in on state in my home)

      friendly_name: Lights on counter
      value_template: >-
        {{ states.light
              |rejectattr('attributes.entity_id', 'defined')
              |selectattr('state', 'eq', 'on')
      icon_template: mdi:lightbulb-group

Template-2: (This counts only the lights inside the template)

  - sensor:
      - name: "lights on"
        unique_id: lights_on
        icon: mdi:lightbulb-on
        state: >
          {% set lights = [
              ] %}
          {{ lights | select('eq', 'on') | list | count }}

Can someone please help me create a template where I can get a number for all the lights that are β€˜on’ but exclude a few light entities?

Moved. Please read the topic descriptions. This is not a community guide.

  - sensor:
      - name: Lights on counter
        state: >-
          {% set exclusions = ['light.office_1', 'light.office_2', 'light.office_3'] %}
          {{ states.light
            | rejectattr('attributes.entity_id', 'defined')
            | rejectattr('entity_id', 'in', exclusions)
            | selectattr('state', 'eq', 'on') | list | count }}
        icon: mdi:lightbulb-group

Thank you so much, this worked perfectly

1 Like