Automation to delete unavailable, restored entities

Hi All,

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:

I know I can delete them manually from the UI by clicking on them and selecting the “DELETE” button in the bottom left corner.

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 }}

Any help?

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. :wink:

Don’t forget to F5/reload/refresh the UI after running the script.

#!/usr/bin/with-contenv bashio

################################################################################################################################################################

function get_restored_entities() {

    local template=$(cat <<-EOTEMPLATE | tr '\n' ' '
    {
        "template": "
            {{ dict(
                 result = 'ok',
                 data = states
                     | selectattr('state' , 'eq' , 'unavailable')
                     | rejectattr('attributes.restored', 'undefined')
                     | selectattr('attributes.restored' , 'eq' , True)
                     | map(attribute='entity_id')
                     | list
               ) | tojson }}
        "
    }
EOTEMPLATE
)

    bashio::api.supervisor POST /core/api/template "${template}"
}

################################################################################################################################################################

declare -a entities=$(get_restored_entities | jq -rcM ".[]")
echo Stopping HA...
ha core stop

echo
cp /config/.storage/core.entity_registry /dev/shm/core.entity_registry
cp /config/.storage/core.restore_state /dev/shm/core.restore_state
for entity in ${entities[@]}; do
    echo Removing ${entity}
    jq -e "del(.data.entities[] | select(.entity_id==\"${entity}\"))" /dev/shm/core.entity_registry > /dev/shm/core.entity_registry.tmp \
        && mv /dev/shm/core.entity_registry.tmp /dev/shm/core.entity_registry
    jq -e "del(.data[] | select(.state.entity_id==\"${entity}\"))" /dev/shm/core.restore_state > /dev/shm/core.restore_state.tmp \
        && mv /dev/shm/core.restore_state.tmp /dev/shm/core.restore_state
done

timestamp=$(date +"%Y%m%d_%H%M%S%")
mv /config/.storage/core.entity_registry "/share/core.entity_registry.${timestamp}"
mv /config/.storage/core.restore_state "/share/core.restore_state.${timestamp}"
mv /dev/shm/core.entity_registry /config/.storage/core.entity_registry
mv /dev/shm/core.restore_state /config/.storage/core.restore_state

echo
echo Starting HA...
ha core start

The result should look like this:

Stopping HA...
Processing... Done.

Command completed successfully.

Removing binary_sensor.updater

Starting HA...
Processing... Done.

Command completed successfully.