Json_attributes_topic - json_attributes deprecated

json_attributes is deprecated. Simple replacing json_attributes with json_attributes_topic gives me some errors.

Could someone give an example on how to use json_attributes_topic right? Is there a new format?

json_attributes_topic
(string)(Optional)The MQTT topic subscribed to receive a JSON dictionary payload and then set as sensor attributes.

json_attributes
(list | string)(Optional)(Deprecated, replaced by json_attributes_topic) A list of keys to extract values from a JSON dictionary payload and then set as sensor attributes.

EDIT: It looks like with adding the whole topic as attributes we can no longer chose which attributes we like to show.
With the new way all attributes are shown. Am I right? Does someone know why this change happened?

EDIT2:
Also this way we have duplicate values as state an attribute, like in this example the battery state:

Anyone…?

I don’t know the answer, but did come across this in another thread:

1 Like

I checked out the unit tests for the mqtt.py (https://github.com/home-assistant/home-assistant/blob/master/tests/components/sensor/test_mqtt.py) component and it seems its expecting a json array.

old:
json_attributes:
- lastRing
- doorbellRing

new:
json_attributes_topic: '["lastRing","doorbellRing"]'

I tried your suggestions, but it doesn’t work. Maybe someone else can confirm that to be sure it’s not my mistake.
The attributes don’t show up.

I think _topic indicates that it’s a mqtt topic this line listens to. Still don’t know if there is a way to filter attributes (only pick the ones we want).

Idea seems to be that there is a separate MQTT topic & payload for attributes. All fields in that payload end up as sensor attributes. This might be handy for something, but doesn’t cover all the cases deprecated json_attributes did, because it allowed to cherrypick necessary attributes from payload. One cannot always change MQTT messages sent by sensor and I think it is impossible to migrate to new json_attributes_topic without changes in system that publishes MQTT messages.

So I think that old json_attributes should be kept also.

Found in another thread

I am getting the following error even though I don’t have any of my MQTT sensors that specify json_attributes:

2019-01-28 19:09:22 WARNING (MainThread) [homeassistant.components.mqtt.sensor] configuration variable "json_attributes" is deprecated, replace with "json_attributes_topic"

Not sure what I’m supposed to be remove/replace if it’s not listed.

hi @apt, did you ever got to fix this?

1 Like

It fixed itself, I believe is my zigbee2mqtt integration that was throwing up the error, once an update to that was made it went away.