I have just added several new MQTT sensors from my newly (almost completed) LoRaWAN setup (Chirpstack). I already have quite a number of working MQTT sensors, thought this is the firsts using JSON data. I am currently outputting test data. The Chirpstack AS outputs the sensor data in JSON as follows (I’ve removed some sensitive stuff):
application/2/device/xxxxxxxxxxxxxxxx/event/up {"applicationID":"2","applicationName":"TestApp","deviceName":"TestDev","devEUI":"xxxxxxxx","rxInfo":[{"gatewayID":"xxxxxxxxxxxxxxxx","uplinkID":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx","name":"xxxxxxxxxxx","time":"2021-05-12T01:54:47.316856Z","rssi":-46,"loRaSNR":11.5,"location":{"latitude":-xx.xxxxxxxxx,"longitude":-xx.xxxxxxxxxxxx,"altitude":xxx}}],"txInfo":{"frequency":915200000,"dr":2},"adr":true,"fCnt":21,"fPort":1,"data":"AWcA4QIB/wMByA==","object":{"digitalOutput":{"2":255,"3":200},"temperatureSensor":{"1":22.5}}}
The values that I am using are in the “object” field. I have confirmed that the correct JSON value (for temp sensor) is x.object.temperatureSensor.1 (from jsonpathfinder - just in case I stuffed up) and have set up the sensors as follows:
- platform: mqtt
name: "Soil Moisture Test Temp"
state_topic: "application/2/device/xxxxxxxxxxxxxxxx/event/up"
qos: 1
value_template: '{{ value_json.object.temperatureSensor.1 }}'
unit_of_measurement: '°C'
- platform: mqtt
name: "Soil Moisture Test WM1"
state_topic: "application/2/device/xxxxxxxxxxxxxxxx/event/up"
qos: 1
value_template: '{{ value_json.object.digitalOutput.2 }}'
unit_of_measurement: 'cB'
- platform: mqtt
name: "Soil Moisture Test WM2"
state_topic: "application/2/device/xxxxxxxxxxxxxxxx/event/up"
qos: 1
value_template: '{{ value_json.object.digitalOutput.3 }}'
unit_of_measurement: 'cB'
However, for each of the sensors I get the following errors:
WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: dict object has no element 1 when rendering '{{ value_json.temperatureSensor.1 }}'
WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: dict object has no element 2 when rendering '{{ value_json.object.digitalOutput.2 }}'
WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: dict object has no element 3 when rendering '{{ value_json.object.digitalOutput.3 }}'
It is getting the messages each time as the number of occurrences of the error are about equal to the received MQTT topic entries and the values are set as zero.
When I removed the “.1” from the temp template (so see what would happen), then I did not get the error for that entry, but the value (for temp) was:
{'1': 22.5}
This is clearly a JSON pair with the correct info (though I am a bit sus of the ’ instead of ").
I am at complete loss as to the source of the problem (I hope I’ve just stuffed up the template) and obviously how to correct it.
I’m running HA core-2021.5.2 under Home Assistant OS 5.13 on Rpi