Fun with custom:button-card

I went back and reviewed your questions and I do see that I was missing something in my answers.
The horizontal space is something that the Home Assistant Lovelace internal programming seems to determine. For instance, if you take the browser window and resize it horizontally, you’ll notice that your containers resize as your do it and then, at some point, the number recalculates and all your containers shift around. It’s supposed to do that because of “responsive design” so that your dashboard will fit nicely on various screen sizes. The key thing here is that the horizontal space doesn’t seem to be under your control.
When I did the layout you’re seeing in this thread (the one a few back where you noted that I have various sizes of containers), I was using very aggressive control over the way my containers stacked.
I started with a horizontal stack 1 item wide, then the first three rows went this way:
1 container with 7 buttons, 1 horizontal stack with 2 items which were containers with 4 buttons and five buttons respectively, 1 horizontal stack with 3 items which were containers with 3 buttons, two buttons, and 3 buttons respectively, 1 horizontal stack with 2 items which were containers with 3 buttons and 4 buttons respectively and so on. Then I used the “aspect ratio” setting on the buttons within those containers to get the containers to be the same height relative to that first 7 button container. That involved a lot of plugging in numbers for aspect ratio and seeing if it looked right. This was a royal pain whenever I shifted things around and the number of buttons changed.
I finally decided that that was too much work. So I changed the design so that every single container was 4 buttons wide and eliminated the single item wide horizontal constraint entirely. That meant I could use one aspect ratio for every button. Here’s what the new layout looks like:


and if you resize the window:

Here’s the beginning of my ui-lovelace.yaml file which does all this:

decluttering_templates: !include lovelace-templates-decluttering.yaml
button_card_templates: !include lovelace-templates-button-card.yaml
title: Home
swipe_nav:
  wrap: true
  animate: swipe
views:
# Main
  - title: Main
    path: main
    icon: 'mdi:home'
    badges: []
    cards:
      - type: grid
        columns: 3
        square: false
        cards:
          - entity: sensor.family_sensor
            name: Presence
            template: presence-tile-compact
            type: 'custom:button-card'
            aspect_ratio: 3/2.87
            variables:
              m1_entity: person.russell_smith
              m1_name: R
              m2_entity: person.janette
              m2_name: J
              m3_entity: person.katie
              m3_name: K
          - entity: climate.thermostat
            name: Thermostat
            template: climate-tile-compact
            type: 'custom:button-card'
            aspect_ratio: 3/2.87
          - type: 'custom:decluttering-card'
            template: mode-thing
            variables:
              - aspect_ratio: 3/2.87
              - card_background_color: darkSlateGrey
              - thing_entity: input_boolean.in_bed
              - thing_name: 'Modes'
              - thing_icon_on: 'mdi:bed'
              - thing_icon_color_on: yellow
              - thing_name_on: 'in bed'
              - thing_icon_off: 'mdi:walk'
              - thing_icon_color_off: deepSkyBlue
              - thing_name_off: 'about'
              - thing_toggle: true
              - m0_show: 'true'
              - m0_entity: input_boolean.security_issue
              - m0_on_state: 'on'
              - m0_icon_on: 'hass:shield-lock'
              - m0_icon_color_on: red
              - m0_status_on: 'security'
              - m0_icon_off: 'hass:shield-outline'
              - m0_icon_color_off: deepSkyBlue
              - m0_status_off: 'all secure'
              - m0_toggle: true
              - m1_show: 'true'
              - m1_entity: sun.sun
              - m1_on_state: 'above_horizon'
              - m1_icon_on: 'mdi:weather-sunny'
              - m1_icon_color_on: yellow
              - m1_status_on: 'day'
              - m1_icon_off: 'mdi:moon-waning-crescent'
              - m1_icon_color_off: deepSkyBlue
              - m1_status_off: 'night'
              - m1_toggle: false
              - m2_show: 'true'
              - m2_entity: switch.presence_simulation
              - m2_on_state: 'on'
              - m2_icon_on: 'mdi:lightbulb-group'
              - m2_icon_color_on: yellow
              - m2_status_on: 'sim.lights on'
              - m2_icon_off: 'mdi:lightbulb-group-outline'
              - m2_icon_color_off: deepSkyBlue
              - m2_status_off: 'sim.lights off'
              - m2_toggle: true
              - m3_show: 'true'
              - m3_entity: binary_sensor.mailbox_contact
              - m3_on_state: 'on'
              - m3_icon_on: 'mdi:mailbox-open-up'
              - m3_icon_color_on: yellow
              - m3_status_on: 'check mail'
              - m3_icon_off: 'mdi:mailbox-outline'
              - m3_icon_color_off: deepSkyBlue
              - m3_status_off: 'mail'
              - m3_toggle: true
