@estley
I get asked this same question / scenario a lot for the SpotifyPlus Integration. Here’s the How do I access dictionary items from a Service Response topic I put together for my FAQ wiki page that explains the issue in more detail.
Hope it helps!
How do I access dictionary items from a Service Response
All of the SpotifyPlus services that retrieve data from the Spotify Web API will return the data in a dictionary format in the service response. The data can then be processed in a template and used however you wish.
For example, consider the following service call to retrieve all albums for an artist:
action: spotifyplus.get_artist_albums
data:
entity_id: media_player.spotifyplus_todd_l
artist_id: 6APm8EjxOHSYM5B4i3vT3q
include_groups: album
limit_total: 10
sort_result: true
The results would look like this (shortened for brevity, actual result will contain MUCH more data):
result:
date_last_refreshed: 1739564010.147239
items:
- album_type: album
artists:
- id: 6APm8EjxOHSYM5B4i3vT3q
name: MercyMe
uri: spotify:artist:6APm8EjxOHSYM5B4i3vT3q
id: 2ezUrMWSvn9nYsI6DvdCgW
image_url: https://i.scdn.co/image/ab67616d0000b2732e7368a96604933f6a87b97a
name: "10"
total_tracks: 25
uri: spotify:album:2ezUrMWSvn9nYsI6DvdCgW
- album_type: album
artists:
- id: 6APm8EjxOHSYM5B4i3vT3q
name: MercyMe
uri: spotify:artist:6APm8EjxOHSYM5B4i3vT3q
id: 4nZS59zzTABfnSPq7a9iP1
image_url: https://i.scdn.co/image/ab67616d0000b2733e8dc4e2eb7551afa8db3339
name: All That Is Within Me
total_tracks: 10
uri: spotify:album:4nZS59zzTABfnSPq7a9iP1
The Problem
When you go to process the data in your template processing, your first thought is to access the repeating items
collection data using something like this:
artist_name: "{{serviceResponse.result.items[0].name}}"
which will result in an error that looks like this:
Error rendering data template: UndefinedError: builtin_function_or_method object has no element 0.
Keep in mind that you are processing data that is returned in a dictionary using Python; the Python dictionary class has a METHOD named items
, which is not to be confused with the items
KEY that is returned in the dictionary. The result.items[0].name
statement is trying to call the Python items
METHOD, which results in the builtin_function_or_method object has no element 0
error.
The Solution
You have to reference items
as a dictionary KEY so that it’s not inferred as a METHOD, like so:
artist_name: "{{serviceResponse.result['items'][0].name}}"
Here’s a simple example I tested in the HA Developer Tools \ Templates
editor:
{%-
set serviceResponse = {
"result": {
"items": [
{
"artists": [
{
"name": "MercyMe",
"uri": "spotify:artist:6APm8EjxOHSYM5B4i3vT3q"
}
],
"image_url": "https://i.scdn.co/image/ab67616d0000b2732e7368a96604933f6a87b97a",
"name": "10",
"uri": "spotify:album:2ezUrMWSvn9nYsI6DvdCgW"
},
{
"artists": [
{
"name": "MercyMe",
"uri": "spotify:artist:6APm8EjxOHSYM5B4i3vT3q"
}
],
"image_url": "https://i.scdn.co/image/ab67616d0000b2733e8dc4e2eb7551afa8db3339",
"name": "All That Is Within Me",
"uri": "spotify:album:4nZS59zzTABfnSPq7a9iP1"
}
]
}
}
-%}
Artist Albums: {{"\n"}}
{%- for item in serviceResponse['result']['items'] -%}
{%- set trackName = item['name'] | default('') -%}
{%- set trackImageUrl = item['image_url'] | default('') -%}
{%- set trackUri = item['uri'] | default('') -%}
{%- set trackArtist = item['artists'][0]['name'] | default('') -%}
- {{ trackName }} ({{trackUri}}) - {{ trackArtist }} {{"\n"}}
{%- endfor -%}
{{"\n"}}
1st item result A = {{ serviceResponse['result']['items'][0]['uri'] }}
1st item result B = {{ serviceResponse.result['items'][0].uri }}
1st item result C = {{ serviceResponse.result.items[0].uri }} <- this fails