How to configure value_template to extract data from JSON array without name

Hi,
I would appreciate help with configuring a REST full sensor to extract multiple sensors from one REST call using templates.
I can extract any value, e.g. temperatur from the first element of the array with below configuration.

However struggle to figure out how I can access any attribute of the second element of the array.
For example the value for JSON Path $.[?(@.etagenname=="OG")].raeume[?(@.name=="Bad")].temperatur

Here is my configuration

sensor:
  - platform: rest
    resource: http://192.168.2.3/get/json/v1/1/temps
    scan_interval: 60
    name: temperatur_sensor
    value_template: 'OK'
    json_attributes:
      - temperature
      - raeume
  - platform: template
    sensors:
      wohnzimmer_ist_temperatur_neu:
        device_class: temperature
        unit_of_measurement: "°C"
        value_template: "{{ states.sensor.temperatur_sensor.attributes.raeume[0].temperatur|round(2) }}"

which returns 17.73 as desired.

My JSON looks like below:

[
    {
        "etagenname": "DG",
        "id": 1,
        "raeume": [
            {
                "total_offset": 0.2,
                "solltemperatur": 12,
                "temperatur": 17.725490196078432,
                "name": "Bad"
            },
            {
                "total_offset": 0.2,
                "solltemperatur": 18,
                "temperatur": 19.137254901960784,
                "name": "Lena"
            }
        ]
    },
    {
        "etagenname": "OG",
        "id": 2,
        "raeume": [
            {
                "total_offset": -0.11529411764705755,
                "solltemperatur": 19.9,
                "temperatur": 16.764705882352942,
                "name": "Bad"
            },
            {
                "total_offset": 0.08,
                "solltemperatur": 16,
                "temperatur": 18.352941176470587,
                "name": "Schlafzimmer"
            }
        ]
    }
]
1 Like

Hmm. I would jump on Discord and ask the devs if the RESTfull sensor supports a JSON array at the top level. Possibly it only grabs the first element.

If not the only suggestion I would have is to retrieve the raw data and load the array into an object then process that, or maybe do it with Node Red.

You can use top level arrays.

Use this tool to get the path to the data you want. Just replace x with value_json

https://jsonpathfinder.com/

You can get all the temperature sensors with the one rest call using the new rest integration rather than the rest sensor platform. No template sensors required:

rest:
  - resource: http://192.168.2.3/get/json/v1/1/temps
    scan_interval: 60
    sensor:
      - name: "DG Bad" #change this to what you want
        value_template: "{{ value_json[0].raeume[0].temperatur }}"
        device_class: temperature
        unit_of_measurement: "°C"
      - name: "DG Lena" #change this to what you want
        value_template: "{{ value_json[0].raeume[1].temperatur }}"
        device_class: temperature
        unit_of_measurement: "°C"
      - name: "OG Bad" #change this to what you want
        value_template: "{{ value_json[1].raeume[0].temperatur }}"
        device_class: temperature
        unit_of_measurement: "°C"
      - name: "OG Schlafzimmer" #change this to what you want
        value_template: "{{ value_json[1].raeume[1].temperatur }}"
        device_class: temperature
        unit_of_measurement: "°C"
2 Likes

Ah - makes sense when you see it done that way.

Thanks for the tip.

This is working, reading the documentation and posts here in the community I however got the impression that using the template sensor is the way how extracting multiple sensors from one REST call should be done.
Could you share a bit of light for a HA green-horn on the difference between the two approaches?
Will your sample extract the sensor values from a single rest call or will a rest call per sensor be initiated?

That’s the way it was done until the rest integration was introduced. You can still do it that way but it’s more work and less efficient.

Thanks for the clarification, sounds great!

is there a way to use a filter to find the index. like raeme[?‘name’=‘Bad’] instead of raeume[1] or raeume[2]

sincearly
Fredrik

Apologies for the hijack but this looks like the same issue that I am trying to solve though I cant seem to get it working using the solution from Post #3.

I too have an unlabelled JSON top level that looks like this;

[
	{
		"releaseDate": "2023-03-05",
		"mediaItem": {
			"id": 11,
			"title": "The Last of Us",
			"releaseDate": "2023-01-15",
			"slug": "the-last-of-us-2023",
			"mediaType": "tv",
			"seen": true
		},
		"episode": {
			"id": 240,
			"title": "When We Are in Need",
			"episodeNumber": 8,
			"seasonNumber": 1,
			"releaseDate": "2023-03-05",
			"seen": true,
			"isSpecialEpisode": false
		}
	},
	{
		"releaseDate": "2023-03-08",
		"mediaItem": {
			"id": 33,
			"title": "The Mandalorian",
			"releaseDate": "2019-11-12",
			"slug": "the-mandalorian-2019",
			"mediaType": "tv",
			"seen": true
		},
		"episode": {
			"id": 267,
			"title": "Chapter 18",
			"episodeNumber": 2,
			"seasonNumber": 3,
			"releaseDate": "2023-03-08",
			"seen": false,
			"isSpecialEpisode": false
		}
	}
]

and my config looks like this…

rest:
  - resource: http://resource url (Produced the above JSON)
    scan_interval: 60
    sensor:
      - name: "upcoming_ep1"
        value_template: "{{ value_json[0].releaseDate }}"
      - name: "upcoming_ep2"
        value_template: "{{ value_json[1].releaseDate }}"

I am only trying to fetch the release date currently as that seemed a simple place to start as it’s a basic entry rather than being a further nested entry but even that is not working. In the Developer > Sates tab, I can’t even find the “upcoming_ep1” or “ep2” entities. I’ve checked configuration and restarted HA and still nothing.

Any ideas what I’ve missed? sure its something simple.

Hi,
if you want to get the release date for the episode, you would need to modify your value_template like below:

rest:
  - resource: http://resource url (Produced the above JSON)
    scan_interval: 60
    sensor:
      - name: "upcoming_ep1"
        value_template: "{{ value_json[0].episode.releaseDate }}"
      - name: "upcoming_ep2"
        value_template: "{{ value_json[1].episode.releaseDate }}"

Must be something in the logs. Your sensor configuration looks good and the template is correct (@urmel — there is a releaseDate as well as an episode.releaseDate). Make sure you only have one top-level rest: declaration.