Fun with custom:button-card

afbeelding
Hi, I have made my own button card (on the right) but I would like to have a circle around my icon as well, if that is possible, but I can’t get it to work. Does anyone know how to do it?

          - type: 'custom:button-card'
            entity: light.woonkamerall
            icon: mdi:lightbulb-multiple
            color_type: icon 
            color: rgba(var(--color-yellow-text),1)
            name: Lampen
            layout: icon_name
            tap_action:
              action: navigate
              navigation_path: /ui-lovelace-minimalist/lampen
            styles:
              card:
                - height: 66px
              name:
                - font-weight: bold
                - font-size: 14px
                - align-self: middle
                - justify-self: start
                - padding-bottom: 0px

Is it possible to color button card based on applied aystem theme? I have my buttons colored black with white font, but on phone where I use light theme, I would like to switch that.
So for instance if I have light theme, display it white/black font for me, however at the same time, if my wife is using black theme, color them black.

try outline and outline-offset

styles:
  icon:
    - border-radius: 50%
    - outline: 2px soild red
    - outline-offset: 10px

I’m working on a little write up to share some other css that looks pretty cool in custom:button-card

You need to work with themes and the corresponding theme variables. :slight_smile:

You set a variable in the theme file and use that variable in your css code for the button. Then your color changes with the used or better selected theme. Take a look around in the theme you’re using.

Note: the selected theme is device, not user specific. :slight_smile:

I’m struggling to figure out how to approach this. Any and all advice would be greatly appreciated.

I have a handful of Wiz Bulbs, integrated through the recently added Wiz Integration. I’m looking to enhance the “color” on a button when one of the bulb effects is in use. The ‘auto’ color works great when the entity attribute [color_mode] either [color_temp] or [rgbw].

However, if I use one of the baked in effects, the entity attribute [color_mode] changes to [brightness], and the icon color defaults to the stock orange-ish color. I’d like to override this with an effect specific color somehow.

Can someone help me out? Have two grabs of the state attributes to show what I’m up against.

min_mireds: 153
max_mireds: 454
effect_list:
  - Ocean
  - Romance
  - Sunset
  - Party
  - Fireplace
  - Cozy
  - Forest
  - Pastel Colors
  - Wake up
  - Bedtime
  - Warm White
  - Daylight
  - Cool white
  - Night light
  - Focus
  - Relax
  - True colors
  - TV time
  - Plantgrowth
  - Spring
  - Summer
  - Fall
  - Deepdive
  - Jungle
  - Mojito
  - Club
  - Christmas
  - Halloween
  - Candlelight
  - Golden white
  - Pulse
  - Steampunk
  - Rhythm
supported_color_modes:
  - color_temp
  - rgbw
color_mode: color_temp
brightness: 128
friendly_name: Office02
supported_features: 4
hs_color:
  - 27.827
  - 13.281
rgb_color:
  - 255
  - 236
  - 221
xy_color:
  - 0.355
  - 0.343
color_temp: 183
min_mireds: 153
max_mireds: 454
effect_list:
  - Ocean
  - Romance
  - Sunset
  - Party
  - Fireplace
  - Cozy
  - Forest
  - Pastel Colors
  - Wake up
  - Bedtime
  - Warm White
  - Daylight
  - Cool white
  - Night light
  - Focus
  - Relax
  - True colors
  - TV time
  - Plantgrowth
  - Spring
  - Summer
  - Fall
  - Deepdive
  - Jungle
  - Mojito
  - Club
  - Christmas
  - Halloween
  - Candlelight
  - Golden white
  - Pulse
  - Steampunk
  - Rhythm
supported_color_modes:
  - color_temp
  - rgbw
color_mode: brightness
brightness: 128
friendly_name: Office02
supported_features: 4
effect: Romance

Hi Sel,

Sorry i add it to my config but still the same it don’t work… Any Idea?

Does anyone know if it is possible to set up an icon like a battery icon and change it by colour to the lowest percentage of a list of entities?

Example:
Battery state of the temperature sensor in the living room is 100%
Battery state of the temperature sensor in the sleeping room is 80%
Battery state of the temperature sensor in the office is 30%

Display the chosen colour for <40 on the icon, because the battery state of the office is the lowest.

What I have got now, and it works for 1 entity, but I want to add another 2 entities to the script.
That shows the lowest percentage as my example.

type: custom:button-card
icon: mdi:battery-outline
size: 80%
show_state: true
show_name: false
group_expand: true
entity: sensor.temperatuur_zolder_battery
styles:
  card:
    - height: 80px
    - width: 80px
state:
  - value: 10
    color: rgb(125, 128, 130)
    operator: <
  - value: 20
    operator: <
    color: rgb(15, 219, 22)
  - value: 30
    operator: <
    color: rgb(222, 209, 22)
  - value: 40
    operator: <
    color: rgb(237, 14, 37)

Think it would be possible if you can group these sensor entities but looking at the helper in HA it isn’t possible to group sensors?

If I use
entities:
instead of
entity:
It would not work.

@JasonVT, My initial thought is using the variables feature of the button card to specify your list of entities as an array variable, then in your state checks you use the scripting feature to get the min value of all the entities in the variable.

A different way to go that I find useful for myself is the battery state card. I have mine set up to be hidden unless batteries are below a specified minimum, and only those low batteries are shown. It also handles color gradients based on percentage of remaining battery.

I have also experimented with conditionally showing a small battery icon overlay on the same button card that I use to show/control the battery operated device. I use the custom_fields feature of the button card for the overlay. The nice thing about overlays is they can be clicked separately from the rest of the button to see the details of the battery.

Hi @electros, hear you go…

Button code when not selected

type: custom:button-card
color_type: card
color: black
name: Living Room
tap_action:
  action: navigate
  navigation_path: /tablet-livingroom/tablet-lr
styles:
  card:
    - height: 45px
    - box-shadow: 0px 0px 5px 4px gray
    - webkit-box-shadow: 0px 0px 50px 4px gray
  name:
    - text-transform: uppercase
    - letter-spacing: 0.2em
    - font-familly: cursive
    - justify-self: middle
    - align-self: middle
    - padding: 1px 1px
    - color: grey
  state:
    - justify-self: start
    - font-size: 2px
    - padding: 1px 1px

This is the code for highlighted buttons.

type: custom:button-card
color_type: card
color: grey
name: Main
tap_action:
  action: navigate
  navigation_path: /tablet-main/tablet-main
styles:
  card:
    - height: 45px
    - box-shadow: 0px 0px 5px 4px gray
    - webkit-box-shadow: 0px 0px 50px 4px gray
  name:
    - text-transform: uppercase
    - letter-spacing: 0.2em
    - font-familly: cursive
    - justify-self: middle
    - align-self: middle
    - padding: 1px 1px
    - color: black
  state:
    - justify-self: start
    - font-size: 2px
    - padding: 1px 1px

2 Likes

I have looked around, but I can’t seem to find an answer to my query - I suspect its simple, but I can’t work it out! I’m guessing its a case of using the button grid, but can’t seem to make it work.

I want to try and create a button that appears like the mock-up I’ve shown below. It would be for an electric car, and uses two sensors - one the battery percent, and the other the mileage range. (Be aware, I accidentally used the total car milage in my mock up, hence the mileage showing so high lol)

So ideally, i just want a button that allows a very quick glance at the range and battery… I plan to then look at changing the icon to orange when the battery goes below 25%. Though need to get the below sorted first.

Any thoughts? Thanks!

carstatus

Hi
taking a cue from this post, I’m inserting animated icons in a picture-element card.
However, I have problems with templates, or rather I’m not very practical.
this is what I am entering to simulate the movement of some lawn sprinklers

i am stuck when i want to add a variable to @keyframes.
I ask if you can tell me how to insert the code to recall the variable:

100% {transform: rotate '[[[return variables.ruota]]]'}

code:

button_card_templates:  
  mp800:
    show_state: false
    show_name: false
    show_entity_picture: true
    styles:
      card:
        - box-shadow: none
        - background-color: rgba(255, 255, 255, .0)
        - transform: '[[[ return variables.origin800 ]]]'
  mp80090:
    template: mp800
    variables:
      ruota: (60deg)
    state:
      - value: 'on'
        entity_picture: /local/floorplan//irrigatore off.png
        styles:
          icon:
            - animation: blink 5s linear infinite
            - width: 50%
      - value: 'off'
        entity_picture: /local/floorplan/irrigatore MP800.png
        styles:
          icon:
            - animation: rotate80090 3s linear infinite
            - transform-origin: center
    extra_styles: |
         @keyframes rotate80090 {
         0% {transform: rotate(0deg)}
         100% {transform: rotate'[[[ return variables.ruota ]]]'}
         }


###############################  sprinklers zona 3
    cards:
      - type: picture-elements
        image: /local/floorplan/irrigazione notte.png
        elements:
          - type: custom:button-card
            tap_action: none
            entity: switch.rainbird_1_artesiano
            template: mp80090
            variables:
              origin800: rotate(0deg)
            style:
              top: 50%
              left: 40%
              width: 20%
          - type: custom:button-card
            tap_action: none
            entity: switch.rainbird_1_artesiano
            template: mp80090
            variables:
              origin800: rotate(-90deg)
            style:
              top: 50%
              left: 50%
              width: 20%`

The [[[…]]] JavaScript template can’t be in the middle like you have it. The whole yaml property value has to be a JavaScript template. You can then return a value with the variable inserted. See original post down in the alerting sensor section for an example.

1 Like

Thanks for the reply.
Now i fixed the links to the javascript template following your advice.
It works, however, wanting to do one more thing, I am still stuck
I hope you can give me another help

I don’t understand why the variable `origin800: … it accept the variation of the value in the various button-cards,
while the variable turn:… which commands me the keyframes value it does not change, it maintains the value of the first over all
I thought it worked the same way?

button_card_templates:
  mp800:
    show_state: false
    show_name: false
    show_entity_picture: true
    styles:
      card:
        - box-shadow: none
        - background-color: rgba(255, 255, 255, .0)
        - transform: '[[[ return variables.origin800 ]]]'
  mp80090:
    template: mp800
    state:
      - value: 'on'
        entity_picture: /local/floorplan//irrigatore off.png
        styles:
          icon:
            - animation: blink 5s linear infinite
            - width: 50%
      - value: 'off'
        entity_picture: /local/floorplan/irrigatore MP800.png
        styles:
          icon:
            - animation: '[[[ return ` rotate80090 3s linear infinite` ]]]'
            - transform-origin: center
    extra_styles: |
      [[[ return `
         @keyframes rotate80090 {
           0% {
             transform: rotate(0deg);
           }
           100% {
             transform: ${variables.turn};
           }
         }
      `]]]
###############################  sprinklers zona 3
    cards:
      - type: picture-elements
        image: /local/floorplan/irrigazione notte.png
        elements:
###### first sprinkler
          - type: custom:button-card
            tap_action: none
            entity: switch.rainbird_1_artesiano
            template: mp80090
            variables:
              origin800: rotate(0deg)
              turn: rotate(60deg)
            style:
              top: 50%
              left: 40%
              width: 80%
###### second sprinkler
          - type: custom:button-card
            tap_action: none
            entity: switch.rainbird_1_artesiano
            template: mp80090
            variables:
              origin800: rotate(-90deg)
              turn: rotate(160deg)
            style:
              top: 50%
              left: 50%
              width: 20%
