Remove test entities

I tried to send sensor values to home-assistant by posting them to the REST API endpoint /api/states/<entity_id>

That works well so far, only that the automatically created entity doesn’t have a unique ID (as described here: This entity does not have a unique ID? - Home Assistant).

Now I want two things:

  1. Clean up the test entities. It is not possible to delete them through the UI because of the missing unique ID.
  2. Does someone have a hint how I can set/refer to a unique ID for the next time?

For reference, this is how I recorded the sensor data:

❯ http -j -A bearer -a $TOKEN POST home-assistant:8123/api/states/sensor.last_backup_test 'attributes[unit_of_measurement]=m' entity_id='my_cool_sensor123' state=32
HTTP/1.1 200 OK
Content-Encoding: deflate
Content-Length: 218
Content-Type: application/json
Date: Thu, 29 Jun 2023 10:06:48 GMT
Location: /api/states/sensor.last_backup_test
Server: Python/3.11 aiohttp/3.8.4

{
    "attributes": {
        "unit_of_measurement": "m"
    },
    "context": {
        "id": "01H439X9RR06X6RR9TVKGF3CGP",
        "parent_id": null,
        "user_id": "fb269c80f987456190f75b97ca16f012"
    },
    "entity_id": "sensor.last_backup_test",
    "last_changed": "2023-06-29T10:06:48.088732+00:00",
    "last_updated": "2023-06-29T10:06:48.088732+00:00",
    "state": "32"
}

The way you can clean them up is by directly editing the entity registry in the .storage directory. Back it up first as if this file gets corrupted by editing it improperly HA will not start.

The API endpoint should allow you to overwrite the state of any entity. So create a template sensor give it a default value as the state (like 0j and a unique id.

Thanks for the reply, I directly tested it. I can remember that I did something similar as you describe in the past with some homematic devices.

But here, the sensor sensor.last_backup_test is not in .storage/core.entity_registry

Only in the file .storage/homeassistant.exposed_entities I found the following entry:

      "sensor.last_backup_test": {
        "assistants": {
          "conversation": {
            "should_expose": false
          }
        }
      }

That’s the only place in .storage where the name appears.

The states that I recorded for the entity are also in MariaDB. It would be best if it was possible to clean up completely.

Ok, to answer a part of my own question:
For cleaning an entity up from the database, the service purge_entities is the solution. I called it from the UI and the states were gone from the DB.
The entity remained visible in the UI, e.g. in /developer-tools/state, but only until I restarted Home assistant. Afterwards it was completely gone.

1 Like