šŸ”¹ Card-mod - Add css styles to any lovelace card

Thanks for chiming in.

I tried - but no luck. My code misses the hui-butotns-base.

Can you share how you generated the buttons? via footer type:buttonss?

yes:

footer:
  type: buttons
  entities:
    - entity: script.turn_on_cameras
      name: Cameras aan
      icon: mdi:record

    - entity: switch.cameras
      name: Cameras

    - entity: script.turn_off_cameras
      name: Cameras uit
      icon: mdi:stop

did you add the code to the theme and Reload themes?

1 Like

I just tried it on a fresh dashboard - and your code works. So the mistake is with me. Thanks once again!

I implement the code directly in the raw configuration editor in my build-and-test dashboard, before i then copy it over into my yaml mode configged live dashboard that is live on the ipads on the walls.

Is there a better workflow/way to implement.

nope, what ever works best for you :wink:

I do have 2 test dashboards, one in yaml, and one using the UI. If they work, I directly copy them over in my main dashboards.

this particular mod is in my themes, I only have those globally set, so always testing inside the production system. Nothing breaks, and if it does, itā€™s an easy revert. commandZ a few times and weā€™re good.

1 Like

I got it to work thanks to you - massive kudos to you! Appreciate your help!

1 Like

wow that was long agoā€¦

because of helping Laineg above, I was reminded I also had those icons changing, and checked the mods in the thread. I can not for the life of me create a correct mod on the button entities them selves or in the footer section as is described there.

