Can't use templates in Universal Media Player config

Hi, I’m trying to set up a universal media player and I’m having some issues.

My goal is to have volume commands call the remote.send_command service if my TV is on, and just do their default behaviour otherwise. The default behaviour of course does nothing to my TV.

I thought templating would be my solution, but I can’t get it working in this specific part of the config. I found an old archived post complaining of the exact same thing - with no solution. That being said, I could just have a syntax error?

Here’s the relevant YAML (looks fine in the template editor btw):

media_player:
  - platform: universal
    name: Active Media Player
    children:
      - media_player.living_room_tv
      - media_player.all_speakers
      - media_player.living_room_speaker
      - media_player.bathroom_speaker
      - media_player.bedroom_speaker
    commands:
      volume_up: >
        {% if is_state('media_player.living_room_tv', 'idle') %}
        service: remote.send_command
        target:
          entity_id: remote.universal_remote
        data:
          device: TV
          command: volumeUp
        {% else %}
        service: media_player.volume_up
        target:
          entity_id: media_player.active_media_player
        {% endif %}
    browse_media_entity: media_player.living_room_tv
    device_class: tv
    unique_id: all_media_players

For reference, this is what the template editor shows on the right:

media_player:
  - platform: universal
    name: Active Media Player
    children:
      - media_player.living_room_tv
      - media_player.all_speakers
      - media_player.living_room_speaker
      - media_player.bathroom_speaker
      - media_player.bedroom_speaker
    commands:
      volume_up: >
        
        service: media_player.volume_up
        target:
          entity_id: media_player.active_media_player
        
    browse_media_entity: media_player.living_room_tv
    device_class: tv
    unique_id: all_media_players

And this is the error I get trying to use this config:

Invalid config for 'universal' from integration 'media_player' at configuration.yaml, line 49: expected a dictionary for dictionary value 'commands->volume_up', got "{% if is_state('media_player.living_room_tv', 'idle') %} service: remote.send_command target:\n  entity_id: remote.universal_remote\ndata:\n  device: TV\n  command: volumeUp\n{% else %} service: media_player.volume_up target:\n  entity_id: media_player.active_media_player\n{% endif %}\n"

i haven’t done a media_player entity like this so i am hesitant about trying to just give you fully working code, however i would tell you that i don’t believe that you can use a template to output a block of text that looks like commands and have it be interpreted as commands.

if i were you, i would have your volume_up command call a script.

service: script.universial_volume_up

and in your script do your conditional properly (using proper yaml… not jinja).

btw, is

- platform: universal

right? instead of

- platform: template

?