Template for media player source selection

I have a stereo receiver media_player.gym_receiver that I want to either play audio from the TV (source=TV Audio) or play music through a Chromecast device (Source=Media Player). I want the chromecast device to dictate the source. If the Chromecast device is “playing” I want the source to be Media Center. Otherwise, I want the source to be on TV Audio.

I’ve tried this code but it doesn’t work.

alias: Gym Audio - Switch source based on what's playing
description: ""
trigger:
  - platform: state
    entity_id:
      - switch.gym_speakers
condition: []
action:
  - service: media_player.select_source
    data:
      source: {{'Media Player' if trigger.to_state.state == 'Playing' else 'TV Audio' }}
    target:
      entity_id: media_player.gym_receiver
mode: single

Any idea how to make this work?

This doesn’t look right.
It would output (if correctly syntaxed):
Media PlayerPlaying Isn’t there supposed to be a space?

{{'Media Player ' ~ if trigger.to_state.state == 'Playing' else 'TV Audio' }}

Perhaps?

I tried your suggestions, unfortunately, it didn’t work.

Try trigger.entity_id

if trigger.entity_id == ‘Playing’