Search Track and Add Track to playlist - getting out the spotify track uri #17
-
Hi, service: spotifyplus.search_tracks
data:
entity_id: media_player.spotifyplus_johannes
criteria: Songname
limit: 1
market: DE Will give the following result: user_profile:
country: DE
display_name: Johannes
email: -
id: -
product: premium
type: user
uri: spotify:user:-
result:
href: >-
https://api.spotify.com/v1/search?query=Songname&type=track&market=DE&offset=0&limit=1
limit: 1
next: >-
https://api.spotify.com/v1/search?query=Songname&type=track&market=DE&offset=1&limit=1
offset: 0
previous: null
total: 802
items:
- album:
album_type: album
artists:
- external_urls:
spotify: https://open.spotify.com/artist/4QASAhLqLPy8tDGy6mVdRw
href: https://api.spotify.com/v1/artists/4QASAhLqLPy8tDGy6mVdRw
id: 4QASAhLqLPy8tDGy6mVdRw
name: CLVR
type: artist
uri: spotify:artist:4QASAhLqLPy8tDGy6mVdRw
available_markets: []
copyrights: []
external_ids: {}
external_urls:
spotify: https://open.spotify.com/album/4gprLCFvypwGPmRZZNdBGB
genres: []
href: https://api.spotify.com/v1/albums/4gprLCFvypwGPmRZZNdBGB
id: 4gprLCFvypwGPmRZZNdBGB
images:
- url: https://i.scdn.co/image/ab67616d0000b273d2c85b752f9de01c79e2c48c
height: 640
width: 640
- url: https://i.scdn.co/image/ab67616d00001e02d2c85b752f9de01c79e2c48c
height: 300
width: 300
- url: https://i.scdn.co/image/ab67616d00004851d2c85b752f9de01c79e2c48c
height: 64
width: 64
label: null
name: NO WIFI
popularity: null
release_date: "2020-03-30"
release_date_precision: day
restrictions: {}
total_tracks: 12
type: album
uri: spotify:album:4gprLCFvypwGPmRZZNdBGB
artists:
- external_urls:
spotify: https://open.spotify.com/artist/4QASAhLqLPy8tDGy6mVdRw
href: https://api.spotify.com/v1/artists/4QASAhLqLPy8tDGy6mVdRw
id: 4QASAhLqLPy8tDGy6mVdRw
name: CLVR
type: artist
uri: spotify:artist:4QASAhLqLPy8tDGy6mVdRw
available_markets: []
disc_number: 1
duration_ms: 120517
explicit: true
external_ids:
ean: null
isrc: GBSMU7795258
upc: null
external_urls:
spotify: https://open.spotify.com/track/5hGqPrM0wNEry9dAEyTtOb
href: https://api.spotify.com/v1/tracks/5hGqPrM0wNEry9dAEyTtOb
id: 5hGqPrM0wNEry9dAEyTtOb
is_local: false
is_playable: true
name: Songname
popularity: 0
preview_url: >-
https://p.scdn.co/mp3-preview/3ac505d1ffebb4d4829255038bb2b4713bada3a6?cid=f35e5081fd6c425b845b7028257bb637
restrictions: {}
track_number: 3
type: track
uri: spotify:track:5hGqPrM0wNEry9dAEyTtOb
The last line with the uri: spotify:track I try to use it in a script to add this song to one of my playlists: sequence:
- service: spotifyplus.search_tracks
data:
entity_id: media_player.spotifyplus_johannes
criteria: Songname
limit: 1
market: DE
response_variable: search_result
- service: spotifyplus.playlist_items_add
data:
entity_id: media_player.spotifyplus_johannes
playlist_id: myplaylistid
uris: "{{ search_result.result.items[0].uri }}" When executing this script unfortunately I'll get the error: Looks like something wrong with my template variable {{ search_result.result.items[0].uri }}. When entering directly a specific uri (without the template, getting the uri from the previous search) the script is working fine. Would be pleased if someone could give me some hint, what I'm doing wrong. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
I believe the problem is that “items” is being interpreted as the Jinja “items()” function rather than the property in this line of code: Change it to the following and it should work: |
Beta Was this translation helpful? Give feedback.
-
Awesome, glad it fixed your issue. Enjoy! |
Beta Was this translation helpful? Give feedback.
-
Closing discussion, as this seems to be fixed. |
Beta Was this translation helpful? Give feedback.
I believe the problem is that “items” is being interpreted as the Jinja “items()” function rather than the property in this line of code:
search_result.result.items[0].uri
Change it to the following and it should work:
search_result.result[“items”][0].uri