Extracting two values from a JSON message

I have an MQTT feed from my solar inverter which publishes a pair of values on the topic “solar/output” every five minutes, in the format such as this:
{"power" : "4.562", "output" : "2.011"}

I want to extract both values into separate sensor states, “solar.power” and “solar.output”. This is what I have in my configuration file:

sensor:
    - platform: mqtt
      state_topic: "solar/output"
      name: "Solar power"
      unit_of_measurement: "kWh"
      value_template: '{{ value_json.power }}'
      name: "Solar output"
      unit_of_measurement: "kW"
      value_template: '{{ value_json.output }}'

and it only gets me solar.output. What am I doing wrong?

Plenty of examples in the docs. You need two sensors:

sensor:
    - platform: mqtt
      state_topic: "solar/output"
      name: "Solar power"
      unit_of_measurement: "kWh"
      value_template: '{{ value_json.power }}'
    - platform: mqtt
      state_topic: "solar/output"
      name: "Solar output"
      unit_of_measurement: "kW"
      value_template: '{{ value_json.output }}'

You were trying to create a single sensor with two value_templates and two unit_of_measurements.

1 Like

It works for me…

Are you sure you are using the correct variable name?

I see what Troon says, and that is correct. But you can also have the values as attributes, if I recall correct then you can have them as attributes.

Thanks. I had tried with two completely separate sensor entries and that didn’t work either. What you’re suggesting is a kind of half way between my current effort and the earlier one.
I’m afraid I found the examples in the documentation, and those in the many forum posts I looked at, all rather confusing.
I’m still confused. I’m not at home right now but I’ll try your suggestion when I’m back.

Thanks, your suggestion worked. I’d still like to understand why my earlier attempt didn’t. It was this:

sensor:
    - platform: mqtt
      state_topic: "solar/output"
      name: "Solar power"
      unit_of_measurement: "kWh"
      value_template: '{{ value_json.power }}'

sensor:
    - platform: mqtt
      state_topic: "solar/output"
      name: "Solar output"
      unit_of_measurement: "kW"
      value_template: '{{ value_json.output }}'

i.e. two “sensor” entries.

In your configuration.yaml you can only have a single sensor: or anything alike for that matter (automation:, input_boolean, script:, etc). Take a look at the following link for further reading.

1 Like

Your issue is here…