###### third sprinkler
.......etc....

i have an group of lights, now i want to display the count of lights turn on in an button-card (as state or name).
i created a template like this:
{{expand(state_attr('binary_sensor.lampen', 'entity_id'))| selectattr('state','eq','on')|map(attribute='entity_id')|list|count}} with home assistant.

I can’t get button-card to work with this template. Can someone explain me how to get the count of lights turn on with button-card
i read something about that plain javascript should be used, but could not find any further informations to query the objects.

Hi @ktownsend-personal

I’m using custom fields and I just can’t figure how to get my battery field to animate only when the battery is charging…
If I just insert - animation: blink 1s linear infinite
Then this works but I only want the animation when battery state changes to charging, any help appreciated thanks.

  custom_fields:
    steps:
      - background-color: black
      - opacity: 70%
      - position: absolute
      - left: 2%
      - top: 5%
      - font-size: 10px
      - color: yellow
      - font-weight: bold
    metres:
      - background-color: black
      - opacity: 70%
      - position: absolute
      - left: 2%
      - top: 15%
      - font-size: 10px
      - color: yellow
      - font-weight: bold
    battery:
      - animation: |-
          {% set state=states('sensor.iphone_battery_state') %}  
            {% if state=='Charging' %}
              return blink 1s linear infinite
      - background-color: black
      - opacity: 70%
      - align-self: right
      - position: absolute
      - right: 2%
      - top: '-2%'
      - font-size: 12px
      - font-weight: bold
      - color: >-
          [[[ if (states["sensor.iphone_battery_level"].state < 30) return
          "#e45649"; if (states["sensor.iphone_battery_level"].state < 50)
          return "#ffa229"; if (states["sensor.home_battery_level"].state < 101)
          return "lime"; else return "#ffc640"]]]

While I admit I do not know anything about custom fields yet (it’s the next thing I want to learn), I was able to get a custom:button-card to flash its icon when my phone is charging.

type: custom:button-card
template: generic_custom_button
entity: sensor.derek_s20_battery_level
name: Phone
styles:
  icon:
    - animation: |
        [[[
          if (states['binary_sensor.derek_s20_is_charging'].state === 'on') return 'blink 2s ease infinite';
        ]]]

The generic_custom_button template just sets the default appearance. Not sure if this is of any use to you but my example uses a slightly different IF comparison. I’m not sure of the differences between how your code is written compared to mine but I notice mine uses brackets instead of curly braces and my IF comparison is enclosed in parenthesis.

type: custom:button-card
tap_action:
  action: toggle
hold_action:
  action: more-info
entity: switch.portao_2_folhas
show_state: false
show_label: true
show_name: true
show_last_changed: false
name: Portão Veículos
size: 54px
state:
  - operator: template
    value: |
      [[[ 
        return states['binary_sensor.portao_2_folhas_external_input'].state === 'off'
      ]]]
    label: Fechado
  - operator: default
    icon: mdi:door-sliding-open
    label: Aberto
styles:
  card:
    - font-weight: bold
    - background: |
        [[[
          if (states['binary_sensor.portao_2_folhas_external_input'].state === 'off') return '#33211f';
          else return '#263926';
        ]]]
  name:
    - font-size: 14px
    - color: |
        [[[
          if (states['binary_sensor.portao_2_folhas_external_input'].state === 'off') return 'white';
          else return 'white';
        ]]]
  icon:
    - animation: |
        [[[
          if (states['binary_sensor.portao_2_folhas_external_input'].state === 'off');
          else return 'blink 2s ease 28';
        ]]]
    - color: |
        [[[
          if (states['binary_sensor.portao_2_folhas_external_input'].state === 'off') return '#DB4437';
          else return '#43A047';
        ]]]
  label:
    - font-size: 12px
    - color: |
        [[[
          if (states['binary_sensor.portao_2_folhas_external_input'].state === 'off') return 'gray';
          else return 'gray';
        ]]]

