Is there a mehtod to filter sensor on a template?

i wuold like to insert and count all domain ligth and all switch.
it is possible without inserting all names separately?
thanks


  sensors: 
    number_of_lights_on:
      friendly_name: Luci Accese
      unit_of_measurement: 'on'
      value_template: >
        {% set lights = [
          states.switch.campbell,
          states.switch.luce_bagno,
          states.switch.luce_bagno_sotto,
          states.switch.luce_ballatoio,
          states.switch.luce_cabina_armadio,
          states.switch.luce_camino,
          states.switch.luce_consolle,
          states.switch.luce_corridoio,
          states.switch.luce_lavanderia,
          states.light.campari,
          states.light.luce_dispensa,
          ] %}
        {{ lights | selectattr('state','eq','on') | list | count }}

{{ states.light | selectattr('state','eq','on') | list | count }}

can you give me complete code with light and switch?

sensors: 
    number_of_lights_on:
      friendly_name: Luci Accese
      unit_of_measurement: 'on'
      value_template: >
        {% set lights_on = states.light | selectattr('state','eq','on') | list | count %}
        {% set switches_on = states.switch | selectattr('state','eq','on') | list | count %}
        {{ lights_on | int(0) + switches_on | int(0) }}

but be aware that this will give you every switch that is on regardless of if it’s connected to a light or not.

can I exclude some?

not easily unless you have a VERY strict naming convention.

like:

      exclude:
         - entity_id: switch.roon_rock
         - entity_id: switch.presa_lavatrice
  sensors: 
    number_of_lights_on:
      friendly_name: Luci Accese
      unit_of_measurement: 'on'
      value_template: >
        {{ expand(states.light, states.switch)
          | selectattr('state', 'eq', 'on')
          | rejectattr('entity_id', 'in', ['switch.roon_rock', 'switch.presa_lavatrice'])
          | list | count }}

thank you… very very much

Be advised that if you have a Light Group, or a Switch Group, (or a Philips Hue light group) it will be counted as an additional entity along with its members. Should you need to reject the counting of Light/Switch Groups, there’s a way to do that.

how to do that?

Rejects Light/Switch Group entities based on the fact they contain an attribute named entity_id.

sensors: 
    number_of_lights_on:
      friendly_name: Luci Accese
      unit_of_measurement: 'on'
      value_template: >
        {{ expand(states.light, states.switch)
          | selectattr('state', 'eq', 'on')
          | rejectattr('entity_id', 'in', ['switch.roon_rock', 'switch.presa_lavatrice'])
          | rejectattr('attributes.entity_id', 'defined')
          | list | count }}

can I also exclude some integrations? or brand as attribute?

What exactly do you want to include/exclude?

You can get entities from an integration through integration_entities('name-of-integration')

You can get manufacturer of a device through device_attr('entity_id', 'manufacturer'). There is no way to use device_attr in combination with rejectattr, selectattr, select, or reject at this time. Well there is a way, but it requires using namespace.

IIRC, in one of the next few releases, you’ll be might be able to use is_device_attr with select and reject.

sorry but I cannot understand well the code… in my bad head…


- platform: template
  sensors: 
    number_of_lights_on:
      friendly_name: Luci Accese
      unit_of_measurement: 'on'
      value_template: >
        {{ expand(states.light, states.switch)
          | selectattr('state', 'eq', 'on')
          | rejectattr('entity_id', 'in', ['switch.roon_rock', 'switch.presa_lavatrice'])
          | rejectattr('attributes.entity_id', 'defined')
          | rejectattr('integration_entities', 'aarlo')
          | list | count }}
- platform: template
  sensors: 
    number_of_lights_on:
      friendly_name: Luci Accese
      unit_of_measurement: 'on'
      value_template: >
        {{ expand(states.light, states.switch)
          | selectattr('state', 'eq', 'on')
          | rejectattr('entity_id', 'in', ['switch.roon_rock', 'switch.presa_lavatrice'])
          | rejectattr('attributes.entity_id', 'defined')
          | rejectattr('entity_id', 'in', integration_entities('aarlo'))
          | list | count }}