Programmatically set Entity Badge color?

Is it possible to set the badge color programmatically

type: entity
show_name: true
show_state: true
show_icon: true
entity: sensor.house_temperature
tap_action:
  action: none
name: Outdoor Temp
color: >
  {% set outside = states("sensor.house_temperature") | float %} 
  {% if (outside>70) %}
    red
  {% else %}
    green
  {% endif %}

Doesn’t work, just remains white…

Screen Shot 2025-03-30 at 12.51.42 PM

Yet…

type: entity
show_name: true
show_state: true
show_icon: true
entity: sensor.house_temperature
tap_action:
  action: none
name: Outdoor Temp
color: >
    green

works.

Main card mod thread - 1st post - “fantastic” link at the bottom - badges

Thanks, but I don’t want to use card mod. Just want to know if/why code doesn’t work? Not allowed, that’s fine, I won’t waste anymore time.

Not allowed.

it’s not a matter of being ‘allowed’.
it’s just not supported. Like that.

You could however use conditional cards, or visibility, and double the yaml only changing the color.

Thank you. As a newb it’s very confusing as to where code is allowed and where it isn’t. The whole system is on-the -fly execution, so the inconsistencies are even more mind boggling. Works here, not there, seems random. And css mods seems like an even bigger hack.

just follow the documentation, and as a base rule: no templating in Dashboards except Markdown, and no css except where explicitly stated…

if its as simple as in your example, and conditional card can fix your trouble, but, when things get more complicated, there is no other way than going custom.

And then the box of Pandora opens, both regarding options and possibilities, but certainly regarding challenges and inconsistencies :wink:

Iow: go with the flow, learn as you need, and dont invent your own options :wink: