Icon and sensor as result of IF-Statement - What am I doing wrong here?

Hi all,

I’m using the custom-banner-card in for my tablet dashboard. I’ve got a lights-on-counter which is displayed within this card. But I would like to have the light counter in front of the icon.

It’s like this now:
image
I want it to look like this:
image

The counter for the lights obviously works:

    lightson_kantoor:
      friendly_name: lightson_kantoor
      value_template: >
        {% set ns = namespace(res=0) %}
        {% for light in state_attr('group.Kantoor', 'entity_id') if is_state(light, 'on') %}
          {% set ns.res = ns.res + 1 %}
        {%endfor%}
        {{ ns.res }}

Now I’ve created a second sensor as a test to combine this number with icon like this:

    test:
      friendly_name: "test"
      value_template: >
        {% if is_state('group.kantoor', 'off') %}
          { states.sensor.lightson_kantoor.state },  mdi:lightbulb-multiple
        {% elif is_state('group.kantoor', 'on') %}
          { states.sensor.lightson_kantoor.state },  mdi:lightbulb-multiple
        {% else %}
          n/a
        {% endif %}

Unfortunately this doens’t work and shows mdi:lightbulb-multiple as text instead of the icon.
What am I doing wrong here?

Cheers,
Per

Home Assistant won’t convert the mdi icon code into an icon character for a Template Sensor’s value_template.

It does that for icon_template.

An entity’s state contains alphanumeric text so you’ll need to find another way to achieve your goal.

That’s what I thought in the beginning but…when I leave the sensor state away, it does work like this:

    test:
      friendly_name: "test"
      value_template: >
        {% if is_state('group.kantoor', 'off') %}
          mdi:lightbulb-multiple
        {% elif is_state('group.kantoor', 'on') %}
          mdi:lightbulb-multiple
        {% else %}
          n/a
        {% endif %}

So…if that works, and the sensor state works (without the mdi:icon), than…I would expect you should be able to combine those two together or…not? :slight_smile:

And…is there a different way to achieve the same goal?

is “lightson_kantoor” a template sensor, or a counter sensor?

It’s a counter sensor as you can see in main post.

:confused: didn’t know you could use templates in counter sensors.

Not.

My mistake :slight_smile: You’re right ofcourse

Can you think of a work-around to get the same result? Because you stated:

Home Assistant won’t convert the mdi icon code into an icon character for a Template Sensor’s value_template .

But it actually does do that, only not when I want to combine it with the sensor, but it does show the icon perfectly when that’s the only result defined.

I am familiar with a Counter and a Template Sensor but not a counter sensor. Can you post a link to its documentation?

If it’s a template sensor, you can use the icon_template, but they won’t stack together, the only thing I can think of is using markdown in you card.

Like I said in my previous reply, it’s a template sensor, both are.

That’s the whole thing, I want them to stick together :slight_smile: Markdown does not really work in the lay-out I’ve got I’m afraid…

In that case, you can’t combine the name of an mdi icon with other text and expect each part of the resulting text string to be interpreted separately. The entire string is interpreted by Home Assistant.

I’ve never tried this (and I suspect it won’t work) but you can try to add the icon code to the Template Sensor’s unit_of_measurement. Most cards display an entity’s state value followed by its unit of measurement.

That’s to bad, I’d expect you could use some kind of symbol to connect them together like ‘&&’

Just tried it but…it doesn’t convert to the actual icon but just displays it as text.

Good to know; I had my doubts it would work but now it’s confirmed.

Unfortunately, I can’t think of a way to get what you want (make the entity’s state a combination of a value and and an icon).

I was afraid of that…I’ll keep searching and trying…I mean…there must be a way :slight_smile: