MQTT Sensor attributes

Hi everybody,
I’ve been trying to send some monitoring stats from a remote server using a scheduled python script that sends MQTT payloads to my HA installation.
It partially works, since I can’t get any attribute.

I’m sending this payload, (and I can also see it with a mqtt dump command):
{"unit_of_measurement": "%", "value": 26.1}

And this is my sensor configuration:

- platform: mqtt
    name: "MediaServer CPU usage"
    state_topic: "server_monitorings/media_server/CPU_usage"
    value_template: "{{ value_json.value }}"
    payload_available: "online"
    payload_not_available: "offline"
    json_attributes_topic: "server_monitorings/media_server/CPU_usage"

Even though I can see the value, there’s no attribute and the history cannot see it as a number.
image

How can I have the numeric value and its attributes?

You have the “value” attribute, so the concept works.
I guess you just cannot override the “unit_of_measurement” (and probably other “reserved” attributes) that way…

1 Like

You can’t use json_attributes_topic to overwrite a sensor’s core attributes.

That capability used to exist but was considered to be an anomaly and was eliminated. Here’s the PR that closed that loophole:

Mates you’re right!
I manually set unit_of_measurement in config and now this sensor is working as expected.

I’m reviving this script after about a year without using it and I’m sure it had been working, and now I now why.

I needed to dynamically change the unit_of_measurement for the free disk space sensor: it could have been TB or GB (or MB if I’ll be really in troubles…).
Now I’ve changed the df -h command with a df -BG to always get value in GB and statically set the attribute.

If I can ask… why this change to mqtt platform?

Probably because no other integration allowed for that kind of change so, in the interests of parity, it was eliminated from all MQTT platforms. However, that’s just my interpretation and the best reference would be the developer.