Template Sensor for media_player status

Hi guys,

Im building a stack in Lovelace showing my media players and a picture of a cinema i like. I want the picture to light up when on of the media players has the status playingor paused. And only showing them when there in those states.

Everything works except for the picture because the picture-entity-card can only have one entity. Someone in discord adviced to look at sensor.template to make it read as one entity. I am not good in templating etc so i hope someone can help me.

The three entities it needs to look at are:

  - media_player.chromecast_woonkamer
  - media_player.chromecast_audio
  - media_player.kodi

If one of those entities has the status: playing or paused it should already light up.

The full stack:

 type: vertical-stack
 cards:
   - type: picture-entity
     entity: ###NEWTEMPLATESENSOR###
     image: /local/the-arc-cinema-screen.jpg
     show_name: false
     aspect_ratio: 16x9
   - type: entity-filter
    entities:
      - media_player.chromecast_woonkamer
      - media_player.chromecast_audio
       - media_player.kodi
     state_filter:
       - paused
       - playing

Hope you guys can help me! Thanks in advance.

try and use that blue bar on top of the page to format your code correctly
Create a binary_sensor:

- platform: template
  sensors:
    media_player_is_on:
      entity_id:
        - media_player.chromecast_woonkamer
        - media_player.chromecast_audio
        - media_player.kodi
      value_template: '{% if state("media_player.chromecast_woonkamer") == "off" and state("media_player.chromecast_audio") == "off" and state("media_player.kodi") == "off" %}off{% else %}on{% endif %}'
      friendly_name: Media Player Is On

Then use that binary_sensor in lovelace

1 Like

Hi, Thanks alot! I changed it, i though i used the right one.

1 Like

I know this is kind of old but it’s all I can find that seems at all relevant.

Is it possible to create a template to trigger a binary sensor based on a media player’s source? I’m really new to templating and just can’t work it out. :cry:

You don’t normally trigger a binary_sensor, you trigger an automation.
That being said you can create a binary sensor that will return a true/false status based on a state / attribute of a media player.
It would work along the lines of

value_template: '{% if state_attr("media_player.name", "attribute_name") == "state to compare" %}true{% else %}false{% endif %}'

Thanks, yes that’s what I meant. I said “trigger” because I’d just been reading about templates by trigger (or something like that).

I had it setup dependent on the “state” until I realised that the states are “playing” etc. I thought it’d need to identify which attribute somehow but I guess not! Thanks again!

Edit: I just noticed the “attribute_name” so ignore my last sentence :upside_down_face:

1 Like