Short script to clean home-assistant's entity_registry.yml

If you’ve added and remove alot of devices in Home Assistant, the entity_registry.yaml file can get pretty big with old unused entries.

I noticed that the api/states provided the currently used entities, so I decided to automate this tedious task.

Download or copy-paste the contents into a regenerate.php file.

Install php-cli (for example on ubuntu just run apt-get install php-cli)

Run the script with

regenerate.php /home/your-user/.homeassistant/entity_registry.yaml “http://your-home-assistant:8123/api/states” delete > output.yaml
now you can inspect the output.yaml and see if everyhing is ok, then replace the original file with this one ( please stop home assistant then replace the file then start it again to make sure everything is ok)

If you do not put “delete” at the end, the unneeded entities will be commented in the output file, instead of being removed.

I removed almost a hundred items :slight_smile:

Enjoy!

7 Likes

This is a great idea - thanks!

THis is awesome. It should be included into HA Configuration as a maintencance function…

Trying to give this a go - command run…

php regenerate.php /home/homeassistant/.homeassistant/entity_registry.yaml “https://MY-URL.duckdns.org/api/states?api_password=MY_PASSWORD” delete > output.yaml

Errors Warnings in terminal…

PHP Warning:  file_get_contents(“https://MY-URL.duckdns.org/api/states?api_password=MY-PASSWORD”): failed to open stream: No such file or directory in /home/homeassistant/.homeassistant/extras/bash_scripts/regenerate.php on line 8
PHP Warning:  Invalid argument supplied for foreach() in /home/homeassistant/.homeassistant/extras/bash_scripts/regenerate.php on line 11

output.yaml is blank.

I can confirm that if I use the URL in a browser I get the JSON that the script is expecting.

Any ideas?

I wonder if it could be converted to a python script then you can easily run it from the frontend?

2 Likes