MQTT sensor "state" attribute missing

Hi Guys;

I have this new MQTT sensor setup, but the “state” (really… it is state of the location) is missing from the attributes.

yaml …

  sensor:
    - name: price monitor
      state_topic: "monitor/price"
      value_template: '{{ value_json.price }}'
      json_attributes_topic: "monitor/price"
      unit_of_measurement: "$"
      availability:
        - topic: "monitor/status"
          payload_available: "online"
          payload_not_available: "offline"

Example payload …

{"price": 9.99, "suburb": "SUB", "state": "ST", "postcode": "77777", "lat": 00, "lng": 00, ..... }

I can confirm from the developer tools that the entity got the price right, got every single attributes from the json, but the “state”.

Please kindly help.

Thanks!

“state” like “value” may be a reserved word in which case you need to use square bracket notation in the value template to get it.

value_template: '{{ value_json["state"] }}'

Same behaviour here with the state attribute not appearing as expected: @tom_l must be right about the reserved word. Here’s how to fix it so that you can have that value in your attributes — the json_attributes_template line duplicates the state value into the state_abbr key:

    - name: price monitor
      state_topic: "monitor/price"
      value_template: '{{ value_json.price }}'
      json_attributes_topic: "monitor/price"
      json_attributes_template: "{{ dict(value_json.items(), state_abbr=value_json['state'])|to_json }}"
      unit_of_measurement: "$"
      availability:
        - topic: "monitor/status"
          payload_available: "online"
          payload_not_available: "offline"

Tested:

1 Like