Value_template for command_line sensor to extract a value from JSON response

I have been searching the forum & Google for hours and hours, but just can’t get it to work…

Background:
I am playing around with the Spotify API, because I would like to be able to use Assist to search for an Artist/Playlist and then send the URI to my Sonos speaker.

Here is where I am stuck:
I have created a command_line sensor that calls the Spotify API (with a fixes search string for now, one step after another):

- sensor:
    name: ES Spotify URI
    scan_interval: 300000 
    command: curl -X GET "https://api.spotify.com/v1/search?q=this+is+Jack+Jonson&type=playlist&limit=1&market=de" -H "Authorization:Bearer {{states('sensor.es_spotify_token')}}" 
    unique_id: es_spotify_uri
    value_template: >
      {{ value_json.playlists.items[0].uri }}

This is the response of CURL when calling it from commad line

{
  "playlists" : {
    "href" : "https://api.spotify.com/v1/search?query=this+is+Jack+Jonson&type=playlist&market=DE&offset=0&limit=1",
    "items" : [ {
      "collaborative" : false,
      "description" : "This is Jack Johnson. The essential tracks, all in one playlist.",
      "external_urls" : {
        "spotify" : "https://open.spotify.com/playlist/37i9dQZF1DZ06evO294Tcc"
      },
      "href" : "https://api.spotify.com/v1/playlists/37i9dQZF1DZ06evO294Tcc",
      "id" : "37i9dQZF1DZ06evO294Tcc",
      "images" : [ {
        "height" : null,
        "url" : "https://thisis-images.spotifycdn.com/37i9dQZF1DZ06evO294Tcc-large.jpg",
        "width" : null
      } ],
      "name" : "This Is Jack Johnson",
      "owner" : {
        "display_name" : "Spotify",
        "external_urls" : {
          "spotify" : "https://open.spotify.com/user/spotify"
        },
        "href" : "https://api.spotify.com/v1/users/spotify",
        "id" : "spotify",
        "type" : "user",
        "uri" : "spotify:user:spotify"
      },
      "primary_color" : null,
      "public" : null,
      "snapshot_id" : "MjgzOTc1MTEsMDAwMDAwMDBkOTRkMTIwYzM4NDEzZjY1YThlYzhkNTBlNGZlNzQ4NA==",
      "tracks" : {
        "href" : "https://api.spotify.com/v1/playlists/37i9dQZF1DZ06evO294Tcc/tracks",
        "total" : 48
      },
      "type" : "playlist",
      "uri" : "spotify:playlist:37i9dQZF1DZ06evO294Tcc"
    } ],
    "limit" : 1,
    "next" : "https://api.spotify.com/v1/search?query=this+is+Jack+Jonson&type=playlist&market=DE&offset=1&limit=1",
    "offset" : 0,
    "previous" : null,
    "total" : 600
  }

What I would like to extract is this (8th row from the bottom):

spotify:playlist:37i9dQZF1DZ06evO294Tcc

But unfortunatly, with the value_template above the sensors value is “unknown”.

Any ideas how too change the template to extract the URI?

Copy-paste the curl response you posted above into JSONpath.com. The first thing it will tell you is that the JSON is malformed. It’s missing a final } character.

After adding the missing brace, it was able to extract the desired URI.

Is that the actual response from curl or was the final brace lost when you copy-pasted it into the forum post?

The closing brace was indeed only lost while copy-pasting.

Thanks for the link, so that tells me that my value_template

{{ value_json.playlists.items[0].uri }}

is indeed correct!? But it is not working here. If I try for example

{{value_json.playlists.next}}

the sensor value is polpulated with the correct value, but for the URI it is still “unknown”

Got it: According to this old post, “items” is a reserved word:

So I tried it with this template, and that work :slight_smile:

    value_template: >
      {{ value_json["playlists"]["items"][0]["uri"] }}