# Master Control
      - type: 'custom:button-card'
        template: container
        color: Grey
        name: Master Control
        custom_fields:
          buttons:
            card:
              type: grid
              columns: 4
              square: true
              cards:
                - entity: input_boolean.zoom_lighting
                  name: Zoom Lights
                  template: light-group-button
                  type: 'custom:button-card'
                  aspect_ratio: 1/1.1
                - template: standard-button
                  type: 'custom:button-card'
                  aspect_ratio: 1/1.1
                  double_tap_action:
                    action: call-service
                    service: script.good_night_02
                    service_data:
                      lights_wait: '0'
                      in_bed_wait: '0'
                  tap_action:
                    action: call-service
                    service: script.good_night_02
                    service_data:
                      lights_wait: '10'
                      in_bed_wait: '15'
                  name: |
                    [[[
                      if (states["input_select.good_night_phase"].state == "ready") return "";
                      if (states["input_select.good_night_phase"].state == "bed") return "in bed...";
                      return "Good Night";
                    ]]]
                  icon: |
                    [[[
                      if (states["input_select.good_night_phase"].state == "lights") return "mdi:lightbulb-group";
                      if (states["input_select.good_night_phase"].state == "bed") return "mdi:bed";
                      return "mdi:power";
                    ]]]
                - template: standard-button
                  type: 'custom:button-card'
                  aspect_ratio: 1/1.1
                  tap_action:
                    action: call-service
                    service: script.dishes
                  name: |
                    [[[
                      return states["input_select.dishes_phase"].state;
                    ]]]
                  icon: |
                    [[[
                      if (states["input_select.dishes_phase"].state == "wash") return "mdi:hand-wash";
                      if (states["input_select.dishes_phase"].state == "put away") return "mdi:stack-overflow";
                          return "mdi:silverware-clean";
                    ]]]
                - type: 'custom:decluttering-card'
                  template: auto-light-thing
                  variables:
                    - thing_entity: input_boolean.grow_light_automated
                    - thing_name: 'Grow Lt'
                    - light_entity: switch.grow_light_outlet
                    - thing_icon_on: 'mdi:lightbulb-auto'
                    - thing_icon_off: 'mdi:lightbulb-auto'
                    - aspect_ratio: 1/1.1
# Side
      - type: 'custom:button-card'
        template: container
        color: LightBlue
        name: Side
        custom_fields:
          buttons:
            card:
              type: grid
              columns: 4
              square: true
              cards:
                - entity: binary_sensor.side_door_sensor_contact
                  name: Outside Door
                  template: door-button-open
                  type: 'custom:button-card'
                  aspect_ratio: 1/1.1
                - type: 'custom:decluttering-card'
                  template: locking-thing
                  variables:
                    - thing_entity: switch.outside_tap
                    - thing_name: 'Bib'
                    - lock_entity: input_boolean.outside_water_valve_lock
                    - thing_icon_on: 'mdi:water-pump'
                    - thing_icon_off: 'mdi:water-pump-off'
                    - aspect_ratio: 1/1.1
                - entity: binary_sensor.upstairs_motion_sensor_motion
                  name: Stairs Motion
                  template: standard-button
                  type: 'custom:button-card'
                  aspect_ratio: 1/1.1
                  styles:
                    name:
                      - font-size: 0.45em
                - entity: camera.blueiris_n_side_cam
                  name: Camera
                  template: standard
                  type: 'custom:button-card'
                  color: SlateGrey
                  aspect_ratio: 1/1.1
