Simple MQTT Sensor question

Hi, I thought I had a simple MQTT sensor setup. This is what i’m receiving on a mqtt topic:

{“tags”:{“type”:“air”,“id”:“Woonkamer2 Lucht”},“values”:{“temp”:26.13,“hum”:56.76,“co2”:448,“pm10”:10,“pm25”:9}}
and this is the value_template i’m using:
value_template: ‘{{ value_json.values.co2 }}’

The result I get is empty. any suggestions?

Hi,

When I validated your json + jsonPath, it validated and worked fine on the site: http://jsonpath.com

Since I only have binary mqtt sensors, you made me curious, how this should be setup in HA, so I build a test sensor.

- platform: mqtt
  state_topic: 'test/sensor/'
  name: 'testSensor'
  unit_of_measurement: 'co2'
  value_template: '{{ value_json.values.co2 }}'

When I posted your json to my mosquito, it also didn’t work.
Then I tried replacing ‘values’ with ‘values2’ and it worked :slight_smile:

  value_template: '{{ value_json.values2.co2 }}'

I can’t find it in the documentation, but it looks like the ‘values’ string is used by the json parser, one way or another.

I hope this also workes for you!

Greets and happy automation!

Great investigation! I tried it by hand with values2 and it seemed to work. The sad news is that it is almost impossible for me to change that in my my ‘production’ setup. I was looking for more documentation on this issue, but haven’t found it

you say “almost” impossible, so it should be possible :slight_smile:

Why is it impossible to change it in production?

I have many adapters/micro-services (>10) that use this format in multiple languages: multiple sensors, automatic mqtt->influxdb storage, custom dashboard, hololens app, etc. I could clone all messages on a new topic and do a small rewrite, but it seems quite silly. I expect there is a better solution - my experience with HA is minimal - or at a good explanation+doc. on this issue.

The solution mentioned in your issue works for me test! :smiley:
https://github.com/home-assistant/home-assistant/issues/7821

Case closed :slight_smile: