Count number of entities by domain

Thanks, I’m aware of that. The original posted secrets were not my actual code verbatim but were altered slightly. :wink:

Duh… should have … sorry. :blush:

No problem! Some people might not be as tech savvy as myself.
I’ve edited my post so it’s more obvious that it’s been obfuscated.

Thanks for the heads up and don’t refrain from doing so in the future!

@Mariusthvdb I wish there was an easier way to get either hard copy printed reports or csv file export of entities, domains, sensors, binary_sensors, devices/entities associated with specific integrations/add-ons, etc., etc. I guess I should learn to program in python…

guys…

I made the python and it works… However, how could the script run upon HA startup instead of manually run service to have the entities sensor available?

You can create an automation with the start event.

yep,

  - alias: Run at startup
    id: Run at startup
    trigger:
      platform: homeassistant
      event: start
    action:
      - service: script.notify_startup
      - service: python_script.family_home
      - etc etc
      - delay:
          seconds: >
            {{states('input_number.ha_delayed_startup')|int}}
      - event: delayed_homeassistant_start # this creates an event on which you can trigger

you might want to add a delay though since many of these wont be ready upon first startup.

  - alias: Run at delayed startup
    id: Run at delayed startup
    trigger:
      platform: event
      event_type: delayed_homeassistant_start
    action:

This is really cool.
May I ask if it would be possible to track which of the entities/domains are being recorded through the built-in Recorder?
That way you could get a quick overview of all the available entities and at the same time see which data is actually being recorded. Could give some nice info if you set the ‘include:’ in your recorder configuration and can then see what you may have forgotten and is now excluded.

Any hints on how to do this in YAML instead of Python?

1 Like

Here is how I am doing it in YAML

template:
  - sensor:
    - name: "Home Assistant count of entities"
      unique_id: homeassistant_countofentitiessensor22
      icon: mdi:counter
      availability: "{{ states is defined }}"
      unit_of_measurement: "entities"
      state: "{{ states | count }}"
      attributes:
        alarm_control_panel: "{{ states['alarm_control_panel'] | count }}"
        alert: "{{ states['alert'] | count }}"
        automation: "{{ states['automation'] | count }}"
        binary_sensor: "{{ states['binary_sensor'] | count }}"
        button: "{{ states['button'] | count }}"
        calendar: "{{ states['calendar'] | count }}"
        camera: "{{ states['camera'] | count }}"
        climate: "{{ states['climate'] | count }}"
        counter: "{{ states['counter'] | count }}"
        device_tracker: "{{ states['device_tracker'] | count }}"
        domain: "{{ states['domain'] | count }}"
        fan: "{{ states['fan'] | count }}"
        group: "{{ states['group'] | count }}"
        humidifier: "{{ states['humidifier'] | count }}"
        image_processing: "{{ states['image_processing'] | count }}"
        input_boolean: "{{ states['input_boolean'] | count }}"
        input_button: "{{ states['input_button'] | 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: "{{ states['light'] | count }}"
        lock: "{{ states['lock'] | count }}"
        media_player: "{{ states['media_player'] | count }}"
        media_source: "{{ states['media_source'] | count }}"
        number: "{{ states['number'] | count }}"
        persistent_notification: "{{ states['persistent_notification'] | count }}"
        person: "{{ states['person'] | count }}"
        proximity: "{{ states['proximity'] | count }}"
        remote: "{{ states['remote'] | count }}"
        scene: "{{ states['scene'] | count }}"
        script: "{{ states['script'] | count }}"
        select: "{{ states['select'] | count }}"
        sensor: "{{ states['sensor'] | count }}"
        sun: "{{ states['sun'] | count }}"
        switch: "{{ states['switch'] | count }}"
        timer: "{{ states['timer'] | count }}"
        tts: "{{ states['tts'] | count }}"
        update: "{{ states['update'] | count }}"
        vacuum: "{{ states['vacuum'] | count }}"
        weather: "{{ states['weather'] | count }}"
        zone: "{{ states['zone'] | count }}"

Here is how I am displaying it

type: vertical-stack
cards:
  - type: entities
    entities:
      - entity: sensor.home_assistant_count_of_entities
  - type: markdown
    content: >
      {% set entity_id = 'sensor.home_assistant_count_of_entities' %}{% set excluded_attributes = ['unit_of_measurement', 'friendly_name'] %}{% set attributes = states[entity_id].attributes | dictsort %}{% for attribute, value in attributes %}{% if attribute not in excluded_attributes %}{{ attribute }}: {{ value }}<br>{% endif %}
      {% endfor %}

5 Likes

wow, this is abandoned …

just wanted to post an alternative:

  - type: entities
    title: Domains and entities
    card_mod:
      class: class-header-margin
    entities:
      - type: custom:hui-element
        card_type: markdown
        card_mod:
          style: |
            ha-card.type-markdown {
              box-shadow: none;
              margin: -8px -16px -16px -16px;
              overflow-y: scroll;
              height: 450px;
            }
        content: >
          {% set x = ['unavailable','unknown'] %}
          {%- for d in states|groupby('domain') %}
          {% if loop.first %} ### Domains: *{{loop.length}}* - Entities: *{{states|count}}* {% endif %}
            **{{- d[0]}}:** *({{states[d[0]]
                                |rejectattr('state','in',x)
                                |list|count}})*
            {% for i in d[1] if i.state not in x -%}
            > {{i.name}}: *{{i.state}}*
            {% endfor %}
          {%- endfor -%}

all in the frontend and markdown jinja :wink:

