Use a template to define a variable before a service call

I have a automation that call a Chromecast device to play a YouTube video. That part of the script looks like this:

- service: media_player.play_media
target:
entity_id:
- media_player.chromecast0324
data:
media_content_type: cast
media_content_id: " { \"app_name\": \"youtube\", \"media_id\": \"DX6SXrSDErY\" }"

I have some template code that looks like this:

{% set video = "" %}

{% if trigger == "Video-Freeze" %}
{% set video = "SnU6YUhSQdg" %}
{% endif %}

{% if trigger == "Video-Spring" %}
{% set video = "kAoLNs2xshU" %}
{% endif%}

What I want to do is use the template code to determine the value of the video and then use that value in the media_content_id where there is currently the DX6SXrSDErY value . I know I could replace that with {{video}} if I can figure out how to include the template code.

Any help greatly appreciate

Script Syntax: Variables

If you need more specific help, post the complete automation so we can see how the pieces fit together instead if having to guess.

So here is the full automation :

alias: Start Video Automation When Tag Scanned
description: ""
trigger:
- id: Video-Freeze
platform: tag
tag_id: 04-43-EB-01-74-05-03
- id: Video-DanceMonkey
platform: tag
tag_id: 04-43-C4-01-5B-05-03
- id: Video-Spring
platform: tag
tag_id: 04-43-D9-01-86-05-03
- id: Video-BabyShark
platform: tag
tag_id: 04-43-A1-01-40-05-03
condition: []
action:
- service: light.turn_on
data: {}
target:
device_id: 2c2698657b6541db250237cd73d24d08
- service: select.select_option
data:
option: "{% set short = trigger.id.split('-')%}{{short[1]}}
target:
entity_id: select.wled_soundreactive_preset_2
device_id:
- 2c2698657b6541db250237cd73d24d08
- b588b1ba6e337a04cc7b00e935bd4145
- service: media_player.play_media
target:
entity_id:
- media_player.chromecast0324
data:
media_content_type: cast
media_content_id: " { \"app_name\": \"youtube\", \"media_id\": \"DX6SXrSDErY\" }"

The above works as is.

What I am trying to do is use one automation to call different effects.

The Tag IDs reflect the name of playlists and WLED effects. So you can see the code that stripes out the part of the tag_ids and uses it to call the playlists and WLED effects.

The last part with the chromecast is to play a video along with the WLED and laser lights. The problem is each YouTube video has it’s own media ID (that DX6SXrSDErY thing) so what I am attempting to do is select that media ID when the NFC is scanned by cross referencing the YouTUbe media ID with the NFC tag id.

Worse comes to worse I could each put each video play call into its own script and just call that but I was looking for a simpler more maintainable solution. The series of IF statements in the first post was my attempt to do the cross reference matrix but I don’t know how to “fit” it into the automation.

BTW this whole thing is going to be a experience room for by autistic grandson - he loves sounds, lights and videos

As with everything in HA are there a couple ways to do this. I would probably use trigger-attached variables… it puts adding, editing, or removing triggers and videos all in one place.

alias: Start Video Automation When Tag Scanned
description: ""
trigger:
  - id: Video-Freeze
    platform: tag
    tag_id: 04-43-EB-01-74-05-03
    variables:
      video: xxxxxxxx
  - id: Video-DanceMonkey
    platform: tag
    tag_id: 04-43-C4-01-5B-05-03
    variables:
      video: xxxxxxxxx
  - id: Video-Spring
    platform: tag
    tag_id: 04-43-D9-01-86-05-03
    variables:
      video: xxxxxxx
  - id: Video-BabyShark
    platform: tag
    tag_id: 04-43-A1-01-40-05-03
    variables:
      video: DX6SXrSDErY
condition: []
action:
  - service: light.turn_on
    data: {}
    target:
      device_id: 2c2698657b6541db250237cd73d24d08
  - service: select.select_option
    data:
      option: "{% set short = trigger.id.split('-')%}{{short[1]}}"
    target:
      entity_id: select.wled_soundreactive_preset_2
      device_id:
        - 2c2698657b6541db250237cd73d24d08
        - b588b1ba6e337a04cc7b00e935bd4145
 - service: media_player.play_media
    target:
      entity_id:
        - media_player.chromecast0324
    data:
      media_content_type: cast
      media_content_id: |-
        {{ {'app_name':'youtube', 'media_id': video}|to_json }}