Template Editor vs. Script Implementation

In order to save myself what little hair I have left I’d like to understand better why something that works in the template editor completely borks when being run as a script.

Using MA’s get_queue action in dev tools I copy the result over to the template editor and can access the object using something like this.

{% set action_response = {"media_player.livingroom_stereo":{"queue_id":"apc... etc" %}
{% set queue_info = action_response['media_player.livingroom_stereo'] %}
{{ queue_info.current_item.stream_title }}

Works like a charm, but if I pass that into a script and try to run it (even just reloading the templates) it borks and throws the error:
Template variable error: 'dict object' has no attribute 'current_item' when rendering '{{ queue_info.current_item.stream_title }}'
Instead I have to use something like this for it to work in a script.

{% set action_response = {"media_player.livingroom_stereo":{"queue_id":"apc... etc" %}
{{ action_response ['media_player.livingroom_stereo'].current_item.stream_title }}

So what’s the difference between how the template editor and the script engine parse this?
The script:

# This works
      - action: music_assistant.get_queue
        target:
          entity_id: media_player.livingroom_stereo
        response_variable: queue_info

      - event: test_script
        event_data:
          stream_title: "{{ queue_info['media_player.livingroom_stereo'].current_item.stream_title }}"
      - stop: null
# This borks
      - action: music_assistant.get_queue
        target:
          entity_id: media_player.livingroom_stereo
        response_variable: queue_info

      - variables:
          queue_two: "{{ queue_info['media_player.livingroom_stereo'] }}"

      - event: test_script
        event_data:
          stream_title: "{{ queue_two.current_item.stream_title }}"
      - stop: null

There’s no definition for queue_two

What’s the full response from your action_response? Need the full response in order to provide a reason why it doesn’t work.

Thanks… copy pasta as Frenck would say, I’ve updated my original post with the variable definition in my script. It was the point of my template editor experiment after all :roll_eyes:

This is what gets copied from dev tools > actions.

{% set action_response = {"media_player.livingroom_stereo":{"queue_id":"apc2f53503cbbc","active":true,"name":"Livingroom Stereo","items":69,"shuffle_enabled":false,"repeat_mode":"off","current_index":9,"elapsed_time":14197,"current_item":{"queue_item_id":"1ad41538ba1e4532bcb3ee6a0e008bed","name":"Dire Straits - Sultans of Swing","duration":350,"media_item":{"media_type":"track","uri":"library://track/2","name":"Sultans of Swing","version":"","image":null,"favorite":false,"explicit":null,"artists":[{"media_type":"artist","uri":"library://artist/1","name":"Dire Straits","version":"","image":null}],"album":{"media_type":"album","uri":"library://album/1","name":"Sultans of Swing: The Very Best of Dire Straits","version":"","image":null,"favorite":false,"explicit":null,"discart_image":null,"artists":[{"media_type":"artist","uri":"library://artist/1","name":"Dire Straits","version":"","image":null}]}},"stream_title":null,"stream_details":{"provider":"filesystem_smb--whAumDbs","item_id":"Music/Dire Straights/Dire Straits- Sultans of Swing .mp3","content_type":"mp3","sample_rate":44100,"bit_depth":16,"bit_rate":320}},"next_item":{"queue_item_id":"cbe87c10e3f047c98f98543bf6cab89b","name":"Timothy Scott McNabb - Friends That Last","duration":206,"media_item":{"media_type":"track","uri":"library://track/368","name":"Friends That Last","version":"","image":"https://i.scdn.co/image/ab67616d0000b2733578e5e080d1cbae86f5faa2","favorite":true,"explicit":false,"artists":[{"media_type":"artist","uri":"library://artist/12","name":"Timothy Scott McNabb","version":"","image":null}],"album":{"media_type":"album","uri":"library://album/40","name":"Bare Bones","version":"","image":"https://i.scdn.co/image/ab67616d0000b2733578e5e080d1cbae86f5faa2","favorite":true,"explicit":null,"discart_image":null,"artists":[{"media_type":"artist","uri":"library://artist/12","name":"Timothy Scott McNabb","version":"","image":null}]}},"stream_title":null,"stream_details":{"provider":"spotify--tux4BSU5","item_id":"0nYxvpk6iX99Lb7Vs1Ci4F","content_type":"ogg","sample_rate":44100,"bit_depth":16,"bit_rate":320}}}} %}

That’s not valid. When the resolver attempts to assign a type to the response, it will fail, which results in queue_two not being defined.

Coming directly from the response, it will work because it’s already typed. But as soon as you attempt to do this:

queue_two: "{{ queue_info['media_player.livingroom_stereo'] }}"

the system is trying to type it again because it’s a new template result.


Long story short, use the response only. Or traverse the object and remove the nulls

THANK YOU! That makes sense to me, I can keep my remaining hair :grinning:.

I do test for values later in the script as they are sometimes defined depending on what’s in the queue, never thought to test the original assignment. Thanks for the lesson.