Rest API enitiy state is interpreted as string

I have devices that send their values to HA via the HA REST API. This works so far, but the entities don’t have a graph. So someone on reddit said that the state is saved/interpreted as string.
Is this a general problem/flaw of the REST API? Or am I missing something?

image image

Request

POST /api/states/sensor.mydevice HTTP/1.1
Authorization: Bearer xxxxxxxxxxxxxxxxxxxxxxxxxx
Host: xxxxxxxxxxx.ui.nabu.casa
Content-Type: application/json
Content-Length: 88

{
    "state": 3.0,
    "unit_of_measurement": "°C",
    "device_class": "temperature"
}

Response

{
    "entity_id": "sensor.mydevice",
    "state": "3.0",
    "attributes": {},
    "last_changed": "2021-02-16T12:46:52.260955+00:00",
    "last_updated": "2021-02-16T12:46:52.260955+00:00",
    "context": {
        "id": "xxxxxxxxx",
        "parent_id": null,
        "user_id": "xxxxxxxxxxxxx"
    }
}

For better or worse, all states are strings in Home Assistant. I have no idea what the reasoning behind this was, but that’s what we have. It rarely causes issues. You can use | float or | int filters to cast the state as a number when needed in templates.

If you want the state to be graphed on an X-Y plot give it a unit_of_measurement in your restful sensor definition.

FYI, sensor attributes can have other types than just string.

I know that, but I have about 50 of these devices, I’d rather use the generated entities than 50 templates.

If you look at the request, I’m already doing this, but it does not work

You are right, the attributes in other responses are float, int, string …

EDIT:

I just realised you are using the rest API not a restful sensor.

And the documentation is a bit sparse in that area.

Yes I’m “pushing” to HA via REST API not pulling

Try it like this:

{"state": "3.0", "attributes": {"unit_of_measurement": "°C", "device_class": "temperature"}}

:scream: It works! Thanks!

I had never thought of putting that there, but when you look at other entities it makes sense.