Fun with custom:button-card

Hi, I’m trying to pass variables to button_card_templates: but something is wrong

button_card_templates:
  orange_button:
    styles:
      name:
        - font-size: 11px
        - word-break: break-word
        - white-space: unset
    variables:
      var_confronto: ''
      var_stato: ''
    card_mod:
      style: |
        div#img-cell > ha-state-icon {
        {% if is_state('[[[ return variables.var_confronto ]]]', 'off') %}
        color: orange !important;
        {% else %}
        color: grey !important;
        {%- endif %}
           }

and in dashboard

cards:
      - type: horizontal-stack
        cards:
          - type: custom:button-card
            show_name: true
            show_icon: true
            tap_action:
              action: toggle
            entity: script.riscaldamento_basso
            hold_action:
              action: none
            icon: valutta:Riscaldamento GIU
            theme: slate
            template: orange_button
            name: Riscaldamento<br/>basso
            variables:
              var_confronto: input_boolean.riscaldamento
              var_stato: 'off'

“if is_state” have something wrong doesn’t change
please help me!

youre mixing the JS (= javascript, native templating to the button-card) with Jinja (native to card-mod), and that wont work

why not use all of the config in the button-card itself, and stop using card-mod.
setting an icon-color in button-card is very simple

also you dont need

in the template. this will be merged with the config, and since you set the variables there, all will be taken from those values

Thank you!!!

type: custom:button-card
show_name: true
show_icon: true
tap_action:
  action: call-service
  service: script.riscaldamento_basso
entity: input_boolean.riscaldamento
hold_action:
  action: none
icon: valutta:Riscaldamento GIU
theme: slate
template: orange_button
name: Riscaldamento<br/>basso
state:
  - value: "off"
    color: orange
  - value: "on"
    color: grey

So better!!!

I created simple button card to notify me about washing machine status. Tricky part is that it overlaps circle icon on top of bacground image (screenshot of washing machine icon) to create washing animation:

type: conditional
conditions:
  - entity: input_text.washing_status
    state: Washing Started
card:
  entity: input_text.washing_status
  icon: mdi:circle-slice-4
  name: Washing Cycle in Progress
  size: 53px
  styles:
    icon:
      - color: var(--cyan2)
      - animation: 2s infinite ease-in-out rotating
    card:
      - background-image: url('/local/washing-machine.png')
      - background-size: 53px
      - background-position: center 5px
      - background-repeat: no-repeat
  tap_action:
    action: none
  type: custom:button-card

Screenshot 2024-12-22 at 11.56.03

It works perfectly as stand alone card. Vut for my target dashboard I’d like to put it on top of picture element card and for this I need to have trasparrent bvackground of the card. But if I apply some card mod CSS style, it also remove washing machine picture, leaving only rotating icon.

card_mod:
  style: |
    ha-card {
      background: none !important;
    }   

Is the any way to remove card solid color bacground leaving the background picture?

Hope this is the correct thread hope someone can explain whatever simple thing I am missing. This YAML is for showing wind speed and direction, the background icon changes dynamically based on wind speed- That works fine, I have a custom sensor that turns the wind direction from degrees to cardinal direction, that works fine. If the wind speed is 0 mph the background is a static image and does not display any MPH or direction data- That works fine BUT… If the MPG is above 0 I want the MPH and wind direction to appear and it does but the PROBLEM is they are overlapped on top of each other and I can not seem to find a way to seperate them!! That is what I need help with. Here is the YAML for the Dasboard button.

`type: custom:button-card
entity: sensor.denmans_mountain_wind_speed
show_icon: false
show_name: false
show_state: false
styles:
  card:
    - height: 150px
    - width: 150px
    - background-size: contain
    - background-repeat: no-repeat
    - background-position: center
    - background-image: >
        [[[
          return `url(${states['sensor.wind_speed_icon'].state})`;
        ]]]
    - display: grid
    - grid-template-rows: auto 1fr auto
    - align-items: center
  custom_fields:
    wind_speed:
      - justify-self: center
      - font-size: 18px
      - font-weight: bold
      - color: var(--primary-text-color)
    wind_direction:
      - justify-self: center
      - font-size: 16px
      - color: var(--secondary-text-color)
custom_fields:
  wind_speed: >
    [[[
      return `${states['sensor.denmans_mountain_wind_speed'].state} mph`;
    ]]]
  wind_direction: >
    [[[
      return `${states['sensor.denmans_mountain_wind_cardinal_direction'].state}`;
    ]]]
`

Any help is appreciated.

Hello!
Please, could someone explain to me why - only using custom_fields in my button card - very often if i refresh the page the card disappear??? If I remove the custom fields all is working well…

Thanks

This is supposed to be dynamically updating the icon based on temp change but it isn’t. it displays the current temp from my local weather station then it should be pulling data from a sensor that pulls hourly temp data based on that if the next hours temp is higher or lower it should change the icon if the temp is the same it displays a"same" icon all I can get it to do is display the same icon. I have checke dto ensure the path is current for the icons and all the sensors are working and pulling data but can’t get this to work any and all help is welcomed! here is the card YAML

entity: sensor.denmans_mountain_temperature
show_icon: false
show_name: false
show_state: true
styles:
  card:
    - height: 150px
    - width: 150px
    - background-image: |
        [[[ 
          const trend = states['sensor.temperature_trend'].state;
          if (trend === 'warmer') {
            return "url('/local/temperature/thermometer-warmer.svg')";
          } else if (trend === 'colder') {
            return "url('/local/temperature/thermometer-colder.svg')";
          } else {
            return "url('/local/temperature/thermometer.svg')";
          }
        ]]]
    - background-size: contain
    - background-repeat: no-repeat
    - background-position: center

here are the template sensors

    # Sensor for the next hour's temperature
    - name: "Next Hour Temperature"
      unique_id: "next_hour_temperature"
      state: >
        {% set forecast = state_attr('sensor.pirate_weather_hourly', 'data') %}
        {% if forecast and forecast[0] %}
          {{ forecast[0].temperature }}
        {% else %}
          "N/A"
        {% endif %}
      unit_of_measurement: "°F"

    # Sensor for the daily high temperature
    - name: "Daily High Temperature"
      unique_id: "daily_high_temperature"
      state: >
        {% set forecast = state_attr('sensor.pirate_weather_daily', 'data') %}
        {% if forecast %}
          {{ forecast | map(attribute='temperature') | max }}
        {% else %}
          "N/A"
        {% endif %}
      unit_of_measurement: "°F"

    # Sensor for the daily low temperature
    - name: "Daily Low Temperature"
      unique_id: "daily_low_temperature"
      state: >
        {% set forecast = state_attr('sensor.pirate_weather_daily', 'data') %}
        {% if forecast %}
          {{ forecast | map(attribute='templow') | min }}
        {% else %}
          "N/A"
        {% endif %}
      unit_of_measurement: "°F"




    # Temperature Trend Sensor
    - name: "Temperature Trend"
      unique_id: "temperature_trend"
      state: >
        {% set current_temp = states('sensor.denmans_mountain_temperature') | float(0) | round(1) %}
        {% set next_hour_temp = states('sensor.next_hour_temperature') | float(0) | round(1) %}
        {% if next_hour_temp > current_temp %}
          "warmer"
        {% elif next_hour_temp < current_temp %}
          "colder"
        {% else %}
          "same"
        {% endif %}

here is the automation to update the trend sensor

description: Update temperature trend every 30 minutes
triggers:
  - minutes: /30
    trigger: time_pattern
actions:
  - data:
      entity_id:
        - sensor.temperature_trend
    action: homeassistant.update_entity
