REST Sensor JSON parsing

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’] }}’
?

Don’t know if it will work but try this:

value_template: '{{ value_json.items[0][timer_value] }}'

No luck and same result.

What about
value_template: '{{ value_json['items'][0]['timer_value'] }}'
?

2 Likes

Just to point out, you can play around with decoding json in the HA template page under ‘Developer Tools’

VDRainer, my hero! Where were you 6 hours ago? =)

That solved it.

value_template: "{{ value_json['items'][0]['timer_value'] }}"

gpbenton, thanks for the tip. I know. I was just out of ideas where to put corner brackets and dots… I was hoping for a stroke of genius from someone else. Lo and behold =).

1 Like

Sorry for the double post… I actually have another question that is still concerning this topic.

Does anyone have an idea how I can parse multiple values from one GET and process them?

For example, my GET returns this:

{
  "page": 1,
  "pages": 687,
  "limit": 3,
  "total": 2061,
  "items": [
    {
      "cleared_at": "2015-04-14 21:32:01",
      "count": null,
      "created_at": "2015-03-31 00:00:00",
      "date": "2015-03-31",
      "date_from_at": "2015-03-31 09:30:00",
      "date_thru_at": "2015-03-31 15:45:00",
      "description": "<snip>",
      "hourly_rate": 10000,
      "id": <snip>,
      "login_id": <snip>,
      "note": "<snip>",
      "number": null,
      "position_id": null,
      "project_id": 22061,
      "timer_value": 375
    },
    {
      "cleared_at": "2015-05-05 20:42:48",
      "count": null,
      "created_at": "2015-04-01 00:00:00",
      "date": "2015-04-01",
      "date_from_at": "2015-04-01 08:00:00",
      "date_thru_at": "2015-04-01 15:00:00",
      "description": "<snip>",
      "hourly_rate": 1000,
      "id": <snip>,
      "login_id": <snip>,
      "note": "<snip>",
      "number": null,
      "position_id": null,
      "project_id": 22075,
      "timer_value": 390
    },
    {
      "cleared_at": "2015-04-14 21:32:01",
      "count": null,
      "created_at": "2015-04-01 00:00:00",
      "date": "2015-04-01",
      "date_from_at": "2015-04-01 08:30:00",
      "date_thru_at": "2015-04-01 09:45:00",
      "description": "<snip>",
      "hourly_rate": 10000,
      "id": <snip>,
      "login_id": <snip>,
      "note": "<snip>",
      "number": null,
      "position_id": null,
      "project_id": 22061,
      "timer_value": 75
    }
  ]
}

Is there a way I can add all ‘timer_value’ values together from the arrays which match the current month or a month I define?

I have to admit this is way over my head, yet would be a very cool thing to have. It’s just that I wouldn’t even know how to start dealing with this. Do I export the values into a table? Can I filter it based on the date field?

I had a look at the templating manuals, as well as the jinja manuals, but I’m lacking know-how how to start tackling it…

1 Like

Just in case anyone would be looking at a similar problem and for a solution: I ended up creating a bash script for it, which does it like this:

#!/bin/bash
user=SNIP
username=SNIP

timeframe=`date +%Y-%m`

timeperiod=monthly

lastpage=$(curl -s -H "Authorization: Bearer SNIP" "https://SNIP/rest/page=1&login_id=${user}" | jq '.' | grep pages | cut -c 12-14 $
secondtolastpage=`expr $lastpage - 1`
currentpagehours=$(curl -s -H "Authorization: Bearer SNIP" "https://SNIP/rest/page=${lastpage}&login_id=${user}" | jq '.' | grep -A $
previouspagehours=$(curl -s -H "Authorization: Bearer SNIP" "https://SNIP/rest/page=${secondtolastpage}&login_id=${user}" | jq '.' |$
if (( $secondtolastpage == 0 )); then
        previouspagehours=0
fi
echo $currentpagehours $previouspagehours | awk '{print $1 + $2}' > /home/pi/scripts/output/"$username"_"$timeperiod".txt

I realize, that I could’ve cleaned it up a bit more, but it does the job fantastically in combination with a file sensor, like so:

  - platform: file
    name: SNIP
    file_path: "/home/pi/scripts/output/SNIP.txt"

have a look at this, it may help in creating successful payloads