# Front Porch
      - type: 'custom:button-card'
        template: container
        color: Brown
        name: Front Porch
        custom_fields:
          buttons:
            card:
              type: grid
              columns: 4
              square: true
              cards:
                - entity: binary_sensor.front_doorbell_motion
                  name: porch
                  template: standard-button
                  type: 'custom:button-card'
                  aspect_ratio: 1/1.1
                - entity: camera.blueiris_front_cam
                  name: Yard
                  template: standard
                  type: 'custom:button-card'
                  aspect_ratio: 1/1.1
                  color: SlateGrey
                - entity: camera.front_doorbell
                  name: Doorbell
                  template: standard
                  type: 'custom:button-card'
                  aspect_ratio: 1/1.1
                  styles:
                    name:
                      - font-size: 0.45em
                  color: SlateGrey
                - entity: switch.front_porch_light
                  name: Light
                  template: light-button
                  type: 'custom:button-card'
                  aspect_ratio: 1/1.1
# Dining Room/Kitchen
      - type: 'custom:button-card'
        template: container
        color: GoldenRod
        name: Dining Room/Kitchen
        custom_fields:
          buttons:
            card:
              type: grid
              columns: 4
              square: true
              cards:
                - entity: light.dining_room_chandelier
                  name: DR Light
                  template: light-group-button
                  type: 'custom:button-card'
                  aspect_ratio: 1/1.1
                  styles:
                    name:
                      - font-size: 0.45em
                - entity: binary_sensor.dining_room_motion_motion
                  name: DR Motion
                  template: standard-button
                  type: 'custom:button-card'
                  aspect_ratio: 1/1.1
                  styles:
                    name:
                      - font-size: 0.45em
                - entity: switch.stove_sink_lights
                  name: K Light
                  template: light-button
                  type: 'custom:button-card'
                  aspect_ratio: 1/1.1
                  styles:
                    name:
                      - font-size: 0.45em
                - entity: binary_sensor.side_kitchen_door_contact
                  name: Side Door
                  template: door-button-open
                  type: 'custom:button-card'
                  aspect_ratio: 1/1.1
# Living Room
      - type: 'custom:button-card'
        template: container
        color: Khaki
        name: Living Room
        custom_fields:
          buttons:
            card:
              type: grid
              columns: 4
              square: true
              cards:
                - entity: binary_sensor.front_door_sensor_contact
                  name: Door
                  template: door-button-open
                  type: 'custom:button-card'
                  aspect_ratio: 1/1.1
                - entity: camera.blueiris_living_room_cam
                  name: Camera
                  template: standard
                  type: 'custom:button-card'
                  color: SlateGrey
                  aspect_ratio: 1/1.1
                - entity: binary_sensor.living_room_motion_motion
                  name: LR Motion
                  template: standard-button
                  type: 'custom:button-card'
                  aspect_ratio: 1/1.1
                  styles:
                    name:
                      - font-size: 0.45em
                  tap_action:
                    action: navigate
                    navigation_path: /lovelace/sub_LRA
                - entity: fan.lr_sonoff
                  name: Ceiling Fan
                  template: fan-button
                  type: 'custom:button-card'
                  aspect_ratio: 1/1.1
                  styles:
                    name:
                      - font-size: 0.45em
                  hold_action:
                    action: more-info
# Living Room Lights
      - type: 'custom:button-card'
        template: container
        color: Khaki
        name: Living Room Lights
        custom_fields:
          buttons:
            card:
              type: grid
              columns: 4
              square: true
              cards:
                - entity: light.lr_sonoff
                  name: Ceiling Light
                  template: light-group-button
                  type: 'custom:button-card'
                  aspect_ratio: 1/1.1
                  styles:
                    name:
                      - font-size: 0.45em
                - entity: switch.r_desk_lamp
                  name: 'R:Desk Lamp'
                  template: light-button
                  type: 'custom:button-card'
                  aspect_ratio: 1/1.1
                  styles:
                    name:
                      - font-size: 0.45em
                - entity: switch.j_desk_lamp
                  name: 'J:Desk Lamp'
                  template: light-button
                  type: 'custom:button-card'
                  aspect_ratio: 1/1.1
                  styles:
                    name:
                      - font-size: 0.45em
                - name: All Lights
                  template: standard-button
                  type: 'custom:button-card'
                  aspect_ratio: 1/1.1
                  icon: 'mdi:power'
                  tap_action:
                    action: navigate
                    navigation_path: /lovelace/sub_LRL
