MQTT JSON parse is not working

Hej Folks,

I’m using MQTT to fetch my energy meter using a python script. The format is valid in JSON and I’m able to fetch the data within node-red. But I want to implement a template sensor as MQTT directly.

My JSON string

{"time":1555400065567,"values":{"power":351270,"power3":236640,"energyOut":0,"power1":43290,"energy":69505866394000,"power2":71340}}

I want to see only the power1 value:

 - platform: mqtt
    name: "mqtt_power1"
    state_topic: "strom"
    value_template: "{{ value_json.values.power1 }}"

using this, I will not get a value

if I only select for example: value_json.time this is working.

I already tried using:

value_json.values.['power1']
value_json.values.[0]
value_json.values['power1']
value_json.values[0]

without success

Try value_json.values[0].power1

Hej David,

its not working

  - platform: mqtt
    name: "mqtt_power1"
    state_topic: "strom"
    unit_of_measurement: "W"
    value_template: "{{ value_json.values[0].power1 }}"

it gives “unknown” as state. I wonder if this could be related as problem with the name “values” in the json string?

I was just taking it from a bash script I was/am using to extract data a few levels deep from.
value_json.values.power1[0] perhaps?

Please format your post as per the banner at the top of the page (code and JSON data). Use the preformatted text button not the quote button.

Pasting your JSON in an online interpreter results in errors because you have used fancy quotes not ".

Hopefully this is because of your post formatting and is not in the actual data.

value_json.values[‘power1’]

Make sure the single quotes are right - when I pasted your json into a parser the double quotes were all wrong. I got the above out of a json and template sensor I use that works (json same level as yours)

this I tried too, but no success. But there is no array. in node-red

msg.payload.values.power1 is directly working so I wonder why not in yaml / mqtt

its just due to html format, sorry

just check my edit… I have a working template from json that uses that exact format and it works

Hej David,

can you paste in your JSON string and the template sensor?

{
  "channel": "stable",
  "supervisor": "160",
  "homeassistant": {
    "default": "0.91.4",
    "qemux86": "0.91.4",
    "qemux86-64": "0.91.4",
    "qemuarm": "0.91.4",
    "qemuarm-64": "0.91.4",
    "intel-nuc": "0.91.4",
    "raspberrypi": "0.91.4",
    "raspberrypi2": "0.91.4",
    "raspberrypi3": "0.91.4",
    "raspberrypi3-64": "0.91.4",
    "tinker": "0.91.4",
    "odroid-c2": "0.91.4",
    "odroid-xu": "0.91.4",
    "orangepi-prime": "0.91.4"
  },
  "hassos": {
    "ova": "2.11",
    "rpi": "2.11",
    "rpi0-w": "2.11",
    "rpi2": "2.11",
    "rpi3": "2.11",
    "rpi3-64": "2.11",
    "tinker": "2.11",
    "odroid-c2": "2.11",
    "odroid-xu4": "2.11",
    "opi-prime": "2.11",
    "intel-nuc": "2.11"
  },
  "hassos-cli": "9"
}

from here: https://s3.amazonaws.com/hassio-version/stable.json

sensor:
  - platform: rest
    resource: https://s3.amazonaws.com/hassio-version/stable.json
    name: Hass Current Stable Version
    scan_interval: 7200
    value_template: "{{ value_json.homeassistant['qemux86-64'] }}"

Again the forum seems to screw up the quotes.

Your formatted json is

{  
   "time":1555400065567,
   "values":{  
      "power":351270,
      "power3":236640,
      "energyOut":0,
      "power1":43290,
      "energy":69505866394000,
      "power2":71340
   }
}

Hej,

still it doesn’t work.
current empty values, but time gets the right one:

configuration:

and here the MQTT

Hej Folks,

I do know the reason. what is still not really nice. But maybe there is a fix?!

I can not use “values” in JSON! if I use something different, it will work!

working:

not working

use this:

{{ value_json['values']['power1'] }}