Wasn’t there an overview with the number of entities in the info page a while ago?
Can’t find it, so i made a little python_script that creates a sensor with this information.
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))
hass.states.set('sensor.ha_overview', count_all, attributes)
The sensor shows the count of all entities,
and the more_info the count of the domains.
The script is called once at HA start.
7 Likes
I use this. Also from the same page you are referring too.
sensor_count:
entity_id: []
friendly_name: 'Number of Sensors'
value_template: >
{%- set domains = ['sensor'] -%}
{%- for domain in domains -%}
{%- for item in states[domain] -%}
{% if loop.first %}
{{loop.length}}
{% endif %}
{%- endfor -%}
{%- endfor -%}
automation_count:
entity_id: []
friendly_name: 'Number of Automations'
value_template: >
{%- set domains = ['automation'] -%}
{%- for domain in domains -%}
{%- for item in states[domain] -%}
{% if loop.first %}
{{loop.length}}
{% endif %}
{%- endfor -%}
{%- endfor -%}
script_count:
entity_id: []
friendly_name: 'Number of Scripts'
value_template: >
{%- set domains = ['script'] -%}
{%- for domain in domains -%}
{%- for item in states[domain] -%}
{% if loop.first %}
{{loop.length}}
{% endif %}
{%- endfor -%}
{%- endfor -%}
switch_count:
entity_id: []
friendly_name: 'Number of Switches'
value_template: >
{%- set domains = ['switch'] -%}
{%- for domain in domains -%}
{%- for item in states[domain] -%}
{% if loop.first %}
{{loop.length}}
{% endif %}
{%- endfor -%}
{%- endfor -%}
binary_sensor_count:
entity_id: []
friendly_name: 'Number of Binary Sensors'
value_template: >
{%- set domains = ['binary_sensor'] -%}
{%- for domain in domains -%}
{%- for item in states[domain] -%}
{% if loop.first %}
{{loop.length}}
{% endif %}
{%- endfor -%}
{%- endfor -%}
lights_count:
entity_id: []
friendly_name: 'Number of Lights'
value_template: >
{%- set domains = ['light'] -%}
{%- for domain in domains -%}
{%- for item in states[domain] -%}
{% if loop.first %}
{{loop.length}}
{% endif %}
{%- endfor -%}
{%- endfor -%}
tracker_count:
entity_id: []
friendly_name: 'Number of Devices'
value_template: >
{%- set domains = ['device_tracker'] -%}
{%- for domain in domains -%}
{%- for item in states[domain] -%}
{% if loop.first %}
{{loop.length}}
{% endif %}
{%- endfor -%}
{%- endfor -%}
1 Like
yes, I use some of them.
have this python script amongst others:
domains = []
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)
hass.states.set('sensor.hassio_main_domains', cnt, {
'custom_ui_state_card': 'state-card-value_only',
'text': text
})
but the final sensor uses a custom-ui-state-card which is now no longer imported…but can be easily take out of course.
will certainly check your script! thanks for that.
btw here is where we made another Loaded component script, based on the config sensor: Template to display loaded components on HA instance?
sensor:
- platform: rest
name: Hassio Rpi4 config
resource: !secret resource_hassio_main_config
# authentication: basic
value_template: >
{{ value_json.version }}
json_attributes:
- components
- unit_system
- config_dir
- version
headers:
Content-Type: application/json
Authorization: !secret api_bearer_token
User-Agent: Home Assistant REST sensor
the loaded components sensor in a markdown card:
because id likes to set the icon and friendly_name in the script, (cant do that using customize with python scripts) I tried this:
count_all = 0
domains = []
entities = {}
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):
entities[domain] = len(hass.states.entity_ids(domain))
hass.states.set('sensor.overview_entities', count_all, {
'entities': entities,
'icon': 'mdi:format-list-numbered',
'friendly_name': 'Entities'})
which shows fine on the button:
clicking the more info now shows this though, which is too bad, because you had it perfectly:
please have a look with me how to incorporate the 2 extra attributes icon and friendly_name, without messing around with the original nice list you had?
1 Like
you do know you can use this:
value_template: >
{{ states.light | count }}
to get the same result? (and of course do that for all domains…)
I did not know that. Gonna try that, shorten my code.
Thx
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)
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)
VDRainer
(🍻)
October 18, 2019, 9:47pm
10
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.
Now you have space for one more of your nice buttons.
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
123
(Taras)
October 18, 2019, 10:38pm
12
It came up in another thread about 2 weeks ago. Someone had a need to count the number of entities in a given domain.
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
1 Like
VDRainer
(🍻)
October 18, 2019, 11:12pm
13
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)
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)
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.
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
myle
(StePhan McKillen (Other kiwi Here))
March 20, 2020, 6:50pm
20
Well Done Bro 10 out 10
so much info for 10 lines code