# Bathroom
      - type: 'custom:button-card'
        template: container
        color: dimgrey
        name: Bathroom
        custom_fields:
          buttons:
            card:
              type: grid
              columns: 4
              square: true
              cards:
                - entity: switch.bathroom_master_switch
                  name: Bath
                  template: light-button
                  type: 'custom:button-card'
                  aspect_ratio: 1/1.1
                  styles:
                    name:
                      - font-size: 0.45em
                - entity: binary_sensor.bathroom_door_contact
                  name: Door
                  template: door-button-closed
                  type: 'custom:button-card'
                  aspect_ratio: 1/1.1
                - entity: binary_sensor.bathroom_multipurpose_sensor_motion
                  name: Motion
                  template: standard-button
                  type: 'custom:button-card'
                  aspect_ratio: 1/1.1
                  styles:
                    name:
                      - font-size: 0.45em
                  tap_action:
                    action: navigate
                    navigation_path: /lovelace/sub_BRA
      - type: grid
        columns: 2
        square: false
        cards:
# R&J Bedroom
          - type: 'custom:button-card'
            template: container
            color: skyBlue
            name: R&J Bedroom
            custom_fields:
              buttons:
                card:
                  type: grid
                  columns: 2
                  square: true
                  cards:
                    - entity: light.rj_sonoff
                      name: Ceiling Light
                      template: light-button
                      type: 'custom:button-card'
                      aspect_ratio: 1/1.1
                    - entity: fan.rj_sonoff
                      name: Ceiling Fan
                      template: fan-button
                      type: 'custom:button-card'
                      aspect_ratio: 1/1.1
# Kt Bedroom
          - type: 'custom:button-card'
            template: container
            color: white
            name: Kt Bedroom
            custom_fields:
              buttons:
                card:
                  type: grid
                  columns: 2
                  square: true
                  cards:
                    - entity: light.k_sonoff
                      name: Ceiling Light
                      template: light-button
                      type: 'custom:button-card'
                      aspect_ratio: 1/1.1
                    - entity: fan.k_sonoff
                      name: Ceiling Fan
                      template: fan-button
                      type: 'custom:button-card'
                      aspect_ratio: 1/1.1
# Back Yard
      - type: 'custom:button-card'
        template: container
        color: Green
        name: Back Yard
        custom_fields:
          buttons:
            card:
              type: grid
              columns: 4
              square: true
              cards:
                - entity: binary_sensor.back_door_sensor_contact
                  name: Door
                  template: door-button-open
                  type: 'custom:button-card'
                  aspect_ratio: 1/1.1
                - type: 'custom:decluttering-card'
                  template: motion-light-thing
                  variables:
                    - thing_entity: switch.back_door_motion_light
                    - thing_name: 'Deck Light'
                    - motion_entity: input_boolean.back_porch_motion
                    - thing_icon_on: 'mdi:lightbulb-on'
                    - thing_icon_off: 'mdi:lightbulb-outline'
                    - aspect_ratio: 1/1.1
                - entity: camera.blueiris_back_yard_cam
                  name: Yard
                  template: standard
                  type: 'custom:button-card'
                  aspect_ratio: 1/1.1
                  color: SlateGrey
                - entity: switch.garage_motion_light
                  name: "Garage Light"
                  template: light-button
                  type: 'custom:button-card'
                  aspect_ratio: 1/1.1
