Help with conditions in template

Hi everyone,

I have a Mushroom Cards Template Card which I am using to display the status (on or off), the source (TV channels, Netflix, Plex, etc.) and the media title of my LG TV.

Right now it is configured like this:

type: custom:mushroom-template-card
primary: TV
secondary: |-
  {% if is_state_attr(config.entity, 'source', 'Canali TV') %}
    {{state_attr(config.entity, 'media_title') | title }}
  {% else %}
    {{state_attr(config.entity, 'source') | title }}
  {% endif %}
icon: |-
  {% if states(config.entity)=='on' %}
    mdi:television
  {% else %}
    mdi:television-off
  {% endif %}
entity: media_player.lg_tv
icon_color: |-
  {% if states(config.entity)=='on' %}
    blue
  {% endif %}
layout: horizontal
tap_action:
  action: call-service
  service: script.accendi_lg_tv
  target: {}
hold_action:
  action: call-service
  service: script.spegni_lg_tv
  target: {}
double_tap_action:
  action: none
badge_color: |-
  {% if states(config.entity)=='on' %}
    orange
  {% else %}
  {% endif %}
badge_icon: |-
  {% if states(config.entity)=='on' %}
    mdi:toggle-switch
  {% else %}
    mdi:toggle-switch-off
  {% endif %}

So, when the TV is “ON” and the source is “TV Channels”, the secondary information field displays the title of the channel that it’s being watched (for example: “SkyTG 24”).

image

Otherwise, then the TV is “ON” but the source is not “TV Channels” the secondary information field displays the name of the source (for example: Netflix).

image

Problem is, that when I turn my tv “OFF”, no information is being shown. And I would like to change that so that when the tv is OFF, the secondary information field shows “Off”.

image

I am new to this and I am struggling to achieve the desired result. Can anyone help me out?

Thank-you

Use {% elif %} to have several statements.

  {% if xxx =='something' %}
    {{state_attr(config.entity, 'media_title') | title }}
  {% elif  xxx =='somethingelse' %}
    {{state_attr(config.entity, 'source') | title }}
  {% else %}
    off
  {% endif %}
1 Like

Thank you @Mats789, it works!

1 Like