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

tbh, I really believe you should be using another card, if you want as much change like that.

You are showing a light, which in HA will show its color and brightness natively upon change, so imho, no need to set a mod on that.

I can see why one would use a different icon, so yeah, I guess you could to that using card_mod.

all of these are native options in eg custom:button-card, why no use that, and use a core type: grid to show your lights including their brightness:

sure, this is a very mature button-card template, but it has been working for the last 5 years :wink: No issue with card_mod at all, all native inside the button-card options

cut it short: my advice would be to use the card that matches your requirements as closely as possible. Use card_mod there where nothing else will do the job. Or when really straightforward.

as for this

I already did in the post above:

just use your template there, replacing the now fixed hidden and it should do what you want, on the entity itself

1 Like

haha, yes, well, my example was only used in a config where I needed 2 rows of 4 columns, and the top row only displayed 3 entities, so I needed that empty placeholder fixed.

this was the only way of doing that at the time, and, considering what I just tested with the visibility property, it still is.

Btw, I did put in (editā€¦) a WTH for conditional in Glance, like we can do in entities: WTH, why cant we use a type conditional in Glance

it would solve a lot of trouble we experience here

Yes, I was a bit intrigued before your edit). Have seen your WTH and voted there already.

1 Like

got you ) that was the trick!
took some time, but I managed to modify the code, so itĀ“s working now!

type: picture-elements
image: /local/floorplan/base.png
elements:
  - type: conditional
    conditions:
      - entity: light.tradfri_light
        state: "on"
    elements:
      - type: image
        tap_action:
          action: none
        hold_action:
          action: none
        entity: light.tradfri_light
        image: /local/floorplan/light.tradfri_light.png
        style:
          left: 50%
          top: 50%
          width: 100%
          mix-blend-mode: lighten
          filter: var(--my-filter_tradfri_light)
          opacity: var(--my-opacity_tradfri_light)
    card_mod:
      style: |
        :host {
          --my-filter_tradfri_light: hue-rotate({{ state_attr('light.tradfri_light', 'hs_color')[0] | int }}deg) saturate(100%) !important;
          --my-opacity_tradfri_light: {{(states['light.tradfri_light'].attributes.brightness / 255)}};
          }
  - type: conditional
    conditions:
      - entity: light.rgb_bad_light
        state: "on"
    elements:
      - type: image
        tap_action:
          action: none
        hold_action:
          action: none
        entity: light.rgb_bad_light
        image: /local/floorplan/light.rgb_bad_light.png
        style:
          left: 50%
          top: 50%
          width: 100%
          mix-blend-mode: lighten
          filter: var(--my-filter_rgb_bad_light)
          opacity: var(--my-opacity_rgb_bad_light)
    card_mod:
      style: |
        :host {
          --my-filter_rgb_bad_light: hue-rotate({{ state_attr('light.rgb_bad_light', 'hs_color')[0] | int }}deg) saturate(100%) !important;
          --my-opacity_rgb_bad_light: {{(states['light.rgb_bad_light'].attributes.brightness / 255)}};
          }          
  - type: state-icon
    entity: light.tradfri_light
    title: null
    style:
      top: 65.71%
      left: 68%
      border-radius: 50%
      text-align: center
      background-color: rgba(255, 255, 255, 0.2)
    tap_action:
      action: toggle
  - type: state-icon
    entity: light.rgb_bad_light
    title: null
    style:
      top: 54.88%
      left: 80.45%
      border-radius: 50%
      text-align: center
      background-color: rgba(255, 255, 255, 0.2)
    tap_action:
      action: toggle

1 Like

Looking nice)

this is broken in 2025.1.0b Home Assistant card (replacing former bar-card config) - #2 by Mariusthvdb as is this battery card

