REST Sensor: Scan JSON file - Set Sensor status if string exists

Hi,

I have the following challenge. This JSON file:

https://discovery.mysterium.network/api/v3/proposals

lists a lot of data of nodes in the Mysterium Network which are up and running. I also have some nodes there are would like to monitor their status.

ONLINE = the node appears in the JSON
OFFLINE = the node ist not listed in the JSON

The string I want to scan / look for is:

provider_id":“0x3094916cfc86aa7b7a644a7e7abeec4692258a08”

I am now looking for a way with a rest sensor to implement this logic but couldn’t figure out how I can implement such a logic.

Any help is appreciated.

Thomas

That JSON is very large.
It could actually be large enough to be noticed when it’s updating.

But you need some template like:

{% if 'provider_id":“0x3094916cfc86aa7b7a644a7e7abeec4692258a08”' in value.json %}
    online
{% else %}
    offline
{% endif %}

@Hellis81 thanks a lot for your help. I implemented the following:

  - platform: rest
    resource: https://discovery.mysterium.network/api/v3/proposals
    scan_interval: 30
    name: myst2    
    value_template: >
      {% if '0x3094916cfc86aa7b7a644a7e7abeec4692258a08' in value.json %}
        online
      {% else %}
        offline
      {% endif %}

But it always shows offline which is incorrect. The log shows:

2022-01-07 10:47:31 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'str object' has no attribute 'json' when rendering '{% if '0x3094916cfc86aa7b7a644a7e7abeec4692258a08' in value.json %}
online
{% else %}
offline
{% endif %}'

Do you have any idea?

Sorry. It should be value_json.
But now that I think about it, value_json is an object and it won’t work.
Either the object needs to be converted to string or you need to find where this key provider_id is and look for it directly.

Huh… I just tried something in developer tools.
It seems you can use filter “string” and it seems to work.

- platform: rest
    resource: https://discovery.mysterium.network/api/v3/proposals
    scan_interval: 30
    name: myst2    
    value_template: >
      {% if '0x3094916cfc86aa7b7a644a7e7abeec4692258a08' in value_json | string %}
        online
      {% else %}
        offline
      {% endif %}

however, this has a flaw that it looks for the string

0x3094916cfc86aa7b7a644a7e7abeec4692258a08     and if it finds 
10x3094916cfc86aa7b7a644a7e7abeec4692258a08    or 
0x3094916cfc86aa7b7a644a7e7abeec4692258a081 

it will say “online”.
Meaning it will do a dumb comparison.
But lets try this first then we can move to a regex comparison and make sure it doesn’t match incorrectly

@Hellis81 That looks much better. It shows online now and no more errors in the log. I am afraid to ask but if you also have a solution for the “dumb comparison” that would be even more awesome.

I think you need:

{% if (value_json | string) | regex_findall_index('\b\"0x3094916cfc86aa7b7a644a7e7abeec4692258a08\"\b') %}
        online
      {% else %}
        offline
      {% endif %}

This should ensure that the string exists in it’s entirety,
However I would say that doing a proper json search is probably the better choice, but I can’t open the link on my work computer and the phone can’t copy the complete json.
So I can’t get the structure of the json.

Thanks for sharing this information i was facing some problems and still wanted to figure it out can any one give me suggestions.

What JSON is it, what are you looking for?
We need details to be able to help.

Thanks I tried it but it seems it doesn’t work. My configuration is:

  - platform: rest
    resource: https://discovery.mysterium.network/api/v3/proposals
    scan_interval: 30
    name: myst1    
    value_template: >
      {% if (value_json | string) | regex_findall_index('\b\"0x3094916cfc86aa7b7a644a7e7abeec4692258a08\"\b') %}
        online
      {% else %}
        offline
      {% endif %}

I get the following error in the log and the sensor doesn’t appear in HA.

2022-01-07 18:09:40 ERROR (MainThread) [homeassistant.components.sensor] Error adding entities for domain sensor with platform rest

Hi,
More than one year later here come some additional info.
The mysterium API works with GET requests so, instead of downloading the full list off nodes each time you do a request, you can specify the node you want to get info on requesting like that :

https://discovery.mysterium.network/api/v3/proposals?provider_id=0x3094916cfc86aa7b7a644a7e7abeec4692258a08

If the node is up you get a JSON else the API replies the string null.
Requesting like that can save you à lot of time and bandwith.