all I can do is set this global mod for the entities card containing the footer buttons:

  - type: entities
    card_mod:
      class: class-header-margin
      style:
        hui-buttons-header-footer $ hui-buttons-base $ .ha-scrollbar:
          ha-assist-chip:
            .: |
              ha-assist-chip:nth-child(2) {
                --card-mod-icon: mdi:{{'stop' if is_state('script.play_tune','on')
                                       else 'play'}}

when the footer config is:

    footer:
      type: buttons
      entities:
        - entity: input_boolean.loop_tune
          name: Loop tune
        - entity: script.play_tune
          name: Speel tune

I did try this too but they fluke so I commented them so I wont forgetā€¦

    footer:
      type: buttons
#       card_mod:
#         style:
#           hui-buttons-base $: |
#             ha-assist-chip:nth-child(2) {
#               {% if is_state('script.play_sound_bite','on') %}
#               --card-mod-icon: mdi:stop;
#               {% else %}
#               --card-mod-icon: mdi:play;
#               {% endif %}
      entities:
        - entity: input_boolean.loop_sound_bite
          name: Loop soundbite
        - entity: script.play_sound_bite
          name: Speel soundbite
#           card_mod:
#             style: |
#               :host {
#                 --card-mod-icon:
#                   {% if states('script.play_sound_bite') == 'on' %} mdi:stop
#                   {% else %} mdi:play
#                   {% endif %};

if you see an obvious error that might fix it, please dont hold back

btw, this IS a nice option to use the iif syntax:

            .: |
              ha-assist-chip:nth-child(2) {
                --card-mod-icon:
                  mdi:{{iif(states('script.play_tune') == 'on','stop','play')}}
              }

I have another issue: the group of buttons in the footer of my entities card breaks nicely on screen resolutions > 450px. At exactly 450px the lines of buttons jump into one line and overflew left and right of the footer.

Anyone with an idea why this is happening and how I can prevent it? Tried to find breakpoint defintions in the theme but couldnt find itā€¦

Hi,
i hope this is the right place for this. I do have a little problem: i set up some colored icons on my dashboard using card-mod. On the browser this is working more or less ok (sometimes the color is not showing, sometimes the code gets changed by itself and i donā€™t know why) but thatā€™s not the thing.

Those colors are working on the browser on different PCā€™s but in my companion app those colored icons not showing, instead they are showing as standard blue icons and the seems not to get the configuration.

My config or code does nothing really exiting. Just coloring the icons on temperature change (this works)

I already googled this issue, did not find a solution. I am pretty new to Home Assistant in general so any advise would be appreciated! Let me know if you need more info, pictures or whatever

Post a SHORT properly formatted code demonstrating the issue.

Thanks for your answer!
This is one part which stays always the same and does not get changed by itself:

type: entities
entities:
  - entity: switch.wohnzimmer_licht_schalter
    card_mod:
      style: |
        :host {
        {% if is_state('switch.wohnzimmer_licht_schalter' , 'on') %}
        --card-mod-icon-color: green;
        {% elif is_state('switch.wohnzimmer_licht_schalter' , 'off') %}
        --card-mod-icon-color: red;
        {% endif %}
        }

This one here is an example of one that gets changed:
How it should look:

  - entity: sensor.192_168_178_23_amd_gpu_gpu_amd0_prozessorauslastung
    card_mod:
      style: |
        :host {
        {% if states('sensor.192_168_178_23_amd_gpu_gpu_amd0_prozessorauslastung') | float(0) > 75 %}
          --card-mod-icon-color: red; --card-mod-icon: mdi:alert-decagram;
        {% elif states('sensor.192_168_178_23_amd_gpu_gpu_amd0_prozessorauslastung') | float(0) > 50 %}
          --card-mod-icon-color: yellow;
        {% elif states('sensor.192_168_178_23_amd_gpu_gpu_amd0_prozessorauslastung') | float(0) <= 50 %}
          --card-mod-icon-color: green;
        {% endif %}
          }

How it gets changed:

  - entity: sensor.192_168_178_23_amd_gpu_gpu_amd0_prozessorauslastung
    card_mod:
      style: >
        :host {

        {% if
        states('sensor.192_168_178_23_amd_gpu_gpu_amd0_prozessorauslastung') |
        float(0) > 75 %}
          --card-mod-icon-color: red; --card-mod-icon: mdi:alert-decagram;
        {% elif
        states('sensor.192_168_178_23_amd_gpu_gpu_amd0_prozessorauslastung') |
        float(0) > 50 %}
          --card-mod-icon-color: yellow;
        {% elif
        states('sensor.192_168_178_23_amd_gpu_gpu_amd0_prozessorauslastung') |
        float(0) <= 50 %}
          --card-mod-icon-color: green;
        {% endif %}
          }

I am using Dahsboard ā†’ Edit ā†’ Edit Card ā†’ Code-Editor ā†’ Paste Code

Guess this was your issue (better to ask in community first before registering an issue).
First, some general remarks:

  1. You can use ā€œconfig.entityā€ variable here to make a code better:
- entity: sensor.192_168_178_23_amd_gpu_gpu_amd0_prozessorauslastung
    card_mod:
      style: |
        :host {
        {% if states('sensor.192_168_178_23_amd_gpu_gpu_amd0_prozessorauslastung') ...

ā†’

- entity: sensor.192_168_178_23_amd_gpu_gpu_amd0_prozessorauslastung
    card_mod:
      style: |
        :host {
        {% if states(config.entity) ...
  1. You got
if ...
...
elif ...
...

w/o a default ā€œelseā€ path - which in some cases may give you bad results.

As for ā€œHow it gets changedā€ - UI editor processes long lines in such way.
In your case you have ā€œ|ā€ replaced by ā€œ>ā€.
Read this.
In my experience, this conversion cannot break card-mod code - i.e. I never observed an issue ā€œcode was working OK before conversion ā†’ conversion ā†’ code stopped workingā€. If you see this - then most probably your code is wrong.

BTW, I never use ā€œā€“card-mod-icon-colorā€ variable & prefer more robust ways.

Thank you!

I see, i closed that one. I opened it before i knew there was a forum dedicated to this.
Anyways: yes my code is pretty bad. your advise did help me. So now i changed it into your suggestion with config.entity, that shortens the code and it does not ā€œbreakā€ anymore.

Do you have an alternative to the variable ā€œā€“card-mod-icon-colorā€?

My initial problem did not solve with adjusting the code.
Still in browser view the icons are colored and working (now better than before) but in the companion app they still do not change.
Android Phone, latest Android Version, latest App Version (not beta), no Energy Saving for App and Full Access granted

There are some cases when this variable may not work, see a related issue on Github (may be the issue is already closed, have not tracked it).
Alternatives:

  1. For Entities card as an example: use simple ā€œcolorize an iconā€ ways - see 1st post ā†’ link at the bottom ā†’ styles for Entities card.
  2. For an entity of some domains (incl. ā€œswitchā€) - check here, define a corr. variable (either for a whole card to affect all entities of same domain or for a particular entity):
type: glance
entities:
  - entity: switch.test_switch
  - entity: switch.test_switch_2
state_color: true
card_mod:
  style: |
    ha-card {
      --state-switch-on-color: red;
      --state-switch-off-color: cyan;
    }

Thanks, that was very helpful!

Now only the problem with the companion app persists and i have no idea why.

When i open the App all symbols appear as they are not edited. When i change something in the browser then i see an ā€œreloadā€ button on the bottom of the app. I press it ā†’ the colors and icons change as they should be ā†’ i close the app ā†’ open it again ā†’ all colors and icons again in default mode

Check with a simple Entities card & a simple card-mod style for colorized icon & come back with results (post a code with which you tested).

This does not work at all. Not even in the preview panel.
When i create an entity card i just get error message that card_mod is not supported

See no issues with my code in Win10+Chrome and in iOS Companion app (iOS 12.x).

i donā€™t know either why it is not working. I ensured the code is the exact same just different entities. I also switched to another computer, opened in friefox, chrome and even edge Win10 + Win11. No difference the code seems not to work.

Interesting is if i do this:

type: glance
entities:
  - entity: switch.kueche_licht_schalter
    icon: mdi:record

The icon does appear also in the app. So it seems to be a problem (as you already said) of the --card-mod-icon variable

Problem is i have to little knowledge about this that i can improve my code as i already tried things like this for example:

    card_mod:
      style: |
        :host {
        {% if is_state(config.entity , 'on') %}
        --icon-color: green;

edit
i shit you not, when i copied your code from above right now it worked, it was EXACTLY the same as i wrote down

BUT: in the companion app the icons are default blue

wait, I never read that beforeā€¦ are you serious? what is not robust in your experience? I must have a look if that could speedup my config maybe.

tbh, Ive never met a case where this does not work, but, given the core support for those state colors, it does sound very interesting, even more ā€˜supported in coreā€™ for that matter.

thing is, there are quite a few domains Not supported in state color theming, sensor being one of the main domains ofc, so we can only use --card-mod-icon(-color).

# pm25_color:
style: |
  :host {
    {% set air = states(config.entity)|float(-5) %}
    --card-mod-icon-color:
      {% if air < 35 %} var(--ok-color)
      {% elif air < 85 %} orange
      {% elif air >= 85 %} var(--alert-color)
      {% else %} var(--no-power-color)
      {% endif %};
  }

is my main way of modding the icon-color and this works domain independent., even overrules device_class nicely without !important

# active_icon:
# replacing the mdi:play/stop now set by the device_class: running
style: |
  :host {
    --card-mod-icon:
        mdi:{{'checkbox-marked-circle' if is_state(config.entity,'on') else
              'radiobox-blank'}};
  }

what I am trying to say: looking for a spot this is not robust, so I can test that seriously

and suppose we should not pursue to use the --paper-item-icon-color variable anymore, so this would be the ā€˜defaultā€™ replacement for what I have above:

    card_mod:
      style:
        hui-generic-entity-row:
          $: |
            state-badge {
              color: {{'red' if is_state(config.entity,'below_horizon') else 'green'}};
            }   

which to me seems more fragile and complex, and most of all, loses the elegance of using both --card-mod-icon and --card-mod-icon-color on the same element in the dom path


(ofc except the case I posted about yesterdayā€¦)