REST API, 1 wire temp sensors and Unique IDs

Hope this is the right place for this. Crossposted from the Discord.

Hi folks,

I’ve got a little script on my raspberry pi pulling temperature off a ds18b20 sensor and sending it to Home Assistant.

Home Assistant tells me the entity doesn’t have a unique ID. Is this a problem? Is there any functionality I am missing by leaving this temperature sensor without a unique ID?

ds18b20’s have a unique number associated with them. Could I modify my python script to include this unique ID? How?

Current code:

# Open the file in read mode

file = open(“/sys/bus/w1/devices/28-3c01d075a93e/temperature”, “r”)

# Read the entire content of the file

datacloset_raw = file.read()

datacloset_final = round(int(datacloset_raw) / 1000, 1)

# Print the content

print(datacloset_final)

# Close the file

file.close()

url = “http://my_ha_ip:8123/api/states/sensor.datacloset_temperature”

headers = {“Authorization”: “Bearer MY_TOKEN_HERE”, “content-type”: “application/json”}

data = {“state”: datacloset_final, “attributes”: {“unit_of_measurement”: “°C”}}

response = post(url, headers=headers, json=data)

print(response.text)

Not really. You’ll miss some functionality like the ability to edit the entity through the UI, though.

You don’t say how the value is pushed in HA, but there will be a way to specify an unique ID at that level.

Thanks for this, it prompted me to edit my original post because I think the way I’m pushing the value was hidden in a mess of bad formatting. I’ve fixed that now.

I think I can include the unique ID in the headers or data section, but looking for guidance on how achieve that.

It’s not documented that you can set a unique_id through the REST api, but it doesn’t hurt to try…

1 Like

So, you cannot set the unique id in the api call. Only states, attributes and force_update are accepted data keys. Link to code below…

In order to have a unique id, you would need to define a template entity (other ways are also valid but assuming no integration for this) that you are updating via the rest api. Your unique id would be set in the template definition.

In terms of your question, i would say no you are not missing much without a unique id for a simple temp sensor like this.

2 Likes

Missing things without unique id are:

  • Being able to assign a sensor to an area (room). They will not appear on area cards or summaries
  • Being able to change human-readable name