Sensor - Count the installation (Mine is bigger than yours-sensor)

A simple sensor that counts all other sensors :slight_smile:

sensor:
- platform: template
  sensors:
    hass_count:
      friendly_name: HASS Count
      icon_template: mdi:home-assistant
      value_template: "{{ states | count }}"
      attribute_templates:
        automation: "{{ states.automation | count }}"
        binary_sensor: "{{ states.binary_sensor | count }}"
        camera: "{{ states.camera | count }}"
        climate: "{{ states.climate | count }}"
        device_tracker: "{{ states.device_tracker | count }}"
        light: "{{ states.light | count }}"
        media_player: "{{ states.media_player | count }}"
        scene: "{{ states.scene | count }}"
        script: "{{ states.script | count }}"
        sensor: "{{ states.sensor | count }}"
        switch: "{{ states.switch | count }}"
        zwave: "{{ states.zwave | count }}"

To list all domains (and current count) in your environment you can run this:

{%- for d in states | groupby('domain') %}
{{ d[0] }} - {{ states[d[0]] | count  }}
{%- endfor %}

And the result
Screenshot 2020-09-11 071427

11 Likes

Groovy. Thanks for the code.

I have no need for the card so I just did it in the template editor:
Screenshot_2020-09-11 Developer Tools - Home Assistant

:+1: Did a small update to the script on the first post to include current count .

1 Like

Thank you. I was trying to get this to work:

{%- for d in states | groupby('domain') %}
{{ d[0] }} - {{ states[d[0]] | count  }}
{%- endfor %}

But stupidly I was using states.d[0] | count

Here is a python_script where you don’t have to specify the domains.

Rikard and Tom’s is much neater than what I use : -

states : {{ states| count }}
{%- set ns = namespace(num = 0) %}
{% for domain in states | map(attribute='domain') | unique %}
{%- set numb = states[domain] | count %}
{%- set ns.num = ns.num + numb %}
{{ domain ~ 's : ' ~ numb }}
{%- endfor %}

Producing : -
HA Instance 20200911

But don’t get into a pissing contest with @Mariusthvdb , He’ll eat you alive !
:rofl:

yeah, the beauty of this python script is it does it all automatically:

count_all = 0
domains = []
attributes = {}

for entity_id in hass.states.entity_ids():
    count_all = count_all + 1
    entity_domain = entity_id.split('.')[0]
    if entity_domain not in domains:
        domains.append(entity_domain)

attributes['Domains'] = len(domains)
attributes['--------------'] = '-------'

for domain in sorted(domains):
    attributes[domain] = len(hass.states.entity_ids(domain))

attributes['friendly_name'] = 'Entities'
attributes['icon'] = 'mdi:format-list-numbered'

hass.states.set('sensor.overview_entities', count_all, attributes)

though I like the way the OP uses the attribute_templates (I still have these (also) as individual sensors, per domain)
Cut another 30 entities :wink: (not are this relieves the state machine, because they still are listed in the attributes as templates.

only gripe I have here, is it counts my light groups to, since they belong to the domain light. here my light sensor isnt

{{states.ligh |count}}

but

      count_lights:
        entity_id: sensor.date
        friendly_name: Lights (only)
        value_template: >
          {{expand('group.all_lights_only')|count}}

@Mariusthvdb

49 Proximity sensors.
34 Zones.

Are you sure you are not automating a small country?

:rofl: :rofl: :rofl:

I didn’t bother with sensors, I just put mine straight into a markdown card (someone is going to tell me why that is a bad idea…)

no, just a large family :wink:

did a small rewrite of the OP sensor with attribute_sensors and will test this shortly:

      entities_domains_counter:
        entity_id: sensor.date  #<--- not sure if this needs to stay coming 115? maybe to keep it from updating too frequently;-)
        friendly_name: Entities per domain
        value_template: >
          {{states|count}}
        attribute_templates:
          headline: >
            {{states|count}} entities in {{states|groupby('domain')|count}} domains
          alert: >
            {{states.alert|count}}
          automation: >
            {{states.automation|count}}
          binary_sensor: >
            {{states.binary_sensor|count}}
          camera: >
            {{states.camera|count}}
          climate: >
            {{states.climate|count}}
          counter: >
            {{states.counter|count}}
          cover: >
            {{states.cover|count}}
          device_tracker: >
            {{states.device_tracker|count}}
          geo_location: >
            {{states.geo_location|count}}
          group: >
            {{states.group|count}}
          input_boolean: >
            {{states.input_boolean|count}}
          input_datetime: >
            {{states.input_datetime|count}}
          input_number: >
            {{states.input_number|count}}
          input_select: >
            {{states.input_select|count}}
          input_text: >
            {{states.input_text|count}}
          light_only: >
            {{expand('group.all_lights_only')|count}}
          media_player: >
            {{states.media_player|count}}
          person: >
            {{states.person|count}}
          proximity: >
            {{states.proximity|count}}
          remote: >
            {{states.remote|count}}
          scene: >
            {{states.scene|count}}
          script: >
            {{states.script|count}}
          sensor: >
            {{states.sensor|count}}
          sun: >
            {{states.sun|count}}
          switch: >
            {{states.switch|count}}
          timer: >
            {{states.timer|count}}
          variable: >
            {{states.variable|count}}
          weather: >
            {{states.weather|count}}
          zone: >
            {{states.zone|count}}
          zwave: >
            {{states.zwave|count}}

btw, Maybe I am overly puristic, but I don’t like the fake icon_templates, so took the out and customize it :blush:

sensor.entities_domains_counter:
  icon: mdi:home-assistant

I know it’s 1 line extra, but the dev’s wont complain if anything goes awol…

bw, missed an important 1: persistent_notifications. But I already had that in my notification package, so added the attibute as

          persistent_notification: >
            {{states('sensor.count_persistent_notifications')}}

You lost 30 sensors. Did one of your zones attempt to proclaim its independence and many sensors were destroyed in the ensuing battle?

even worse!
Ive lost several Waze sensors, and Weatherbit integration:
at startup

after a while

the culprits:

hi!
not sure why you’d post that, but if your ‘pulling the data’ from this Roldy's Home Assistant - 190+ sensors - 440+ entities - 70+ automations - Floorplan you now should have noticed that could be done much better…

consider that code:

   count_automations:
     entity_id: sensor.date
     value_template: >
       {%- set domains = ['automation'] -%}
       {%- for domain in domains -%}
         {%- for item in states[domain] -%}
           {% if loop.first %}
             {{loop.length}}
           {% endif %}
         {%- endfor -%}
       {%- endfor -%}

and compare it to

{{states.automation|count}}

Hey @uiguy come here and blow their minds…

Template editor tool:

{%- for d in states | groupby('domain') %}
{{ d[0] }} - {{ states[d[0]] | count  }}
{%- endfor %}
1 Like

my code on @Mariusthvdb link above came initially from @Mariusthvdb i seem to remember… oh and sorry I did not credit you before… :slight_smile:

So, I threw this into the Template editor and it popped up a long list of domains as it should, but one in particular caught my eye -
sensor - 666
Thanks 2020!

1 Like

What would be the best way to display this in Lovelace? I tried a Markdown Card but the formatting was ugly, not having each domain on a new line. Is there a way for format the use of new lines in the Markdown Card?

I tried to put it in a table but failed. This is the best I can do.

type: markdown
content: |-
  {% for d in states|groupby('domain') %}
  **{{ d[0]|replace('_', ' ')|title }}**  {{ states[d[0]]|count  }}
  {%- endfor %}
title: Domain Counts

2 Likes