REST sensor struggles with no parent name

I have some JSON that I would like to extract some information from here: https://nordvpn.com/api/server

There isn’t a parent name and all the examples I’ve seen online are using the parent name to return data. Also the data is too large to fit into a sensor state so I’ve been trying to use an attribute to get around that problem.

I have been testing different combinations of the json_attributes_path and json_attributes from the REST sensor but cannot get anything to populate into the sensor.

Does anyone have any ideas on how I can achieve this?

Which values do you want?

e.g.

this is the first instance of “load”:

value_template: "{{ value_json.[0].load }}"

Ideally I would like it to return ip_address and domain but for all entries.

I tried the above as a test but could only get it to work without the dot:

value_template: "{{ value_json[0].load }}"

I have tried to specify the values in json_attributes but only the first one gets returned.

Yeah sorry that dot should not be there.

:astonished:

That is 11,014 sensors, 5,507 if you put the domain as an attribute of the IP address sensors.

Ahh that is probably too many! It can be narrowed down to any domains that begin with “nz” if possible.

The problem I seem to have is that the json path is numbered 0, 1, 2 etc so there is no way to specify an attribute without hard setting it to a number.

For anyone with a similar issue in the future this is how I went about solving this:

Create a Command Line sensor with the following:
curl -sb -H ‘https://nordvpn.com/api/server’ | jq -r ‘.[] | select(.ip_address == “{{ state_attr(‘sensor.vpn_state’, ‘server_ip’) }}”) | .name’

To break it down a bit:

  • curl -sb -H 'https://nordvpn.com/api/server' gets json contents from the webpage
  • use jq to narrow the results via the pipe symbol
    • -r returns the resulting jq output as a raw string omitting the double quotes which would otherwise encapsulate it
    • .[] returns entire output of curl
    • select(.ip_address == "{{ state_attr('sensor.vpn_state', 'server_ip') }}") returns object literals where the “ip_address” key equals the value of my predefined attribute
    • .name returns the value associated to the “name” key

This results in a sensor containing a state for just the value I need. It also gets around the need to try and store a large amount of data into a state where the 255 character limit would otherwise be hit.