mudman
April 21, 2020, 7:55pm
1
Hi having troble parsing information from a http get resource formatted in JSON
I am looking to extract status vlaue from the following
{"type":"0","ch":"0","mode":"10","loop":"0","eq":"0","status":"stop","curpos":"1696411","offset_pts":"1696411","totlen":"0","Title":"69+�lT","Artist":"","Album":"","alarmflag":"0","plicount":"0","plicurr":"0","vol":"0","mute":"1","iuri":"687474703A2F2F7579657365652E7674756E65722E636F6D2F73657475706170702F7579657365652F6173702F66756E632F64796E616D4F442E6173703F65783435763D333733663833366661616466316662306236646163623232633035393234643833373366383336666161646631666230623664616362323263303539323464382669643D3938343630","uri":""}
I have tried in value_template:
“{{ value_json.status}}” this returns unknown as state
“{{value_json[‘status’]}}” this returns unknown as state
If the value template is left out home assistant returns an error of max length for state
Sorry if this is really simple but thanks for any help
123
(Taras)
April 21, 2020, 9:16pm
2
Both value_templates are valid.
You can go to Developer Tools > Template and paste this into the Template Editor:
{% set value_json = {"type":"0","ch":"0","mode":"10","loop":"0","eq":"0","status":"stop","curpos":"1696411","offset_pts":"1696411","totlen":"0","Title":"69+�lT","Artist":"","Album":"","alarmflag":"0","plicount":"0","plicurr":"0","vol":"0","mute":"1","iuri":"687474703A2F2F7579657365652E7674756E65722E636F6D2F73657475706170702F7579657365652F6173702F66756E632F64796E616D4F442E6173703F65783435763D333733663833366661616466316662306236646163623232633035393234643833373366383336666161646631666230623664616362323263303539323464382669643D3938343630","uri":""} %}
{{ value_json.status }}
{{ value_json["status"] }}
It will successfully report the value of status
which is stop
.
If you exclude the value_template form the sensor, Home Assistant will definitely complain about “max length” because an entity’s state
can hold a maximum of 255 characters and the example you posted is over 500 characters in length.
Please post the configuration of the sensor that receives this data.
mudman
April 21, 2020, 9:37pm
3
- platform: rest
resource: http://192.168.0.61/httpapi.asp?command=getPlayerStatus
method: GET
name: player status
value_template: "{{ value_json.status }}"
force_update: true
Thanks for the reply
123
(Taras)
April 21, 2020, 9:47pm
4
The RESTful Sensor’s configuration seems to be fine.
Does the JSON data it receives always include the “status” key? If the data fails to include it then Home Assistant will report the sensor’s state is unknown
.
mudman
April 21, 2020, 9:55pm
5
Yes it always reports a “status” key. Confirmed using command line. While playing, paused, stopped from different media sources e.g. internet radio, USB, network storage. The only time the device does not report is when it is turned off.
That was my thinking as well the sensor configuration looked good.
Try putting player status in name field in quotes.
mudman
April 22, 2020, 6:31am
7
This still gives the state as unknown
123
(Taras)
April 22, 2020, 10:55am
8
Go to Developer Tools > Logs and check if there are any errors reported when this sensor periodically acquires and processes its data.
mudman
April 22, 2020, 6:58pm
9
No error logs given after sensor updates.
Going to try setting it up as a command line sensor and using a script and see if that sheds any light on the problem
mudman
April 23, 2020, 6:35am
10
After setting it up as a command line sensor it gives the following error.
Error rendering template: TemplateSyntaxError: unexpected char ‘�’ at 177
Suggesting non ascii character in the JSON dictionary. Will have to do further investigation