Using the results of a simple GPS Rest call?

I am trying to call a simple rest api and use the results. The call is:

http://server/n365_ilist.php?hw=iOS&imei=IMEI&pw=PASSWORD

The result is then:

[{"login":"loginnumber","imei":"imeinumer","name":"303_707","carno":"1","gps":"time and coords","log":"OUT 2021-11-30 22:01:04","google":"googlecoords","baidu":"baiducoords","speed":null,"bat":"0","icon":"1","marker":"11","device":"303","ver":"93;0000;0","sec":"300","level":"100","expdate":null,"loc":"MX","onoff":"03","gexpdate":null,"iccid":"number","ggkey":null,"startdate":"2021-07-21","pic":"2"}]

I want to get the fields of gps, google and log.

I think I have played around with all the REST integrations I could find, but as I am not sure what I am doing, I dont know how to get the results. Anyone have any tips?

This was my latest try: (but it results in nothing. I really cant find out what the value of json_attributes_path should be). But perhaps that is not even the issue.

sensor:
  - platform: rest
    name: cargps
    resource: http://server/n365_ilist.php?hw=iOS&imei=IMEI&pw=PASSWORD
    scan_interval: 300 
    json_attributes_path: "$"
    json_attributes:
       - google
       - log
       - gps
    json_attributes_path: "$.0"

You should also set a state value.

thanks for the reply, i changed the path to $.0 but nothing happens. The sensor just does not appear in Home Assistant.

What do you mean with “You should also set a state value.”?

You have set the sensor attributes but you have not given the sensor a state.

Like this:

sensor:
  - platform: rest
    name: cargps
    resource: http://server/n365_ilist.php?hw=iOS&imei=IMEI&pw=PASSWORD
    scan_interval: 300 
    value_template: "{{ value_json.0.log }}"  ### <----- This is your sensor state
    json_attributes_path: "$.0"
    json_attributes:
       - google
       - log
       - gps

Or if you don’t want the sensor sate to record one of the available values:

sensor:
  - platform: rest
    name: cargps
    resource: http://server/n365_ilist.php?hw=iOS&imei=IMEI&pw=PASSWORD
    scan_interval: 300 
    value_template: "OK"
    json_attributes_path: "$.0"
    json_attributes:
       - google
       - log
       - gps

Ah I see, ty. I added the modifications.

I now do see the sensor, but its not filled. In the log I see an error “REST result could not be parsed as JSON”. Trying to find the reason for this.

Could it be the “[” at the beginning and the “]” at the end? But a JSON validator sees both with or without the square brackets as valid JSON

I have also tried without the brackets but seems JSON validator in HA is not approving this JSON, while it is valid. As this is a different issue, we can close this topic

The square brackets mean the result is an array of JSON objects, which in this case happens to only be one object. It’s perfectly valid JSON.

thats indeed the strange thing. Home Assistant complains with REST result could not be parsed as JSON

I tried it both with the rest platform and also with a curl / comand_line platform. It just wont parse this json

The only thing I can think of - is have you checked that the HTTP Headers being returned from the web server are specifying a

Content-Type: application/json

So Home Assistant knows it should be expecting JSON?

I can’t replicate the problem.

HA seems to parse the json perfectly fine.

What happens if you try this:

sensor:
  - platform: rest
    name: cargps
    resource: http://server/n365_ilist.php?hw=iOS&imei=IMEI&pw=PASSWORD
    scan_interval: 300 
    json_attributes:
       - "0"

I get these errors if I do that
image

The first one is “homeassistant.exceptions.InvalidStateError: Invalid state encountered for entity ID: sensor.cargps. State max length is 255 characters.”

These are the headers

image

Ok…
You need to set the state it seems.

sensor:
  - platform: rest
    name: cargps
    resource: http://server/n365_ilist.php?hw=iOS&imei=IMEI&pw=PASSWORD
    scan_interval: 300 
    value_template: 1 # this should set the state to "1"
    json_attributes:
       - "0"

Like I said 10 posts ago.

I did that, and that indeed solves the state Error.
But the JSON is still not parsed. I always get this

image

In that case I believe the page returns an invisible character that when copy pasted is transformed to a space, but in the rest call breaks the json.

Can Node red parse it?

I dont think its an invisible character. As I even tried this:

  - platform: command_line
    name: cargps
    command: 'curl -X GET -H "Content-type: application/json" -H "Accept: application/json" "http://server/n365_ilist.php?hw=iOS&imei=IMEI&pw=PASSWORD" | jq "."'

jq would make it valid json, but I still get the same error.

I dont have node red, so can not try that.

i too have the same device and same error

when using a command line sensor i get :

2021-12-06 11:44:35 WARNING (SyncWorker_2) [homeassistant.components.command_line.sensor] JSON result was not a dictionary

when using rest sensor i get :
2021-12-06 11:44:37 WARNING (MainThread) [homeassistant.components.rest.sensor] REST result could not be parsed as JSON

when i remove the JQ “.” from the command line sensor i get :

2021-12-06 11:49:16 WARNING (SyncWorker_7) [homeassistant.components.command_line.sensor] Unable to parse output as JSON: [{"login":"123456","imei":"123456","name":"Pepe","carno":"3","gps":"2021-12-06 10:23:23,5,1,0,210,12.123456,1.123456,-45","log":"IN 2021-12-06 10:40:23","google":"12.123456,1.123456","baidu":"12.123456,1.123456","speed":null,"bat":"100","icon":"1","marker":"8","device":"303X","ver":"2;0000;0","sec":"600","level":"100","expdate":null,"loc":"BG","onoff":"B","gexpdate":null,"iccid":"12345614005537671F","ggkey":null,"startdate":"2021-10-20","pic":"2"}]

what could be wrong?

The only thing that stands out in these strings is the null values.
When I use the strings in the template editor and try and output “expdate” I get nothing as response.
This is just a guess, perhaps that is the cause of the issue? Perhaps the parsing crashes at null?

Perhaps if null is replaced with "null"?
That is the only value in the strings that is not strings, so in my theory the only ones that could case an issue. (?)