Can I show an attribute in markdown only when a state has a certain value?

I have a card that shows what’s playing in Foobar2K around the zones, each are like this:

{{ state_attr('media_player.sone9', 'media_title') }}

That works. But I would like it to only show when the states, {{ states('media_player.sone9') }} is playing, not when it’s off or idle. Is there a way to do that in the template? I would prefer not to create a markdown card for each zone and make them visible according to the state, if I can avoid it.

Some options. More robust if the attribute is non existing:

{{ state_attr('media_player.sone9', 'media_title') or '' }}

Or (try what works for you):

{{ state_attr('media_player.sone9', 'media_title') | default('') }}

If you want to test, based on state:

{{ state_attr('media_player.sone9', 'media_title') if is_state('media_player.sone9', 'playing') else  '' }}
1 Like

Thank you very much! Number three is perfect for me!

1 Like