Help find deletable entities (template)

Hi everybody,

I have countless “dead” entities that have been picked up through an 433MHz proxy ages ago that I finally want to get rid off.

I have tried this, which does not work (it needs an actual list of all entities, no wildcard)

action: ha_registry.remove_entity
data: 
  entity_id: *acurite*

So then I was hoping to find all these entities with this template, but it only returns an empty string.

{%- for state in states -%}
  {% if "*acurite*" in state.entity_id  %}
  - {{ state.entity_id }}
  {% endif %}
{%- endfor -%}

There are over 300 entities with acurite alone that I would like to remove, then countless more that I haven’t even filtered for yet because I don’t know how to list them all.

Can you please help me remove these entities? They were auto-discovered and the only other way to delete them seems to click on each and every device, click the three dots next to MQTT INFO, then DELETE. But that would take forever.

I have already tried running a script that will clear all MQTT messages that were set to “retain”, hoping that this might delete these ages old devices, but it did not. They are still part of Home Assistant, and I am currently trying to get rid off everything that doesn’t need to be in my HASS instance.

(I cannot add / remove favorites on my smart watch because - at least I believe so - there are too many entities; it loads, and loads, and eventually crashes or simply quits the Home Assistant app, which might be caused by the app trying to load every single entity before displaying them all…)

Thank you in advance for your help :slight_smile:

Remove the * and the loop will work, I mean both of them

Thank you, but this doesn’t seem to be the case. Sorry, I had already tried this, but forgot to include it in my initial post:

Could it be because they don’t have a state? All seem to be unknown.
Not sure how this states loop work if there is no state.

EDIT:

No that does not seem to be it.
I tested this:

{%- for state in states -%}
  {% if "unknown" in state.state  %}
  - {{ state.entity_id }}
  {% endif %}
{%- endfor -%}

And it returned the entities that is unknown.

They are MQTT devices.
There are probably topics on the MQTT broker that keeps them alive.
Use MQTT Explorer to see what is on the MQTT broker and delete the topics there first.

1 Like

That works for me.

However, it also includes entities that I would like to keep, for example, some bluetooth sensors that have just not transmitted a state yet (but are in daily use and should become available soon again).

Use MQTT Explorer to see what is on the MQTT broker and delete the topics there first.

I have done just that. I cleared all those topics quite some time ago, but it did not remove them from Home Assistant:

What are your actual entity IDs? Your screenshot appears to show a list of devices, as entities would not have a “model” attribute.

Thus I would look to do it something like this, assuming what you need is a list of entity IDs:

{% set devices = states | map(attribute='entity_id')
| map('device_id') | reject('none') | list %}
{{ zip(devices, devices | map('device_attr', 'model'))
| selectattr(1, '==', 'Acurite-986') | map(attribute=0)
| map('device_entities') | sum(start=[]) }}

Edit: Changed the final list to sum or you’ll get a list of lists!

But since your screenshot shows a list of devices, aren’t what you really want to do to remove a bunch of devices…? Which surely would need a list of device IDs instead, and an action that can operate on them.

Here is an example sensor.acurite_986_1r_1088_battery. So acurite is definitely part of entity_id. Unfortunately, your template is too advanced for me, I have no idea how to modify it so that it would search for just part of the entity_id.

You don’t need to search for part of an entity ID, my template returns a list of all entities belonging to devices whose model is Acurite-986, as in your screenshot above.

But again, even if you manage to remove all of those entities, won’t the encapsulating devices still be left untouched? I think what you need to do is remove the devices, which should also delete all of the entities that belong to them.

Oh, I see what you mean.

Is there a way to do both? Remove the devices as well as all entities that belong to them? I was only focused on the entities themselves; if I click on developer-tools → state, it takes forever until the page loads, because there are so many entities. Many of which I had for testing, or where discovered from neighbors etc.