so the mod is this, and it doesnā€™t color the background anymore.

  - entity: sensor.processor_use
    name: Cpu
    card_mod: &perc
      style: |
        hui-generic-entity-row {
          background:
            {% set perc = states(config.entity)|float(0) %}
            /*{% set rest = 100 - perc %}*/
            {% if perc >= 59 %} {% set bar = '255,0,0' %}
            {% elif perc >= 44 %} {% set bar = '128,0 0' %}
            {% elif perc >= 24 %} {% set bar = '255,165,0' %}
            {% elif perc >= 9 %} {% set bar = '0,100,0' %}
            {% else %} {% set bar = '0,128,0' %}
            {% endif %}
            /*linear-gradient(to left,ivory {{rest}}%, {{bar}} {{perc}}%);*/
            linear-gradient(to right, rgb({{bar}},0.9) 0%, rgb({{bar}},0.6) {{perc}}%,
                                      rgba({{bar}},0.3){{perc}}%, rgba({{bar}},0.1) 100%);
        }

setting it in the Inspector works as beforeā€¦

need to check if this causes the change Update hui-generic-entity-row.ts Ā· home-assistant/frontend@85bd643 Ā· GitHub

Marius, what PR this commit belong to?
(do not think this commit is related)
Check with a simple style:

card_mod:
  style: |
    :host {color: red}

does nothing not even with

  - entity: sensor.processor_use
    name: Cpu
    card_mod: &perc
      style: |
        host {
          color: red !important;
        }

Bram said they changed attributes (not sure which, but that PR was the only thing I could find) and also this:

that restores the attribute, but it has a different name, and will be in the next beta

all I know is my cards where perfect until this cycle, maybe even b0, I will downgrade to check for sure (downgrade seems to not work, so suppose we need to wait and see for the PR merge in b2)

UPDATE
Downgrade to b0 again worked after all, and the same is happening, none of these backgrounds on hui-generic-entity-row are applied.
Will check B0 for changes in those properties

Hmmmm, interesting. Weā€™ll see.

same goes for this card where the info should not display because of

    card_mod:
      style:
        hui-generic-entity-row $: |
          .info {
            display: none;
          }

:


and this on the fold-entity-row:

# fold-entity-row scroll_fold
style:
  ha-icon:
    $: |
      ha-svg-icon {
        color: var(--primary-color);
        width: 24px;
      }
  .: |
    #measure {
      max-height: {{states('input_number.max_scroll_hoogte')}}px;
      overflow-x: hidden;
      overflow-y: scroll;
    }

no longer scrollsā€¦
man they really bring about pain for us card_modders this new years releaseā€¦ and all custom, so not supported in Core anyways, weā€™re on our own here

I wonder what changed. Do not recall a PR which could affect it.
If this simple

ha-card {color: red}

for Entities work?

Letā€™s wait for some beta-releases and post it into beta-discord.

But unteil now, you dodnā€™t show, if it is still ingested into dom. :wink: Content of in the corresponding DOM tree.

Can I use card-mod to set the background in a dashboard to the value of a specific sensor?

body {
  background-image: url('{{ states('sensor.unsplash_background') }}');
  background-size: cover;
  background-position: center;
}

Iā€™ve tried different things but Iā€™m not able to get any css output.

more disaster:

    filter:
      template: >
        [{% set threshold = states('input_number.power_threshold')|float(0) %}
        {% for s in states.sensor
         |rejectattr('entity_id','search','solaredge|samen')
         |selectattr('entity_id','search','_actueel|_current_power')
         if s.state|float(0) > threshold %}
           {{
              {
                'entity': s.entity_id,
                'card_mod':
                  {'style':
                      ':host {
                        --card-mod-icon-color:
                          {% set state = states(config.entity)|float(-5) %}
                          {% if state > 2000 %} purple
                          {% elif state > 1000 %} maroon
                          {% elif state > 450 %} darkred
                          {% elif state > 300 %} firebrick
                          {% elif state > 250 %} crimson
                          {% elif state > 150 %} darkorange
                          {% elif state > 70 %} orange
                          {% elif state > 10 %} lightsalmon
                          {% elif state > 0 %} var(--power-color)
                          {% else %} var(--no-power-color)
                          {% endif %};
                      }'
                  }
              }
           }},
          {%- endfor %}]

no longer colors the icon based on states.