# Garage
      - type: 'custom:button-card'
        template: container
        color: MidnightBlue
        name: Garage
        custom_fields:
          buttons:
            card:
              type: grid
              columns: 4
              square: true
              cards:
                - entity: cover.overhead_garage_door
                  name: Overhead
                  aspect_ratio: 1/1.1
                  template: garage-cover
                  type: 'custom:button-card'
                - entity: camera.blueiris_garage_cam
                  name: Garage
                  template: standard
                  type: 'custom:button-card'
                  aspect_ratio: 1/1.1
                  styles:
                    name:
                      - font-size: 0.45em
                  color: SlateGrey
                - entity: binary_sensor.garage_door_hinge_sensor_contact
                  name: Door
                  template: door-button-open
                  type: 'custom:button-card'
                  aspect_ratio: 1/1.1
                - entity: camera.alley_camera
                  name: Alley
                  template: standard
                  type: 'custom:button-card'
                  aspect_ratio: 1/1.1
                  color: SlateGrey

You’ll note I’m using Decluttering templates as well as some specific button_card templates. Those are how I get a few specialized cards like those 3 massive information dump cards at the top and fun cards like my locking hose bib card and the motion controller light card. You’ll also see that I’m still using the aspect_ratio setting, but only to make the cards slightly taller so I can display the text and graphics well.

1 Like

You’ll also note that I “cheated” once when it comes to the bedrooms. That’s going back to a grid of 2 items with containers of 2 buttons each. (You can see that I changed from horizontal stacks to grids somewhere along the way too.)

Awesome! that’s the answer I was after, thank you. I can now configure around this. Thank you Russell

Look into trigger updates on the documentation page for custom:button-card. You can define an entity that the card will react to for updates.

1 Like

Almost done with my Roku Remote… Defenitly been having “fun” with custom:button-card :smirk:

8 Likes

I’m sure this is probably a daft question, but how do you create a second row of buttons in the same container? When I add more buttons they just appear on the same row and make each button smaller.

That’s what they do. To get rows, I just make additional containers. As you’ll notice above, you also can’t count on them being in the same column with different screen sizes.

Been looking all over without finding a easy solutiuon. Is there a way to “scale” the picture used in custom button, to have it use more of the button size. It actually scales in size, if I do a custom button in panel view the picture is larger, however same “share” of the button

The below is a very simple version of my button, just to show how it works

type: custom:button-card
aspect_ratio: 3
color: white
entity: sensor.kokkenipad_browser_path
show_last_changed: false
show_state: false
show_name: false
show_icon: false
entity_picture: /local/f1logo_1.jpg
show_entity_picture: true
tap_action:
  action: navigate
  navigation_path: /lovelace-andet/0

Resulting in this view
image

This seems like it should be simple, but I can not figure it out.

I have a sensor that I am trying to display in a button card for the time. The sensor pulls the time in “military” time, but I want to display that time in 12-hour format. Can I add a template to my code to display that time in like I am hoping?

entity: sensor.time
show_icon: false
show_label: false
show_name: false
show_state: true
state:
  - operator: template
    value: >-
      {{ as_timestamp(strptime(states('sensor.time'),'%H:%M')) |
      timestamp_custom('%I:%M %p') }}
styles:
  state:
    - font-size: 4em
    - font-weight: 200
    - justify-self: left
    - padding: 0px 0px 0px 20px
  card:
    - border: 0px solid
    - height: 50px
    - background-color: hsla(0, 0%, 0%, 0)
type: custom:button-card
card_mod:
  style: |
    ha-card {
      box-shadow: none;
      margin: 0px 0px 0px 0px;
    }

custom:button-card only supports Javascript-templating. Jinja2 is not supported

