I’m just sharing a script that creates and updates a statistics related sensor with number of different entities, the state of the sensor being the total number of entities in your system.
Prerequisites:
- you have to have jq package installed
- hass_header.txt contains the needed http header together with the long-lived access token
Create then an entry in shell_commands.yaml:
stats: /home/username/homeassistant_conf/scripts/stats"
Create an automation that runs e.g. daily:
alias: "Entities stats"
initial_state: "on"
id: entities_stats
triggers:
- trigger: time_pattern
hours: /23
action:
- service: shell_command.stats
The script:
#!/bin/bash
curl -s -H @$HOME/hass_header.txt http://localhost:8123/api/states | jq -r ".[].entity_id" \
|sort|awk -F\. '{print $1}'|uniq -c \
|awk 'BEGIN {print "{\"attributes\": {";t=0} \
{ if (NR == 1) \
{ print "\"" $2 "\": \"" $1 "\""} \
else { print ",\"" $2 "\": \"" $1 "\"" }; \
t=t+$1 \
} END {print "}, \"state\": \"" t "\"}"}'| \
curl -s -H @$HOME/hass_header.txt -X POST http://localhost:8123/api/states/sensor.stats \
-d @-
The result: