Rest sensor: State max length is 255 characters

Hello,

I used to monitor my AtagOne thermostat with the rest sensor.

  • platform: rest
    name: atagone
    resource: http://10.0.0.204:10000
    method: POST
    payload: ‘{“retrieve_message”:{“seqnr”:0,“account_auth”:{“user_account”:"",“mac_address”:“6C-F0-49-50-DD-0E”},“info”:15}}’

Since i updated to 57.1 its just says:

homeassistant.exceptions.InvalidStateError: Invalid state encountered for entity id: sensor.atagone. State max length is 255 characters.

Is there any workaround for that?

1 Like

I have the same problem with a file sensor.

From the breaking changes:

Fix recorder crash for long state string - enforce a maximum state of 255 characters at core level (@milanvo - #9696) (breaking change)

I think this is too small and there are other sensors that send bigger values.

BTW, the rest sensor has a value_template, so maybe you can extract your value from the returned json.

Thanks for your suggestion. But i use this sensor to extract a lot of information from my atagone thermostat. And 255 characters is much too small. I think i have to implement another solution. Just now i am thinking of an own rest script that reads the atagone and splits the information in a few files that i can read with the file sensor.

1 Like

Would be easier to create a custom rest sensor that split the data into attributes of that sensor. Take a look at the pvoutput sensor platform that one is doing this.

The idea of a custom rest-sensor looks great. The pvoutput sensor platform example gives me enough knowledge to implement my own. I will try to implement that.

At the moment i am developing my custom rest sensor. Is there a way to test the code without restarting hass every time i change the code while testing?

There’s also an open PR about this. Just for info!

That PR doesn’t fix the issue… I have created a 3 line patch in a new PR (https://github.com/home-assistant/home-assistant/pull/21556), that does actually fix this issue… It discards non JSON_attributes field values and truncates value at 255 chars while still allowing all attributes to be associated with the state.

Did you patch got added to HASS? I run the latest version and now I have this issue too.

No. In the PR’s last comment, the PR was closed without merging it.

OK, thanks.
So what is the correct way to deal with it, I don´t even know which sensor it is that triggers the error

You can’t store more than 255 characters in an entity’s state property.

However, you can store more than 255 characters in an entity’s attribute. Use the json_attributes option to put the received data into one or more attributes. Here’s an example of how to do that:

A workaround for this I use, aligned with @123 above, is using a dummy sensor value and extracting the useful values via json attributes, like so:

  - platform: rest
    name: EC2 Tunnel and Public API
    resource: http://localhost:9002
    value_template: '1'  # dummy value, not used; avoids the "State max length is 255 characters" error
    json_attributes:
      - ec2TunnelState
      - publicApiState
    scan_interval: 120
2 Likes

Thats a nice workaround and good to know. When my Custom Rest sensor breaks in the future i may implement this workaround.

My apologies. I didn’t mean to cause any problems. I’ll delete my duplicated posts now.

Hello. I am hoping you can help me on this one. I’ve created a template sensor grabbing data from a local calendar event which is over 255 char. The initial creation of the sensor went fine. When it tried to trigger, it has an error in log saying max char 255. Ok fine. So, I have deleted the sensor and the event day that had char over 255.

When I tried to create the same sensor using the same name, I kept getting this char 255 error and the sensor is not visible when I check in developer tools. I can create using a different name, but I wish to continue using that same name. I have tried removing the sensor in core.restore_state and homeassistant.exposed_entities; It keeps returning after a restart of ha and or re creating with the same name in core.restore_state.

    {
      "state": {
        "entity_id": "sensor.units_arriving",
        "state": "none",
        "attributes": {
          "friendly_name": "Units Arriving"
        },
        "last_changed": "2023-06-08T02:32:14.144551+00:00",
        "last_updated": "2023-06-08T02:32:14.144551+00:00",
        "context": {
          "id": "01H2CDHW2014NZSPHK0KD4P00Z",
          "parent_id": null,
          "user_id": null
        }
      },
      "extra_data": {
        "native_value": "Highway: x1 Container [Evergreen, Green Celebrity],\nMIB: x1 Container [Evergreen Green Celebrity],\nEM: x1 Container [Evergreen, Wan Hai 266],\nAndrew: x2 Containers [Evergreen, Wan Hai 266],\nMEL: x1 Container [Evergreen, Wan Hai 266],\nLY: x3 Containers [Evergreen, Wan Hai 266],\nPaolo x2 Containers [Evergreen, Green Celebrity, Wan Hai 266]",
        "native_unit_of_measurement": null
      },
      "last_seen": "2023-06-08T21:15:16.390805+00:00"
    },

I don’t have any experience with the situation you have described.

So to answer my question. After a few days, that entity was cleared from HA. I can now create using that name again.