[SOLVED] How to use json_attributes? (e.g. MQTT Sensor)

But also; if you could define a single sensor for each object, instead of 5 or 6 - then I think things would be a lot tidier. It’s fine if you only have several such devices, but I intend to have up to 15 of these things. In that scenario, 75 sensors might be excessive

Understood, though attributes are not really first-class objects and if the values are important, you’ll likely find yourself fighting with HA. I’m not 100% sure, but I don’t think that you can fire automations based on changes in the state of attributes vs. the state of the entity. And if you want to look at the history, or feed the data into some other database (like in my case, InfluxDB).

All in all, I think that adding more entities isn’t really a huge heavyweight thing if you’re going to use them somehow and need to extract their values.

@mattp May I suggest marking this post as solved?

I have raised a Pull request with an example to the MQTT Sensor page

The example sensor below shows a configuration example which uses JSON in the state topic to add extra attributes. It also makes use of the availability topic. Attributes can then be extracted in Templates;
Example to extract data from the sensor below
'{{ states.sensor.bs_client_name.attributes.ClientName }}'

# Example configuration.yml entry
sensor:
  - platform: mqtt
    state_topic: "HUISHS/BunnyShed/NodeHealthJSON"
    name: "BS RSSI"
    unit_of_measurement: "dBm"
    value_template: '{{ value_json.RSSI }}'
    availability_topic: "HUISHS/BunnyShed/status"
    payload_available: "online"
    payload_not_available: "offline"
    json_attributes:
      - ClientName
      - IP
      - MAC
      - RSSI
      - HostName
      - ConnectedSSID  

See this example in use: Genestealer/Home-Assistant-Configuration

3 Likes

Yes definitely, I didn’t realise there was that option.

I didn’t get mine working, but I think the problem was that the status payload from my device is malformed, so the JSON isn’t parsed correctly

@mattP Sure, just click the little checkbox next to my post with the details. It’s marked “Select if this reply solves the problem”

1 Like

I just wanted to add that I just upgraded to 0.64.0 and the same YAML that wasn’t working previously is now working as it should.

1 Like

Thanks for that - I also upgraded and my MQTT JSON is working perfectly

guys,

Curious,Im integrating HA with Sonoff+Tasmota. the data comes as a JSON object via MQTT

{“RfReceived”:{“Sync”:12550,“Low”:430,“High”:1220,“Data”:“D41D3E”,“RfKey”:1}}

Within HA I can define a sensor like so
sensor rfbridgeResult:
- platform: mqtt
name: “RFBridgeResult”
state_topic: “tele/sonoffRFridge/RESULT”
value_template: ‘{{ value_json.RfReceived.RfKey }}’
expire_after: 20
json_attributes:
- RfReceived

The value template works fine but where im struggling is to sort out the other attributes
if I use the above json_attributes section then the attributes is a single object with the JSON string.

I thought i could use
json_attributes:
-RfReceived.Sync

but that doesnt produce anything (no error)…

Any ideas?
Thanks

2 Likes

Hi, this works for me with Tasmota and an MQTT sensor, using the telemetry (as you have):

Sensor YAML:

- platform: mqtt
  name: "Bedroom Light"
  value_template: '{{ value_json }}'
  state_topic: "tele/bedlight/STATE"
  qos: 1
  json_attributes: 
    - Time
    - Uptime
    - Vcc
    - POWER

This results in a sensor called ‘sensor.bedroom_light’. The attributes defined under json_attributes are gleaned using templates like (case sensitive):

{{ states.sensor.bedroom_light.attributes.Vcc }}

Without the json_attributes entry, the values all seem to be lumped in as the ‘state’ and I cannot access them programatically within Home Assistant. So re-reading your post - I think the value_template: ‘{{ value_json }}’ part should free things up so that everything tasmota sends is accessible.

Hope that helps!

2 Likes

WARNING json_attributes is no longer deprecated; it has been REMOVED

2 Likes

Can anyone help me to build a discovery topic for these mqtt messages:

I want to keep track of the target/high value (currently its 35). Is there any chance to realize this?
Many mqtt topics look like this.

Try this:

value_template: ‘{{ value_json[“zone1”][“heat”][“target”].high }}’