Mqtt sensor - how to load attributes

Here’s an example of using json_attributes_template.

If given this topic:

sensor/cpu

and this payload:

{
	"used_space": 25,
	"sys_clock_speed": 1500,
	"data": {
		"cpu_temp": 43.0,
		"voltage": 0.8500,
		"cpu_load": 1.25
	},
	"memory": "False",
	"swap": "False"
}

This MQTT Sensor extracts values from the payload and assigns them to attributes using custom names.

  - platform: mqtt
    name: monitor
    state_topic: sensor/cpu
    value_template: "{{ value_json.used_space }}"
    json_attributes_topic: sensor/cpu
    json_attributes_template: >
      { "speed": {{value_json.sys_clock_speed}},
        "temperature": {{value_json.data.cpu_temp}},
        "voltage": {{value_json.data.voltage}},
        "load": {{value_json.data.cpu_load}},
        "memory": "{{value_json.memory}}",
        "swap": "{{value_json.swap}}" }

If the our data you wish to use for attributes is not published to a single topic then json_attributes_topic and json_attributes_template probably can’t help you.

I say “probably” because there is one thing I have not tried. You can try setting json_attributes_topic to:

shellies/shellydw-pc/sensor/#

That’s a ‘wild card’ character and means to subscribe to all topics below shellies/shellydw-pc/sensor. If that is even allowed in this context, the next challenge is composing a template that understands what was just received and to which attribute it belongs to. The only thing it will receive (if it works) will be the raw data and won’t know if it belongs to tilt or lux or whatever.

6 Likes