Can someone help me with this? I want the animation working only when I click on the button. In my code the animation start to work when the sensor binary turns on.

This is amazing @Shan, is there any chance you could share the yml for your person tracking card(s)? I’d love to shamelessly plagiarise it - it’s exactly what I’ve been attempting to create to no avail!

Hear you go…

look for sensors for phone, watch and person.abc entities. When charging icon changes on devices and location.
a

type: custom:button-card
entity: device_tracker.pixel_6_pro
show_icon: false
show_name: false
styles:
  card:
    - background-color: black
    - box-shadow: none
    - border-radius: 0%
    - padding: 1%
    - font-size: 12px
    - text-transform: capitalize
  grid:
    - grid-template-areas: '"i zone" "i battery""i watch" "ln1 ln2" "ln1 ln3"'
    - grid-template-columns: 100px auto
    - grid-template-rows: auto
  custom_fields:
    i:
      - justify-self: left
    zone:
      - margin-left: 5px
      - justify-self: left
      - padding-top: 4px
    battery:
      - margin-left: 5px
      - justify-self: start
      - '--text-color-sensor': >-
          [[[ if (states["sensor.pixel_6_pro_battery_level"].state <= 20) return
          "red"; ]]]
    watch:
      - margin-left: 6px
      - justify-self: start
      - padding-bottom: 4px
      - '--text-color-sensor': >-
          [[[ if (states["sensor.sj_galaxy_watch4_battery_level"].state <= 20)
          return "red"; ]]]
    ln1:
      - justify-self: left
      - margin-left: 1px
    ln2:
      - white-space: normal
      - text-align: start
      - text-align-last: left
      - margin-left: '-70px'
    ln3:
      - white-space: normal
      - text-align: start
      - text-align-last: left
      - margin-left: '-70px'
custom_fields:
  i: |
    <img height= 100px src='/local/pics/ABC.jpg'>
  zone: |
    [[[
      if (states['person.abc'].state == 'home')
        return `<ha-icon
          icon="mdi:home"
          style="width:22px;height:22px;color:white">
          </ha-icon><span> <span style="color: var(--text-color-sensor);">${states['person.abc'].state}</span></span>`;
      if (states['person.abc'].state == 'not_home')
        return `<ha-icon
          icon="mdi:map-marker-radius-outline"
          style="width:22px;height:22px;color:yellow">
          </ha-icon><span> <span style="color: var(--text-color-sensor);">Away</span></span>`;
       else 
        return `<ha-icon
          icon="mdi:map-marker-alert-outline"
          style="width:22px;height:22px;color:DarkOrange">
          </ha-icon><span> <span style="color: var(--text-color-sensor);">${states['person.abc'].state}</span></span>`
    ]]]
  battery: |
    [[[
      if (states['binary_sensor.pixel_6_pro_is_charging'].state == 'off')
        return `<ha-icon
          icon="mdi:cellphone"
          style="width:22px;height:22px;">
          </ha-icon><span> <span style="color: var(--text-color-sensor);">${states['sensor.pixel_6_pro_battery_level'].state}%</span></span>`;
      else
        return `<ha-icon
          icon="mdi:cellphone-charging"
          style="width:22px;height:22px;color:lime">
          </ha-icon><span> <span style="color: var(--text-color-sensor);">${states['sensor.pixel_6_pro_battery_level'].state}%</span></span>`;
    ]]]
  watch: |
    [[[
      if (states['binary_sensor.sj_galaxy_watch4_is_charging'].state == 'off')
        return `<ha-icon
          icon="mdi:watch"
          style="width:22px;height:22px;color:white">
          </ha-icon><span> <span style="color: var(--text-color-sensor);">${states['sensor.sj_galaxy_watch4_battery_level'].state}%</span></span>`;
      else
        return `<ha-icon
          icon="mdi:watch"
          style="width:22px;height:22px;color:lime">
          </ha-icon><span> <span style="color: var(--text-color-sensor);">${states['sensor.sj_galaxy_watch4_battery_level'].state}%</span></span>`;
    ]]]
  ln1: |
    [[[
      if (states['person.abc'].state == 'home')
        return `<ha-icon
          icon="mdi:google-maps"
          style="width:22px;height:22px">`
      else
        return `<ha-icon
          icon="mdi:google-maps"
          style="width:22px;height:22px;color:lime">`
    ]]]
  ln2: |
    [[[
      if (states['person.abc'].state == 'home')
        return `${states['sensor.pixel_6_pro_geocoded_location'].attributes['thoroughfare']}`
      else
      return `${states['sensor.pixel_6_pro_geocoded_location'].attributes['thoroughfare']}`
    ]]]
  ln3: |
    [[[
      if (states['person.abc'].state == 'home')
        return `${states['sensor.pixel_6_pro_geocoded_location'].attributes['sub_locality']}`
      else
      return `${states['sensor.pixel_6_pro_geocoded_location'].attributes['sub_locality']}`
    ]]]

