Need Help: Script to play a random album by artist (Spotify, Alexa)

Hello,

my Kids listen to a story every night and I would like to have a script that plays a random album (story) by a specific artist. I do know all the steps for doing it, but I have a problem with my script that I’m not able to resolve.

Here are all integrations that I use with this script:

Here is my script:

alias: Play Random Story
sequence:
  - service: spotifyplus.get_artist_albums
    data:
      entity_id: media_player.spotifyplus_bot
      artist_id: 61qDotnjM0jnY5lkfOP7ve
      include_groups: album
      limit: 1
      offset: 0
    response_variable: get_total_albums_result
  - variables:
      total_albums: "{{ get_total_albums_result.result.total }}"
  - variables:
      random_album: "{{ (range(0, (total_albums | int) - 2) | random | int) }}"
  - service: spotifyplus.get_artist_albums
    data:
      entity_id: media_player.spotifyplus_bot
      artist_id: 61qDotnjM0jnY5lkfOP7ve
      include_groups: album
      limit: 1
      offset: "{{ random_album }}"
    response_variable: get_album_result
  - variables:
      artist: "{{ get_album_result.result.items[0].artists[0].name }}"
      album: "{{ get_album_result.result.items[0].name }}"
  - service: media_player.play_media
    target:
      entity_id: media_player.children
    data:
      entity_id: media_player.children
      media_content_id: Album {{ album }} by {{ artist }}
      media_content_type: SPOTIFY
description: ""
icon: mdi:folder-music-outline

The result of service call “spotifyplus.get_artist_albums” is as followed"

get_album_result:
  user_profile:
    country: CA
    display_name: ********
    email: ***********
    id: ***********
    product: premium
    type: user
    uri: spotify:user:*****************
  result:
    href: >-
      https://api.spotify.com/v1/artists/61qDotnjM0jnY5lkfOP7ve/albums?offset=47&limit=1&include_groups=album
    limit: 1
    next: >-
      https://api.spotify.com/v1/artists/61qDotnjM0jnY5lkfOP7ve/albums?offset=48&limit=1&include_groups=album
    offset: 47
    previous: >-
      https://api.spotify.com/v1/artists/61qDotnjM0jnY5lkfOP7ve/albums?offset=46&limit=1&include_groups=album
    total: 141
    items:
      - album_type: album
        artists:
          - external_urls:
              spotify: https://open.spotify.com/artist/61qDotnjM0jnY5lkfOP7ve
            href: https://api.spotify.com/v1/artists/61qDotnjM0jnY5lkfOP7ve
            id: 61qDotnjM0jnY5lkfOP7ve
            name: TKKG
            type: artist
            uri: spotify:artist:61qDotnjM0jnY5lkfOP7ve
        available_markets:
          - AR
          - AU
          - AT
          - BE
          - BO
          - BR
          - BG
          - CA
          - CL
          - CO
          - CR
          - CY
          - CZ
          - DK
          - DO
          - DE
          - EC
          - EE
          - SV
          - FI
          - FR
          - GR
          - GT
          - HN
          - HK
          - HU
          - IS
          - IE
          - IT
          - LV
          - LT
          - LU
          - MY
          - MT
          - MX
          - NL
          - NZ
          - NI
          - 'NO'
          - PA
          - PY
          - PE
          - PH
          - PL
          - PT
          - SG
          - SK
          - ES
          - SE
          - CH
          - TW
          - TR
          - UY
          - US
          - GB
          - AD
          - LI
          - MC
          - ID
          - JP
          - TH
          - VN
          - RO
          - IL
          - ZA
          - SA
          - AE
          - BH
          - QA
          - OM
          - KW
          - EG
          - MA
          - DZ
          - TN
          - LB
          - JO
          - PS
          - IN
          - BY
          - KZ
          - MD
          - UA
          - AL
          - BA
          - HR
          - ME
          - MK
          - RS
          - SI
          - KR
          - BD
          - PK
          - LK
          - GH
          - KE
          - NG
          - TZ
          - UG
          - AG
          - AM
          - BS
          - BB
          - BZ
          - BT
          - BW
          - BF
          - CV
          - CW
          - DM
          - FJ
          - GM
          - GE
          - GD
          - GW
          - GY
          - HT
          - JM
          - KI
          - LS
          - LR
          - MW
          - MV
          - ML
          - MH
          - FM
          - NA
          - NR
          - NE
          - PW
          - PG
          - PR
          - WS
          - SM
          - ST
          - SN
          - SC
          - SL
          - SB
          - KN
          - LC
          - VC
          - SR
          - TL
          - TO
          - TT
          - TV
          - VU
          - AZ
          - BN
          - BI
          - KH
          - CM
          - TD
          - KM
          - GQ
          - SZ
          - GA
          - GN
          - KG
          - LA
          - MO
          - MR
          - MN
          - NP
          - RW
          - TG
          - UZ
          - ZW
          - BJ
          - MG
          - MU
          - MZ
          - AO
          - CI
          - DJ
          - ZM
          - CD
          - CG
          - IQ
          - LY
          - TJ
          - VE
          - ET
          - XK
        external_urls:
          spotify: https://open.spotify.com/album/0NyIaGxbQlRPnA201NEbkE
        href: https://api.spotify.com/v1/albums/0NyIaGxbQlRPnA201NEbkE
        id: 0NyIaGxbQlRPnA201NEbkE
        images:
          - url: https://i.scdn.co/image/ab67616d0000b273b5d2e3d9f441ab3aef894360
            height: 640
            width: 640
          - url: https://i.scdn.co/image/ab67616d00001e02b5d2e3d9f441ab3aef894360
            height: 300
            width: 300
          - url: https://i.scdn.co/image/ab67616d00004851b5d2e3d9f441ab3aef894360
            height: 64
            width: 64
        name: 192/Feuer auf Gut Ribbeck!
        release_date: '2015-06-05'
        release_date_precision: day
        restrictions: {}
        total_tracks: 40
        type: album
        uri: spotify:album:0NyIaGxbQlRPnA201NEbkE

My problem is with this part in my script:

variables:
  artist: '{{ get_album_result.result.items[0].artists[0].name }}'
  album: '{{ get_album_result.result.items[0].name }}'

Error: UndefinedError: builtin_function_or_method object has no element 0

How can I access the first element in the array “items” and “artists”?

@j.loewen
I believe the problem is that “items” is being interpreted as the Jinja “items()” function rather than the property in this code:

artist: '{{ get_album_result.result.items[0].artists[0].name }}'
album: '{{ get_album_result.result.items[0].name }}'

Change it to the following and it should work:

artist: '{{ get_album_result.result["items"][0].artists[0].name }}'
album: '{{ get_album_result.result["items"][0].name }}'

See this thread for more details …

Hope it helps!

1 Like

Hey @thlucas,

yes, that was the Problem. Thank you very much, it works now.

1 Like