Not show attribute if value equal 'null'

I am having a slight syntax issue. For the online status integration of Discord if someone is playing a game it shows the title of the game. If not it displays “null”. In that case I do not want to show the subtitle. Any idea of how the correct syntax might be?

  - entity: sensor.extrem_****
    name: Extrem
    type: custom:multiple-entity-row
    secondary_info:
      attribute: game

I already tried the following syntax but it did not work:

  - entity: sensor.extrem_****
    name: Extrem
    type: custom:multiple-entity-row
    secondary_info:
      attribute: >
        {% if state_attr('sensor.extrem_****', 'game') %}
        game
        {% endif %}

The attribute will always show. So just put an else there with something different. You can even try a space but templates usually remove whitespace.

      attribute: >
        {% set game =  state_attr('sensor.extrem_****', 'game') %}
        {{ game if game is not none else ' ' }}

If that doesn’t work, then you have to use a string for the else.

      attribute: >
        {% set game =  state_attr('sensor.extrem_****', 'game') %}
        {{ game if game is not none else 'Not Playing' }}

Does not seem to work either … if I put that in there Status switches to “unavailable”:

well, i’ve never used that custom card. I assumed you knew how to use it. But according to the docs, that card doesn’t support templates. So your only option is:

  - entity: sensor.extrem_****
    name: Extrem
    type: custom:multiple-entity-row
    secondary_info:
      attribute: game

Also, reading the docs I found this

which means this will get you what you want.

  - entity: sensor.extrem_****
    name: Extrem
    type: custom:multiple-entity-row
    secondary_info:
      attribute: game
      hide_unavailable: true

I tried that as well … unfortunatly it does not seem to accept “null” as unavailable.

Then there’s nothing you can do outside creating a template sensor

…damn … but thanks for the help!

I am looking at this picture:

and I wonder why to use multiple-entity-row since you are showing ONE state.
Use secondaryinfo-entity-row or template-entity-row instead, both of them support templates for secondary_info.
Also, if using multiple-entity-row is critical for you then use this method for secondary_info (works with stock entity rows as well).

2 Likes

That works! Awesome.

      - entity: sensor.extrem_****
        name: extrem
        type: custom:secondaryinfo-entity-row
        secondary_info: >
          {% set game =  state_attr('sensor.extrem_****', 'game') %}
          {{ game if game is not none else ' ' }}

Had to install “secondaryinfo-entity-row” and “card-tools” via HACS.

Is there a way in the language to reference to the initial entity? So I do not have to copy paste every time. Something like this:

      - entity: sensor.extrem_****
        name: extrem
        type: custom:secondaryinfo-entity-row
        secondary_info: >
          {% set game =  state_attr('sensor.self', 'game') %}
          {{ game if game is not none else ' ' }}

Possible with template-entity-row or entity-row+card_mod:

type: entities
entities:
  - entity: input_boolean.test_boolean
    name: entity-row + card_mod
    secondary_info: true
    style:
      hui-generic-entity-row:
        $: |
          .info.pointer .secondary::after {
            content: "{% set SENSOR = config.entity -%}
              {%- set CONTENT = 'value is '-%}
              {%- if is_state(SENSOR,'on') -%}
              {%- set CONTENT = CONTENT + 'set'-%}
              {%- else -%}
              {%- set CONTENT = CONTENT + 'unset'-%}
              {%- endif -%}
              {{CONTENT}}";
          }
  - type: custom:secondaryinfo-entity-row
    entity: input_boolean.test_boolean
    name: secondaryinfo-entity-row
    secondary_info: >-
      {% set SENSOR = 'input_boolean.test_boolean' -%}
      {%- set CONTENT = 'value is '-%}
      {%- if is_state(SENSOR,'on') -%}
      {%- set CONTENT = CONTENT + 'set'-%}
      {%- else -%}
      {%- set CONTENT = CONTENT + 'unset'-%}
      {%- endif -%}
      {{CONTENT}}
  - type: custom:template-entity-row
    entity: input_boolean.test_boolean
    name: template-entity-row
    toggle: true
    secondary: >-
      {% set SENSOR = config.entity -%}
      {%- set CONTENT = 'value is '-%}
      {%- if is_state(SENSOR,'on') -%}
      {%- set CONTENT = CONTENT + 'set'-%}
      {%- else -%}
      {%- set CONTENT = CONTENT + 'unset'-%}
      {%- endif -%}
      {{CONTENT}}

image