MQTT and attributes

Hi there,
I’m triyng to configure MQTT sensors and got problems with attributes.
I carefully read documentation to create attributes and extract value, but even the provided examples does not work: when extracting attibute I got message

“WARNING (MainThread) [homeassistant.components.mqtt.mixins] Erroneous JSON: None”

and attribute not shown.

- platform: mqtt
  name: "RSSI"
  state_topic: "home/sensor1/infojson"
  unit_of_measurement: "dBm"
  value_template: "{{ value_json.RSSI }}"
  json_attributes_topic: "home/sensor1/attributes"
  json_attributes_template: "rssi:{{ state_attr('sensor.rssi', 'ClientName') }}"

Node Red flow used to send data

[{"id":"f182e1bb.b82e2","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"b26bcb3.d9d0838","type":"inject","z":"f182e1bb.b82e2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":579,"y":96,"wires":[["bf2f028b.5716a","e50ef35.d428a1"]]},{"id":"bf2f028b.5716a","type":"function","z":"f182e1bb.b82e2","name":"StatePayload","func":"msg.topic=\"home/sensor1/infojson\"\nmsg.payload={\"ClientName\": \"Client\", \"IP\": \"IP\", \"MAC\": \"MAC\", \"RSSI\": \"17\", \"HostName\": \"Host\", \"ConnectedSSID\": \"SSID\"}\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":759,"y":96,"wires":[["77979987.c56388","7860a32b.9414cc"]]},{"id":"77979987.c56388","type":"mqtt out","z":"f182e1bb.b82e2","name":"Send conf for volume","topic":"","qos":"0","retain":"false","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"66e1f27a.12f31c","x":1099,"y":76,"wires":[]},{"id":"e3b60056.3a483","type":"mqtt out","z":"f182e1bb.b82e2","name":"","topic":"","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"66e1f27a.12f31c","x":1019,"y":176,"wires":[]},{"id":"e50ef35.d428a1","type":"function","z":"f182e1bb.b82e2","name":"AttributesPayload","func":"msg.topic=\"home/sensor1/attributes\"\nmsg.payload={\"ClientName\": \"NOM du client\", \"IP\": \"IP\", \"MAC\": \"MAC\", \"RSSI\": \"RSSI\", \"HostName\": \"Host\", \"ConnectedSSID\": \"SSID\"}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":779,"y":176,"wires":[["e3b60056.3a483","cba5637b.5e836"]]},{"id":"7860a32b.9414cc","type":"debug","z":"f182e1bb.b82e2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1069,"y":136,"wires":[]},{"id":"cba5637b.5e836","type":"debug","z":"f182e1bb.b82e2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1069,"y":236,"wires":[]},{"id":"66e1f27a.12f31c","type":"mqtt-broker","name":"MQTT","broker":"192.168.1.32","port":"1883","clientid":"","usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"/douche","birthQos":"0","birthRetain":"false","birthPayload":"{\"Hello\"}","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]

I could create one sensor by attributes but I have about 10th and like to use attributes instead of state, ultimately I’d like doing it using MQTT discovery, but one step after the other, I’d like first understand why it does not work and how is should configure manually.
Any helps appreciated;

The error would imply this topic returns nothing.
Do you confirm the topic is actually filled in? Is it supposed to be filled with the node red data?

“home/sensor1/attributes” returns the following data

{"ClientName":"NOM du client","IP":"IP","MAC":"MAC","RSSI":"RSSI","HostName":"Host","ConnectedSSID":"SSID"}

I found that the following config is OK.

json_attributes_template: '{{ {"Client" : value_json.ClientName} | tojson}}'

Anyway it does not allow to use this as a sensor value. I assume I don’t understant correctly attributes, as they are not aimed to be used as a value but just as an additionnal info.
I’ll define all my sensors and I guess it will be allright.
If one can point me to a ‘decent’ explanation of home assistant concept (state / attribute / entities) , it would be great, for the moment it’s trial & error and time consuming

I am having issues with the attributes here:

- platform: mqtt
  unique_id: lora_netwrok_chirpstack
  name: Chirpstack_Coverage
  state_topic: "application/15/device/ffffff100001977d/event/up"
  value_template: "{{ value_json.rxInfo[0].rssi | int }}"
  json_attributes_topic: "application/15/device/ffffff100001977d/event/up"
  json_attributes_template: >
    { "latitude": {{ value_json["object"]["latitude"] | tojson}},
      "longitude": {{ value_json["object"]["longitude"] | tojson}},
      "gateway": {{ value_json.rxInfo[0].name | tojson}}',
      "accuracy": {{ value_json["object"]["accuracy"] | tojson}}',
      "altitude": {{ value_json["object"]["altitude"] | tojson}}

I am getting the following error:

Logger: homeassistant.components.mqtt.mixins
Source: components/mqtt/mixins.py:341
Integration: MQTT (documentation, issues)
First occurred: 10:35:21 (22 occurrences)
Last logged: 10:41:08

Erroneous JSON: { "latitude": 0, "longitude": 0, "gateway": Laird_IoTCI_HQ', "accuracy": 0', "altitude": 65535

Any thoughts?

Many thanks

I would remove the single quotes present in your json_attributes_template…