Problem writing sensor values via REST API

I created a sensor via YAML

template:
   sensor:
       - name: "T_Wood1"
         unique_id: t_Wood1_2
         unit_of_measurement: °C
         device_class: temperature
         state: "0"

I try to set the state via the REST API:

http://xxxxx:8123/api/states/sensor.t_wood1
requests.post(API_URL, headers=headers, json=datasend)
with datasent is “state: 59.5”

whatever I try the REST API doesn’t use the existing entity created through the template but creates another entity.
like in the image:

How can I avoid this?

Not sure if it’s the actual issue, but in your screenshot, the “real” sensor is sensor.t_wood1_3

Delete your template config, and create the entity via the API.

{"state": "59.5",
 "attributes": {"unit_of_measurement": "°C",
                "device_class": "temperature", 
                "name": "T_Wood1"}
}

although you don’t need to set the attributes every time.

image

1 Like

Thanks that worked seems to work.

I tried to avoid to make the request more complicated and wanted to use a templated sensor.

Is there a more elegant solution to push some measurement Values from an old Windows Machinte into a Home Assistant entity? (first time for me writing a python scriptlet)

If you can make the Windows machine respond to requests rather than push values, you could set up a RESTful sensor.

The way you’re trying to do it is at odds with the design of HA. Sensors are not designed to be overwritten with external “push” values, except for special cases like via the REST API or specific “push” integrations. Template sensors are intended to render their template. If you push a new value, it conflicts with the pre-set template (in your example, "0").