How to show an Icon in a Markdown Card based on the Status of the Entity

Thank you for the code, Marius!

I’ve substituted “item” by “sensor.tronity_q4_sportback_e_tron_plugged”, but that doesn’t work. How can I implement your code into mine?

I’ve also tried

<ha-icon icon= {{states('binary_sensor.audi_eingesteckt')}}></ha-icon>

, but that leaves a blank field in the table…

Well, of course the template should return a valid
Icon….

Test it in dev tools templates

There’s a space to much in this line, and it’s missing the quotes around it.

I already answered in the other topic, with a long post and some examples. :wink:

<ha-icon icon="{{ states('binary_sensor.audi_eingesteckt') }}"></ha-icon>
----------------------^^--------------------------------------------------------------------------^

Anyway, this example is likely not to work, as you are displaying the state, not the icon. The example from Marius only works, if you setup a template sensor before. so out-of-the-box, it won’t work.

yes, and the reason I chose to do it with the external template entity is this:

      - unique_id: meteoalarm_icon
        state: >
          {% if is_state('binary_sensor.meteoalarm_brabant','on') %}
            {% set condition =
                 state_attr('binary_sensor.meteoalarm_brabant','awareness_type')
                 .split('; ')[1] %}
            {% set weather = {'Fog':'fog',
                              'Wind':'windy',
                              'snow-ice':'snowy-heavy',
                              'Thunderstorm':'lightning',
                              'Rain':'pouring',
                              'Zware regen':'pouring',
                              'Zicht':'hazy',
                              'Windstoten':'windy',
                              'Onweersbuien':'lightning'} %}
            {% set climate = {'Extreme high temperature':'thermometer-chevron-up',
                              'Extreme low temperature':'thermometer-chevron-down',
                              'high-temperature':'thermometer-alert',
                              'Coastal Event':'waves',
                              'Forestfire':'camp-fire',
                              'Avalanches':'snowflake-alert',
                              'Rain-Flood':'home-flood',
                              'flooding':'home-flood',
                              'Flooding':'home-flood'} %}
            mdi:{% if condition in weather %}weather-{{weather[condition]}}
            {% elif condition in climate %}{{climate[condition]}}
            {% else %}alert
            {% endif %}
          {% else %} mdi:help
          {% endif %}

Imagine having to fix all that inside a Markdown card… :wink: (could probably take out several mappings there, somehow there are 2 languages in it…)

But, as said above, if you use a template, always test it in dev tools Template first, so you are sure there is a valid output.

As a side note: considering a dedicated template sensor for stuff you need in other places can be very useful. In this particular case, I use the same template for showing my view tab icon (set with card_mod theming)

      paper-tab[aria-label='Weer'] {
        --card-mod-icon: {%- if meteo %} {{states('sensor.meteoalarm_icon')}}
                         {%- else %} {{states('sensor.weather_icon')}}
                         {%- endif %};
        color: {{'var(--alert-color)' if meteo else
                 states('sensor.temperature_color_name')}};
      }

and an alert button in my Alerts dashboard:

type: custom:button-card
template:
  - button_body
  - styles_cf_notification
entity: binary_sensor.meteoalarm_brabant
name: MeteoAlarm
variables:
  text_color: >
    [[[ var param = entity.attributes.severity;
        var colors  = {'Safe':'var(--ok-color)','Moderate':'gold','Severe':'darkorange',
                       'High':'var(--alert-color)'};
        return colors[param]||'var(--no-power-color)'; ]]]
  icon_color: >
    [[[ return entity.state == 'on' ? 'var(--alert-color)' :'var(--no-power-color)'; ]]]
icon: >
  [[[ return states['sensor.meteoalarm_icon'].state; ]]]

so that would certainly be part of my answer to this topic: use a template entity

1 Like

If I test

<ha-icon icon= {{states('binary_sensor.audi_eingesteckt')}}></ha-icon>

in developer tools the result is <ha-icon icon= off></ha-icon>

With

<ha-icon icon="{{ states('binary_sensor.audi_eingesteckt') }}"></ha-icon>

it is <ha-icon icon="off"></ha-icon>

What should the result be?

There is an icon for the entity that changes based on the status:

image

ha-icon is used to show icons in the installed icon packs in HA, like f.ex. mdi.
<ha-icon icon="mdi:weather-sunset-up">

You can find an overview of MDI icons here: Material Design Icons

the result should be a valid icon string, like mdi:help

but honestly, if this is still abracadabra to you, it might be a bit too complex yet.

You need to understand what you are doing here.
Which is a combination of html coding, (in the Markdown card) and Jinja templating (for the state dependent icon and icon color)

1 Like

Fiddling with it will be a good teaching experience and at most the single card can be messed up, so no big deal.

I’m just trying to understand the external template entity: You generate a new entity “meteoalarm_icon” and define it’s name based on the state of the binary_sensor.meteoalarm_brabant, right?

Currently the result in developer tools is

- unique_id: meteoalarm_icon
        state: >
           mdi:help

Is this the result I should expect? Shouldn’t it rather be

 mdi:help

?

Thank you, i’ve found that already!

no that is fine.
but that is exactly what I mean, you need to get a better understanding of the HA system. The output is correct, but is it also shows the rest of the syntax itself…

here to help you with that though

1 Like

I’ve read your post only know, after I’ve posted my question. But I think I’m getting closer to understand it. :slight_smile:

1 Like

thank you, that’s just what I’m trying to do! :slight_smile:

guess this essential :wink:

without all of that code, what would your icons needs to be, based on which sensor, or state?

1 Like

So I have to get rid of the rest of the syntax? How?

by starting with the basics in Markdown card - Home Assistant

create a pure card with some content only

next, add a single icon

next, add color

if all of that works, then add templates to the icon and icon color

dont do this

but learn and understand. There is no other way.

1 Like

The icon should be
image
based on the state “off” (or German translation “Aus”?) of the template “binary_sensor.audi_eingesteckt”.

{{ 'on' if is_state('sensor.tronity_q4_sportback_e_tron_plugged', 'True') else 'off' }}

That’s what I meant. Code step by step, fix errors and learn and understand from it. :slight_smile:

see my post above…

first try your self and follow that order I posted

(besides all that, is there no binary_sensor available for that? Or maybe even a battery sensor. my guess is there should be, and that sensor should already have the correct icon, if programmed correctly)

again, this is basic knowledge:
the HA system is in English in the backend, and the German translation is only used in the Frontend dashboard. Most of the time…

thats why I also said to Always check in the dev tools, because those are always 100% factual.
dev tools states shows that entity too, with the state you need in the template

1 Like