mode: single ```

Custom_field not correctly working after HA 2025.0

type: custom:button-card
entity: switch.oven_inschakelen
hold_action:
  action: none
name: Oven
icon: mdi:toaster-oven
color_type: card
show_state: true
show_entity_picture: true
custom_fields:
  percentage:
    card:
      type: custom:button-card
      entity: sensor.oven_programma_voortgang
      tap_action:
        action: none
      show_name: false
      show_state: true
      show_icon: false
      show_entity_picture: false
      color_type: card
      color: rgba(225,225,225,0.8)
styles:
  card:
    - width: 155px
    - height: 155px
    - margin: 0px
  name:
    - display: block
    - position: absolute
    - left: 8px
    - bottom: 21px
    - font-weight: bold
    - font-size: 16px
    - font-family: Sans-serif
  img_cell:
    - margin: 0px
    - padding: 0px
  state:
    - display: block
    - position: absolute
    - bottom: 5px
    - left: 12px
    - font-size: 14px
    - font-family: Sans-serif
    - text-transform: capitalize
  icon:
    - width: 100px
    - margin: 0px 0px 5px 0px
  custom_fields:
    percentage:
      - width: 50px
      - display: block
      - position: absolute
      - top: 8px
      - right: 8px
      - font-size: 14px
state:
  - value: "on"
    color: rgba(241,233,233,0.6)
    entity_picture: /local/pictures/entities/device/household/oven_icon_136.png?v=1
  - value: "off"
    color: rgba(65,65,63,0.8)
    entity_picture: /local/pictures/entities/device/household/oven_icon_136.png?v=1
    styles:
      card:
        - filter: opacity(50%)
      icon:
        - filter: grayscale(100%)
  - value: unavailable
    color: rgba(65,65,63,0.8)
    entity_picture: /local/pictures/entities/error_icon_136.png?v=1
    styles:
      card:
        - filter: opacity(50%)
      img_cell:
        - margin: 0px
        - padding: 0px
      icon:
        - width: 70px
        - margin: 0px 0px 10px 0px
  - value: unknown
    color: rgba(65,65,63,0.8)
    entity_picture: /local/pictures/entities/question_icon_136.png?v=1
    styles:
      card:
        - filter: opacity(50%)
      img_cell:
        - margin: 0px
        - padding: 0px
      icon:
        - width: 70px
        - margin: 0px 0px 10px 0px

the color does not apply anymore, and also font styles do nothing anymore :frowning:

Anyone has the same?

custom field

It was and should be white and the font shoud be 14px. but its Ha original font size now.


It seems that the fonts/text are not applying anymore because of a element
ha-card.button-card-main.

Is there a way to override this?

I’m trying to create a condition in a button card template that retrieves the name of the card itself.
I am defining the name in the dashboard for the template and am trying to access it to compare it to an Input select. I have tried this.name and config.name to no avail.

I’m trying to avoid having to define the card name in variables.

Does anyone have an idea?

Hi all. I have a problem w dashboard with type SECTION and use card with custom:button-card

When use and use section and put my code my card with button-card , tit no working no load correct and no load all cards…
When use tile view and also use the same cards all works ok and load correct.

This is my code:
https://pastebin.com/YxvNdjQq

Problem is only on first view in dashboard. When use it with secion on second view all load ok.
I dont know where is problem… but only on view section…
anybody has solution for it ?

Button card custom_fields are not working correctly anymore

The problem is button card, not card_mod. Some styles set in the “style” option from the button_card do not work anymore.
The issue is simple: ha-card.button-card-main, has default values for “fonts” “witdh” “padding”. All the values you want te edit/set in the custom_fields do not work anymore. inside custom_fields the button_card styles always stay the default values that are in “ha-card.button-card-main”.

This means someday, the other “styles” in button_card will also not work anymore.

A “fix” is only possible because there is a card_mod. That has the power to overrule this again. Still means button_card is broken.

version 3.5 from card_mod did NOT overrule them anymore. Version 3.4.4 does overrule them again.

Example:

type: custom:button-card
....
custom_fields:
  Number:
    card:
      type: custom:button-card
      entity: sensor.hp_printer_pages_total_remaining
      tap_action:
        action: none
      show_name: false
      show_state: true
      show_icon: false
      show_entity_picture: false
      color_type: card
      color: rgba(255,255,255,0.8)
      card_mod:
        style: |
          ha-card {
            font-size: 14px !important;
            border-color: #d9d9d9 !important;
            box-shadow: none !important;
          }

....

styles:
  card:
    - width: 155px > works
  name:
    - display: block > works
    - position: absolute > works
    - bottom: 21px > works
    - left: 8px > works
    - font-weight: bold > works
    - font-size: 16px > works
    - font-family: Sans-serif > works
  state:
    - display: block > works
    - position: absolute > works
    - bottom: 5px > works
    - left: 12px > works
    - font-color: rgba(255, 0, 0, 0.8) > works
    - font-size: 14px > works
    - font-family: Sans-serif > works
    - text-transform: capitalize > works
  icon:
    - width: 110px > works
    - margin: 0px 0px 1px 0px > works
  custom_fields:
    Number:
      - width: 50px > works because not in ha-card.button-card-main
      - display: block > works because not in ha-card.button-card-main
      - position: absolute > works because not in ha-card.button-card-main
      - top: 8px > works because not in ha-card.button-card-main
      - right: 8px > works because not in ha-card.button-card-main
      - font-size: 14px > does NOT work because IS in ha-card.button-card-main
      - font-family: Sans-serif > does NOT work because IS in ha-card.button-card-main
      - text-transform: capitalize > does NOT work because IS in ha-card.button-card-main
state:

Than we use card_mod for the values that do not work anymore in the style from button_card

      card_mod:
        style: |
          ha-card {
            font-size: 14px !important; > works in 3.4.4 not anymore in 3.5
            border-color: #d9d9d9 !important; > works in 3.4.4 not anymore in 3.5
            box-shadow: none !important;  > works in 3.4.4 not anymore in 3.5
          }

did you open an issue in button-card without any card_mod?

we can only followup if the exact order of things is presented and no other elements are involved.

as button-card has not been changed at all, this means you say this is because button-card is not compatible with the changes made in HA 2025.1 ?

or are you not on HA 2025.1

this has nothing to do with button-card, but with some specifics in card_mod, which was retracted indeed.

you probably need to downgrade to 3.4.3 nd then upgrade again t0 3.4.4.

edit

sorry you did write it up… and we’ve answered there already… including myself, duuh. sorry about that
check HA 2025.0 custom_fields not working anymore · Issue #896 · custom-cards/button-card · GitHub

+1 to @VietNgoc 's call out: dont blame the card/author for user-errors

and check GitHub - custom-cards/button-card: ❇️ Lovelace button-card for home assistant and the Hassos button

1 Like

Thats what i tried to say yes, the problem startet with HA 2025.0 and .1

I tried also everything without the use of card_mod. Could not get it to work.

I know, but i used card_mod to change what i could not anymore with button_card.
And indeed only with 3.4.4 again.

I will look at github, thanks.

? :wink:

? HA 2025.0 and HA 2025.1.
Im on 2025.1

Edit: 2025.2

haha, no you’re missing the month… there is no 2025.0,

was a mere joke. you are running 2025.1.2

Indeed, doing to much at the same time :stuck_out_tongue:
Excuse meee

I am trying to change the background color depending on the state of my printer:

type: custom:button-card
...
custom_fields:
  Number: |
    [[[
      if (states['sensor.hp_printer_pages_total_remaining'].state >= '9') {
      var rgba = 'rgba(225,225,225,0.8)';
      }
      else {
      var rgba = 'rgba(0,0,0,0.8)';
      }
        return `
          <ha-card style="width: 50px; height: 25px; padding: 0px 2px 0px 2px;
            color: rgba(62, 62, 62, 1.0); border-color: #d9d9d9;
            font-size: 14px; text-transform: capitalize; font-family: Sans-serif;">
            <span>${states['sensor.hp_printer_pages_total_remaining'].state}</span>
            background-color="var(--rgba2);"          
</ha-card>`
    ]]]

...

styles:
  state:
    - display: block
    - position: absolute
    - bottom: 5px
    - left: 12px
  icon:
    - width: 110px
    - margin: 0px 0px 1px 0px
  custom_fields:
    Number:
      - display: block
      - position: absolute
      - top: 8px
      - right: 8px
      - overflow: hidden
state:
  - value: "on"
    color: rgba(241,233,233,0.6)
    entity_picture: /local/pictures/entities/device/hp_printer_icon-136.png?v=1
...

Depending if it is more than 9 left it has a white color. or less than 9 it has a “red” (black in example) color.

But I have no idea how to make it. var does not work and $rgba also not.

Can someone help please?

Just use the built in parameters for state

entity: light.pc_lights
type: custom:button-card
icon: |
          [[[ return entity.state === 'on' ? 'mdi:light-switch' : 'mdi:light-switch-off';
           ]]]
show_state: false
show_name: false
size: 5em
tap_action:
          action: toggle
state:
          - value: "off"
            styles:
              card:
                - background: red
              icon:
                - color: blue
          - value: "on"
            styles:
              card:
                - background: yellow
              icon:
                - color: green