SOLVED: Using template to parse JSON from MQTT

Hi there,

I’m now trying for hours to get that up and running and now complete lost any idea.

Here is the problem:

I’m sending via MQTT the following JSON payload:
{ "id": "01", "name": "WeatherStation", "sensors": [ { "id": "01", "type": "HUD21", "temperature": 26.09, "airPressure": 101573.00, "humidity": 12.09, "lightLevel": 45145.0, "batteryCharge": 12.09, "batteryVoltage": 12.12 }, { "id": "02", "type": "DHT21", "temperature": 26.09, "humidity": 56.73, "heatIndex": 34.22, "dewPointDHT": 34.22 } ] }

The message arrives in HA and I receive a number of error messages, that “Error parsing value: ‘value_json’ is undefined”.

56

However, if I use the template test configurator everything works fine. See picture below.

I tested various ideas in my configuration.yaml but nothing seems to work.
Here three different ideas:

  • platform: mqtt
    name: “WeatherStation - Humidity - DHT”
    state_topic: “WeatherStation/data”
    unit_of_measurement: ‘%’
    value_template: ‘{{ value_json.sensors.1.humidity }}’

  • platform: mqtt
    name: “WeatherStation - Humidity - DHT”
    state_topic: “WeatherStation/data”
    unit_of_measurement: ‘%’
    value_template: ‘{{ states.sensors.1.humidity }}’

  • platform: mqtt
    name: “WeatherStation - HeatIndex - DHT”
    state_topic: “WeatherStation/data”
    unit_of_measurement: ‘HI’
    value_template: ‘{{ states.sensor.sensors.1.heatIndex }}’

  • platform: mqtt
    name: “WeatherStation - Dew Point - DHT”
    state_topic: “WeatherStation/data”
    unit_of_measurement: ‘°C’
    value_template: “{{ value_json.state }}”
    payload_available: “online”
    payload_not_available: “offline”
    json_template_attributes:
    id: “{{ state.sensor.sensors.0.temperature }}”

Thanks for any hint!
Cheers
Stefan

Try this {{ value_json.sensors[0].humidity }}

It brings the same error: “Error parsing value: ‘value_json’ is undefined”.

Do I have to define value_json anywhere or register it for MQTT JSON? When I use the template tester the first line is “Imitate available variables: {% set value_json = …” then I test my template again “value_json”. But how is it defined without using the template tester.

Even with the simplest JSON it is not working (no nesting JSON). It is strange.

It is defined automatically within the the bowels of HA. You don’t have to do anything except refer to it in your value_template. I believe the full example should be

  platform: mqtt
  name: “WeatherStation - Humidity - DHT”
  state_topic: “WeatherStation/data”
  unit_of_measurement: ‘%’
  value_template: {{ value_json.sensors[0].humidity }}

Try removing all the other references to this topic - they may be causing the error inadvertently.

Also, the name may cause problems in the future. Try to keep the name to simple lower case ascii.

Thank you very much gpbenton for your guidance. It works now, as you proposed. Seems like using several variants in the config file crashed the “value_json”. Even the naming with “-” seems like problematic.

Thanks

1 Like