Question: how to handle JSON payload with multiple items?

I have a weather sensor which collects 5 different items (temperature, humidity, wind speed, rainfall) which I would like to send to HA using MQTT in a JSON string something like this:

{
  "Weather/patio": {
    "Temperature": "20 C",
    "humidity": "50%",
    "wind speed": "25 kph".
   "rain": "25 mm"
  }
}

If I were doing it one at a time I might send it to this topic 
"Weather/patio/temperature" with this payload "20 C".  And I should 
find it via the MQTT explorer with that topic.  But if I use a multiple 
items in the payload,  how will they show up in the explorer? 
Thanks....RDK

As a message containing a JSON string on a single topic, probably something like this:

{"Weather/patio":{"Temperature":"20 C","humidity":"50%","wind speed":"25 kph","rain":"25 mm"}}

There is a fault in your payload. The . after kph should be a ,

Then you can do this:

mqtt:
  sensor:
    - name: "Temperature"
      state_topic: "your_topic_here"
      value_template: "{{ value_json['Weather/patio']['Temperature'] }}"
      device_class: temperature
      state_class: measurement
      unit_of_measurement: "°C"
    - name: "Humidity"
      state_topic: "your_topic_here"
      value_template: "{{ value_json['Weather/patio']['humidity'] }}"
      device_class: humidity
      state_class: measurement
      unit_of_measurement: "%"
    - name: "Wind Speed"
      state_topic: "your_topic_here"
      value_template: "{{ value_json['Weather/patio']['wind speed'] }}"
      device_class: wind_speed
      state_class: measurement
      unit_of_measurement: "km/h"
    - name: "Rain"
      state_topic: "your_topic_here"
      value_template: "{{ value_json['Weather/patio']['rain'] }}"
      device_class: precipitation
      state_class: measurement
      unit_of_measurement: "mm"
1 Like

Thank you. I’ll study this and do some experiments…RDK

Side Note: IMHO, using “/” as a key name is super-bad practice but possibly you are not generating this data, only getting it. If it is a key, I would use “Weather_Patio” or “WeatherPatio” or “Weather-Patio” as the “/” and further extraction could be handled different on different systems.

Internally for many JSON parsers tthat is not “Weather/patio” … it is probably “Weather_X2f_patio” … example:

1 Like

Thanks. Today I got back to this project. For some background, until I get a new Raspberry Pi configured to directly pass the real sensor readings to HA via MQTT, I have set up another Pi to publish some test data in MQTT JSON format:

# sudo apt install python3-paho-mqtt 
import paho.mqtt.client as mqtt
...
    Temp = randrange(30)
    ValTemp = Temp
    client.publish("Weather/Patio/Temperature",  str(Temp))

This is working as I see the data in the HA MQTT-Explorer. I have added this code to the “Studio Code Server”:

mqtt:
  sensor:
    - name: "Patio Sensor"
      state_topic: "Weather/Patio/Temperature"
      value_template: "{{ value_json['Weather/Patio']['Temperature'] }}"
      device_class: temperature
      state_class: measurement
      unit_of_measurement: "°C"

which does seem to connect to the “Patio Sensor” as, on the default dashboard, I see Patio Sensor but “unknown” for value. If I remove the “value_template” line then it works and I see the temperature value. However, I can not seem to move/transfer that sensor onto my personal dashboard??

The data information I posted in the original question and answered by @tom_l would have multiple sensor readings is the same MQTT message:

{
  "Weather/patio": {
    "Temperature": "20 C",
    "humidity": "50%",
    "wind speed": "25 kph",
   "rain": "25 mm"
  }
}

I modified my Raspberry Pi program to send this MQTT message:

    Value = '"Temperature": ' + str(Temp) + ', "humidity": ' + str(Humid) + ',  "wind speed": ' + str(Wind) + ', "rain": ' + str(Rain)
    client.publish("Weather/Patio/Temperature", Value)
ex:    Value String  "Temperature": 10, "humidity": 8,  "wind speed": 86, "rain": 8

Again, I see the MQTT full message in the MQTT explorer. Here is the code, using @tom_l suggestions, in the “Studio Code Server” for all four sensors:

mqtt:
  sensor:
  - name: "Patio Temperature"
      state_topic: "Weather/Patio/Temperature"
    #  value_template: "{{ value_json['Weather/Patio']['Temperature'] }}"
      device_class: temperature
      state_class: measurement
      unit_of_measurement: "°C"

    - name: "Patio Humidity"
      state_topic: "Weather/Patio/Humidity"
    #  value_template: "{{ value_json['Weather/Patio']['humidity'] }}"
      device_class: humidity
      state_class: measurement
      unit_of_measurement: "%"

    - name: "Patio Wind Speed"
      state_topic: "Weather/Patio/Wind"
    #  value_template: "{{ value_json['Weather/Patio']['wind speed'] }}"
      device_class: wind_speed
      state_class: measurement
      unit_of_measurement: "km/h"
      
    - name: "Patio Rain"
      state_topic: "Weather/Patio/RainFall"
    #  value_template: "{{ value_json['Weather/Patio']['rain'] }}"
      device_class: precipitation
      state_class: measurement
      unit_of_measurement: "mm"

All four sensors are shown on the default dashboard, but all are “Unknown” with or without the “value_template” line in the code.

Any help, suggestion and comments will be welcomed…RDK

You’re confusing some things:

  • the topic is where you publish the data to. It should be taken literally: if you publish to Weather/patio/Temperature, you need to use that as state_topic for every sensor that you want to use to extract data from the payload.
  • then the payload: this is the data published to the topic. If you want to publish multiple values, the convention (not requirement) is to format the data as JSON. Nothing in the payload is relevant for use in state_topic.

First start by formatting your data as proper JSON:

{"Temperature": 10, "humidity": 8,  "wind speed": 86, "rain": 8}

(this is exactly the data that value_json will be used on)

Then publish it to the topic that makes most sense for this:

client.publish("Weather/Patio",  '{"Temperature": 10, "humidity": 8,  "wind speed": 86, "rain": 8}')

Then these sensors should work:

sensor:
  - name: "Patio Temperature"
    state_topic: "Weather/Patio"
    value_template: "{{ value_json['Temperature'] }}"
    ...

  - name: "Patio Humidity"
    state_topic: "Weather/Patio"
    value_template: "{{ value_json['humidity'] }}"
    ...

Many thanks. I’ll get back to this tomorrow. Thanks again.