Complete confusion over résponse variable

Hey All,

The more I read more confused I get!. Blueprints/templates/JSON etc. etc.

Thought this would be so simple.
The error is (of course!) dict object has no attribute tracks - so somehow/somewhere random_track needs to be defined??.
Works in developer mode i.e. get a valid output
Read that I need to have a proper trigger - so set that up i.e. no manual UI trigger

Can’t see any obvious documentation how/where this response variable lives/is defined.

Any help much appreciated in the quest to learn!

sequence:
  - action: music_assistant.get_library
    data:
      media_type: track
      order_by: random
      limit: 1
      search: Adele
      config_entry_id: 01JGEMZEHQR5E03K1GD0FNT4V7
    response_variable: random_track
  - action: music_assistant.play_media
    data:
      media_id: "{{ random_track.tracks[0].uri }}"
      media_type: track
      enqueue: play
      radio_mode: true
    target:
      device_id: a6edf1c0a30c59581f4be8590e50c088type or paste code here

I have not tested this nor the time but the get_library kicks-back ‘items’ and then a list
Should it then not be

{{ random_track.items[0].uri }}

Can you paste that output here, formatted as code?

Open your automation and click Traces in the top right corner, then click the music_assistant.get_library action and see what you get in the tab Changed Variables. Is your response variable random_track populated with the expected result?

Firstly thanks for all the help.
I think I’m almost there!

Some issues - get_library random is bugged and sometimes pulls in an empty list - I can see this noted on GitHub .
Also it seems some tagging Issues - Miss Peyroux makes an appearance!

Also as I’m using assist; the variable ‘artist’ causes an intent error - whilst ‘album’ appears ok. So though I’m looking for an artist to play randomly I use the album variable .

But when it kind of works output is this:

items:
  - media_type: album
    uri: library://album/173
    name: Half The Perfect World
    version: ""
    image: >-
      https://www.theaudiodb.com/images/media/album/thumb/half-the-perfect-world-5026364c7e958.jpg
    artists:
      - media_type: artist
        uri: library://artist/147
        name: Madeleine Peyroux
        version: ""
        image: null
  - media_type: album
    uri: library://album/57
    name: "30"
    version: ""
    image: https://www.theaudiodb.com/images/media/album/thumb/rxsa9j1637333381.jpg
    artists:
      - media_type: artist
        uri: library://artist/46
        name: Adele
        version: ""
        image: null
limit: 25
offset: 0
order_by: random
media_type: album

To hopefully play a random track:

    action: 
      - service: "music_assistant.get_library"
        data:
          config_entry_id: 01JGEMZEHQR5E03K1GD0FNT4V7
          media_type: album
          search: "{{ album }}"
          order_by: random
          album_artists_only: false
        response_variable: random_album
      
      - service: "music_assistant.play_media"
        data:
          media_id: "{{ random_album[0].uri }}"
          media_type: album
          enqueue: play
          radio_mode: true
        target:
          entity_id: "media_player.squeezeplay_dc_a6_32_b1_40_c3"
    speech:
      text: "playing random"

As already suggested by @vingerha above, it looks like your response variable does not contain a list but rather a dict where the list is under the key items. Have you tried following his suggestion to replace random_album[0].uri with random_album.items[0].uri?

Sorry yes tried that. TBH even changing it back to my original text I now get “an unexpected error occurred” when using assist . So now it doesn’t work at all; did a reboot - the same.
Assist intents seem very hit or miss.

random_album.items[0].uri throws this error:

Error: Error rendering data template: UndefinedError: builtin_function_or_method object has no element 0

My current theory is that because random_album has an items attribute, Jinja thinks it’s a dictionary, when it’s not.

I’ve raised a bug with MA as their example doesn’t work

As I just happened to see in other threads (related to fetching data from REST APIs), you cannot access a dict key with the name items using dot notation in Jinja. This because it conflicts with the already existing items() function. To avoid this conflict and get at a key items you must use bracket notation:

random_album['items'][0].uri
1 Like

…and correct quotes; while you’re at it, always use bracket notation for safety.

random_album['items'][0]['uri']

That works, thanks guys!

Yeah sorry about that, typed it up on the phone and couldn’t figure out how to get the correct character on the onscreen keyboard and barely not even see whether it was correct or not in the message box (obvious to see it is wrong in the thread though when rendered in a markdown code block).

Hmmmm , not working for me .

“ {{ random_album[‘items’][0][‘uri’] }} “

Tried dropping the {{ in hope but no joy

System log says can’t resolve to playable media .
Developer action tools ( don’t use random ) as it’s busted so use ‘name’ seems ok