Enque to chromecast (youtube)

I am trying to make an “enque” button, that queues a video on my chromecast.
i can get it worknig with “static” video id. but i would like to get the video id from a text_input helper.
This is a working button service call:

# This button works fine. but is not dynamic. 
type: button
tap_action:
  action: call-service
  service: media_player.play_media
  service_data:
    media_content_type: cast
    media_content_id: >
      {"app_name": "youtube","enqueue": "true","media_id": "5qap5aO4i9A"}
  target:
    entity_id: media_player.tv_tv
icon: mdi:animation-play
name: Add video to queue

but this is not:

# This button gives this error:
# Failed to call service media_player/play_media. Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

type: button
tap_action:
  action: call-service
  service: media_player.play_media
  service_data:
    media_content_type: cast
    media_content_id: >
      {% set msg = { "app_name": 'youtube', 'enqueue': 'true', 'media_id': states('input_text.vidid') } %}
      {{ msg|to_json }}
  target:
    entity_id: media_player.tv_tv
icon: mdi:animation-play
name: Add video to queue

the service call above does work in the developer tools (services):

# this works, and queues the video on the chromecast

service: media_player.play_media
target:
  entity_id: media_player.tv_tv
data:
  media_content_type: cast
  media_content_id: >
      {% set msg = { "app_name": 'youtube', 'enqueue': 'true', 'media_id': states('input_text.vidid') } %}
      {{ msg|to_json }}

I have been checking my templates throughout the process in the developer tools, and they render to valid json strings.
But i get errors when trying to get the button working regardless.

so i am totally stuck as to why this won’t work.

Any help is greatly appreciated!

I havent been able to familiarize myself with the home assistant codebase yet, so maybe that’s my next step?

Aha! The issue is that buttons do not support templates.
So i just wrote it as a script and called the script from the button.

sequence:
  - service: media_player.play_media
    target:
      entity_id: media_player.tv_tv
    data:
      media_content_id: >-
        {% set msg
        ={'app_name':'youtube','enqueue':'true','media_id':states('input_text.vidid')}%}{{
        msg|to_json }}
      media_content_type: cast
mode: single
alias: Queue to chromecast
icon: mdi:playlist-music-outline
alias: Play on chromecast
sequence:
  - service: media_player.play_media
    target:
      entity_id: media_player.tv_tv
    data:
      media_content_id: >-
        {% set msg
        ={'app_name':'youtube','media_id':states('input_text.vidid')}%}{{
        msg|to_json }}
      media_content_type: cast
mode: single
icon: mdi:youtube

(the line breaks in the template is generated by the UI, i just pasted the template into the “sequence” of the script)

and this is the button:

type: button
tap_action:
  action: call-service
  service: script.queue_to_chromecast
  service_data: {}
  target: {}
hold_action:
  action: call-service
  service: script.play_on_chromecast
  service_data: {}
  target: {}
name: play on
show_icon: false
1 Like