I am pretty sure removing the corresponding file in .storage would do the trick, but that would delete everything. I was hoping to just delete all the stuff that I know I wouldn’t need. That’s why I didn’t want to use homeassistant.delete_all_orphaned_entities (if that would even have worked…?) - because there might be orphaned entities that are actually there for a reason and that are not actually orphaned, but perhaps just don’t report often enough are are falsely considered orphaned due to a recent restart or similar.

HA Registry appears to be a custom component from HACS, and it does not include any corresponding action for operating on devices. So… I have no idea whether this is possible.

I would start by manually testing this action and manually feed it all entities from just the one device and see what happens. Does it successfully delete the entities? Does it also remove the device, or do you end up with a device that no longer has entities?

I can try this, but it still leaves the original issue: generating a list of all entities that include - for example - acurite. Even if this does not remove the devices as such, it would help immensely if I could remove the separate entities.

Would it help to completely remove the MQTT integration (and / or even the mqtt add-on)? Or could this introduce different problems?

Did you test my template above? It should give you a list of all entities belonging to those devices. (Note that I made some small changes after initially posting it.)

If you don’t use MQTT any longer, then obviously remove the integration which should remove all of its devices and entities along with it.

1 Like

Brilliant, thank you. The changed template works, it gives me

[
  "sensor.acurite_986_1r_10244_temperature",
  "sensor.acurite_986_1r_10244_battery",
  "sensor.acurite_986_1r_10244_rssi",
  "sensor.acurite_986_1r_10244_temperature",
  "sensor.acurite_986_1r_10244_battery",
  "sensor.acurite_986_1r_10244_rssi",
  "sensor.acurite_986_1r_10244_temperature",
  "sensor.acurite_986_1r_10244_battery",
  "sensor.acurite_986_1r_10244_rssi"
]

Is it, by any chance, possible to get all acurite entities? I have tried this, but that only returns TypeError: 'in <string>' requires string as left operand, not NoneType. I then tried different things (asterisk, ~, …), but nothing will list all acurite entities.

{% set devices = states | map(attribute='entity_id')
| map('device_id') | reject('none') | list %}
{{ zip(devices, devices | map('device_attr', 'model'))
| selectattr(1, 'in', 'Acurite') | map(attribute=0)
| map('device_entities') | sum(start=[]) }}

Thing is, acurite is only one of the devices / entities I want to get rid of. So while this is already going in the right direction, it would still require quite some manual work to get everything purged that needs to be gone.

If you don’t use MQTT any longer, then obviously remove the integration which should remove all of its devices and entities along with it.

I do use MQTT. But I was hoping that removing, then re-adding the integration and/or re-installing the add-on might do what I am trying to do manually - but on it’s own. According to your answer, this should work. I’ll remove integration and add-on (both just to be safe), then set them up again. And then all these devices (as well as the appropriate entities) should be gone, but everything will automatically re-populate, right?

Of course, if you can define how to find them. Do all the model names begin with Acurite? Or what is their manufacturer string, which ought to be the same for all of them?

You cannot use an in test with strings that way. It would check if the string on the “left” is a substring of the string on the “right”, the exact opposite. Instead use a match test which uses the regular expression engine for the comparison.

selectattr(1, 'match', 'Acurite')

Or as already mentioned, if the devices all have the exact same manufacturer string (which may also be “Acurite”?), you can extract that instead of the model name and compare against.

map('device_attr', 'manufacturer')
1 Like

Thank you!

Okay, I remove those entities. Result: device still there, just without entities. Which is what I initially wanted, but now that you’ve mentioned it, I would also like to get rid of the devices themselves.

I will try creating a backup, then getting rid of all MQTT stuff, then re-installing it and see what happens.

Not sure how safe it is, but you could try to edit the config/.storage/core.device_registry file and remove the offending devices (or move them to the deleted_devices section). It is just a JSON formatted text file. Make a backup of the file first though!

The devices are tied to the MQTT integration.
You would have to disable the MQTT integration, then restart HA and then remove the devices, which should be orphaned after the restart.