How to show how many devices and sensors on your system

Hi all,

I’m wondering if it’s possible to show how many devices and sensors I have on my system on a glance card or similar. Does anyone know how to do this?

2 Likes

Fantastic. Thanks.

Came here to say the same thing (Template Sensors) … but using shorter templates:

{{ states.light | count}}
{{ states.switch | count }}
{{ states.binary_sensor | count }}
{{ states.sensor | count }}
etc
5 Likes

Oh, so instead of
value_template: “{{ states.automation | list | length }}”
for example, you put
value_template: “{{ states.automation | count }}” ?

1 Like

That’s correct.

For testing purposes, you can paste the two versions into the Template Editor and compare the results.

Other than for standing around comparing numbers (typically men discussing car performance stats or worse sports !).
I couldn’t help implementing this (well … I am a man too) but its worth pointing out that I got 0 for lights and 0 for switches.
So, depending on your hardware/platform you ‘may’ need to do what I did.
I have a trigger on hassstart anyway, so at the end I appended a 6 minute delay and then updated those sensors. You should adjust this to account for your timing needs (mine was the usual z-wave squabbling)

Edit1: Additional ; something is amis though, I do not have 32 lights and 20 switches (19 and 9) - so I’m going to look into the list and see just what it’s counting, BBL

Edit2: Well … some ‘things’ I did not consider switches are … switches (though 2 off single switches report as do their double switch bretheren) so yes I have 20 switches (gonna count lights now :blush: ! ) : - “{{ states.switch | count - 2 }}”

Edit3: Hmmmm ! It appears that although I only have 16 lights installed (of the 19) and each light can only control one circuit, each has two inputs for scene setting or triggering another automation etc. So it appears that I do (also) have 32 lights. For accuracy I went to divide the output by 2 and got 16.0 (even when cast to int) instead of 16. More reading coming up, BBABL

Edit4: Stupid Boy ! … just use: “{{ (states.light | count / 2) | round(0) }}” :blush:

I only implemented for sensors and automations. Does it work for hue lights as well? I hadn’t thought of adding a counter for them, but it wouldn’t be a terrible idea I suppose :stuck_out_tongue:

It counts all lights but, as far I know, there’s no way to detect an individual light entity’s platform (Hue, Lifx, zigbee, z-wave, etc). That information is not exposed as an entity’s attribute. So you can’t create a template able to report a count of all lights based on a specific platform, like Hue.

That’s actually fine, as I only have hue lights. I felt it better (and easier) to have them all on a single platform. Something else to add, that will give me numbers!!! :stuck_out_tongue:

Na Na Ne Na Na … My Number’s Bigger Than YOUR number !!! :rofl:

See, We are men, and it’s what we do.

I humbly apologise to ANY woman who comes across this thread and shakes their head in sad recognition :unamused:

Non sequitur much?

Eh! It bears directly on the above, being numbers for numbers sake.

Thank you for this. I integrated this in my Home Assistant “just because” (no real need to, but I think it is a neat thing to add to my “janitor” tab).

However, when I put {{ states.sensor | count }} in developer tools => templates, I get 426; the template sensor I created only states 416. I just restarted Home Assistant and checked the sensor, then the template, to assure that this doesn’t have anything to do with the sensor not being updated in time or similar, but it is still the same.

sensor:
  - platform: template
    sensors:
      anzahl_switches:
        friendly_name: "[Anzahl] Switches"
        value_template: "{{ states.sensor | count }}"
        unit_of_measurement: "Stück"

This is not a big deal for me, as I don’t rely on this for anything at the moment; however, have any of you experienced different values as well? Or is there something wrong with my code (usually that’d be likely, but in this case I don’t see what it might be :wink: ).

To ask the same question, in exactly the same format should give you the same response.
That is weird.
When I get home I’ll try the same thing.
I’d suggest casting to list instead and going through the list to see where the differences occur, but as you have 416/426 items I’m not sure how you’d get that from the sensor (that reads the sensor count, even if changed to list) in the ui.

BTW: I approve the thoroughness in testing in both locations :+1:

Edit: so given Marius’s input below, it’s akin to lights not being available at start, so same answer applies.

for the updating of sensors, you still should try to manually update using the update service, because not all sensors are initilized at startup and could take a while.

I have that same effect with my lights, where tradfri lights are immediate, but the Hue lights need extra time to be seen.
Using an automation myself that takes care of that:

  - alias: 'Delayed Startup notification'
    id: 'Delayed startup notification'
    trigger:
      platform: event
      event_type: delayed_homeassistant_start
    condition: []
    action:
      - service: script.notify_delayed_startup
      - service: input_boolean.turn_off
        entity_id: input_boolean.just_started
      - delay:
          seconds: 30
      - service: homeassistant.update_entity
        entity_id: sensor.count_lights

as for the counting of Hue lights only, that should be possible. If you create a group containing those lights, and count the members of that group… That is especially useful since Hue creates groups with the light. domain so simply counting {{states.light|count}} would also count the groups to be a light …

list them

{{states.light |selectattr('entity_id','in',state_attr('group.all_inside_lights','entity_id'))| 
             selectattr('state','eq','on') | map(attribute='name') | list %}

count them:

{{states.light |selectattr('entity_id','in',state_attr('group.all_inside_lights','entity_id'))| 
             selectattr('state','eq','on') | map(attribute='name') | list|count }}

could give an idea how to count these lights in a group (and drop the |
selectattr(‘state’,‘eq’,‘on’) to show all lights, not only the ‘on’ ones)

note that this doesnt update dynamically because of the group construction, and needs either a restart or a manual update. Which for these sensors isn’t a real issue. As said, one doesn’t check the amount of sensors each minute :wink:

otoh, when used like this:

{% set lights_on = states.light |
             selectattr('entity_id','in',state_attr('group.all_inside_lights','entity_id'))| 
             selectattr('state','eq','on') | map(attribute='name') | list %}
          {% if lights_on | length == 0 %}
            No lights on. Sleep well..
          {% elif lights_on | length == 1 %}
            The {{ lights_on[0] }} light is on.
          {% elif lights_on | length == 2 %}
            The {{ lights_on[0] }} and {{ lights_on[1] }} lights are on.
          {% else %}
            The {{ lights_on[:-1] | join(', ') }}, and {{ lights_on[-1] }} lights are on.
          {% endif %}

it nicely shows what’s the status run at sleep time automatically…

1 Like

where do you place code? In sensors.yaml?

tnx