How to represent an input_number with state dependent icon And color

as repo owner of custom-ui, I am slowly (…) trying to move away from that, as modern UI changes often and more options get implemented in the Dashboards.
However, there is still no way to easily glob a certain customization, and icons are even more difficult to change in core entities cards

consider:

    input_number.*_volume:
      hide_attributes: templates
      templates:
        icon: >
          var vol = parseFloat(state);
          if (vol === 0.0) return 'mdi:volume-off';
          if (vol <= 0.3) return 'mdi:volume-low';
          if (vol <= 0.6) return 'mdi:volume-medium';
          return 'mdi:volume-high';
        icon_color: >
          var vol = parseFloat(state);
          if (vol === 0.0) return 'gray';
          if (vol <= 0.3) return 'dodgerblue';
          if (vol <= 0.6) return 'green';
          if (vol <= 0.8) return 'orange';
          return 'firebrick';

which allowed to do:

easily, and that for all input_number.*_volume in 1 go…

there is simply no way in core, so I tried a template-entity-row:

    - type: entities
      entities:
        - type: custom:template-entity-row
          entity: input_number.alarm_volume
          state: >
            {{states(config.entity)|float(0)}} %
          icon: >
            {% set vol = states(config.entity)|float(0) %}
            {% if vol == 0.0 %} mdi:volume-off
            {% elif vol <= 0.3 %} mdi:volume-low
            {% elif vol <= 0.6  %} mdi:volume-medium
            {% else %} mdi:volume-high
            {% endif %}
          card_mod:
            style:
              div#wrapper:
                state-badge $: |
                  ha-state-icon {
                    color:
                      {% set vol = states(config.entity)|float(0) %}
                      {% if vol == 0.0 %} gray
                      {% elif vol <= 0.3 %} dodgerblue
                      {% elif vol <= 0.6 %} green
                      {% elif vol <= 0.8 %} orange
                      {% else %} red
                      {% endif %}
                  }

but as we know that doesnt show the slider:


other than that, its perfect. But we need the slider.

we can use slider-entity-row:

        - type: custom:slider-entity-row
          entity: input_number.alarm_volume
#           state: >
#             {{states(config.entity)|float(0)}} %
#           icon: >
#             {% set vol = states(config.entity)|float(0) %}
#             {% if vol == 0.0 %} mdi:volume-off
#             {% elif vol <= 0.3 %} mdi:volume-low
#             {% elif vol <= 0.6  %} mdi:volume-medium
#             {% else %} mdi:volume-high
#             {% endif %}
          card_mod: &volume_color
            style: |
              :host {
                --paper-item-icon-color:
                  {% set vol = states(config.entity)|float(0) %}
                  {% if vol == 0.0 %} gray
                  {% elif vol <= 0.3 %} dodgerblue
                  {% elif vol <= 0.6 %} green
                  {% elif vol <= 0.8 %} orange
                  {% else %} red
                  {% endif %}
              }

but cant customize the icon:


and that in the end is the same (almost) as the core slider entity with the same mod:


        - entity: input_number.alarm_volume
          card_mod: *volume_color


so, the challenge still stands: how to change those icons And colors, in a way to easily apply to all *_volume entities (whihc would probably end up being a decluttering template, so that the least of my concerns)

as an exercise I tried a tile card (or the mushroom variants ) (also button-card with embedded slider)

but the issue remains… cant template that, and tbh, I dont really like those Huge sliders at all (though admittedly their tactic handling is way better than the slider)

Would appreciate suggestions you might have to fill the gap taking out custom-ui leaves here (and many other spots with the same aspects).

any other card that would do the job, or maybe another mod (to change the icon on the entity),
thanks for having a look and letting me know what you all do

tried a first mushroom template card with:

type: custom:mushroom-template-card
primary: ''
secondary: ''
icon: |-
  {% set vol = states(config.entity)|float(0) %}
  {% if vol == 0.0 %} mdi:volume-off
  {% elif vol <= 0.3 %} mdi:volume-low
  {% elif vol <= 0.6  %} mdi:volume-medium
  {% else %} mdi:volume-high
  {% endif %}
entity: input_number.alarm_volume
icon_color: |-
  {% set vol = states(config.entity)|float(0) %}
  {% if vol == 0.0 %} gray
  {% elif vol <= 0.3 %} dodgerblue
  {% elif vol <= 0.6 %} green
  {% elif vol <= 0.8 %} orange
  {% else %} red
  {% endif %}
multiline_secondary: false
fill_container: false
tap_action:
  action: more-info

but that only shows the icon and icon_color correctly…

well, turns out I had forgotten to delimit the 2 templates inside the card_mod customization I started out with (didnt post that)

        - entity: input_number.alarm_volume
          card_mod:
            style: |
              :host {
                --card-mod-icon:
                  {% set vol = states(config.entity)|float(0) %}
                  {% if vol == 0.0 %} mdi:volume-off
                  {% elif vol <= 0.3 %} mdi:volume-low
                  {% elif vol <= 0.6  %} mdi:volume-medium
                  {% else %} mdi:volume-high
                  {% endif %};
                --paper-item-icon-color:
                  {% set vol = states(config.entity)|float(0) %}
                  {% if vol == 0.0 %} gray
                  {% elif vol <= 0.3 %} dodgerblue
                  {% elif vol <= 0.6 %} green
                  {% elif vol <= 0.8 %} orange
                  {% else %} red
                  {% endif %};
              }

