HA.core REST API POST unique_id for entities?

I am currently trying to create/update entities using the REST API (POST), which works in principle. But these entities don’t have a unique ID, I tried to set the unique ID in the payload JSON but apparently this value is ignored.

Furthermore it is not possible to delete/alter these entities via the interface because they don’t have a unique ID, but since there is no entry in YAML for this either, I don’t know how I can delete these entities?

2 Likes

I stumbled upon exactly the same thing. I was playing around with the REST API, deciding whether to use MQTT or native REST API for a certain DIY project, and was failing to achieve an entity creation with unique_id.

Did you succeed? I found this thread through searching this problem, and maybe a canonical response may benefit future users --even an “it’s not possible”, as I don’t know if I’m not looking properly at the documentation or if that happens by design.

No I didn’t find any solution for this …

This work for me. Put it in configuration.yaml or where appropriated.

template:
  - sensor:
    - name: your_sensor_name
      unique_id: your_sensor_name
      state: ''

Then the REST API POST will update state as usual. And you can customize this entity through UI.

5 Likes

It would be great to get some sort of official response, we are not asking for something complex :wink:

Can we or can we not define the unique_id directly from the REST API?

No you can’t.
You should see rest API as update only.

Just create your sensor via a template first, you can even do this via the UI now:
Settings-> devices&services-> helpers

The next api call will update that sensor:
/api/states/sensor.shared_energy

This can only be described as clunky for addons that want to add sensors to HA, as I developer of a number of addons relying on the end user to finish the install is sub optimal…

1 Like

I bumped into this same issue when I set up my sensor arrangement POST their data to HA. The script performing the POST to /api/states/ processes the sensor data and generates all the necessary attributes as well, including unique_id but indeed that one is not respected by HA, so I cannot edit those sensors in UI.

As the sensors are algoritmically created by the POST script “on the fly” I cannot be bothered to manually create a template sensor for each of them.

1 Like

Seconded, same issue here. HA does not respect unique_id set in post request payload. This sucks a lot because I have like 60 sensors and creating empty template sensors in ui is… problematic at least.

1 Like

Worse, if you already created the entity by POST-ing via the REST API, you can’t delete or re-use it. You have to create a template with a different ID and use the state template to update from the older one. Clunky and tedious.

This is better than using the UI as it allows to convert an existing entity into a template while keeping the same ID.