REST API: entity behavior questions

Hi,

I’m sending some values to HA through REST API like so:

curl -X POST --header "Content-Type: application/json" --header "Authorization: Bearer XXXXXXXXXXX" -d '{"state": "10", "attributes": {"unit_of_measurement": "°C"}}' "http://x.x.x.x:8123/api/states/sensor.test"

After sending the first value, I went to Settings → Customizations and added a friendly_name to the newly created “test” entity. The entity now has the customized name displayed in the UI.

I then update the value with the same curl command. Now the friendly_name is no longer displayed in the UI even though the customization setting still exists.

  • Is it expected behavior that the friendly_name attribute is not honored after the entity receives a new value through REST (which does not assign a friendly_name attribute)?

I also noticed that the entities created through REST API disappear from the UI after a core restart. They reappear (along with their history) when the entity receives a new value.

  • Why do the entities disappear after core restart (until they receive a new value)? Can I change this behavior?

I also tried to create a unique ID for the entity like so:

curl -X POST --header "Content-Type: application/json" --header "Authorization: Bearer XXXXXXXXXXX" -d '{"state": "10", "attributes": {"unique_id": "test1234"}}' "http://x.x.x.x:8123/api/states/sensor.test"

The UI however still says that the entity does not have a unique id. Unique_id shows up as a normal attribute.

  • Is it not possible to assign a unique ID through the REST API?

Thanks.

1 Like

Yes, you’d need to send the friendly name in your rest call.

They disappear because they don’t get a value from anywhere when HA restarts. I don’t think you can change this behaviour.

Personally I’d use MQTT as a means to communicate with HA, this way you could send messages with retained flag and your entities would reappear automatically on HA restart. Also the problem with the friendly name wouldn’t exist.

Thank you for the clarification. I will certainly keep the MQTT approach in mind. For now it’s simpler to use curl since there’s zero overhead and nothing to set up on either end.

The UI however still says that the entity does not have a unique id. Unique_id shows up as a normal attribute.

  • Is it not possible to assign a unique ID through the REST API?

I am also trying to figure out setting a unique_id via the REST API, has anyone had any luck?