Parsing JSON data from RESTful sensor

Hi!

My first post in the forum, but I’ve been using Home Assistant for a while now.

I’m trying to parse some JSON data from my Worx Landroid with the RESTful Sensor. I’m able to connect to the endpoint and all that, but when the request is done it only gets the eighth element from the list. Without me specifying any template value.

The config is

- platform: rest
resource: http://192.168.1.227/jsondata.cgi
username: admin
password: 0000
authentication: basic
name: Glenn

The response normally looks like this

{
"CntProg":100,
"lingua":10,
"ore_funz":[75,75,75,75,75,0,0],
"ora_on":[9,9,9,9,9,0,0],
"min_on":[0,0,0,0,0,0,0],
"allarmi":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
"settaggi":[0,0,0,0,1,1,0,1,1,1,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
"mac":[xxx,xxx,xxx,xxx,xxx,xxx],
"time_format":1,
"date_format":2,
"rit_pioggia":180,

...

But the sensor only gets:

Entity          State                           Attributes
sensor.glenn	{"mac":[xxx,xxx,xxx,xxx,xxx,xxx]}	friendly_name: Glenn

Is there anything wrong in my config? Is there any way for me to debug better to see where the data gets lost? It would be nice to see the raw response of the request to the endpoint.

A few other questions related to this sensor: How frequent will it update? Also, it seems that if the endpoint is offline when I restart home assistant the sensor won’t show up when it comes online again. Should it be like that?

I hope someone can help me with this!
/Erik

Think you need a value template in there

I’ve been playing around with one but then I don’t get any value at all. I’ve tried both using index and using values/names. But since the request without a value template gets some response it seems weird it just gets some and not all. And very weird why it returns the 8th and not the first or last.

Have you tried out the Template tool? That’s the place to figure this stuff out

Would you mind giving an example on how I could use it? If I type:

{{ states("sensor.glenn") }}

I get the same output with only the mac-address attribute.
What can I change? It seems like some data is missing already?

Should be of form

value_template: ‘{{ value_json.state.mac }}’

I struggled with this too. The trick is to use the template tool.
Step one is to assign your resultant json to value_json
Then template out your results like this:
{%value_json==your_json_result%}

{{}}

In my case I wanted to get the lake height from this site:
http://waterservices.usgs.gov/nwis/iv/?format=json&indent=on&sites=06921325&parameterCd=62614

My resultant value template is this.

value_template : '{{value_json["value"]["timeSeries"][0]["values"][0]["value"][0]["value"]|float}}'
1 Like

I give up on this. I’ve been debugging this for a while now and the response from the Requests lib is the same as what is being set to the state. I can’t find where the data gets lost. A value_template wouldn’t solve anything here since the response only contains one json object. There are no other objects to choose from.

I’m not sure if I am doing something wrong or if there is a bug in the component or in the lib. But I don’t know a good way to debug this further.

I will move on to the command line sensor and use curl instead to get the correct data.

Thank You!! I’ve been trying to figure out how to parse the output from their site for 2 days. None of the documentation I found on JSON showed any examples like what you’ve got.

1 Like

I finally found what was causing this issue. Now the documentation states the following:

This configuration shows how to extract multiple values from a dictionary with json_attributes and template. This avoids flooding the REST service by only requesting the result once, then creating multiple attributes from that single result using templates. By default, the sensor state would be set to the full JSON — here, that would exceed the 255-character maximum allowed length for the state, so we override that default by using value_template to set a static value of OK.

That probably explains why I only would get a part of the response.