Rest sensor is getting on my nerves

The following is my config:

- platform: rest
    name: waze
    resource: "http://my.domain/waze/routeHome.php?lat={{ state_attr('person.andreash', 'latitude') }}&lon={{ state_attr('person.andreash', 'longitude') }}"
    value_template: '{{ value_json["route 1 name"] }}'
    json_attributes:
      - "route 1 name"
      - "route 1 duration"
      - "route 1 delay"
      - "route 2 name"
      - "route 2 duration"
      - "route 2 delay"
      - "route 3 name"
      - "route 3 duration"
      - "route 3 delay"
      - "route 4 name"
      - "route 4 duration"
      - "route 4 delay"
    scan_interval: 120

The link equals in template tools:

http://my.domain/waze/routeHome.php?lat=56.00865849&lon=12.76764844

The actual GPS position is set using a mock locations app.

Page returns the following:

{
   "route 1 name":"E6 \/ E20 S; Fjelievägen",
   "route 1 duration":37,
   "route 1 delay":0,
   "route 2 name":"E6 \/ E20 S; M 923",
   "route 2 duration":44,
   "route 2 delay":0,
   "route 3 name":"N\/A",
   "route 3 duration":"N\/A",
   "route 3 delay":"N\/A",
   "route 4 name":"N\/A",
   "route 4 duration":"N\/A",
   "route 4 delay":"N\/A"
}

“N/A” you ask…?
Yes… The PHP code makes sure there is four groups of values:

$i=1;
foreach($routes as $name => $time){
    $arr['route ' . $i . " name"] = $name;
    $arr['route ' . $i . " duration"] = $time['timeMin'];
    $arr['route ' . $i . " delay"] = ($time["timeMin"]-$time["timeNMin"]);
    $i++;
}
for($i; $i<=4;$i++){
    $arr['route ' . $i . " name"] = "N/A";
    $arr['route ' . $i . " duration"] = "N/A";
    $arr['route ' . $i . " delay"] = "N/A";
}
echo json_encode($arr, JSON_UNESCAPED_UNICODE );

Meaning, if routes array has less than four items then the for loop below will “trigger” and create the remaining, as the json above shows.

Now… This is what HA gives me:

And that can only happen if the link HA uses is incorrect with lat/lon that returns no results at all…
But since my debug link is copied from a template tool output, that can’t happen.
Or can it?
Can anyone see the error?

Or is there anything that could make this easier? I can’t seem add an array as json_attribute, or is that possible?

Try using resource_template instead of resource as the docs say that this is the way to use it (Don’t know if that’s still the case)

1 Like

It’s so annoying when it’s so simple…

Yeah, I hope we can get rid of all the _template stuff, the service calls/data was already taken care of with release 0.115, so I tend to stumble over that quite frequently as well :slightly_smiling_face:

The thing is that rest sensor was used elsewhere earlier, i just changed the name, url and attributes but it was a template previously also.
It worked back then, but not now.