RESTful sensor parse nested json to sensor attributes

I need to get the ‘name’ as an attribute in the restful sensor.

{"datum":"2019-02-05T10:16:56+01:00","fracties":[{"name":"Garbage","detailUrl":"https:\/\/*****"}]}

Below is my config:
- platform: rest
  resource: https://api.....
  method: GET
  name: waste_collection
  scan_interval: 3600
  json_attributes:
    - datum
    - fracties[0].name

The date is filled out correctly as an attribute. But the ‘name’ component has an issue.
No error, just no data.
I’ve tried several approaches:

  • fracties.0.name
  • fracties[0][‘name’]

  • Not working.
    How can I get the data into the sensor attributes?

You can’t, not the way the REST Sensor is currently implemented. The items you list under json_attributes must be “top level” fields in the JSON response. The best you can do is list just fracties, and then you’ll see the entire value of that field as an attribute. Then, if you need just the value of the name key, you could use a template sensor to get it.

Thanks for that info. I’ll try the template sensor then.

How can I reach the items in the array?
Something like:

value_template: "{{ sensor.waste_collection.fracties[0].name }}"

I’ve stumbled upon this topic, but this also states that I need a custom component:

I don’t see why you would need a custom component. (I didn’t read that topic, though. :slight_smile:)

This should work:

value_template: "{{ states.sensor.waste_collection.attributes.fracties[0].name }}"

You can always try the template out on the Templates page first.

1 Like

Yep, that’s working! Tnx.