Help with script to output YAML code instead of URl

Hello,

I use this script to select and send a radiostream to a mediaplayer. So far so good. However, my players are Chromecast devices and i also want to stream youtube video’s for example.
Also i want to add in artist, image etc as metadata.
I have this working by extending the code but that means that for each choise i need to edit/add the variable thruout the whole script file. My goal here is to put everything in 1 “block” of YAML code as a variable but i cant seem to get the code right to output the multiline YAML code.

In short, How do i change this code:

{% if is_state("input_select.radio_source", "Radio 538") %} http://playerservices.streamtheworld.com/api/livestream-redirect/RADIO538 {% elif is_state("input_select.radio_source","Sublime") %} https://playerservices.streamtheworld.com/api/livestream-redirect/SUBLIME.mp3 {% endif %}

To this (but then working abviously :slight_smile: ):

{% if is_state("input_select.radio_source", "Hallo Kids Radio") %} media_content_type: audio/mp3 media_content_id: https://ice.cr1.streamzilla.xlcdn.com:8000/sz=Hallokidsradio=mp3 extra: metadata: metadataType: 3 title: HalloKids Radio images: - url: >- https://www.hallokidsradio.nl/wp-content/uploads/2021/03/Logo_HKR-300x150.png {% elif is_state("input_select.radio_source","Youtube Video") %} service_data: name: Openhaard media_content_type: cast media_content_id: ' { "app_name": "youtube", "media_id": "xGN0iBi1zUY" }' {% endif %}

That way each choice variable is nicely together in one codeblock instead of scatered around the script plus gives more plexibility for different mediatypes.

Any help is much apriciated and will share the resulting script to the public.

You can’t template YAML code.

Home Assistant processes YAML first, then processes the Jinja2 templates. That’s why you can’t use Jinja2 templates to selectively produce YAML statements.

I suggest you use choose.

1 Like