I am using some home assistant integrations that automatically create entities when I e.g. change a configuration inside of a 3rd part app.
For this example lets use the Alexa Media Player integration (because I think it is the most popular of the ones affected for me) - but others are affected, too.
So if I create a new group or rename an Alexa Device, home assistant creates a new entity.
The problem here is that this entity remains, even if the device or group was deleted / renamed again.
So from time to time I have entities looking like this inside of Home Assistant:
Never the less I am using home assistant to automatically handle things for me… so what I am searching for is a possibility to automatically delete these entites…
I was able to create a template that returns affected entites (see below) , but it seems there is no service to delete an entity.
{{ states | selectattr('state' , 'eq' , 'unavailable') | selectattr('attributes.restored' , 'eq' , True) | map(attribute='entity_id') | list }}
The only way I found to delete these entities is to delete the entries from the core.entity_registry and core.restore_state files while HA is stopped.
I’ve used the below script to do it automatically. Use the command line to execute the script. It saves the original files in the /share folder, but it is better to backup your system before running anything on it.
Don’t forget to F5/reload/refresh the UI after running the script.