Rejectattr

Hi, I have a component that counts active switches:

 - platform: template
    sensors:
      count_switch:
        friendly_name: 'Switch attivi'
        unique_id: switch_attivi111
        value_template: "{{ states.switch | selectattr('state', 'eq', 'on') | rejectattr('entity_id','in',('switch.shelly_em', 'switch.luce_divano', 'switch.luce_cucina', 'switch.luce_tavola', 'switch_scheduler')) | list | count }}"      

Is there a way to exclude all the switches of an integration (xiaomi)? I inserted a robot vacuum cleaner that has 20…

If the integration’s name is xiaomi then you can use the integration_entities function to get list off all its entity_ids. Then you can select only the one containing switch.

value_template: >
  {% set rejects = ['switch.shelly_em', 'switch.luce_divano', 'switch.luce_cucina', 'switch.luce_tavola', 'switch_scheduler'] %}
  {% set xiaomi_rejects = integration_entities('xiaomi') | select('match', 'switch') | list %}
  {{ states.switch 
    | rejectattr('entity_id', 'in', rejects)
    | rejectattr('entity_id', 'in', xiaomi_rejects)
    | selectattr('state', 'eq', 'on') |
    | list | count }}

it doesn’t work… this is the integration

Code:

value_template: >
          {% set rejects = ['switch.shelly_em', 'switch.luce_divano', 'switch.luce_cucina', 'switch.luce_tavola', 'switch_scheduler'] %}
          {% set xiaomi_rejects = integration_entities('xiaomi_robot_vacuum_x20') | select('match', 'switch') | list %}
          {{ states.switch | rejectattr('entity_id', 'in', rejects) | rejectattr('entity_id', 'in', xiaomi_rejects) | selectattr('state', 'eq', 'on') | list | count }}

What is the URL of that screenshot? It should look like:

http://IP:8123/config/integrations/integration/xxxxxx

The integration name will be the xxxxxx at the end of it — put that into the integration_entities function.

In your first post you said the integration’s name is xiaomi.

However, if the suggested template doesn’t work then it means the integration’s name is not xiaomi but something else.

Replace the word xiaomi in:

integration_entities('xiaomi')

with the integration’s actual name. It might be dreame_vacuum if you’re using this custom integration.

integration_entities('dreame_vacuum')

Troon’s post shows one way of determining the actual name.