same for the unknown/unavailable quest, adapt the excluded domains list to your liking:

  - type: entities
    title: Unknown entities
    card_mod:
      class: class-header-margin
    entities:
      - type: custom:hui-element
        card_type: markdown
        card_mod:
          style: |
            ha-card.type-markdown {
              box-shadow: none;
              margin: 0px -16px -16px -16px;
              overflow-y: scroll;
              height: 450px;
            }
        content: >
          {% set x = ['unavailable','unknown'] %}
          {% set exclude_domains = ['alarm_control_panel','alert','automation','button',
                                    'counter','media_player','proximity','scene'] %}

          {%- for d in states|groupby('domain') if
              states[d[0]]|selectattr('state','in',x)|list|count != 0 %}
            **{{- d[0]}}:** *({{states[d[0]]
                               |selectattr('state','in',x)
                               |list|count}})*
            {% for i in d[1] if i.state in x -%}
            > {{i.name}}: *{{i.state}}*
            {% endfor %}
          {%- endfor -%}
5 Likes

I’ve updated this to include additional domains

I added some criteria to exclude some entities from unavailable lists. because I am not bothered about those. But there is an additional space between entity names. how can I get rid of that.

type: entities
title: Unavailable entities
entities:
  - type: custom:hui-element
    card_type: markdown
    content: |
      {% set x = ['unavailable'] %}
      {%- for d in states|groupby('domain') %}
      {% set filtered_states = states[d[0]]
                               |selectattr('state','in',x)
                               |rejectattr('name','contains','Dining')
                               |rejectattr('name','contains','Signify Netherlands')
                               |rejectattr('name','contains','Ceiling Light')
                               |list %}
      {%- if filtered_states|count != 0 %}
        **{{ d[0] }}:** *({{ filtered_states|count }})*
        {% for i in filtered_states %}
         > {{ i.name }}: *{{ i.state }}*
        {% endfor %}
      {%- endif %}
      {%- endfor %}
    card_mod:
      style: |
        ha-card.type-markdown {
          box-shadow: none;
          margin: 0px -16px -16px -16px;
          overflow-y: scroll;
          height: 450px;
        }

Read up on whitespace control: Template Designer Documentation — Jinja Documentation (3.1.x).

2 Likes

Who needs a python script?
Here’s a template sensor…

- sensor:
    ####################################################
    #  HA Sensors originally Courtesy Thanasis:                               #
    ####################################################
    - name: automation_count
      unique_id: e18ffcab-82ef-43ed-8ef5-e3643e361716
      state: "{{ states.automation | count }}"
      attributes:
        friendly_name: "Aut"
    - name: binary_sensor_count
      unique_id: 59be6953-88af-4077-bfd4-64b29bd72f3d
      state: "{{ states.binary_sensor | count }}"
      attributes:
        friendly_name: "BiSn"
    - name: camera_count
      unique_id: 2b63e6dc-ba73-46eb-bf1e-df5fd4f9b2a0
      state: "{{ states.camera | count }}"
      attributes:
        friendly_name: "Cam"
    - name: button_count
      unique_id: 8ac9df4c-ed33-460f-91ae-a0651999c7f4
      state: "{{ states.button | count }}"
      attributes:
        friendly_name: "But"
    - name: entities_count
      unique_id: 7292b69f-9fe1-42f6-b0fd-0d8ec891d74c
      state: "{{ states | map(attribute='entity_id') | list | count}}"
      attributes:
        friendly_name: "Ent"
    - name: group_count
      unique_id: cdc28f59-2665-4606-93ef-459df0a76ff2
      state: "{{ states.group | count }}"
      attributes:
        friendly_name: "Grp"
    - name: inputs_count
      unique_id: 3ff946b1-083f-489b-87fb-7e15ae050dba
      state: "{{ states | map(attribute='entity_id') | map('regex_search','^input') | select('true') | list | count }}"
      attributes:
        friendly_name: "Inpt"
    - name: lights_count
      unique_id: 8211f15d-7bcd-4f1b-a6f5-9626da81bce6
      state: "{{ states.light | count }}"
      attributes:
        friendly_name: "Lte"
    - name: media_player_count
      unique_id: d94b4860-8197-41d4-8f81-7f60560dfeff
      state: "{{ states.media_player | count }}"
      attributes:
        friendly_name: "MPly"
    - name: script_count
      unique_id: e25dd733-c5f9-40d6-ba20-62f5a12bceeb
      state: "{{ states.script | count }}"
      attributes:
        friendly_name: "Scpt"
    - name: sensor_count
      unique_id: 57513040-58b3-4f58-9e0e-142280cf876d
      state: "{{ states.sensor | count }}"
      attributes:
        friendly_name: "Sen"
    - name: switch_count
      unique_id: 017de0ff-6cfb-46f4-95a7-f98357c744f2
      state: "{{ states.switch | count }}"
      attributes:
        friendly_name: "Swt"
    - name: zones_count
      unique_id: 890ea1a7-0210-4af7-8291-9108a4ccf6e2
      state: '{{ states.zone | count }}'
      attributes:
        friendly_name: "Zone"

and this command_line sensor:

# Count the lines of YAML in my set-up
  name: lines
  unique_id: 4d677d66-68d8-43db-9ce3-e715ecb5997f
  scan_interval: 6000
  command: 'find . -name "*.yaml" -not -path "*/custom_components/*" -not -path "*/Purgatory/*" | xargs wc -l | grep "total" | grep "[0-9]*" -o'

Screenshot_20240421_124458