[HELP][RESTful sensor] How can I seek an array from a specific key?

Hi,

I have this json array I wish to extract one value from:

[
  {
    "label": "amdgpu 1",
    "value": 42,
    "unit": "C",
    "type": "temperature_core",
    "key": "label"
  },
  {
    "label": "amdgpu 2",
    "value": 31,
    "unit": "C",
    "type": "temperature_core",
    "key": "label"
  },
  {
    "label": "k10temp 1",
    "value": 42,
    "unit": "C",
    "type": "temperature_core",
    "key": "label"
  }
]

I created a rest sensor and used this in value template:
value_template: "{{ value_json[2].value }}"

This works as intended but I would like to not rely on hardcoded constant in case this array changes order (devices might be added or removed).

Is there a way to seek the array based on a certain key value? I would like to get the “value” key from the entry containing the “k10temp 1” label. Is there a way with jinja2 to seek an array based on a certain key value?

Sorry if I’m not using the proper terminology here. I’m a real noob when it comes to REST and JSON.

The key lines “key”: “label” in every members of the array is perplexing to me. Feels like something that would instruct on how to seek this particular array. But then again, noob here. I’m way over my head on this!

Thanks!

Not very pretty, but you can try something like the following which goes through all entries of the array, looks for a specific label and then outputs the entry’s label. This only works correctly if the label values in your JSON are unique.

value_template: >
  {% for entry in value_json %}
    {% if entry.label == 'k10temp 1' %}
      {{ entry.value }}
    {% endif %}
  {% endfor %}

Works fine thanks!

Of course having a way to index the array using a specific key through the [] operator would have been nice, but the solution you propose is concise and effective.