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

?

1 Like

That’s actually exactly what I ended up doing! I guess I had some wrong ideas about how jinja works in HA. Calling a script is at least… valid! Still having some issues with getting it all working 100%, but I’ll be sure to post my yaml once I have it all ironed out.
Thanks!

cool. holler and post your yaml if you need help…

It’s been a while, but I did end up getting back to this and getting it working!
In case it helps anyone else, I’ll post some of my YAML:

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: script.volume_up_script
      volume_down:
        service: script.volume_down_script
      volume_mute:
        service: script.volume_mute_script
    browse_media_entity: media_player.living_room_tv
    device_class: tv
    unique_id: all_media_players

And the scripts are all pretty similar, here’s the volume up one (Note that I have another universal media player named active_speaker, and I directly set the volume on it if the TV is not active)

volume_up_script:
  alias: volume_up_script
  sequence:
  - choose:
    - conditions:
      - condition: or
        conditions:
        - condition: state
          entity_id: media_player.living_room_tv
          state: idle
        - condition: state
          entity_id: media_player.living_room_tv
          state: 'off'
        - condition: state
          entity_id: media_player.living_room_tv
          state: unavailable
      sequence:
      - if:
        - condition: or
          conditions:
          - condition: state
            entity_id: media_player.active_speaker
            state: 'on'
          - condition: state
            entity_id: media_player.active_speaker
            state: playing
          - condition: state
            entity_id: media_player.active_speaker
            state: paused
          - condition: state
            entity_id: media_player.active_speaker
            state: buffering
        then:
        - service: media_player.volume_up
          target:
            entity_id:
            - media_player.active_speaker
          data: {}
    default:
    - service: remote.send_command
      metadata: {}
      data:
        device: TV
        command: volumeUp
      target:
        entity_id: remote.universal_remote
  mode: single
  icon: mdi:volume-plus
1 Like