RESTful sensor and array

Hi, everyone!
I’m trying to configure some restful sensors. I’ve been looking here and there, but not found something like I’m confronting…

I’m trying to have info about 6 items I have published in a web service. Right now I do it one by one, in six calls, with an answer like this:

{
    "value_I_care_about": 98
}

The point now is that I can do just one call (and maybe after template N sensors) with all information for the 6 items. The response I get doing this call is a JSON array like this:

[
    {
        "Id": 1,
        "value_I_care_about": 34,
        *some other values...*
    },
    {
        "Id": 2,
        "value_I_care_about": 34,
        *some other values...*
    },
    *and so on...*
]

And I’m completely stuck and without ideas about how to configure the sensor for having those internal values available :frowning:
Please, help!
(thank you very much!)

You can create many sensors in one call with the rest integration (as opposed to the restful sensor platform).

You can use this to find the path to your values:

https://jsonpathfinder.com/

1 Like

Done! I was looking at the wrong solution! Thank you very much!

1 Like

How do you iterate the array?

Give us an example of an array you’re trying to iterate and what you want to get from it. There are lots of solutions depending on the data structure, starting from something as simple as getting the first item:

    value_template: "{{ value_json[0]['value_I_care_about'] }}"

or the one where Id is 42:

    value_template: "{{ (value_json|selectattr('Id','eq',42)|first)['value_I_care_about'] }}"

It’s a lnd channel list. Don’t know the length of the array beforehand. I want to visualize the aliases and balances. Looks like:


 {
    "channels": [
        {
            "active": true,
            "capacity": "150000",
            "local_balance": "10",
            "remote_balance": "147120",
            "total_satoshis_sent": "0",
            "total_satoshis_received": "10",
            "local_chan_reserve_sat": "1500",
            "peer_alias": "xyz",
        },
        {
            "active": true,
            "capacity": "100000",
            "local_balance": "99026",
            "remote_balance": "0",
            "total_satoshis_sent": "0",
            "total_satoshis_received": "0",
            "local_chan_reserve_sat": "1000",
            "peer_alias": "abc",
        }
    ]
}

What do you mean exactly, though — what output do you want to generate? Accessing the data from that structure is easy, but we can’t give any guidance on how to do it until we know what “it” is.

For example, play with this in the Developer Tools / Template editor:

{% set value_json = {
    "channels": [
        {
            "active": true,
            "capacity": "150000",
            "local_balance": "10",
            "remote_balance": "147120",
            "total_satoshis_sent": "0",
            "total_satoshis_received": "10",
            "local_chan_reserve_sat": "1500",
            "peer_alias": "xyz",
        },
        {
            "active": true,
            "capacity": "100000",
            "local_balance": "99026",
            "remote_balance": "0",
            "total_satoshis_sent": "0",
            "total_satoshis_received": "0",
            "local_chan_reserve_sat": "1000",
            "peer_alias": "abc",
        }
    ]
} %}

Aliases: {{ value_json['channels']|map(attribute='peer_alias')|list }}

{% set ns = namespace(bl=[]) %}
{% for i in value_json['channels'] %}
  {% set ns.bl = ns.bl + [{'alias': i['peer_alias'], 'balance': i['local_balance']}] %}
{% endfor %}
{{ ns.bl }}
1 Like

Thanks, did not know that there’s a for-loop

See the two links at the top of the template editor then. Lots of useful info to get you started.