Mqtt json: automatically list all keys and values?

Hi everybody,

I am trying to submit multiple values to my Home Assistant via mqtt. The payload sent to my mqtt broker will look like this

{"CPU0": "+41.0°C", "CPU1": "+42.0°C", "CPU2": "+42.0°C", "CPU3": "", "sda2 Kapazität": "109G", "sda2 Belegt": "101G", "sda2 Frei (Size)": "2,5G", "sda2 Frei (Prozent)": "98%", "Uptime": " 15:03:56 up 1:14, 3 users, load average: 0,71, 0,65, 0,81", "Letztes Update": "2020-06-21-15:03:56", "IP 1": "10.10.10.40/24", "IP 2": "192.168.178.155/24"}

I have tried setting it up like this

sensor:
  - platform: mqtt
    name: "Beelink CPU0"
    value_template: "{{ value_json.CPU0 }"
  - platform: mqtt
    name: "Beelink CPU1"
    value_template: "{{ value_json.CPU1 }"
  - platform: mqtt
    name: "Beelink CPU2"
    value_template: "{{ value_json.CPU2 }"
# etc.

But Home Assistant requires a state_topic, so this will not work. Is there something similar to the REST sensor, where you can at least define multiple json_arguments (see here under multiple values)?

Or is there some way to just subscribe to the topic itself, and have every key and value be displayed? I named each key so that it would be what I’d like it to be named in Home Assistant, anyway.

I have the option to get each of these values as a payload only to separate topics, for example test/cpu0 would be +41.0°C, test/cpu1 would be +42.0°C, etc. but I would very much prefer to send this single json payload only and parse all the results from it - rather than having countless mqtt topics and payloads.

Thank you for your ideas :slight_smile:

???

How were you planning for this sensor to acquire a payload without a state_topic?

  - platform: mqtt
    name: "Beelink CPU0"
    value_template: "{{ value_json.CPU0 }}"

(BTW, there’s a missing } at the end of the template.)

There are several ways you can receive the payload. You can continue with the approach you’re currently using and simply add state_topic to each sensor you’ve defined:

  - platform: mqtt
    name: "Beelink CPU0"
    state_topic: sensor/whatever
    value_template: "{{ value_json.CPU0 }}"

Simply publish the payload to whatever you choose for the state_topic.

Alternately, you can create a single sensor that stores the received data as attributes.

  - platform: mqtt
    name: "Beelink"
    state_topic: sensor/whatever
    value_template: "{{ value_json['Letztes Update'] }}"
    json_attributes_topic: sensor/whatever

Yet another option is to create a “demulitplexer” automation that receives the payload and republishes each JSON key’s value to a separate MQTT topic.

  • beelink/cpu0
  • beelink/cpu1
  • etc

Then you define sensors that subscribe to their own MQTT topic:

  - platform: mqtt
    name: "Beelink CPU0"
    state_topic: beelink/cpu0

  - platform: mqtt
    name: "Beelink CPU1"
    state_topic: beelink/cpu1
1 Like

Thank you!

I worked on this too much yesterday. Of course I need a state_topic. D’oh! I don’t know how I missed this.

Fixed the code, and it works now. However, is there some way (perhaps via templating) to tell HA “subscribe to this topic; create a sensor for each key/value pair in the json payload”? So if I were to add anything to my mqtt script on the beelink host, HA would realize this and display the newly added segment? (I am not planning to add anything, it’d just be nice to know for future projects)

Yes but it’s much easier, and more efficient, to use json_attributes_topic to put each key-value pair into a sensor’s attributes.

Your request can be realized by an automation similar to the “demultiplexer” automation I had described. It would publish each key to its own MQTT Discovery topic, with an appropriate payload describing the sensor, thereby causing Home Assistant to create a new sensor. Then it would publish the key’s value. However, it would do this every time a new Beelink payload is received! I don’t think this is the best use of Home Assistant’s resources.

1 Like