🔹 Card-mod - Add css styles to any lovelace card

I am currently setting up a dashboard that uses classes. That way i can define all styling in a theme, and don’t have to write it down in each card. To do this i use the syntax

card_mod:
      class: overview-card

I want this to be a reactive to the state of a light. Elsewhere i can use conditions to display certain text or colors. Applying this format to the card_mod however does not seem to work. What i want is the following.

card_mod:
      class: |-
        {% if is_state("light.opslag_licht","on") %}
        overview-card-active
        {% else %}
        overview-card
        {% endif %}

Maybe someone has had this problem before, and managed to solve it.

You cannot use templates for classes. There is an open FR for this.

Thanks for replying, not sure what FR means (i’m a newbie sorry)

feature request

1 Like

Anyone can help me?

quoting the docs:

In short; if things seem to be working intermittently - try splitting up the chain into several steps

tbh, I didn’t check your code, but your description seems to fit. check the examples on DOM navigation and scroll down to the end, maybe it can help you out here?

I cannot reproduce your issue. Your DOM paths are correct.
Can you see the issue on a normal PC, not buggy Androids & Safari?

Suggestions:

  1. (unrelated) Provide default values for “int” conversions (makes a code safer).
  2. Instead of last “elif” - use “else”.

agreed with Ildar, make your template more robust (though I dont necessarily see the need to overload the template with the color:. I always have that outside, and it never fails).

          {% set state = states('sensor.co2_eg_wohnen')|int(-1) %}
          {% if state < 1000 %} color: green;
          {% elif 1000 <= state < 1400 %} color: orange;
          {% else %} color: red;
          {% endif %}

main thing here is the one time setting of the variable and its default value |int(-1), which is no longer required in the rest of the template. see eg:

        color:
          {% set range = states('sensor.golf_combustion_range')|int(0) %}
          {% if range < 100 %} crimson
          {% elif range < 300 %} darkorange
          {% elif range < 450 %} orange
          {% elif range < 600 %} gold
          {% else %} lightgreen
          {% endif %};

see if it makes a change.

other than that, sometimes it simply has nothing to do with these mods, but more with the way the Dashboard is being rendered upon state changes, or user manipulating the Frontend

navigating to another dashboard and returning will help at those times, reloading a view not always helps.

also, be sure to configure card_mod like this

frontend:
  extra_module_url:
    - /hacsfiles/lovelace-card-mod/card-mod.js

and not via the resources, as this preloads, and seems your best change preventing timing issues in the rendering

Just wanted to bump this once. Not trying to be annoying won’t do it again.

Just can’t find the answer anywhere for something that should be so simple

The “expander-card” seems to be not widely used; have you tried to search a DEDICATED thread for this card? May be there are more chances to get answers there…

I did and I didn’t get a response. I was hoping this was more of a card mod thing. I tried to hit F12 do the whole inspect thing, but I’m not very good at it. Was hoping that was all that needed to be to be done to target the icon I’m trying to change.

This is weird though. Different. Because assuming I’m doing it right which I’m probably not lol I can get it to change on the edit view before I hit save but as soon as I hit, save none of my changes show up so maybe it’s just a garbage card and I need to try and find something new.

sometimes the simpler → the better

I’m trying some advanced css to animate a custom svg icon, specifically a thermometer bar:
image

I am struggling to get to the shadow root/element for the bar:
image

the closest I get really, is that I can color the entire thermometer (red) in an overcomplicated way since the below code also just works when I reference ha-state-icon:

                  card_mod:
                    style:
                      ha-state-icon $ ha-icon $ ha-svg-icon $: |
                        svg {
                          color: red;
                        }

Can anyone help just getting the bar to assume a color, from there on I believe the animation is a breeze.

EDIT: I assume I will have to adjust the path values but there a so many that I was hoping there’s an easier way to do it

Yes this also happened on desktop browsers Chrome and Firefox.

Thanks for the tips, I adjusted the code.

Thanks for your detailed answer. Especially for the hint how to implement card_mod correctly.
I am going to test all suggestions in the next days and hopefully can find out what caused my problems.

Hi, I have following entity card, and I would like to change the color of „Now“ entity. If the entity’s state is more than 50%, then green color, if less than 50%, then red color. So, in this case, the number „11%“ should be red.
Thank you

type: custom:mod-card
card:
  type: horizontal-stack
  title: Battery
  cards:
    - type: entities
      entities:
        - entity: sensor.tpj4cjm04k_statement_of_charge_2
          name: Now
          card_mod:
            style:
              hui-generic-entity-row $: |
                state-badge {
                  display: none;
                }
        - entity: sensor.max_baterie_fve_dnes_2
          name: Max today
          card_mod:
            style:
              hui-generic-entity-row $: |
                state-badge {
                  display: none;
                }
      show_header_toggle: false
      card_mod:
        style:
          .: |
            #states > * {
              margin: 2px -10px !important;
              margin-left: -25px !important;
              font-size: 10.5px;
              --mdc-icon-size: 18px; 
            }

This should work for you…

type: custom:mod-card
card:
  type: horizontal-stack
  title: Battery
  cards:
    - type: entities
      entities:
        - entity: sensor.tpj4cjm04k_statement_of_charge_2
          name: Now
          card_mod:
            style:
              hui-generic-entity-row $: |
                state-badge {
                  display: none;
                  }
                .state {
                 color: {{'green' if states(config.entity) | int >= 50 else 'red'  }}
                  }

why the mod-card, if you don’t mod the card inside it?

Great, it works, thank you :+1::blush:

Agreed, it was part of the original code and I let it be.