Large payload in MQTT sensor

Dear All,
I am trying to display a list of dhcp clients from my router. The list of clients is coming in an MQTT message in a payload in the following format (this is an actual sample, made shorter):

[
  {
    "field1": "1648269597",
    "field2": "50:02:91:65:ac:a8",
    "field3": "192.168.1.103",
    "field4": "officedoor-3240",
    "field5": "*"
  },
  {
    "field1": "1648256271",
    "field2": "00:24:8c:66:7f:54",
    "field3": "192.168.1.222",
    "field4": "test",
    "field5": "01:00:24:8c:66:7f:54"
  },
  {
    "field1": "1648259323",
    "field2": "b8:27:eb:cf:ed:a6",
    "field3": "192.168.1.3",
    "field4": "pihole",
    "field5": "01:b8:27:eb:cf:ed:a6"
  },
  {
    "field1": "1648257943",
    "field2": "e0:cb:4e:b5:13:2c",
    "field3": "192.168.1.99",
    "field4": "homeassistant",
    "field5": "01:e0:cb:4e:b5:13:2c"
  },
  {
    "field1": "1648268067",
    "field2": "50:02:91:e0:a9:a3",
    "field3": "192.168.1.113",
    "field4": "vNode-E0A9A3",
    "field5": "*"
  }
]

Unfortunately, the payload is larger than 255 characters, so I get the following error message in HA:

Logger: homeassistant.util.logging
Source: util/logging.py:114
First occurred: 5:58:02 PM (1 occurrences)
Last logged: 5:58:02 PM

Exception in message_received when handling msg on 'viktak/network/dhcp':[payload here] 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 284, in message_received self.async_write_ha_state() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 553, in async_write_ha_state self._async_write_ha_state() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 681, in _async_write_ha_state self.hass.states.async_set( File "/usr/src/homeassistant/homeassistant/core.py", line 1363, in async_set state = State( File "/usr/src/homeassistant/homeassistant/core.py", line 1049, in __init__ raise InvalidStateError( homeassistant.exceptions.InvalidStateError: Invalid state encountered for entity ID: sensor.hostnames. State max length is 255 characters.

I wonder how one would go about it. I can’t create a sensor for each device as they keep changing in number.

Is there a way to work around this limitation?

Thanks for any pointers!

Store it as an attribute of the MQTT Sensor instead of its state value. An attribute is not limited to storing a maximum of 255 characters and can also store data as a dictionary, list, float, etc not only as a string.

Refer to JSON Attributes Template Configuration.

2 Likes

Awesome, and I didn’t think about it… Just assumed attributes would have the same limitation…

Thank you so much!!!

Hi does anyone have a example like my example does not even come close to working but should show what I’m trying to learn about.

create a sensor in YAML with a string attribute, then use the service: mqtt.publish to put data in that sensor.

Thanks in advance.

mqtt:
  sensor:
  - name: "Jarvis Last Msg3"
    state_topic: "house/polly/lastmsg3"
    value_template: "{{ value_json.other }}"
    json_attributes_topic: "house/polly/lastmsg3"
    json_attributes_template: "{{ value_json.other | tojson}}"

and

  - service: mqtt.publish
    data:
      topic: "house/polly/lastmsg3"
      retain: true
      qos: "0"
      payload: >-
        {  "lastmsg3": {{states('sensor.jarvis.last.msg3') string }}, 
        }