I’m trying to parse some JSON Data. For the life of me, I can’t figure out, what I’m doing wrong. I feel like I’ve gone through every thread that mentions REST and JSON.
This is my sensor:
- platform: rest
resource: https://api.easybill.de/rest/v1/time-trackings?limit=1
authentication: basic
value_template: '{{ value_json.items[0].timer_value }}'
headers:
Content-Type: application/json
` Authorization: Bearer <snip>`
This is the normal JSON response:
{
"page": 1,
"pages": 6191,
"limit": 1,
"total": 6191,
"items": [
{
"cleared_at": "2015-04-09 23:31:36",
"count": null,
"created_at": "2015-03-31 00:00:00",
"date": "2015-03-31",
"date_from_at": "2015-03-31 11:00:00",
"date_thru_at": "2015-03-31 18:30:00",
"description": "<snip>",
"hourly_rate": 9000,
"id": <snip>,
"login_id": <snip>,
"note": "<snip>",
"number": null,
"position_id": null,
"project_id": 22067,
"timer_value": 450
}
]
}
Now assuming I’m trying to get the last value: “timer_value”.
I just get empty responses or ‘unknown’ in the sensor data.
It’s definitely a problem with the array or how I’m trying to access it with the block quotes. I’ve tried without.
I can easily get the value for “pages” with
value_template: '{{ value_json.pages }}'
This is the Home Assistant error:
Error parsing value: builtin_function_or_method object has no element 0 (value: {"page":1,"pages":6191,"limit":1,"total":6191,"items":[{"cleared_at":"2015-04-09 23:31:36","count":null,"created_at":"2015-03-31 00:00:00","date":"2015-03-31","date_from_at":"2015-03-31 11:00:00","date_thru_at":"2015-03-31 18:30:00","description":"<snip>","hourly_rate":9000,"id":<snip>,"login_id":<snip>,"note":"<snip>","number":null,"position_id":null,"project_id":22067,"timer_value":450}]}, template: {{ value_json.items[0].timer_value }})
Any help would be much appreciated!
Solution by VDRainer
What about
value_template: ‘{{ value_json[‘items’][0][‘timer_value’] }}’
?