How to parse mqtt json object in configuration.yaml

I’m capturing some data from a lorawan MQTT broker generating JSON that looks like:

"uplink_message": { 
"session_key_id": "AX83GUHX/GsNQk83g8lvpg==",
"f_port": 1,
"f_cnt": 199,
"frm_payload": "Xm4rdQ==",
"decoded_payload": {
     "degreesC": 19.8974609375,
     "humidity": 32.2998046875
     }
}

Now I want to parse the temp and humidity data using templates in the configuration.yaml. I’ve tried the following, but I get ‘unknown values’:

- platform: mqtt
    state_topic: "v3/mqtt-rpi3b@ttn/devices/eui-70b3d57ed004d338/#"
    name: "Temp DS"
    value_template: '{{ value_json.uplink_message.decoded_payload.degreesC }}'
    unit_of_measurement: "°C"
  - platform: mqtt
    state_topic: "v3/mqtt mqtt-rpi3b@ttn/devices/eui-70b3d57ed004d338/#"
    name: "Hum_DS"
    value_template: '{{ value_json.uplink_message.decoded_payload.humidity }}'
    unit_of_measurement: "%"    

The state_topic is correct, so the value_template must be incorrect. Can someone help me with a template that would work?

A double thank you if you can show me a node-red function that would also work.

    value_template: >
	  {% if value_json is defined and value_json.uplink_message.decoded_payload.session_key_id == AX83GUHX/GsNQk83g8lvpg %}
	    {{ value_json.uplink_message.decoded_payload.degreesC }}
      {% else %}
        {{ states('sensor.temp_ds') }}
      {% endif %

Hi
Try this in developer tools template

I simplified it a little but still shows ‘unknown’ in developer tools template

{## Imitate available variables: ##}
{% set my_test_json = {
  "uplink_message.session_key_id": "AX83GUHX", 
  "uplink_message.decoded_payload.degreesC": 25,
} %}

value_template: >

	  {% if value_json is defined and value_json.uplink_message.session_key_id == "AX83GUHX" %}
	    {{ value_json.uplink_message.decoded_payload.degreesC }}
      {% else %}
        {{ states('sensor.temp_ds') }}
      {% endif %}

Sorry of course my template won’t work in the tool . The tool has no idea where the json came from in the mqtt state topic.

One can do it like this:

{% set value_json = {
                      "uplink_message": { 
                        "session_key_id": "AX83GUHX/GsNQk83g8lvpg==",
                        "f_port": 1,
                        "f_cnt": 199,
                        "frm_payload": "Xm4rdQ==",
                        "decoded_payload": {
                          "degreesC": 19.8974609375,
                          "humidity": 32.2998046875
                        }
                      }
                    }
%}

{{ value_json.uplink_message.decoded_payload.session_key_id }}

{% if value_json is defined and value_json.uplink_message.session_key_id == 'AX83GUHX/GsNQk83g8lvpg==' %}
  {{ value_json.uplink_message.decoded_payload.degreesC }}
{% else %}
  'something else'
  {# uncomment the line below and remove the one above #}
  {# {{ states('sensor.temp_ds') }} #}
{% endif %}
1 Like

Thank you, that works!

Cool, you can mark my answer as the solution, which will help others find it more easily, if you’d like.