Problem in parsing mqtt topic

Hi,

I made a search about my problem but I didn't find a solution because I'm a noob....
I have one sensor that publishes (trough a Lorawan gateway) an mqtt message like this :

{
  "applicationID": 15,
  "data": {
    "err": 0,
    "messages": [
      {
        "measurementId": 4099,
        "measurementValue": 146,
        "type": "report_telemetry"
      }
    ],
    "payload": "010310503A02009A34",
    "valid": true
  },
  "devEUI": "2cf7f1c0737000ca",
  "deviceName": "luce-esterna"
}

and I created one sensor in Home Assistant in this way

mqtt:
  sensor:
    - name: "luce esterna"
      state_topic: "milesight/luce"
      value_template: "{{ value_json.measurementValue }}"
      qos: 0
      unit_of_measurement: "Lux"

but this sensor gives no value.

What is wrong ?

Use:

value_template: "{{ value_json['data']['messages'][0]['measurementValue'] }}"

Try it out in the Template Editor:

The value you want is nested inside:

  • the data dictionary, then
  • the messages list,
  • in the first (index 0) item,
  • under the measurementValue key.

Also, Lux is usually abbreviated as lx.

1 Like

@Troon It works, thank You very much for the solution and also the explanation (so I understand also the concepts)

1 Like

This might help you in the future as well.
Troubleshooting MQTT? MQTT-Explorer can help you be successful!
If you have Apps available, try adding this App repository to your Apps list. GitHub - GollumDom/addon-repository · GitHub.
With this you can see that the broker sees and likely find the bug.