Media_player.template

You’re using the wrong service for pause; media_play_pause makes that combined button (play-pause) and is overriding the others.

Try this:

pause:
          service: media_player.media_pause
          data:
              entity_id: media_player.chromecast_cucina

That should fix the problem. If you’re still having trouble, I’ll post my full configuration since I’m getting all the buttons correctly.

1 Like

nice looking UI.

which template and icons are those?

Hi Seperis,

can you please share your full config? I retake this and it still doesn’t work as expected.

Thank you in advance!

Whoo boy, I’ve updated since then, but I think this one is what you’re looking for. I’m pasting the whole thing so you have context; just take whatever parts you need:

- platform: media_player_template
  media_players:
    jenn_television:
      friendly_name: Jennifer Television
      value_template: "{{ states('sensor.jenn_television_current_status') }}"
      current_source_template: "{{ states('sensor.jenn_television_current_source') }}"
      current_volume_template: "{{ states('sensor.jenn_television_current_volume') }}"
      turn_on:
        service: switch.toggle
        data_template:
          entity_id: switch.jenn_tv
      turn_off:
        service: switch.toggle
        data_template:
          entity_id: switch.jenn_tv
      play:
        service: media_player.media_play
        data_template:
          entity_id: media_player.nvidia_shield_tv
      stop:
        service: media_player.media_stop
        data_template:
          entity_id: media_player.nvidia_shield_tv
      pause:
        service: media_player.media_pause
        data_template:
          entity_id: media_player.nvidia_shield_tv
      next:
        service: media_player.media_next_track
        data_template:
          entity_id: media_player.nvidia_shield_tv
      previous:
        service: media_player.media_previous_track
        data_template:
          entity_id: media_player.nvidia_shield_tv
      seek:
        service: media_player.media_seek
        data_template:
          entity_id: media_player.nvidia_shield_tv
          seek_position: '10'
      play_media:
        service: media_player.media_seek
        data_template:
          entity_id: media_player.nvidia_shield_tv
          media_content_id: 101
          media_content_type: episode
      volume_up:
        service: media_player.volume_set
        data_template:
          entity_id: media_player.polk_command_bar
          volume_level: "{{ states.media_player.polk_command_bar.attributes.volume_level - '.05' | float }}"
      volume_down:
        service: media_player.volume_set
        data_template:
          entity_id: media_player.polk_command_bar
          volume_level: "{{ states.media_player.polk_command_bar.attributes.volume_level + '.05' | float }}"
      set_volume:
        service: input_number.set_value
        data:
          entity_id: input_number.jenn_television_selected_volume
          value: "{{ volume * 100 | float }}"
      mute:
        service: media_player.volume_mute
        data_template:
          entity_id: media_player.polk_command_bar
          is_volume_muted: True
      inputs:
        TV:
          service: media_player.select_source
          data_template:
            entity_id: media_player.jenn_tv_smart
            source: "TV"
        Home Assistant:
          service: media_player.select_source
          data_template:
            entity_id: media_player.jenn_tv_smart
            source: "Home Assistant"
        Home Theatre:
          service: media_player.select_source
          data_template:
            entity_id: media_player.jenn_tv_smart
            source: "Home Theatre"
        Shield TV:
          service: remote.send_command
          data_template:
            device: polk_command_bar
            command: HDMI 2
        Shield Home:
          service: androidtv.adb_command
          data_template:
            entity_id: media_player.nvidia_shield_tv
            command: HOME
        Prime:
          service: androidtv.adb_command
          data_template:
            entity_id: media_player.nvidia_shield_tv
            command: am start -n com.amazon.amazonvideo.livingroom/com.amazon.ignition.IgnitionActivity
        CBS:
          service: media_player.select_source
          data_template:
            entity_id: media_player.nvidia_shield_tv
            source: "CBS All Access"
        Disney+:
          service: media_player.select_source
          data_template:
            entity_id: media_player.nvidia_shield_tv
            source: "Disney+"
        Google Play:
          service: media_player.select_source
          data_template:
            entity_id: media_player.nvidia_shield_tv
            source: "Google Play"
        Google Video:
          service: media_player.select_source
          data_template:
            entity_id: media_player.nvidia_shield_tv
            source: "Google Video"
        HBO:
          service: media_player.select_source
          data_template:
            entity_id: media_player.nvidia_shield_tv
            source: "HBO"
        Netflix:
          service: media_player.select_source
          data_template:
            entity_id: media_player.nvidia_shield_tv
            source: "Netflix"
        Pandora:
          service: media_player.select_source
          data_template:
            entity_id: media_player.nvidia_shield_tv
            source: "Pandora"
        Peacock:
          service: media_player.select_source
          data_template:
            entity_id: media_player.nvidia_shield_tv
            source: "Peacock"
        Plex:
          service: media_player.select_source
          data_template:
            entity_id: media_player.nvidia_shield_tv
            source: "Plex"
        Prime Music:
          service: androidtv.adb_command
          data_template:
            entity_id: media_player.nvidia_shield_tv
            command: "Amazon Music"
        Tiny Cam:
          service: media_player.select_source
          data_template:
            entity_id: media_player.nvidia_shield_tv
            source: "Tiny Cam"
        YouTube:
          service: media_player.select_source
          data_template:
            entity_id: media_player.nvidia_shield_tv
            source: "YouTube"
      title_template: "{{ states('sensor.jenn_television_media_title') }}"
      media_content_type_template: "{{ states('sensor.jenn_television_media_content_type') }}"
      media_image_url_template: "{{ states('sensor.jenn_television_media_image_url') }}"
      media_episode_template: "{{ states('sensor.jenn_television_media_episode') }}"
      media_season_template: "{{ states('sensor.jenn_television_media_season') }}"
      media_series_title_template: "{{ states('sensor.jenn_television_media_series_title') }}"
      current_position_template: "{{ states('sensor.jenn_television_media_current_position') }}"
      media_duration_template: "{{ states('sensor.jenn_television_media_duration') }}"
      artist_template: "{{ states('sensor.jenn_television_artist') }}"
      album_template: "{{ states('sensor.jenn_television_album') }}"
      media_album_artist_template: "{{ states('sensor.jenn_television_media_album_artist') }}"
      album_art_template: "{{ states('sensor.jenn_television_album_art') }}"
      current_sound_mode_template: "{{ states('sensor.jenn_television_current_sound_mode') }}"
3 Likes

Thank you!

I was wondering what are those sensors:

  value_template: "{{ states('sensor.jenn_television_current_status') }}"
  current_source_template: "{{ states('sensor.jenn_television_current_source') }}"
  current_volume_template: "{{ states('sensor.jenn_television_current_volume') }}"

Those are custom sensors I wrote for only the media template to use. They use data from the sensors created by the SmartThings integration for Samsung SmartTV, the Android integration for NVIDIA Shield, and the Alexa integration for the soundbar.

The current source sensor integrates the source drop down from Samsung with the NVIDIA’s streaming apps (SmartThings plus android services). It’s still in first form; it works, but it’s not very elegant. It’s probable to get the sensors as precise as I want, using the sensor template isn’t going to cut it and I’ll have to switch to writing python script to create them at restart every time. But that’s a problem for when I’m better at python; I just created a working! (WORKING!) script to create entities out of each attached device on my netgear router (after netgear_enhanced runs to get teh raw data), so it may not be as long as I thought.

    jenn_television_current_status:
      friendly_name: Jenn Television Status
      value_template: >-
        {% if is_state("switch.jenn_tv", "off") -%}
          {% set stateValue = "Off" %}
        {% else %}
          {% set sourceValue = states('sensor.jenn_tv_media_input_source') %}
          {% if sourceValue == "TV" %}
            {% set stateValue = "On" %}
          {% elif sourceValue == "HDMI1" %}
            {% set stateValue = "Idle" %}
          {% else %}
            {% set playValue = states('media_player.nvidia_shield_tv') %}
            {% if playValue != 'unavailable' %}
              {% set stateValue = playValue -%}
            {% else %}
              {% set stateValue = "On" %}
            {% endif %}
          {% endif %}
        {% endif %}
        {{ stateValue }}

    jenn_television_current_source:
      friendly_name: Jenn Television Source
      value_template: >-
        {% set samSource = states('sensor.jenn_tv_media_input_source') %}
        {% if samSource == "HDMI1" %}
          {% set source = "Home Assistant" %}
        {% elif samSource == "HDMI2" %}
          {% set playValue = states('media_player.nvidia_shield_tv') %}
          {% if playValue != 'unavailable' %}
            {% set sourceName = states('sensor.nvidia_shield_tv_current_app') -%}
            {% if sourceName == "Home" %}
              {% set source = "Shield Home" %}
            {% elif sourceName == "Amazon Prime" %}
              {% set source = "Prime" %}
            {% elif sourceName == "Amazon Music" %}
              {% set source = "Prime Music" %}
            {% elif sourceName == "CBS All Access" %}
              {% set source = "CBS" %}
            {% else %}
              {% set source = sourceName %}
            {% endif %}
          {% else %}
            {% set source = "Home Theatre" -%}
          {% endif %}
        {% else %}
          {% set source = "TV" %}
        {% endif %}
        {{ source }}

    jenn_television_current_volume:
      friendly_name: Jenn Television Current Volume
      value_template: >
        {% if is_state('media_player.polk_command_bar','offline') -%}
          {% set volume = 0 %}
        {% else %}
          {% set volume = state_attr('media_player.polk_command_bar', 'volume_level') | float %}
        {% endif %}
        {{ volume }}
2 Likes

Popping in here to say thanks to everyone. First time using HA and didn’t really know how to approach this so this forum/module was super handy in mindlessly getting things working.

I have an old TV I really wanted to control via Google Assistant and HA + this custom module + node_red was perfect in connecting my Raspberry Pi (running HA) connecting to my TV via Serial to control it. Obviously, it’s not perfect but it does exactly what I need at the moment.

As a note, I had to use helper variables (Configuration > Helpers) to make it work properly since I didn’t really know a better way to build an entity with variables that’ll stick.

media_player:
  - platform: media_player_template
    media_players:
      pitv:
        friendly_name: Pi TV
        device_class: tv
        media_content_type_template: video
        value_template: "{{ states('switch.pitv') }}"
        current_is_muted_template: >
          {% if is_state("input_boolean.pitv_mute_state", "on") -%}
            True
          {%- else -%}
            False
          {%- endif %}
        current_volume_template: "{{ states('input_number.pitv_volume') }}"
        current_source_template: "{{ states('input_text.pitv_source') }}"
        turn_on:
          service: switch.toggle
          data_template:
            entity_id: switch.pitv
        turn_off:
          service: switch.toggle
          data_template:
            entity_id: switch.pitv
        volume_up:
          service: remote.send_command
          data:
            entity_id: remote.command
            command:
              - VolumeUp
            device: pitv
        volume_down:
          service: remote.send_command
          data:
            entity_id: remote.command
            command:
              - VolumeDown
            device: pitv
        set_volume:
          service: input_number.set_value
          data:
            entity_id: input_number.pitv_volume
            value: "{{ volume | float }}"
        mute:
          service: input_boolean.toggle
          data:
            entity_id: input_boolean.pitv_mute_state
        inputs:
          TV:
            service: input_text.set_value
            data_template:
              entity_id: input_text.pitv_source
              value: "TV"
          HDMI 1:
            service: input_text.set_value
            data_template:
              entity_id: input_text.pitv_source
              value: "HDMI 1"
          HDMI 2:
            service: input_text.set_value
            data_template:
              entity_id: input_text.pitv_source
              value: "HDMI 2"
          HDMI 3:
            service: input_text.set_value
            data_template:
              entity_id: input_text.pitv_source
              value: "HDMI 3"
          AV:
            service: input_text.set_value
            data_template:
              entity_id: input_text.pitv_source
              value: "AV"
          PC:
            service: input_text.set_value
            data_template:
              entity_id: input_text.pitv_source
              value: "PC"
          USB:
            service: input_text.set_value
            data_template:
              entity_id: input_text.pitv_source
              value: "USB"
        play:
          service: media_player.media_play_pause
          data_template:
            entity_id: media_player.bed_tv
        next:
          service: media_player.media_next_track
          data_template:
            entity_id: media_player.bed_tv
        previous:
          service: media_player.media_previous_track
          data_template:
            entity_id: media_player.bed_tv

On the UI I have the play/pause thing showing up so there’s probably something related to the media content type / device id but i can tell google to “play next/previous on Pi TV” and it’s ok “set volume to X on Pi TV” works as well as setting the source. For my TV, the serial doesn’t return states but even though the current states might be wrong but outside of mute which is a toggle the rest will realign itself. I probably should change my mute command to be related to the state rater than just toggle but it’s also fine.

I am encountering the same issue as some other members with the Prev/Next buttons missing.

If I’m understanding correctly, this happens when anything but media_player.media_play is used as the service for the play: action. And both play: and pause: must be explicitly defined.

In my configuration I am calling individual scripts for play: and pause: actions, which is also causing the Prev/Next buttons to disappear.

Is there a possible workaround to bring back the Prev/Next buttons and also retain the dynamic Play + Pause button? I have a feeling before this can be fixed, we’ll need a template value to dictate the state of the dynamic Play + Pause button…

Config below for posterity:

media_player:
  - platform: media_player_template
    media_players:
      speakeraio:
        friendly_name: SpeakerAIO
        device_class: speaker
        media_content_type_template: music
        value_template: "{{ state_attr('sensor.speaker', 'power') }}"
        current_source_template: "{{ states('sensor.speaker') }}"
        current_volume_template: "{{ state_attr('media_player.speaker', 'volume_level') }}"
        current_is_muted_template: "{{ state_attr('media_player.speaker', 'is_volume_muted') }}"
        artist_template: "{{ state_attr('sensor.speaker', 'artist') }}" 
        title_template: "{{ state_attr('sensor.speaker', 'song_title') }}"
        media_image_url_template: "{{ state_attr('sensor.speaker', 'cover') }}"
        turn_on:
          service: switch.turn_on
          data_template:
            entity_id: switch.speaker_power
        turn_off:
          service: switch.turn_off
          data_template:
            entity_id: switch.speaker_power
        volume_up:
          service: media_player.volume_up
          data_template:
            entity_id: media_player.speaker
        volume_down:
          service: media_player.volume_down
          data_template:
            entity_id: media_player.speaker
        set_volume:
          service: media_player.volume_set
          data_template:
            entity_id: media_player.speaker
            volume_level: "{{ volume_level | float }}"
        mute:
          service: media_player.volume_mute
          data_template:
            entity_id: media_player.speaker
            is_volume_muted: "{{ not state_attr('media_player.speaker', 'is_volume_muted') }}"
        
        # Prev and Next buttons do not show up.
        play:
          service: script.speaker_play
        pause:
          service: script.speaker_pause
        next:
          service: script.speaker_next
        previous:
          service: script.speaker_previous

I can not get it to display the next and previous buttons as well.

Also, I can not get the progress/seek bar as well for some reason, even though I had defined the current_position and media_duration. Also, how can I implement the playmedia button that lets you play a media from home assistant, as it could be seen on the chrome cast player image below?

So, I have 3 problems: next/previous, progress bar, play media.

I would appreciate any help with this, however, I think there has to be something with the code. maybe there is something going on with using it with a Chromecast player since this is very similar to Jokerigno’s problem.

top player is template and bottom is Chromecast
Screen Shot 2021-11-22 at 7.34.34 PM

I also have everything else working fine, set volume, mute, inputs, sound modes, on/off…
Screen Shot 2021-11-22 at 7.34.13 PM
Screen Shot 2021-11-22 at 7.33.58 PM

I’m actually using the media_player.template as an interface for my esphome rs232 controller for my sony avr. nice thing is that I can also use media_player.template to expose it to google home so I can control all the settings with voice using a google home mini.

The trick with using it with google is to make it recognize the player as a set-top box or an AVR. I believe google uses AI to determine what type it is from the device name since I think there is no way to specify the device type.
Google home device types are explained here

here is how the same player can have different device types in google home:

here is my config:

media_player:
  - platform: media_player_template
    media_players:
      receiver:
        friendly_name: "Audio-Video Receiver"
        device_class: receiver
        current_source_template: "{{ states('sensor.avr_source') }}"
        value_template: >
          {% if is_state("switch.avr_power", "on") -%}
            on
          {%- else -%}
            off
          {%- endif %}
        turn_on:
          service: switch.turn_on
          data_template:
            entity_id: switch.avr_on
        turn_off:
          service: switch.turn_on
          data_template:
            entity_id: switch.avr_off
        volume_up:
          service: switch.turn_on
          data_template:
            entity_id: switch.avr_volume_up
        volume_down:
          service: switch.turn_on
          data_template:
            entity_id: switch.avr_volume_down

        play:
          service: media_player.media_play
          data_template:
            entity_id: media_player.str_dn1080_2
        pause:
          service: media_player.media_pause
          data_template:
            entity_id: media_player.str_dn1080_2
        stop:
          service: media_player.media_stop
          service_data:
            entity_id: media_player.str_dn1080_2            
        next:
          service: media_player.media_next_track
          data_template:
            entity_id: media_player.str_dn1080_2
        previous:
          service: media_player.media_previous_track
          data_template:
            entity_id: media_player.str_dn1080_2

        sound_modes:
          Direct:
            service: switch.turn_on
            data_template:
              entity_id: switch.avr_soundfield_direct
          Multi Channel Stereo:
            service: switch.turn_on
            data_template:
              entity_id: switch.avr_soundfield_multi_channel_stereo
          2 Channel Stereo:
            service: switch.turn_on
            data_template:
              entity_id: switch.avr_soundfield_2_channel_stereo
          Front Surround:
            service: switch.turn_on
            data_template:
              entity_id: switch.avr_soundfield_front_surround
          Neural:X:
            service: switch.turn_on
            data_template:
              entity_id: switch.avr_soundfield_neural_x
          Dolby Surround:
            service: switch.turn_on
            data_template:
              entity_id: switch.avr_soundfield_dolby_surround
          A.F.D.:
            service: switch.turn_on
            data_template:
              entity_id: switch.avr_soundfield_a_f_d

        inputs:
          Turntable:   # SA-CD/CD
            service: switch.turn_on
            data_template:
              entity_id: switch.avr_source_sa_cd_cd
          Computer:     # VIDEO 1
            service: switch.turn_on
            data_template:
              entity_id: switch.avr_source_video_1
          Apple TV:  # VIDEO 2
            service: switch.turn_on
            data_template:
              entity_id: switch.avr_source_video_2
          Cable TV:  # SAT/CATV
            service: switch.turn_on
            data_template:
              entity_id: switch.avr_source_sat_catv
          TV:
            service: switch.turn_on
            data_template:
              entity_id: switch.avr_source_tv
          DVD:   # BD/DVD
            service: switch.turn_on
            data_template:
              entity_id: switch.avr_source_bd_dvd
          GAME:   # GAME
            service: switch.turn_on
            data_template:
              entity_id: switch.avr_source_game
          FM Tuner:
            service: switch.turn_on
            data_template:
              entity_id: switch.avr_source_fm_tuner
          USB:
            service: switch.turn_on
            data_template:
              entity_id: switch.avr_source_usb
          Bluetooth:
            service: switch.turn_on
            data_template:
              entity_id: switch.avr_source_bluetooth
          Home Network:
            service: switch.turn_on
            data_template:
              entity_id: switch.avr_source_home_network
          Music Service List:
            service: switch.turn_on
            data_template:
              entity_id: switch.avr_source_music_service_list
