Count number of entities by domain

Just add them to the attributes dictionary.

...

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

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

hass.states.set('sensor.ha_overview', count_all, entities)
1 Like

magic… thanks!
gotta love python, and of course rewrite it again using attributes:

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)

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)

43

more info

beautiful!

did the same for this domain script, but the list is not formatted correctly. Would you guide me setting that the same way as the entity list please?

domains = []
attributes = {}
for s in hass.states.all():
    res = str(s.domain)
    if res not in domains:
        domains.append(res)

cnt = len(domains)
domains.sort()

domainlist = []

for d in domains:
    domainlist.append('- {}'.format(d))

# join each component with a carriage return
domainlist = '\n'.join(domainlist)

text = '*========== {} Loaded Domains ========\n' \
       '{}'.format(cnt, domainlist)

attributes[d] = domainlist
attributes['friendly_name'] = 'Domains'
attributes['icon'] = 'mdi:domain'

# hass.states.set('sensor.overview_domains', cnt, {
#     'icon': 'mdi:domain',
#     'friendly_name': 'Domains',
#     'text': text})

hass.states.set('sensor.overview_domains', cnt, attributes)

37

I think one sensor is enough.
The value is already in the code, so why not show it.

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.ha_overview', count_all, attributes)

The ‘—’ attribute is crazy, but it works. :upside_down_face:

Now you have space for one more of your nice buttons.:wink:

1 Like

haha yes, I did think of that, and have now adopted your suggestion.
Only thing is of course this buttons state is of the Entities count (as is says in the name…), and my other domain script is reading the sensor for the domain count.

1 Like

It came up in another thread about 2 weeks ago. Someone had a need to count the number of entities in a given domain.

1 Like

This should be enough to play with.

count_all = 0
count_domains = 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)

count_domains = len(domains)

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

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

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

hass.states.set('sensor.ha_overview', str(count_all) + '/' + str(count_domains), attributes)

Auswahl_208

or, if you want a new sensor

count_all = 0
count_domains = 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)

count_domains = len(domains)

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

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

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

hass.states.set('sensor.ha_domains', count_domains, attributes)

Auswahl_209

yes, indeed, so nice, thanks.
gettingback to this though, which is a script that formats the list nicely and show all components/domains with their respective sub-domains, indented.

I use it on a dedicated card

type: markdown
content: >
  <ha-icon icon='mdi:format-list-bulleted-type'></ha-icon> {{ state_attr('sensor.overview_components','text')}}

to show the list as:

but the more-info on a button/sensor looks a bit awkward:

how would I need to change that so it shows nicely as the other more-info’s used above? With that I mean to not show the word ‘text’ on the left side, and display the list of components, just as on the dedicated card.

attributes = {}
components = hass.states.get('sensor.hassio_rpi4_config').attributes['components']
cnt = len(components)
components.sort()

# Make a dictionary of all main domains, add each sub domain to the main domain list.
compdict = {}
for component in components:
    if component.count('.') == 0 and component not in compdict:
        compdict[component] = []
    if component.count('.') == 1:
        domain, subdomain = component.split('.')
        compdict[domain].append(subdomain)
        
# Make the dictionary into a flat list of strings.

complist = []

for key, value in compdict.items():
    if value:
        value.sort()
        # Adding a domain & series of sub domains
        complist.append('- {}: \n --> {}'.format(key, ', '.join(value)))
    else:
        complist.append('- {}'.format(key))

# join each component with a carriage return
complist = '\n'.join(complist)

text = '{} Loaded Components:\n' \
       '{}'.format(cnt, complist)

attributes['friendly_name'] = 'Components'
attributes['icon'] = 'mdi:format-list-bulleted-type'
attributes['text'] = text

hass.states.set('sensor.overview_components', cnt, attributes)

Hello VDRainer

Thank you for this, works brilliant to my needs from the first post. Which card did you use, to display the two screenshots ?

/M

It’s just an entity in an entities card, and the second is the ‘more_info’ of the sensor.

Ahh, Thanks !

/M

BTW, you can display the more_info dialog of an emtity as a card with the more-info-card.

Ahh yes, good point. Thanks VDRainer

/M

Well Done Bro 10 out 10

so much info for 10 lines code

What would be the secret for this secret??

I’m trying to figure out how to get a list (or lists) of entities by integration (i.e. OpenZWave, etc.)…

The api endpoint of your HA instance e.g.

http://192.168.1.100:8123/api

yes, in my case I use:

resource_ha_rpi4_config: https://mydomain.duckdns.org:port/api/config

Error loading /config/configuration.yaml: Secret api_bearer_token not defined

You need to create a token and add a secret for this as well.
See the REST API docs here for more info.