Accessing template.sensor via API without removing all properties

Hello, I am a new user and have some problems creating a temperature sensor that I want to update via an API written in VB.

I have the following:
Sensor definition

 sensor:
    - name: "dupline_a1_temperature"
      unique_id: dupline_a1
      device_class: temperature
      icon: mdi:thermometer
      state: "{{ states('min_dupline_a1_temperatur', with_unit=True) }}"
      unit_of_measurement: °C

This makes a sensor as expected.

State: unknown
properties :
unit_of_measurement: °C
device_class: temperature
icon: mdi:thermometer
friendly_name: Stuen Temperatur

I am sending this json string from VB:

client.UploadString(url, "POST", jsonData)
# url =		 http://homeassistant.local:8123/api/states/sensor.min_dupline_a1_temperature "{"state":"18.20"}"
# jsonData=  	"{"state":"18.20"}"

The sensor will be updated with new value, but all properties are now deleted?

State: 18.2
properties :

It seems like I’m defining a brand new sensor with no properties, but I can’t figure out what’s going wrong, does anyone have any suggestions?

I’m a bit confused about what your goal is. Your dupline_a1_temperature sensor’s state is just whatever the min_dupline_a1_temperature sensor’s state is. Why do you need two sensors at all if they are the same?

You also didn’t mention which sensor (of those two) you say is updated, but with no properties. If it’s min_dupline_a1_temperature, this is exactly as expected since you didn’t provide any attributes when you created it using the REST API.

Since I don’t know what it is you’re really trying to accomplish, I’m not sure how to suggest a path forward. You could just have the one sensor that you create using the API (no need to declare it in YAML at all). Since the attributes are all static, you can include them in the json data in your VB script.

Finally, fwiw, I wouldn’t consider instantiating sensors via REST API calls to be a very mainstream usage of Home Assistant. Maybe if you describe your use case in more detail, we could suggest a more straightforward approach.

You can’t specify a unit of measurement for a sensor with a non-numeric state, which it will be if you use with_unit=True.

This is the right answer.

It’s perfectly valid — I monitor my church network gear like this.

The gold is the following:

I get a lot of input from various PLCs, Dupline etc…

All values end up in a program written in visual basic, for example a temperature sensor: “termofoler_dupline_a1_temperature”

My wish is to send the values to the Home Assistant where I can present the temperature and perform some automation.

The question is how this can be done, and how I get both values and units sent to Home Assistent, I think a jason string should be sent, I just don’t know the format.

When making your API call from your VB script, just add all of the additional attributes inside of a map called “attributes”, like this:

{
  "state": "18.20",
  "attributes": {
      "device_class": "temperature",
      "icon": "mdi:thermometer",
      "unit_of_measurement": "°C"
  }
}

I didn’t mean to imply that it was wrong or invalid. Just that a self-professed new HA user who landed on this approach might have missed something easier or more accessible. Perhaps that’s not the case here.

1 Like

I have worked quite a bit with HA, but am relatively new to YAML and Json :slightly_smiling_face:

I have tried your suggestion unfortunately I get error “400“, I can’t see what’s wrong.

This works perfectly

Dim client As New System.Net.WebClient()
        client.Headers.Add("Authorization", "Bearer " & token)
        client.Headers.Add("Content-Type", "application/json")
        Dim reply As String = client.UploadString(url, "POST", jsonData)
#jsonData  = {"state":"18.20"}

This doesn’t work: “The remote server returned an error: (400) Bad request”

Dim client As New System.Net.WebClient()
        client.Headers.Add("Authorization", "Bearer " & token)
        client.Headers.Add("Content-Type", "application/json")
        Dim reply As String = client.UploadString(url, "POST", jsonData)
#jsonData  = {"state":"18.20","attributes":{"device_class":"temperature","icon":"mdi:thermometer","unit_of_measurement":"°C"}}

I don’t see the error either. In fact, I copied your json string and combined it with the curl example given in the HA Rest API docs, and it worked.

curl \
   -H "Authorization: Bearer $TOKEN" \
   -H "Content-Type: application/json" \
   -d '{"state":"18.20","attributes":{"device_class":"temperature","icon":"mdi:thermometer","unit_of_measurement":"°C"}}' \
   http://localhost:8123/api/states/sensor.kitchen_temperature

I don’t know VB though. What’s going on with the #jsonData declaration? Is it possible it’s changing the JSON object somehow or maybe doesn’t like nested objects? Also, what URL are you using? Your original post had the JSON object repeated at the end of the URL which was a bit confusing…

The content of the jsonDate, that I show comes directly from debug, so it is exactly what I sends.

URL is http://homeassistant.local:8123/api/states/sensor.min_dupline_a1_temperature

Sorry for the repeated JSON object at the end of the URL in the original post, it was a copy/paste error I had overlooked.

Is there a way I can see what happened in HA ? troubleshooting in HA

I have now found the error as you suggested the error occurred in VB. Many thanks for the help, without your input I wouldn’t know where to troubleshoot.
This is what I was missing in the VB code: client.Encoding = System.Text.Encoding.UTF8

  Dim client As New System.Net.WebClient()
        client.Encoding = System.Text.Encoding.UTF8                     
        client.Headers.Add("Authorization", "Bearer " & token)
        client.Headers.Add("Content-Type", "application/json")
        Dim reply As String = client.UploadString(url, "POST", JsonData)