Using value_json in Multipscrape/value_template

Hi,

I’m trying to use the HACS mutliscrape integration to get some data into sensors. I have managed to retrieve the whole JSON, but cannot figure out how to use value_json in templates properly. Are there any docs?

In particular the part of the JSON I want looks like this:

{
    "current": {
        "fromDateTime": "2022-03-29T12:10:55.843Z",
        "tillDateTime": "2022-03-29T13:10:55.843Z",
        "values": [
            {
                "name": "PM1",
                "value": 13.74
            },
            {
                "name": "PM25",
                "value": 21.99
            },
            {
                "name": "PM10",
                "value": 30.03
            },
            {
                "name": "PRESSURE",
                "value": 1009.85
            },
            {
                "name": "HUMIDITY",
                "value": 61.52
            },
            {
                "name": "TEMPERATURE",
                "value": 15.23
            },
            {
                "name": "SO2",
                "value": 1.84
            },
            {
                "name": "CO",
                "value": 424.65
            }
        ],

I want to get e.g. the value associated with the name PM1, 13.74. I think the problem is that the value key is a list. I’ve tried something like

value_template: "{{value_json.current.values[0].value}}"

and various others iterations but I always get the error that

* Template variable error: builtin_function_or_method object has no element 0 when rendering '{{ value_json.current.values[0].value }}'

Could someone help?

Assuming you forgot the last few closing brackets from your sample JSON, if you change the key value to pretty much anything else, it works.
Here is what I tested and worked:

Json:

{
    "current": {
        "fromDateTime": "2022-03-29T12:10:55.843Z",
        "tillDateTime": "2022-03-29T13:10:55.843Z",
        "valuesvalues": [{
                "name": "PM1",
                "value": 13.74
            },
            {
                "name": "PM25",
                "value": 21.99
            },
            {
                "name": "PM10",
                "value": 30.03
            },
            {
                "name": "PRESSURE",
                "value": 1009.85
            },
            {
                "name": "HUMIDITY",
                "value": 61.52
            },
            {
                "name": "TEMPERATURE",
                "value": 15.23
            },
            {
                "name": "SO2",
                "value": 1.84
            },
            {
                "name": "CO",
                "value": 424.65
            }
        ]
    }
}

Here is the value_template:

value_json.current.valuesvalues[0].value

Note the only thing I did was to change the key values to valuesvalues in the Json (and also made it a valid json).

Hope it helps!

Yeah, I cut off a big part of the json, which I don’t care about, so it is valid.

Your observation is funny - I guess there is some clash with an internal variable – either value or values. I’ve managed to get it to work like this:

value_template: "{{ value_json.current['values'][0]['value'] }}

This doesn’t clash for … reasons. Thank!