Hidden entities show up in templates?

I am working on a template to see how many switches are on or off, and which ones.
Then put those in a card so I can see them and control them.

I have a lot of helpers to convert a switch to a light domain, or to a fan domain, depending if it is controlling a light or a fan.
All that works great.

Now I am working on a template, and here is the code:

{%- set search_state = 'off' %}
        {%- set ns = namespace(switches=[]) %}
        {%- for switch in states.switch 
            | selectattr('state','eq', search_state) 
            | rejectattr('entity_id', 'search', 'upstairs')
            | rejectattr('entity_id', 'search', 'downstairs')
            | rejectattr('entity_id', 'search', 'water')
            | rejectattr('entity_id', 'search', 'charging')
            | rejectattr('entity_id', 'search', '_detections_motion') 
            | rejectattr('entity_id', 'search', '_detections_person') 
            | rejectattr('entity_id', 'search', '_detections_vehicle')
            | rejectattr('entity_id', 'search', '_show') 
            | rejectattr('entity_id', 'search', '_hdr') 
            | rejectattr('entity_id', 'search', '_status_light_on')
            | rejectattr('entity_id', 'search', 'nvr_')
            | rejectattr('entity_id', 'search', '_detections_smoke_co')
            | rejectattr('entity_id', 'search', '_fps')
            | rejectattr('entity_id', 'search', '_privacy_mode')%}
            {%- set ns.switches = ns.switches + [ switch.entity_id ] %}
        {%- endfor %}
        {{ ns.switches| join('\n') }}

There are a lot of rejectattr because of the unifi cameras, some of the HVAC, etc., which all have some switch entity in them. I will look into removing those with something better than filtering the name later.

The issue I have is all of the results still show all of the switches which I had converted to lights and fans.

I have the following card code:

      type: vertical-stack
      cards:
        - type: custom:auto-entities
          show_empty: false
          card:
            type: glance
            title: Switches On
            column: 3
          filter:
            include:
              - domain: switch
                state: "on" 
                options:
                  tap_action:
                    action: toggle
            exclude:
              - entity_id: "*upstairs*"
              - entity_id: "*downstairs*"
              - entity_id: "*water*"
              - entity_id: "*charging*"
              - entity_id: "*_detections_motion*"
              - entity_id: "*_detections_person*"
              - entity_id: "*_detections_vehicle*"
              - entity_id: "*_show*"
              - entity_id: "*_hdr*"
              - entity_id: "*_status_light_on*"
              - entity_id: "*nvr_*"
              - entity_id: "*_detections_smoke_co*"
              - entity_id: "*_fps*"
              - entity_id: "*_privacy_mode*"
        - type: custom:auto-entities
          show_empty: false
          card:
            type: entities
            title: Switches Off
            show_header_toggle: false
          filter:
            include:
              - domain: switch
            exclude:
              - state: "on"
              - state: "unavailable"
              - hidden_by: "Integration"
              - entity_id: "*upstairs*"
              - entity_id: "*downstairs*"
              - entity_id: "*water*"
              - entity_id: "*charging*"
              - entity_id: "*_detections_motion*"
              - entity_id: "*_detections_person*"
              - entity_id: "*_detections_vehicle*"
              - entity_id: "*_show*"
              - entity_id: "*_hdr*"
              - entity_id: "*_status_light_on*"
              - entity_id: "*nvr_*"
              - entity_id: "*_detections_smoke_co*"
              - entity_id: "*_fps*"
              - entity_id: "*_privacy_mode*"

The issue is, all of them show up on the card also.

Is there a way to not have these switches show up?
I added the - hidden_by: "Integration" above to the exclude but did not seem to work. All still show up.
In the entities list, I confirmed that all switches entities are Hidden, and only the Light entities are enabled.

The 3 switches which are showing up are actually also 2 lights and a fan, but they also show up.

Any pointers here?

EDIT: Adding the details of one of the switches which is showing up
image

{%- set search_state = 'off' %}
{{ states.switch | select('state','eq', search_state) 
                 | map(attribute='entity_id')
                 | reject('is_hidden_entity')
                 | reject('search', 'upstairs|downstairs|water|charging|_detections_|_show|_hdr|_status_light_on|nvr_|_fps|_privacy_mode')
                 | list }}

Thank, that worked. I just added into my code. I was not able to plug it in as is into the template in developer, but I added the is_hidden_entity test and solved my issue with the polling.

Is there a way to do the same and exclude from the Auto-entity-card?

you can use the same template for auto-entities

I get the following when I copy your code into the Template developer:
TemplateRuntimeError: No test named ‘state’.

Trying to debug there so I can use the Template function under filter for the auto-entity

change select to selectattr in that first line

1 Like

Worked

        - type: custom:auto-entities
          show_empty: false
          card:
            type: entities
            title: Switches Off
            show_header_toggle: false
          filter:
            template: |
              {%- set search_state = 'off' %}
              {{ states.switch | selectattr('state','eq', search_state) 
                | map(attribute='entity_id')
                | reject('is_hidden_entity')
                | reject('search', 'upstairs|downstairs|water|charging|_detections_|_show|_hdr|_status_light_on|nvr_|_fps|_privacy_mode')
                | list }}

I know it is unrelated to this topic now, but is there a way to add the tap_action for the glance card on a list of entities?
Looking at the documentation, seems the tap action is on a per entity basis.

I’m trying more or less the same.

  • I want to include switches that are ON
  • I want to exclude switches that are hidden
  • I have an extra exclude list

But I also get switches that are OFF, and
those with the hidden-attribute are still visible.

  - type: custom:auto-entities
    show_empty: false
    card:
      type: entities
      title: switches on
      show_header_toggle: false

    filter:
      template: |
        {%- set search_state = 'on' %}
        {{ states.switch | selectattr('state','eq', search_state) 
          | map(attribute='entity_id')
          | reject('is_hidden_entity')
          | list }}
      include:
        - domain: switch
          state: "on"
          options:
            tap_action:
              action: toggle
            secondary_info: last-changed
      exclude:
        - entity_id: switch.kelder_rack_poe1
        - entity_id: switch.kelder_rack_ups

And when I remove the ‘include’ section, I lose my ‘options’.

Okay, and where do the options go when only using a template?

    filter:
      template: |
      options:

or

    filter:
      template: |
    options:

both are not working

Since I only wanted to hide entities that were renamed from switch… to light… adding

      exclude:
        - hidden_by: integration

was enough, and I don’t need the template.