just does both of the mods nicely:

so, the core HA input_number with card_mod is the first in the entities card:

problem solved…

throw that style in a secret in secrets.yaml

style_volume:
  style: |
    :host {
      --card-mod-icon:
        {% set vol = states(config.entity)|float(0) %}
        {% if vol == 0.0 %} mdi:volume-off
        {% elif vol <= 0.3 %} mdi:volume-low
        {% elif vol <= 0.6  %} mdi:volume-medium
        {% else %} mdi:volume-high
        {% endif %};
      --paper-item-icon-color:
        {% set vol = states(config.entity)|float(0) %}
        {% if vol == 0.0 %} gray
        {% elif vol <= 0.3 %} dodgerblue
        {% elif vol <= 0.6 %} green
        {% elif vol <= 0.8 %} orange
        {% else %} red
        {% endif %};
    }

and call it like

        - entity: input_number.alarm_volume
          card_mod: !secret style_volume

and we have ourselves a system wide solution.

Here is my attempt at updating the code, however is not working?

type: entities
entities:
  - entity: input_number.alarm_volume
    card_mod:
      style: |
        :host {
          --paper-item-icon-color: 
            {% set vol = states(config.entity) | float(0) %}
            {% if vol == 0.0 %} gray
            {% elif vol <= 0.3 %} dodgerblue
            {% elif vol <= 0.6 %} green
            {% elif vol <= 0.8 %} orange
            {% else %} red
            {% endif %};
          color:
            {% set vol = states(config.entity) | float(0) %}
            {% if vol == 0.0 %} gray
            {% elif vol <= 0.3 %} dodgerblue
            {% elif vol <= 0.6 %} green
            {% elif vol <= 0.8 %} orange
            {% else %} red
            {% endif %};
        }
Why wont this code work together, i remove the style the icon changes, if i remove the :host portion, the color changes fine, togther only the icon change works
type: entities
entities:
  - entity: input_number.brightnessfirebl
    icon: mdi:account-group
    card_mod:
      style: |
        ha-card {
          background-color: 
            {% set vol = states('input_number.brightnessfirebl') | int %}
            {% if vol == 1 %} pink
            {% elif vol == 2 %} yellow
            {% elif vol == 3 %} green
            {% elif vol == 4 %} red
            {% endif %};
        }
        :host {
          --card-mod-icon: 
            {% set vol = states('input_number.brightnessfirebl')|float(0) %}
            {% if vol == 1 %} mdi:volume-off
            {% elif vol <= 2 %} mdi:volume-low
            {% elif vol <= 3 %} mdi:volume-medium
            {% else %} mdi:volume-high
            {% endif %};
        }type or paste code here

do you happen to run the current 2025.1 beta?

if yes, that breaks many card_mod customizations on the generic-hui-entity-row. I can confirm these to be no longer functional

see 2025.1.0 beta breaks several fundamental card_mods · Issue #420 · thomasloven/lovelace-card-mod · GitHub
and 🔹 Card-mod - Add css styles to any lovelace card - #7753 by Mariusthvdb and several more following in that thread

btw this is what was working prior to 2025.1

style: |
  :host {
    {% set vol = states(config.entity)|float(0) %}
    --card-mod-icon:
      {% if vol == 0.0 %} mdi:volume-off
      {% elif vol <= 0.3 %} mdi:volume-low
      {% elif vol <= 0.6  %} mdi:volume-medium
      {% else %} mdi:volume-high
      {% endif %};
    --card-mod-icon-color:
      {% if vol == 0.0 %} gray
      {% elif vol <= 0.3 %} dodgerblue
      {% elif vol <= 0.6 %} var(--success-color)
      {% elif vol <= 0.8 %} orange
      {% else %} var(--alert-color)
      {% endif %};
  }

Nope running the latest 2024. I’ll try again this evening and report back. Thanks

1 Like

Working :slight_smile:

type: entities
entities:
  - entity: input_number.brightnessfirebl
    icon: mdi:account-group
card_mod:
  style: |
    :host {
      {% set vol = states('input_number.brightnessfirebl') | float(0) %}

      --card-mod-icon: 
        {% if vol == 1 %} mdi:volume-off;
        {% elif vol == 2 %} mdi:volume-low;
        {% elif vol == 3 %} mdi:volume-medium;
        {% else %} mdi:volume-high;
        {% endif %};

      --card-mod-icon-color: 
        {% if vol == 1 %} gray;
        {% elif vol == 2 %} dodgerblue;
        {% elif vol == 3 %} green;
        {% elif vol == 4 %} orange;
        {% endif %};
    }
    ha-card {
      background-color: 
        {% set vol = states('input_number.brightnessfirebl') | int %}
        {% if vol == 1 %} pink;
        {% elif vol == 2 %} yellow;
        {% elif vol == 3 %} green;
        {% elif vol == 4 %} red;
        {% endif %};
    }

great.
but no need to do this then. I mean the icon you set there.

also, you set a lot of semi colons. only require that at the end of the template

gotcha… thank you.

Brand new to this - learning quickly at the seat of my pants …

then hop over to https://community.home-assistant.io/t/card-mod-add-css-styles-to-any-lovelace-card/ and learn all about those customizations