Json_attributes from rest sensor

Hey guys,

I’m trying to use the rest sensor to extract information from a JSON output.

- platform: rest
    name: Coles Express Clayton
    json_attributes:
      - amount
      - updated
    resource: https://petrolspy.com.au/webservice-1/station/box?neLat=-37.923184&neLng=145.2405589474115&swLat=-37.928&swLng=145.04522305258843&ts=1537590523242&_=1537590522545
    value_template: "{{ value_json['message']['list'][3]['prices']['U91'] }}"

The return I get is with the state value:

{'id': '5ba4887de4b0e9f6d7c629ca', 'stationId': '5213005e03644ac3ec69ac3c', 'type': 'U91', 'updated': 1537509501231, 'relevant': True, 'amount': 144.7}

the json_attributes are not working as the attributes field of the sensor is blank. What am I doing wrong guys?

The value_template part extracts the value for the sensor’s state. And no matter what it looks like, it’s a string (like all states.)

The json_attributes currently only support extracting top-level JSON fields (like message in your case.) But the good thing about them is they will be dictionaries, not strings, so it’s easy to get at the individual fields after the fact.

About the only thing you can do (using standard sensors, that I’m aware of), is only list message for the json_attributes parameter. (You do need something for value_template, too, even if it’s not something very useful itself.) Then use template sensors to extract the fields from sensor.coles_express_clayton.attributes.message, e.g., sensor.coles_express_clayton.attributes.message.list[3].prices.U91.amount.

Thanks very much for this. I figure it was only high level attributes.