@Shan, thank you - super helpful! I’ve fiddled around and adjusted it slightly to suit my needs and have everything working less for the zone icon updating. Despite the zone state txt (home/away) updating correctly I cannot get the icon to update with the corresponding state.

I’ve removed some of your java as I don’t use geolocation data and instead track home/away presence via network tracking but fear I might’ve broken something and can’t find the issue!
I’ve also followed the guidance of the github and added the triggers-update function but that doesn’t seem to make a difference.

N.B - the below yaml doesn’t include the phone java as that’s working as intended.

Also if it helps when the card is first created and the state is ‘home’ it display the correct icon. It’s only when the card updates to away the icon then get stucks on that icon.

Do you (or anyone else have any suggestions);

type: custom:button-card
template: container
color: '#D1EC7B'
name: People
custom_fields:
  buttons:
    card:
      type: horizontal-stack
      cards:
        - type: custom:button-card
          entity: device_tracker.NAMEHERE
          triggers_update: person.NAMEHERE
          show_icon: false
          show_name: false
          styles:
            card:
              - background-color: white
              - box-shadow: true
              - border-radius: 5%
              - padding: 15%
              - font-size: 12px
              - text-transform: capitalize
              - font-familly: cursive
            grid:
              - grid-template-areas: '"i zone" "i battery""i watch" "ln1 ln2" "ln1 ln3"'
              - grid-template-columns: 100px auto
              - grid-template-rows: auto
            custom_fields:
              i:
                - justify-self: left
              zone:
                - justify-self: left
                - padding-top: 20px
                - padding-bottom: 5px
                - font-size: 15px
              battery:
                - justify-self: start
                - font-size: 15px
                - '--text-color-sensor': >-
                    [[[ if (states["sensor.NAMEHERE"].state <=
                    20) return "red"; ]]]
              ln1:
                - justify-self: left
                - margin-left: 1px
              ln2:
                - white-space: normal
                - text-align: start
                - text-align-last: left
                - margin-left: '-70px'
          custom_fields:
            i: |
              <img height= 100px src='/local/_20220825_155544.JPG'>
            zone: |
              [[[
                if (states['person.NAMEHERE'].state == 'Home')
                  return `<ha-icon
                    icon="mdi:home"
                    style="width:20px;height:20px;color:SteelBlue">
                    </ha-icon><span> <span style="color: var(--text-color-sensor);">${states['person.NAMEHERE'].state}</span></span>`
                 else 
                  return `<ha-icon
                    icon="mdi:map-marker-radius-outline"
                    style="width:22px;height:22px;color:orange">
                    </ha-icon><span> <span style="color: var(--text-color-sensor);">${states['person.NAMEHERE'].state}</span></span>`;
              ]]]

thanks for the assistance