Get dynamic icon from media_player entity for a switch icon

Hello everyone,

I have set up a switch for being able to power on and off a LG WebOS based Smart TV. I have included such switch entity in the dashboard:


  - platform: template
    switches:
      lg_tv_28:
        turn_on:
          service: script.lg_tv_28
        turn_off:
          service: webostv.command
          data:
            entity_id: media_player.lg_webos_smart_tv
            command: system/turnOff
        friendly_name: "TV LG"
        value_template: "{{ is_state('media_player.lg_webos_smart_tv', 'on') }}"
        icon_template: "{{ state_attr('media_player.lg_webos_smart_tv') }}"

The media_player “parent” entity media_player.lg_webos_smart_tv displays a different icon depending on what input or App is selected in the TV. That’s part of the integration feature and it works great:

The switch itself works fine. However, I have tried to get such dynamic icon into the switch entity with the icon_template code stated in the code above. Tried different options but neither of them work:

Could anyone help to achieve this, please?

Maybe share the attributes and the state available for the media_player.lg_webos_smart_tv…

Is this what you are suggesting to share? Got this from the States tab in Developer Tools:


source_list:
  - AV
  - Componente
  - DAZN
  - Disney+
  - Foto y Video
  - HDMI 1
  - HDMI 2
  - LG Channels
  - LG Content Store
  - Live TV
  - Movistar+
  - MĂşsica
  - Navegador web
  - Netflix
  - Prime Video
  - Spotify – Música y podcasts
  - YouTube
assumed_state: true
device_class: tv
friendly_name: LG webOS Smart TV
supported_features: 24381
volume_level: 0.07
is_volume_muted: false
media_content_type: channel
media_title: Telecinco
source: Live TV
sound_output: tv_external_speaker

state_attr() needs TWO arguments.

What argument should the second one be?

Already checked that, but I cannot find a way to set this specific expected behaviour up.

For example (in my case when my AppleTV is playing):

{{ state_attr("media_player.g_appletv","entity_picture") }}

You have to find the correct attribute for your TV

Thank you, Mats789. Just noticed that after a while, another attribute is displayed under States tab:


source_list:
  - AV
  - Componente
  - DAZN
  - Disney+
  - Foto y Video
  - HDMI 1
  - HDMI 2
  - LG Channels
  - LG Content Store
  - Live TV
  - Movistar+
  - MĂşsica
  - Navegador web
  - Netflix
  - Prime Video
  - Spotify – Música y podcasts
  - YouTube
volume_level: 0.01
is_volume_muted: false
source: Movistar+
sound_output: tv_external_speaker
assumed_state: true
device_class: tv
entity_picture: >-
  /api/media_player_proxy/media_player.lg_webos_smart_tv?token=9055fb58c17171e27d99f537cb2667ef4f92f10d2a2d98f55e26bd2783988d53&cache=6e29b417822c6c30
friendly_name: LG webOS Smart TV
supported_features: 24381

One of them is actually the “entity_picture” one, so it should work. However, after updating the switch code accordingly, the icon is not updated, although the template tester returns the attribute content fine:


  - platform: template
    switches:
      lg_tv_28:
        turn_on:
          service: script.lg_tv_28
        turn_off:
          service: webostv.command
          data:
            entity_id: media_player.lg_webos_smart_tv
            command: system/turnOff
        friendly_name: "TV LG"
        value_template: "{{ is_state('media_player.lg_webos_smart_tv', 'on') }}"
        icon_template: "{{ state_attr('media_player.lg_webos_smart_tv', 'entity_picture') }}"

Any advise?

Wrong.
image is not an icon

Try to put “entity_picture_template” instead of “icon_template” (untested).

Hey! That worked! And that’s actually in the documentation. Will have to read better next time.

I had to reset cache and reboot the server a couple of times, but finally it is being displayed. Thanks!

1 Like