simpler version of that:

style: |
  :host {
    {% set signal = states(config.entity)|int(-5) %}
    --card-mod-icon:
      {% if signal == 'unknown' %} mdi:help
      {% elif signal >= 80 %} mdi:wifi-strength-4
      {% elif signal >= 50 %} mdi:wifi-strength-3
      {% elif signal >= 40 %} mdi:wifi-strength-2
      {% elif signal >= 30 %} mdi:wifi-strength-1
      {% elif signal >= 20 %} mdi:wifi-strength-outline
      {% else %} mdi:wifi-strength-alert-outline
      {% endif %};
    --card-mod-icon-color:
      {% if signal == 'unknown' %} gray
      {% elif signal >= 80 %} darkgreen
      {% elif signal >= 50 %} green
      {% elif signal >= 40 %} lightgreen
      {% elif signal >= 30 %} gold
      {% elif signal >= 20 %} orange
      {% else %} maroon
      {% endif %};
  }

all stopped being appliedā€¦icon or icon-color

this must be some generic dom path thingy change, or otherwise were borked. been working for, well, years

setting a straight forward simple color and icon doesnā€™t work either, so its not the template (of course it isnt)

really need some help test running the beta cycle this time, hope anyone of you can join.

We should consider this the end of times of card_mod orā€¦ hope its a simple matter of adapting our mods in the config. given the amount of trouble I now see, I fear the formerā€¦

in the UI:

type: entities
entities:
  - entity: sensor.slaapkamer_hygro_temp_temperature
    name: Slaapkamer
    card_mod:
      style: |
        :host {
          --card-mod-icon-color: red;
        }

and yet, these all do work as before:

        entities:
          - entity: sensor.ws_5500_wind_direction
            name: Wind Direction
            card_mod: !include /config/dashboard/card_mods/wind_richting.yaml
          - entity: sensor.ws_5500_wind_gust
            name: Wind Gust
            card_mod: !include /config/dashboard/card_mods/wind_snelheid_kmu.yaml
          - entity: sensor.ws_5500_max_daily_gust
            name: Max Daily Gust
            card_mod: !include /config/dashboard/card_mods/wind_snelheid_kmu.yaml

or even


in a glance card

and they have the exact same format:

#wind_direction:
style: |
  :host {
    --card-mod-icon:
      {% set dir = states(config.entity)|int(0) %}
      {% set icons = ['down','bottom-left','left','top-left','up','top-right',
                      'right','bottom-right'] %}
      {% set quadrant = (dir/45)|round %}
      {% if quadrant < icons|count %} mdi:arrow-{{icons[quadrant]}}
      {% else %} mdi:arrow-down
      {% endif %};
  }

or

# wind_snelheid:
style: |
  :host {
    --card-mod-icon-color:
      {% set state = states(config.entity)|int(-5) %}
      {% if state == 'unknown'%} gray
      {% elif state < 0.5 %} lightblue
      {% elif state < 1.5 %} paleturquoise
      {% elif state < 3.3 %} aquamarine
      {% elif state < 5.5 %} greenyellow
      {% elif state < 7.9 %} lime
      {% elif state < 10.7 %} mediumspringgreen
      {% elif state < 13.8 %} yellowgreen
      {% elif state < 17.1 %} navy
      {% elif state < 20.1 %} gold
      {% elif state < 24.4 %} orange
      {% elif state < 28.4 %} tomato
      {% elif state < 32.6 %} orangered
      {% else %} crimson
      {% endif %};
  }

so its truly mesmerizing some of these work, and some dont

background of the view, you need card-mod theming for that.
background of a card: yes you should be able to do that using card-mod

I will try to see what commits could affect this.
Not using 2025.1 yetā€¦

Marius, how to instal that 25.1 version?
Imho, Docs were ā€œimprovedā€ in way I cannot find info which I earlier used to find fast.

Settings>System>Updates>3 dot menu>Join beta channel

They borked browser_mod as well, might be related?

No, nothing.


Also, why do I need an installed HA to setup a beta version?..

OK, offtop stopped.