Lovelace: Button card

state_display display doesn’t exist in the state object, it’s a main configuration parameter. :wink:

I forgot to bump the version in the code, my fault :blush:

That should be now better since 3.3.0, no ? :slight_smile:
But yes, button-card is quite compute intensive with all the options it has now, so if your device is not very fast, you’ll definitely feel it… :man_shrugging:

Yes, mostly everything is possible with this card, but please read the documentation. There is a lot of examples there. Especially the second one here with icons.

I said it’d be something obvious :blush:
Thanks.

@RomRider thanks for replying. I read the docu again and again - I think it’s on me ;-(

My last try was:

custom_fields:
  notification:
    icon: mdi:air-conditioner

In this case, the circle contains “icon:…” as a string, but not as an icon.

it’s for sure easy for an expert, but for me as a total noob I can’t figure it out ;-(

What you are doing is not what is described in the example linked in my answer.
If you want an icon:

custom_fields:
  notification: |
    <ha-icon icon="mdi:harddisk" style="width: 12px; height: 12px; color: deepskyblue;"></ha-icon>

Change the values to suit your needs.

Hello @Luxor73, do you have this templates working?
I am trying to put a custom button inside another custom button using custom fields (like for example you put custom_field “bt_set” inside button template “light_room_classic”). For me, the button inside looks well but it doesn’t allow me to use tap_action nor hold_action.

Thank you!!

I have begun to replace my tiles cards with this card and it has been a steep learning curve but I have nearly everything sorted except for two things.

  1. I would have expected the label template below to display the brightness (when on) or Off. Instead it displays the brightness or nothing. Any idea how to get it to do what I want?
    label: >
      [[[ var bri = Math.round(entity.attributes.brightness / 2.55);  if
      (entity.state === 'on') return (bri ? (bri+"%") : 'Off') ]]]
  1. Is it possible to specify the card/button width as a percentage of the column width? This would allow me to display the buttons on my pc, iphone, and the problem child of an ipad pro that has far narrower columns. If I specify a width in px that works well on the narrow columns of the ipad there is a lot of wasted space on the other displays. If I specify it to fit my pc or phone the buttons overrun and are clipped by the columns.

You code says that

if it's on
  -> if brightness is defined
    -> return brightness
    -> else return Off

So it will never return Off :man_shrugging:

[[[ var bri = entity.attributes.brightness && Math.round(entity.attributes.brightness / 2.55);  if
      (entity.state === 'on') return (bri ? (bri+"%") : 'On'); else return "Off"; ]]]

Dont specify the width (that should be only used for very specific use cases…), or use the aspect_ratio instead :slight_smile:

Thanks Rom. #1 makes sense.

The problem with using aspect ratio is that all the buttons end up different sizes when there are different numbers of buttons horizontally stacked:

Screenshot_2020-05-10 Overview - Home Assistant(1)
I would prefer they were the same size as the row with 4 buttons.

about 1:

like this?

Schermafbeelding 2020-05-09 om 23.50.08

Schermafbeelding 2020-05-09 om 23.50.16

I figured it out a different way, because I also wanted to style the separate elements of the label, so I used a custom_field stateDisplay (which now has been adopted in core button, but I didnt change to that yet)

  state:

    - operator: template
      value: >
        [[[
          var id = entity.entity_id.split('.')[1] ;
          var reachable = 'binary_sensor.' + id + '_reachable';
          return states[reachable] && states[reachable].state == 'off' ;
        ]]]
      custom_fields:
        stateDisplay: >
          [[[  var fakestate = `<span style="color: grey;"> ${entity.state}:</span>`;
               var fakelabel = `<span style="color: grey;"> ${Math.round(entity.attributes.brightness / 2.55)} %</span>`;
               return 'Unrch: ' + fakestate +' ' + fakelabel;]]]
      styles:
        custom_fields:
          stateDisplay:
            - font-size: 11px
            - font-family: Helvetica
            - justify-self: start
            - text-transform: capitalize
            - font-weight: bold
            - color: steelblue
        card:
          - color: grey
          -  background-color: lightgrey
        name:
          - color: steelblue

    - value: 'on'
      custom_fields:
        stateDisplay: >
          [[[  var fakestate = `<span style="color: #F0C209;"> ${entity.state}:</span>`;
               var fakelabel = `<span style="color: #F0C209;"> ${Math.round(entity.attributes.brightness / 2.55)} %</span>`;
               return fakestate +' ' + fakelabel;]]]
      styles:
        card:
          - color: black
        custom_fields:
          stateDisplay:
            - font-size: 11px
            - font-family: Helvetica
            - justify-self: start
            - text-transform: capitalize
            - font-weight: bold
      id: on-icon
    - value: 'off'
      custom_fields:
        stateDisplay: >
          [[[ return entity.state ]]]

will check @romriders template, because it is waaaay shorter :wink:

using this grid:

    grid:
      - grid-template-areas: '"i" "n" "stateDisplay"' #'"i" "n" "s" "l"'
      - grid-template-columns: 1fr
      - grid-template-rows: 1fr min-content min-content

I guess I could also specify the label as ‘off’ using the button card states options.

That’s no longer my issue.

Obtaining consistent button sizes with varying numbers of buttons in a horizontal stack is now the main roadblock to using this card.

If I specify a fixed width it causes issues with varying column widths on different displays. If I use aspect_ratio the buttons end up different sizes when there are different numbers of buttons in the horizontal stack.

So how do I get a button/card width as a percentage of the home assistant column width?

EDIT: never mind. An invisible spacer button template is how it’s done.

You can use this to make the same number of buttons on each row, then they are all the same size

- type: custom:button-card
  color_type: blank-card

EDIT: Ooopss… should’ve read to the end before I replied. Sorry.

1 Like

Well… I actually designed a blank card. Good to know I don’t need it. Thanks.

As you see on the right the pictures get cut off at the top and bottom if i make them to small. Whats causing this problem and how can i solve it?
Info: the original pictures are squared.

I used empty mardown cards to have them on the right side. thats why the code is pretty big, but it worked the best except the pictures are getting cut off.

image

card:
  cards:
    - content: |
        ...  
      type: markdown
    - content: ' '
      style: |
        ha-card {
           --ha-card-background: rgba(0,0,0,0.0);
           box-shadow: none;
         }    
      type: markdown
    - content: ' '
      style: |
        ha-card {
           --ha-card-background: rgba(0,0,0,0.0);
           box-shadow: none;
         }    
      type: markdown
    - content: ' '
      style: |
        ha-card {
           --ha-card-background: rgba(0,0,0,0.0);
           box-shadow: none;
         }    
      type: markdown
    - content: ' '
      style: |
        ha-card {
           --ha-card-background: rgba(0,0,0,0.0);
           box-shadow: none;
         }    
      type: markdown
    - aspect_ratio: 1/1
      entity: person.sam
      entity_picture: /local/sam_home_2.jpg
      show_entity_picture: true
      show_name: false
      show_state: false
      style: |
        ha-card {
           box-shadow: var(--card-box-shadow);
           border-radius: 100px;
         }
      styles:
        entity_picture:
          - width: 100%
          - height: 100%
        grid:
          - grid-template-areas: '"i" "i" "i"'
          - grid-template-columns: 1fr
          - grid-template-rows: 1fr min-content min-content
        img_cell:
          - align-content: center
      type: 'custom:button-card'
    - aspect_ratio: 1/1
      entity: person.melli
      entity_picture: /local/melli_home_2.jpg
      show_entity_picture: true
      show_name: false
      show_state: false
      style: |
        ha-card {
           box-shadow: var(--card-box-shadow);
           border-radius: 100px;
         }
      styles:
        entity_picture:
          - width: 100%
          - height: 100%
        grid:
          - grid-template-areas: '"i" "i" "i"'
          - grid-template-columns: 1fr
          - grid-template-rows: 1fr min-content min-content
        img_cell:
          - align-content: center
      type: 'custom:button-card'
  type: horizontal-stack
style: |
  ha-card {
    {% if is_state('input_boolean.show_outlines', 'on') %} outline: 1px solid red; {% endif %}
    padding: 4px;
  }
type: 'custom:mod-card'

Multiple things:

  • No need for card-mod with button-card
  • grid-template-areas: '"i" "i" "i"': No need for 3 lines when you only have 1 item (entity_picture)
  • You problem is probably related to the padding of the card

So… Give this a try:

    - aspect_ratio: 1/1
      entity: person.sam
      entity_picture: /local/sam_home_2.jpg
      show_entity_picture: true
      show_name: false
      show_state: false
      styles:
        card:
          - box-shadow: var(--card-box-shadow)
          - border-radius: 50% # this makes a circle out of something square
          - padding: 0px
        entity_picture:
          - width: 100%
          - height: 100%
        grid:
          - grid-template-areas: '"i"'
          - grid-template-columns: 1fr
          - grid-template-rows: 1fr
        img_cell:
          - align-content: center
      type: 'custom:button-card'

thanks alot that works! I was fiddling around with this for so long. Iam still trying to understand things.

Best way to test CSS/play with CSS is to use chrome inspect (or FF inspect) and then play with the css values of the elements to see the live result and also highlight your element (you’d have seen that there was a padding).
And then once you’re satisfied, just set the value in the code and you’re good to go.

Hi all, I’m having a problem with the color styling of some of the icons in my cards. I intend for the icon colors to change based on an entity state (e.g. entity on = icon orange), but they frequently – not always – fail to return to the null color option when the entity state changes back to off.

The icons themselves are also set to change based on the same entity_states, but that’s done separately under the custom_fields object of the config. There’s no problem with the icons changing back and forth. So I often end up with the right icon, but the wrong color.

Relevant config is below. As you can see, I’m using decluttering card.

      styles:
        custom_fields:
          icon1:             
            - color: |
                [[[ 
                  if (states["[[icon1_ent]]"].state == '[[icon1_state1]]') return "[[icon1_color1]]"; 
                  if (states["[[icon1_ent]]"].state == '[[icon1_state2]]') return "[[icon1_color2]]"; 
                  if (states["[[icon1_ent]]"].state == '[[icon1_state3]]') return "[[icon1_color3]]";                   
                  else return "null";
                ]]]  

(One solution I’ve tried is a template under custom_fields that controls both the icon and its color… but ran into problems, and anyway this allows me to more easily make icon/color combinations based on entity states, so that’s why I’m doing it this way.)

Any ideas? Thank you very much in advance.

I’ve been doing ok replacing all my tiles cards with custom button card stacks, except for one. The scene select card:

The one above is still the tiles card.

The one below is what I’ve managed so far (aspect ratios in green)

Screenshot_2020-05-10 Overview - Home Assistant(1)

All my other buttons are 4/3 aspect ratio as 1/1 was too tall and the default horizontal stack of 4 buttons without defining a ratio was just a bit too short.

I cant for the life of me work out how to extend a button twice or four times the normal width without messing with the icon height. It disappears altogether on the full width button.

I’m obviously doing something stupid. Can anyone tell me how to keep the icon the same size (28px) while making the button wider but not taller?

Card config:

type: entities
title: Scene Select
show_header_toggle: false
entities:
  - type: 'custom:hui-element'
    card_type: horizontal-stack
    cards:
      - type: 'custom:button-card'
        entity: script.lounge_macro_watch_tv
        template: menu_button
        service: script.lounge_macro_watch_tv
        name: Watch TV
        icon: 'mdi:television-classic'
        aspect_ratio: 8/3
      - type: 'custom:button-card'
        entity: script.lounge_macro_watch_movie
        template: menu_button
        service: script.lounge_macro_watch_movie
        name: Watch Movie
        icon: 'mdi:filmstrip'
        aspect_ratio: 8/3
        styles:
          icon:
            - width: 200%
  - type: 'custom:hui-element'
    card_type: horizontal-stack
    cards:
      - type: 'custom:button-card'
        entity: script.lounge_macro_listen_music
        template: menu_button
        service: script.lounge_macro_listen_music
        name: Airplay
        icon: 'mdi:airplay'
        aspect_ratio: null
      - type: 'custom:button-card'
        entity: script.lounge_macro_watch_youtube
        template: menu_button
        service: script.lounge_macro_watch_youtube
        name: PC Sound
        icon: 'mdi:monitor-speaker'
        aspect_ratio: null
  - type: 'custom:hui-element'
    card_type: horizontal-stack
    cards:
      - type: 'custom:button-card'
        entity: script.lounge_macro_goodnight
        template: menu_button
        service: script.lounge_macro_goodnight
        name: All Off (Lights Delayed 30sec)
        icon: 'mdi:weather-night'
        aspect_ratio: 16/3
style: |
  ha-card {
    border: solid 2px var(--primary-color);
  }

Button template:

  menu_button:
    aspect_ratio: 4/3
    color_type: icon
    hold_action:
      action: none
    layout: vertical
    show_label: false
    show_state: false
    show_name: true
    styles:
      card:
        - border-radius: 10px
        - border: solid 2px var(--primary-color)
        - box-shadow: none
        - padding: 5% 0% 5% 5%
        - margin: 0px 0px
        - '--paper-card-background-color': 'rgba(0, 0, 0, 0)'
      icon:
        - width: 28px
      name:
        - justify-self: centre
        - font-size: 14px
        - padding: 7.5% 0% 0% 0%
        - color: var(--secondary-text-color)
    tap_action:
      action: call-service

I’m probably going about this all wrong.

Hi @ dgarciadonado, yes I have it and it’s working well.
Maybe you can check out on entity field of the button inside

I also update the template with the follows: