Media_player.template

Good day ToR,
No i just followed the instructions in the first post.

How to install:

  1. Create a ‘custom_components’ folder in your config folder(if it doesn’t exist)
  2. Create ‘template’ folder in the custom_components folder
  3. Copy all the file from https://github.com/home->assistant/core/tree/dev/homeassistant/components/template to this folder (I read that this is needed otherwise the component isn’t overriden)
  4. Copy my media_player.py to this folder

Ok, the installation text is a little confusing for a newbie.
I installed the the Integration with HACS and did the Installation instructions i mentioned in the precious text.

#Media Player Template
media_player:

  • platform: template
    media_players:
    playstation_5:
    device_class: tv
    unique_id: media_player.playstation_5
    friendly_name: “PS5”
    media_image_url_template: >
    {% if states(‘sensor.nick_s_ps5_activity’) == “playing” %}
    {{ state_attr(‘sensor.nick_s_ps5_activity’,‘title_image’) }}
    {% endif %}
    turn_off:
    data_template:
    entity_id: sensor.nick_s_ps5_activity
    service: switch.turn_off
    turn_on:
    data_template:
    entity_id: sensor.nick_s_ps5_activity
    service: switch.turn_on
    current_source_template: “{{ state_attr(‘sensor.nick_s_ps5_activity’,‘title_name’) }}”
    value_template: “{{ states(‘sensor.nick_s_ps5_activity’) }}”
    title_template: “{{ state_attr(‘sensor.nick_s_ps5_activity’,‘title_name’) }}”

but I’m getting an error:

Error loading /config/configuration.yaml: while parsing a block mapping
in “/config/configuration.yaml”, line 23, column 9
expected , but found ‘’
in “/config/configuration.yaml”, line 37, column 10

those lines are:

device_class: tv

and

service: switch.turn_on

Hi @fitsou,
Seems like you have a problem with how your YAML is formatted.
It could be one of the following:

  1. Indentation Issues: YAML relies heavily on correct indentation. Make sure that you use spaces (not tabs) for indentation and that your indentation is consistent throughout the file.
  2. Missing Colon: Each key-value pair should be separated by a colon. If you forget to include a colon after a key, you’ll encounter this error.
  3. Incorrect Spacing: Ensure that there is proper spacing around colons and that key-value pairs are aligned correctly.
  4. Incorrect Nesting: If you’re defining lists or dictionaries (nested structures), make sure they are indented properly and nested at the correct level.
  5. Missing or Extra Characters

Please avoid posting code with “Blockquote” since it makes it impossible to see any formatting errors. Instead use the “Preformatted text”. You can post in-line or multi-line code using that.

1 Like

Hi,

I just installed your templated to solve a pretty tricky problem. it is working pretty good so far except…

the number one reason I am installing this is to create a “now playing” entity that will store media_artist, media_album and most important entity_picture. I have set the entity_picture value to:

    entity_picture_template: "{{ states('input_text.music_current_album_art') }}"

I don’t get any errors in the yaml configuration.

Even when there is a value for “{{ states(‘input_text.music_current_album_art’) }}”, the entity_picture isn’t showing up… here is the output from my developer tools screen:

source_list: source 1, source 2
media_title: Under the Bridge
media_artist: Red Hot Chili Peppers
entity_picture_local: null
device_class: speaker
friendly_name: Now Playing
supported_features: 68992
media_position_updated_at: 2023-08-21T12:15:00.511097+00:00

The whole yaml configuration I created is:

media_player:

  • platform: media_player_template
    media_players:
    now_playing:
    friendly_name: Now Playing
    device_class: speaker
    value_template: >
    {% if is_state(“input_boolean.music_playing”, “on”) -%}
    playing
    {%- else -%}
    idle
    {%- endif %}
    turn_on:
    service: switch.turn_on
    data_template:
    entity_id: switch.receiver_on
    turn_off:
    service: switch.turn_on
    data_template:
    entity_id: switch.receiver_off
    volume_up:
    service: switch.turn_on
    data_template:
    entity_id: switch.volume_up
    volume_down:
    service: switch.turn_on
    data_template:
    entity_id: switch.vol
    inputs:
    source 1:
    service: input_boolean.turn_on
    data_template:
    entity_id: input_boolean.source_1
    source 2:
    service: input_boolean.turn_on
    data_template:
    entity_id: input_boolean.source_2
    entity_picture_template: “{{ states(‘input_text.music_current_album_art’) }}”
    title_template: “{{ states(‘input_text.currenttitle’) }}”
    artist_template: “{{ states(‘input_text.currentartist’) }}”

Is entity_picture supported? If so, Am I doing something wrong in the above configuration?

Thanks in advance

1 Like

please paste your code in preformatted text to make it readable.
it should look like this:

switch:
  - platform: template
    switches:
      sony_tv:
        value_template: "{{ is_state('remote.sony_kd_65x89j', 'on') }}"
        turn_on:
          service: remote.turn_on
          target:
            entity_id: remote.sony_kd_65x89j
        turn_off:
          service: remote.turn_off
          target:
            entity_id: remote.sony_kd_65x89j

you need the sources for image url, artist name etc to define in your template.
maybe this might help you?

        title_template: |-
          {%- if is_state("sensor.avr_source", "Google TV") -%}
            {%- if states.media_player.spotify_tor.attributes.source is defined and states.media_player.spotify_tor.attributes.media_title is defined%}
              {%- if is_state_attr("media_player.spotify_tor",'source', 'Google TV 4K') and is_state('media_player.spotify_tor','playing') -%}
                {%- set media_title = states.media_player.spotify_tor.attributes.media_title -%}
                {%- set media_album_name = states.media_player.spotify_tor.attributes.media_album_name -%}
                {%- if media_title == media_album_name -%}
                  {{media_title}}
                {%- else -%}
                  {{media_title}} ({{media_album_name}})
                {%- endif %}                
              {%- endif -%}
            {%- else -%}
              {% if states.media_player.google_tv_4k.attributes.media_title is defined %}
                {{states.media_player.google_tv_4k.attributes.media_title}}
              {%- else -%}
                {{states.media_player.google_tv_4k.attributes.app_name}}
              {%- endif -%}
            {%- endif -%}
          {%- elif is_state("sensor.avr_source", "FM Tuner") -%}
            {{states.number.avr_tuner_frequency.state}}
            {%- set prst = states.number.avr_tuner_preset.state|int -%}
            {%- if prst == 1 -%}: TRT 3{%- endif -%}
            {%- if prst == 2 -%}: CNN Türk{%- endif -%}
            {%- if prst == 3 -%}: Bloomberg{%- endif -%}
            {%- if prst == 4 -%}: TRT 1{%- endif -%}
            {%- if prst == 5 -%}: Lounge FM{%- endif -%}
            {%- if prst == 6 -%}: Eksen{%- endif -%}
            {%- if prst == 7 -%}: Metro{%- endif -%}
            {%- if prst == 8 -%}: RS FM{%- endif -%}
            {%- if prst == 9 -%}: TRT Haber{%- endif -%}
            {%- if prst == 10 -%}: Power FM{%- endif -%}
            {%- if prst == 11 -%}: Fenomen{%- endif -%}
            {%- if prst == 12 -%}: Ulusal Radyo{%- endif -%}
            {%- if prst == 13 -%}: TRT Nağme{%- endif -%}
            {%- if prst == 14 -%}: Number 1{%- endif -%}
            {%- if prst == 15 -%}: PAL FM{%- endif -%}
            {%- if prst == 16 -%}: Radyo Voyage{%- endif -%}
          {%- else -%}
            {{states.media_player.str_dn1080_2.attributes.media_title}}
          {%- endif -%}
        # Sub Title (regular) --- 
        artist_template: |-
          {%- if is_state("sensor.avr_source", "Google TV") -%}
            {%- if states.media_player.spotify_tor.attributes.source is defined %}
              {%- if is_state_attr("media_player.spotify_tor",'source', 'Google TV 4K') and is_state('media_player.spotify_tor','playing') -%}
                {{states.media_player.spotify_tor.attributes.media_artist}}
              {%- endif -%}
            {%- else -%}
              {% if states.media_player.google_tv_4k.attributes.media_artist is defined %}
                {{states.media_player.google_tv_4k.attributes.media_artist}}
              {%- else -%}
                {% if states.media_player.google_tv_4k.attributes.media_title is defined %}
                  {{states.media_player.google_tv_4k.attributes.media_title}}
                {%- endif -%}
              {%- endif -%}
            {%- endif -%}
          {%- elif is_state("sensor.avr_source", "FM Tuner") -%}
            FM Tuner
          {%- else -%}
            {{states.media_player.str_dn1080_2.attributes.media_artist}}
          {%- endif -%}
        # Hidden Title ---
        album_template: |-
          {%- if is_state("sensor.avr_source", "Google TV") -%}
            {%- if states.media_player.spotify_tor.attributes.source is defined %}
              {%- if is_state_attr("media_player.spotify_tor",'source', 'Google TV 4K') and is_state('media_player.spotify_tor','playing') -%}
                {{states.media_player.spotify_tor.attributes.media_album_name}}
              {%- endif -%}
            {%- else -%}
              {% if states.media_player.google_tv_4k.attributes.media_album_name is defined %}
                {{states.media_player.google_tv_4k.attributes.media_album_name}}
              {%- else -%}
                {% if states.media_player.google_tv_4k.attributes.app_name is defined %}
                  {{states.media_player.google_tv_4k.attributes.app_name}}
                {%- else -%}
                  {{states.media_player.google_tv_4k_adb.attributes.source}}
                {%- endif -%}
              {%- endif -%}
            {%- endif -%}
          {%- else -%}
            {%- if states('media_player.str_dn1080_2')=='playing' -%}{{states.media_player.str_dn1080_2.attributes.media_album_name}}{%- endif -%}
          {%- endif -%}
        media_content_type_template: |-
          {%- if is_state("sensor.avr_source", "Google TV") -%}
            music
          {%- elif is_state("sensor.avr_source", "FM Tuner") -%}
            music
          {%- else -%}
            {{states.media_player.str_dn1080_2.attributes.media_content_type}}
          {%- endif -%}
        media_image_url_template: |-
          {%- if is_state("sensor.avr_source", "Google TV") -%}
            {% if states.media_player.google_tv_4k.attributes.entity_picture is defined %}
              {{states.media_player.google_tv_4k.attributes.entity_picture}}
            {%- else -%}
              {%- if states.media_player.spotify_tor.attributes.source is defined %}
                {%- if is_state_attr("media_player.spotify_tor",'source', 'Google TV 4K') and is_state('media_player.spotify_tor','playing') -%}
                  {{states.media_player.spotify_tor.attributes.entity_picture}}
                {%- endif -%}
              {%- else -%}
                {{states.media_player.google_tv_4k_adb.attributes.entity_picture}}
              {%- endif -%}
            {%- endif -%}
          {%- else -%}
            {{states.media_player.str_dn1080_2.attributes.entity_picture}}
          {%- endif -%}

Sorry, didn’t see that it came over with lack formatting. Here it is again:

media_player:
  - platform: media_player_template
    media_players:
      now_playing:
        friendly_name: Now Playing
        device_class: speaker
        value_template: >
          {% if is_state("input_boolean.music_playing", "on") -%}
            playing
          {%- else -%}
            idle
          {%- endif %}
        turn_on:
          service: switch.turn_on
          data_template:
            entity_id: switch.receiver_on
        turn_off:
          service: switch.turn_on
          data_template:
            entity_id: switch.receiver_off
        volume_up:
          service: switch.turn_on
          data_template:
            entity_id: switch.volume_up
        volume_down:
          service: switch.turn_on
          data_template:
            entity_id: switch.vol
        inputs:
          source 1:
            service: input_boolean.turn_on
            data_template:
              entity_id: input_boolean.source_1
          source 2:
            service: input_boolean.turn_on
            data_template:
              entity_id: input_boolean.source_2
        entity_picture_template: "{{ states('input_text.music_current_album_art') }}"
        title_template: "{{ states('input_text.currenttitle') }}"
        album_template: "{{ states('input_text.currentalbum') }}"
        artist_template: "{{ states('input_text.currentartist') }}"
1 Like

Thanks for responding.

If you mean by source, another media player’s entity, artist, etc… that is what I am trying to avoid. Instead of using a Universal Media player template, I am using this so I can put my own values in such as text variables. I am using multiple media players to create a now-playing media player. I am putting the artist, title, entity_picture I need to use in text helpers. The Artis, album, title is working. the one one not working is the entity_picture.

Taking another screenshot of what developer tools shows for the output of above:

source_list: source 1, source 2
device_class: speaker
friendly_name: Now Playing
supported_features: 68992
media_title: — Waiting For Next Selection —
media_artist: — You —
media_album_name: unknown
entity_picture_local: /api/media_player_proxy/media_player.now_playing?token=f97a2d51c9094baedfbad3425ec07513d30eb9f6f30a5de42355322762c8fd73&cache=24469974c13ad996

It appears to create an entity_picture_local field - possibly from my url that I supplied in entity_picture_template. This does work for

My need is to actually have the entity_picture entry in place. I am using a different user interface for my UI (sharptools.io). I have been liking the layout of this for my console better than Lovelace (without sever work). Sharptools takes a media player, and it it appears that it is using the entity_picture (not the local one for album art.

you know entity picture is just an icon sized picture inplace of the icons right?
this is entity_picture:

I’m not sure media_player.template supports templating entity_picture.
However you can template media_image_url_template which looks like this:

yes the media_player.template shows an entity_picture in the states but it’s media_image_url_template.
you can use custom:mini-media-player to make it appear like mine.
or use the media-control card.

thank you for all of your recommendations. Again, I am not trying to use Lovelace media player. I am using a different 3rd party tool for my dashboard (sharptools.io).

I found a work-around. It appears that I can write the value of the entity_picture into the now_playing media player later in an automation, using python, like this:

service: python_script.hass_entity
data:
  action: set_attributes
  entity_id: media_player.now_playing
  attributes:
    - entity_picture: "{{ states('input_text.music_current_album_art') }}"

This carries the value of the entity_picture over to sharptools and appears to be working

Thank you very much for your help.

3 Likes

Ok i’m sending the code in preformatted text, cause I’m unable to find the error.

media_player:
  - platform: template
    media_players:
      playstation_5:
        device_class: tv
        unique_id: media_player.playstation_5
        friendly_name: "PS5"
        media_image_url_template: >
          {% if states('sensor.nick_s_ps5_activity') == "playing" %}
           {{ state_attr('sensor.nick_s_ps5_activity','title_image') }}
          {% endif %}
        turn_off:
          data_template:
            entity_id: sensor.nick_s_ps5_activity
          service: switch.turn_off
        turn_on:
          data_template:
            entity_id: sensor.nick_s_ps5_activity
          service: switch.turn_on                 
        current_source_template: "{{ state_attr('sensor.nick_s_ps5_activity','title_name') }}"
        value_template: "{{ states('sensor.nick_s_ps5_activity') }}"
        title_template: "{{ state_attr('sensor.nick_s_ps5_activity','title_name') }}"

Platform error media_player.template - No module named 'homeassistant.components.template.media_player'

The platform is not template but media_player_template

1 Like

ok i make it work. but i do not get the image of the game, it just changes the background color of playstation media template.

I don’t think your issue is specific to the media_player.template.
look at the following as an example. All four cards use the same media_player entity.

type: vertical-stack
title: Spotify Player
cards:
  - type: entities
    entities:
      - entity: media_player.spotify_tor
  - type: custom:mini-media-player
    entity: media_player.spotify_tor
  - type: media-control
    entity: media_player.spotify_tor
  - type: custom:mini-media-player
    entity: media_player.spotify_tor
    artwork: full-cover
    tap_action: none
    toggle_power: false
    hide:
      name: true
      icon: true
      info: true
      power: true
      source: true
      sound_mode: true
      controls: true
      prev: true
      next: true
      play_pause: true
      play_stop: true
      jump: true
      volume: true
      volume_level: true
      mute: true
      progress: true
      runtime: true
      runtime_remaining: true
      artwork_border: true
      power_state: true
      icon_state: true
      shuffle: true
      repeat: true
      state_label: true

and this is how these look:

Thanks for replying.
There is some delay some times in showing the artwork from PlayStation. Ι cannot pinpoint when this delay is happening. but I can live with it.

Now I’m trying to have my Sony Bravia TV to show the artwork of the movie/series from the apps (Netflix, Disney, Plex, etc)

Got it to work with my Nintendo Switch, like I did with my PS5:

PS5HA

botw

  - platform: media_player_template
    media_players:
      nintendo_switch:
        device_class: tv
        unique_id: media_player.nintendo_switch
        friendly_name: "Switch"
        media_image_url_template: "{{ state_attr('sensor.nintendo_switch_activity','imageUri') }}"
        turn_off:
          service: switch.turn_off
          data_template:
            entity_id: switch.nintendo_switch
        turn_on:
          service: switch.turn_on
          data_template:
            entity_id: switch.nintendo_switch
        volume_up:
          service: media_player.volume_up
          data_template:
            entity_id: media_player.onkyo
        volume_down:
          service: media_player.volume_down
          data_template:
            entity_id: media_player.onkyo                   
        mute:
          service: media_player.mute
          data_template:
            entity_id: media_player.onkyo 
        inputs:
          Onkyo:
            service: media_player.select_source
            data_template:
              entity_id: media_player.onkyo
              source: "game"
          TV:
            service: media_player.select_source
            data_template:
              entity_id: media_player.lg55b6v
              source: "Onkyo"	    
        current_source_template: "{{ states('input_text.selected_source') }}"
        value_template:  >
          {% if is_state("sensor.nintendo_switch_activity", "ONLINE") -%}
            on
          {%- else -%}
            off
          {%- endif %}
        title_template: "{{ state_attr('sensor.nintendo_switch_activity','name') }}"   
2 Likes

You can create something similar using a custom button card.