Mqtt sensor error "State max length is 255 characters"

Hi There,

I’m trying to add a sensor where the output is an invalid json, so I have to manipulate it before use.

I was able to manipulate the data using the Template on Developer Tools, but doesn’t matter how many characters I’m filtering using regex_replace, however when I try to configure the sensor, I’m receiving the below message from the logs:

2022-07-14 13:03:18 ERROR (MainThread) [homeassistant.util.logging] Exception in message_received when handling msg on 'lollipop/cameraStatus/return': '42["messageReply",{"method":"cameraStatus","id":50,"params":{"appSessionId":"xxxxxxxxxxxx","info":"owner"},"result":{"status":1,"bindSensor":"xx:xx:xx:xx:xx:xx","bindSensorName":"Lollipop-Sensorwwwwr","bindStatus":2,"temp":24.77,"humidity":52.420000000000002,"noise":68.629999999999995,"air":44.0,"wifiSsid":"xxxxxxxx","wifiQuality":73.0,"playingMusic":true,"firmwareVersion":"2021121500-g4c517b2","standby_mode":false,"privacyMode":false}}]'
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/components/mqtt/debug_info.py", line 47, in wrapper
msg_callback(msg)
File "/usr/src/homeassistant/homeassistant/components/mqtt/sensor.py", line 302, in message_received
self.async_write_ha_state()
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 532, in async_write_ha_state
self._async_write_ha_state()
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 646, in _async_write_ha_state
self.hass.states.async_set(
File "/usr/src/homeassistant/homeassistant/core.py", line 1438, in async_set
state = State(
File "/usr/src/homeassistant/homeassistant/core.py", line 1099, in __init__
raise InvalidStateError(
homeassistant.exceptions.InvalidStateError: Invalid state encountered for entity ID: sensor.lollipop_temperature. State max length is 255 characters.

I could understand from the topic Track only json_attributes, truncate value if len > 255 by deftdawg · Pull Request #21556 · home-assistant/core · GitHub the data is discarded, is there any way to avoid it so I can parse my string to json correctly?

kr,
Felipe.

Put the data in an attribute rather than the sensor state. Attributes don’t have the 255 character length limit.

Hi @tom_l , thank you for your reply. I’m pretty new on hass and I’m making my first project, so I’m not familiar with those structures, could you help me to archive it if not complicated?

Currenty this is my setting on configuration.yaml:

mqtt:
  sensor:
    - name: "Lollipop Noise"
      state_topic: "lollipop/liveNote"
      value_template: "{{ value_json.result.noise | round(1) }}"
      device_class: signal_strength
      unit_of_measurement: dB
      icon: mdi:surround-sound
    - name: "Lollipop Temperature"
      state_topic: "lollipop/cameraStatus/return"
      value_template: "{{ (value | regex_replace(find='(42\["messageReply",|\])', replace='') | from_json).result }}"

Lollipop Noise is working perfect!
Lollipop Temperature is the one I’m struggle with.

Thank you!.