Is it possible to change BG based on value like this one(Outside and feel ? Tried several things and look in different places but dont have sucess.

Thanks in Advance.

Thanks for the response! I’m not familiar with javascript-templating much. Do you have an idea of the javascript-templating that would work for this situation?

Should be something like this:

value: >-
  [[[
    var time = entity.state
    var dt = new Date("1970-01-01T" + time)
    var hours = dt.getHours()
    var AmOrPm = hours >= 12 ? 'pm' : 'am';
    hours = (hours % 12) || 12;
    var minutes = dt.getMinutes()
    var finalTime = hours + ":" + minutes + AmOrPm;
    return finalTime
  ]]]

But this will do nothing to your card as the state-operator expects a template that returns True or False.

It’s better to show either the name or label and insert the above template to the correct key

@G3GhostTech GhostTech
Share your code, please? I want to do something similar with my FIOS remote.

Hi guys,

I have this button-card for my boiler. I want to swap the icon for the state of my sensor.boiler_temp that states the current temp of my boiler. Any idea how do i achieve this ?

tnx

type: custom:button-card
icon: mdi:water-boiler
entity: switch.boiler_switch_1
name: Boiler
show_label: true
label: null
show_state: true
layout: vertical
size: 8%
styles:
  card:
    - height: 100px
    - width: 90px
    - background-color: light gray
    - border: 1px solid rgb(80,80,80)
    - border-radius: 10%
    - font-family: sans-serif
    - padding: 5%
  label:
    - color: gray
    - font-size: 9px
    - justify-self: start
    - padding: 0px 5px
  name:
    - text-transform: none
    - justify-self: left
    - padding: 0px 5px
  icon:
    - color: rgb(243,156,15)
    - width: 50px
    - height: 50px
    - padding: 0px 55px 5px 20px
  state:
    - justify-self: left
    - font-size: 9px
    - padding: 0px 5px
state:
  - value: 'on'
    styles:
      state:
        - color: green
      card:
        - filter: drop-shadow(0 0 1.0rem rgb(243,156,15))
        - border: 0px
  - value: 'off'
    styles:
      state:
        - color: rgb(186,192,198)
      name:
        - color: rgb(186,192,198)
      icon:
        - color: rgb(186,192,198)

Hi @ktownsend-personal,

Apologies if you’ve answered this before, but how did you achieve the near full size icon as a background with the text over? It’s part of your multi options in the overview image but I can’t find all the pieces to your template. Also new to templating so I may just be missing where it’s nested.

Cheers :slight_smile:

Lint

Add this below size: 8%, before the styles:

icon: >-
          [[[ if (states[entity.entity_id].state >= 50) return
          "mdi:your_icon1"; if (states[entity.entity_id].state < 50) return
          "mdi:your_icon2"; else return "your_icon3" ]]]

entity.entity_id refers dynamically to your switch_boiler_switch_1
but you can also replace it with a hard coded entity. Also, you can have multiple ifs (as by the example) and even have two conditions checked (&& for and, || for or, and so on). If you want it to be equal to a string, then just replace
>= 50
with
== "state_value"

Cheers

1 Like

Hey,
looking through this topic and als tried some things…but i don’t seem to have the syntax correct imho…
Just want to show the nbr of lights on in a group…and the error is in the custom field notification…
Anyone a idea whats wrong?

type: custom:button-card
icon: mdi:lightbulb
entity: light.groupbureau
aspect_ratio: 1/1
styles:
  grid:
    - position: relative
  custom_fields:
    notification:
      - background-color: red
      - border-radius: 50%
      - position: absolute
      - left: 60%
      - top: 10%
      - height: 100px
      - width: 100px
      - font-size: 20px
      - line-height: 90px
custom_fields:
  notification: |
    [[[
      var lights = 'light.bureau','light.hue_play_1','light.hue_play_2';
      var lights2 =  expand('lights') | selectattr('state','eq','off') |  list | count ;     
      return lights2
    ]]]

Hi,

Can you or anyone please provide me an example or code of a custom button card for a dimmable light that the icon’s opacity can reflect the percentage or intensity of the brightness?

Much appreciated. Thank you!!

I’m having some issues with the auto color option. If the state of a background color falls back to auto, it wont update to the normal theme color unless the dashboard has been refreshed?!

Can someone help out?

styles:
  card:
    - background-color: >-
        [[[ if (states[entity.entity_id].state == "unavailable" ||
        states[entity.entity_id].state == "unknown") return "red"; if
        (states[entity.entity_id].state == "off") return "auto"; else return "orange" ]]]