Hi,
for some statistics template sensors I need the following information, which doesn’t seem to be available in the recorder database (using {{ states.something | list | length }}
unfortunately.
Where is this information stored? (wild guesses: a) database but not exposed to Jinja therefore SQL selects needed, b) directly in e. g. core.device_registry file of hidden config subfolder)
And much more important: How can I access that information the easiest way?
Few examples what I’m currently specifically looking for:
1. INTEGRATION INFORMATION
-
Number of devices integrated by a specific integration (e. g. Shelly, deCONZ, …)
→ Using a command_line sensor withcommand: 'cat /config/.storage/core.device_registry | grep 'deleted_devices' -B 100000 | grep -c '"integration_shortname"''
whereintegration_shortname
can be e. g.deconz
,shelly
,mobile_app
,kodi
,fritz
etc. -
Number of entities integrated/provided by a specific integration (e. g. Shelly, deCONZ, …)
→ See solution for 1.1 above, but use thecore.entity_registry
file instead: using a command_line sensor withcommand: 'cat /config/.storage/core.entity_registry | grep -c '"integration_shortname"''
whereintegration_shortname
again can beshelly
etc.
→ There’s a much smarter solution for this, see Metrics: how to access advanced integration, Supervisor and HA internal information - #12 by e-raser / Metrics: how to access advanced integration, Supervisor and HA internal information - #8 by CentralCommand. -
Number of instances of a specific integration (e. g. number of mobile devices integrated using HA Companion App/“Mobile App”)
→ Depends on how the integration handles multiple instances. E. g. forkodi
andmobile_app
integrations every device integrated with that integration is handled as “instance”. Therefore see solution for 1.1 above. -
Attribute information of a device, e. g.:
→ This information is also stored in thecore.device_registry
file, see 1.1 above. Without deep diving into “how to query a json file using the command line (with all the limitations HASS OS has)” it’s a bit tricky to exactly get what is needed, but it’s there. To get the version string information from the screenshot I used'cat /config/.storage/core.device_registry | grep -A 3 "Dresden Elektronik" | grep "sw_version" | cut -c32- | rev | cut -c3- | rev'
as command for a command_line sensor, so it’s very specific. I could not find an easier way unfortunately.
→ There’s a much smarter solution for this, see Metrics: how to access advanced integration, Supervisor and HA internal information - #12 by e-raser / Metrics: how to access advanced integration, Supervisor and HA internal information - #8 by CentralCommand.
2. SUPERVISOR INFORMATION
-
Number of installed Supervisor addons
→ Using a command_line sensor withcommand: 'curl http://supervisor/supervisor/info -H "Authorization: Bearer $(printenv SUPERVISOR_TOKEN)" | grep -o '"name"' | wc -l'
→ Alternative command with same output:curl http://supervisor/addons -H "Authorization: Bearer $(printenv SUPERVISOR_TOKEN)" | grep -o '"installed": true,' | wc -l
→ There’s a smarter solution for this if using the hassio (Supervisor) integration, see Metrics: how to access advanced integration, Supervisor and HA internal information - #12 by e-raser / Metrics: how to access advanced integration, Supervisor and HA internal information - #8 by CentralCommand. -
Number of (in)active Supervisor addons
→ Using a command_line sensor withcommand: 'curl http://supervisor/supervisor/info -H "Authorization: Bearer $(printenv SUPERVISOR_TOKEN)" | grep -o '"state": "started",' | wc -l'
→ There’s a smarter solution for this if using the hassio (Supervisor) integration, see Metrics: how to access advanced integration, Supervisor and HA internal information - #12 by e-raser / Metrics: how to access advanced integration, Supervisor and HA internal information - #8 by CentralCommand.
3. HA INTERNAL INFORMATION
-
Number of (active) HA user accounts (not persons, that´s easy using
{{ states.person | list | length }}
)
→ Using a command_line sensor withcommand: 'cat /config/.storage/auth_provider.homeassistant | grep -c '"username":'''
-
Number of existing blueprints of type “automation” or “script”
→ Using a command_line sensor withcommand: 'find /config/blueprints/automation/ -type f | wc -l'
(gives all blueprints of typeautomation
, use folder/script
for Scripts, using/blueprints
will count all blueprints) -
Number of areas
→ Using either a command_line sensor withcat /config/.storage/core.area_registry | grep -o '"name": ' | wc -l
(counts all areas, doesn’t matter if there are devices or entities assigned to it)
→ Or{{ states|map(attribute='entity_id')|map('area_name')|reject("==",None)|unique|sort|list|count }}
(thanks to @MizterB (see Metrics: how to access advanced integration, Supervisor and HA internal information - #13 by MizterB) which only works if all areas contain at least one active entity.
→ Hint: Remove| wc -l
or|count
for a list of all area names, otherwise all areas are count.
Thankful for everything - from a simple hint “maybe you wanna try this direction…” up to “here it is: use this magical piece of code…!”
Update: job done!
A few hours of investigation, research, mostly try & error I made everything on my own, step by step. I added what I found/used on every single step / information demand above. Without any guarantee, some things are very specific to my environment (integrations, hardware and so on I use). Without investing further hours I could not find smarter ways to get the information I need.
Update: added smarter solutions!
Smarter solutions have been added, highlighted with the icon. Always referring to Metrics: how to access advanced integration, Supervisor and HA internal information - #12 by e-raser / Metrics: how to access advanced integration, Supervisor and HA internal information - #8 by CentralCommand.
- Quite helpful resource:
Entities: integrating devices & services | Home Assistant Developer Docs explaining area, entity and device registry (the places HA stores everything which is not kept in the database, so those are really the base for whole HA) - see e. g./config/.storage/core.area_registry
but CAUTION don’t change or delete anything (reading should be safe)! Backup is key anyway. - For basic information (the stuff you usually start with and which should be sufficient for basic statistics needs) have a look at How much entities do you have in Homeassistant?, particularly this posting with a simple 3-liner: How much entities do you have in Homeassistant? - #9 by poudenes.