#MQTT - Workarounds to parse MQTT JSON automatically.

1 messages · Page 1 of 1 (latest)

crude patio
#

Hi! I've got a case where my MQTT produces following JSON:

{
  "Information": {
    "Model": "Fineoffset-WH2", 
    "ID": 151
  }, 
  "Metadata": {
    "mic": "CRC", 
    "mod": "ASK", 
    "freq": 433.9912, 
    "rssi": -0.0843086, 
    "snr": 7.21689, 
    "noise": -7.3012
  }, 
  "Measurements": {
    "Temperature": 24.5, 
    "Humidity": 67
  }
}

Is there a way how I could include all the values of the Metadata so that they Key is used for the name and value of course for the value, instead of doing all individually?

The current way how I've implemented the Measurements, are individual methods, as shown here:

sensor:
  - name: "Temperature"
    state_topic: "si"
    unique_id: "Storage_Temperature"
    value_template: "{{ value_json.Measurements.Temperature }}"
    unit_of_measurement: "°C"
    device_class: "temperature"
    device:
      name: "Storage Temperature sensor"
      manufacturer: "Telldus"
      identifiers: "Storage Temperature Sensor"
      model: "313159"

  - name: "Humidity"
    state_topic: "si"
    unique_id: "Storage_Humidity"
    value_template: "{{ value_json.Measurements.Humidity }}"
    unit_of_measurement: "%"
    device_class: "humidity"
    device:
      name: "Storage Temperature sensor"
      manufacturer: "Telldus"
      identifiers: "Storage Temperature Sensor"
      model: "313159"
rustic ruin
#

use yaml anchors

#

e.g.

#
sensor:
  - name: "Temperature"
    unique_id: "Storage_Temperature"
    value_template: "{{ value_json.Measurements.Temperature }}"
    unit_of_measurement: "°C"
    device_class: "temperature"
    <<: &si_device
      state_topic: "si"
      device:
        name: "Storage Temperature sensor"
        manufacturer: "Telldus"
        identifiers: "Storage Temperature Sensor"
        model: "313159"

  - name: "Humidity"
    unique_id: "Storage_Humidity"
    value_template: "{{ value_json.Measurements.Humidity }}"
    unit_of_measurement: "%"
    device_class: "humidity"
    <<: *si_device
crude patio
rustic ruin
#

No

crude patio
# rustic ruin No

So it's not technically possible to automate the creation of the unique_id by MQTT JSON message, not even with discovery?

rustic ruin
#

it's possible with an automation providing the MQTT discovery. But that's the only way.

crude patio
#

Or do I have to specificy every single device in discovery rules manually?

#

(And don't worry, I'm not asking you to create correct syntax or config, I just want to confirm before I dip to the world of Discovery rules)

rustic ruin
#

you can't template those fields without having that informatoin before hand

crude patio
#

Ah, good point. I think it would be the easiest if I make a Python script that creates the YAML files for those 🙂 Thank you for your assistance and confirmations!

crude patio
#

@rustic ruin Sorry to ping you but I figured out a way how to do the update of the value without needing to add something to the value, simple force_update was the trick to have it 🙂