#------------------------------------------------       
        set_volume:
          service: number.set_value
          data:
            entity_id: number.avr_volume
            #value: "{{((volume*74)|round|string|replace('.0', ''))|int}}"
            value: "{% set vol = ((volume*100)|round|string|replace('.0', ''))|int  -%}{% if vol > 74 %}74{% else %}{{vol}}{% endif %}"
        #current_volume_template: "{{ states('number.avr_volume')|int/74 }}"
        current_volume_template: "{{ states('number.avr_volume')|int/100 }}"    
        mute:
          service: switch.toggle
          data:
            entity_id: switch.avr_mute
        current_is_muted_template: >
          {% if is_state("switch.avr_mute", "on") -%}
            True
          {%- else -%}
            False
          {%- endif %}
        title_template: "{{ states.media_player.str_dn1080_2.attributes.media_title }}"
        media_content_type_template: "{{ states.media_player.str_dn1080_2.attributes.media_content_type }}"
        media_image_url_template: "{{ states.media_player.str_dn1080_2.attributes.entity_picture }}"
        media_duration_template: "{{ states.media_player.str_dn1080_2.attributes.media_duration }}"
        current_position_template: "{{ states.media_player.str_dn1080_2.attributes.media_position }}"
        artist_template: "{{ states.media_player.str_dn1080_2.attributes.media_artist }}"
        album_template: "{{ states.media_player.str_dn1080_2.attributes.media_album_name }}"
        current_sound_mode_template: "{{ states('sensor.avr_sound_field') }}"

I hope this would be useful for someone.

1 Like

implement next_action and previous action.

Not sure what’s required to make this appear. I would assume it’s controlled by the content type, position, and duration.

implement play_media_action, however the docs say (not tested) meaning the dev didn’t try it and it may not work.

I’m really puzzled why you’re thinking that I haven’t already implemented the next_action or previous_action because I believe that I already implemented both next and previous actions???

This is what I have as I already posted my whole config

I think the progress bar is not implemented in the media_player.template so that will have to be a feature request I believe.

Thanks for referring me to your post on github.
However, I think I tried current_volume_template: as you have in your config. It was not working for me.
However, as I was using other media_players for my config, I’m now using the universal media player, which works fine for use case.

Small advice, data_template: is depreciated, you can just use data:. And in the use case of your post above, target: would be the right key to use.

1 Like

Sorry for the late responses. I will check to see if i can fix the volume part

Everything for all devices works off supported features. If the supported features match up with features the front end is looking for, it will create those controls. If you implemented those actions and it does not produce them in the frontend, then the integration doesn’t properly create the supported features in the backend.

Thanks a lot for your suggestion but that didn’t change anything, unfortunately.
My player still does not expose next and previous.


        play:
          service: media_player.media_play
          target:
            entity_id: media_player.str_dn1080_2
        pause:
          service: media_player.media_pause
          target:
            entity_id: media_player.str_dn1080_2
        stop:
          service: media_player.media_stop
          target:
            entity_id: media_player.str_dn1080_2            
        next:
          service: media_player.media_next_track
          target:
            entity_id: media_player.str_dn1080_2
        previous:
          service: media_player.media_previous_track
          target:
            entity_id: media_player.str_dn1080_2

Do you think I have something wrong with my config or there is something going on with the template regarding the previous/next to appear. Jokerigno had the same problem too. I would really appreciate if you could take a look at this issue.

I will try to check it today. I haven’t updated the component in a long time so maybe something changed in HASS

1 Like

So I tested somethings (I needed to reread my code long time ago I checked it)
The problem is the value_template:
If you set this to on it won’t show you next, previous, stop and play buttons because the media player is on but nothing is playing.
The value template should check if switch.avr_power is on and what the state is of media_player.str_dn1080_2

@Sennevds, thank you so much. That fixed it.
So apparently that’s why @seperis’s configuration enabled the next and previous buttons.

Your first configuration example a year and a half ago had on/off values for value_template, so I thought those were the only possible values. Apparently, it can also be playing, stopped, and paused as well.

This also fixed the display of the progress bar as well.

Screen Shot 2021-12-04 at 3.16.13 PM

Now I believe only two features left/remain to be implemented:

  1. updating the playback position from the progress bar
  2. playing a file from the HA library.

I would really appreciate it if you would implement those features too.

2 Likes

First one is already there. It’s just my documentation isn’t up-to-date. The action is ‘seek’ which needs to be implemented just like next previous play etc. You have a extra parameter ‘position’ which gives the position you selected on the card in lovelace.
So something like this should work I think:

seek:
          service: number.set_value
          data:
            entity_id: number.position
            value: "{{ position }}"

The second isn’t implemented you could play something from a url but not through the ha library. At this moment I don’t have time to create this. You could always put in a feature request on github and when I have time maybe I will implement it

1 Like