Lovelace: Button card

I was a beginner as well :wink: I started using Home Assistant little over a year ago and have learned a lot in the first 3 months. Most I have learned by studying other codes (instead of copy/pasting) and reading all documentations and most importantly: making mistakes. I learned most by making mistakes, so I had to figure out what the issue was. Forcing me to understand the docs which allowed me to use and combine different cards in very creative ways, like this project I did for a media player card: Fully loaded media player card (picture-elements project) - Share your Projects! - Home Assistant Community (home-assistant.io)

There are also people who use copied codes and call it a day. It works on the short term, but they find the most issues down the road when updates to Home Assistant and custom cards happen. Then they have no idea what broke their interface and why (since they never bothered to understand what the card actually is about and ignore all changelogs).

The docs can be overwhelming, but it doesn’t mean you have to make it all work in one day. Start small using simple buttons and use that code for other projects. So it was just a friendly advise, since I was in your place as well a year ago :smiley:

HI maxym,

How did you change the icon for the windows?

at it looks like this:

grafik

Thank You.

Hello.
Moving icon around the buttons is easy. you need to reconfigure grid of the button. In short you can create own grid of areas and define which one will display what, using specific names. For example grid area named “i” will handle an icon, “n” is for entity name, and ‘s’ is for status

But what I’ve done is, that one button has even more 4 icons. To achieve that I embedded more button cards into parent button card.

Here is grid definition of my card:

    styles:
      grid:
        - grid-template-areas: '"i window" "n n" "misc temp"'
        - grid-template-columns: 1fr 60%
        - grid-template-rows: 1fr 1fr 1fr

It splits main button into 2x3 matrix. Then in first row has 2 areas: i for icon and custom called window. Second row is dedicated for entity name. Third row has again 2 custom areas: misc and temp

Here is complete code of my approach a published a few days ago in response of someone else.

Hi kongo09,

I have tried to user your scripts and I wanted to ask you a few things. I have an error for example when I try to change the Fan speed/preset and I got this error : Failed to call service script/airpurifier_mode. Error rendering data template: UndefinedError: ‘None’ has no attribute ‘index’ . “airpurifier_mode” is actually the alias Toggle AC fan speed script. I have no idea why is not working maybe you can guide me to fix the problem.
The rest of the buttons are working :slight_smile:
Also I saw that you have custom icons used. Is it something that you can share?
Thank you

Hi @ovidiudr and welcome to the community. Please note that Home Assistant has changed the fan integration, so the script needs an update:

alias: Toggle AC fan speed
sequence:
  - service: fan.set_preset_mode
    data_template:
      entity_id: '{{ entity_id }}'
      preset_mode: >
        {%- set current = state_attr(entity_id, 'preset_mode') %} {%- set modes
        = state_attr(entity_id, 'preset_modes') %} {%- set index =
        modes.index(current) %} {%- set next = 1 if current == modes[-1] else
        index + 1 %} {{ modes[next] }}
mode: single
icon: 'philips:fan_speed_button'
fields:
  entity_id:
    description: Entity of fan
    example: fan.air_ac2729
description: Set the speed mode of philips AC

Please also make sure that you use at least version 0.4.0 of the custom integration for the Philips fan. You can integrate this with HACS by using this repository: GitHub - betaboon/philips-airpurifier-coap: 💨 Philips AirPurifier custom component for Home Assistant. With support for new Devices with CoAP protocol. Tested on AC2729/10 (bought early 2020)

Yes, I have created my own custom icons from the Philips icons. I’m not sure if it is ok to share the code here, as Philips holds the copyright to the design and I don’t have a license. However, I documented my approach here.

Hello,

I am quite new to Home Assistant and have a question.
I am trying to override the STATE NAME that is displayed.

This is what my current code looks like.
When the STATE changes to Scene_1 I want the STATE to show “ON”.

                - type: 'custom:button-card'
                  entity: input_text.office_lights_current_scene
                  show_state: true
                  icon: mdi:ceiling-light
                  name: 'Deckenlicht: Normal'
                  template: base_2x2
                  gridrow: 2
                  gridcol: 2 / 4
                  tap_action:
                    action: call-service
                    service: scene.turn_on
                    service_data:
                      entity_id: scene.buro_deckenleuchte_rechts_links_normal
                  styles:
                    card:
                      - box-shadow: >
                          [[[ return (entity.state === 'scene_1' || entity.state === 'home') ? '5px 10px 18px rgba(0, 0, 0, 0.8)' : '5px 10px 18px rgba(0, 0, 0, 0.0)'; ]]]

                      - color: >
                          [[[ return (entity.state === 'scene_1' || entity.state === 'home') ? 'rgba(0, 0, 0, 0.6)' : 'rgba(255, 255, 255, 0.2)'; ]]]
                      
                      - background-color: >
                          [[[ return (entity.state === 'scene_1' || entity.state === 'home') ? 'rgba(255, 255, 255, 0.8)' : 'rgba(115, 115, 115, 0.2)'; ]]]
                  state:
                    - value: 'scene_1'
                      styles: 
                       state:
                        - name: 'On'

It would be really great if someone has an idea why it currently fails.

I think the issue is how you’re using style. Basically you’re saying: when the state of the entity
input_text.office_lights_current_scene is scene_1 , use this styling: and then you style state with name: On. But that is not a style.

Styles are things like colors, width, filters etc. The variable show_state: true displays the actual state of the entity you list there. I believe you can’t change what state actually says. That’s kinda the whole use case of state, so it shows the acual state of the entity in HA. You should use label if you want to have something below the name that you can change.

Try:

                - type: 'custom:button-card'
                  entity: input_text.office_lights_current_scene
                  show_label: true
                  label: 'Off'
                  show_state: false
                  icon: mdi:ceiling-light
                  name: 'Deckenlicht: Normal'
                  template: base_2x2
                  gridrow: 2
                  gridcol: 2 / 4
                  tap_action:
                    action: call-service
                    service: scene.turn_on
                    service_data:
                      entity_id: scene.buro_deckenleuchte_rechts_links_normal
                  styles:
                    card:
                      - box-shadow: >
                          [[[ return (entity.state === 'scene_1' || entity.state === 'home') ? '5px 10px 18px rgba(0, 0, 0, 0.8)' : '5px 10px 18px rgba(0, 0, 0, 0.0)'; ]]]

                      - color: >
                          [[[ return (entity.state === 'scene_1' || entity.state === 'home') ? 'rgba(0, 0, 0, 0.6)' : 'rgba(255, 255, 255, 0.2)'; ]]]
                      
                      - background-color: >
                          [[[ return (entity.state === 'scene_1' || entity.state === 'home') ? 'rgba(255, 255, 255, 0.8)' : 'rgba(115, 115, 115, 0.2)'; ]]]
                  state:
                    - value: 'scene_1'
                      label: 'On'
                      styles:

What this basically tells the button to do is: put a label under the name as default with the text ‘Off’. But when the state of the entity input_text.office_lights_current_scene changes to scene_1 then it will change the label to ‘Off’.

You can also add stylings (I put it at the bottom) there, so that icons, label, name etc changes colors for instance when the state has changed to scene_1. Make sure you use the indents under there, just like you did on the main styles. If you don’t use styles there, it will just keep the default style on the state. This way you can create multiple states with variables (like icon, label, name) and styles that change based on the state of the entity.

you need to use the state_display option, which was introduced for this especially, and of course, it supports templates.

example:

state_display: >
  [[[ return 'Dst: ' + entity.attributes.next.clock ]]]

or somewhat more complex:

  state_display: >
    [[[ function capitalizeFirstLetter(string) {
        return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
        }
        var id = entity.entity_id.split('_').pop(-1);
        var fakestate = `<span style="color: var(--active-color);"> ${entity.state}</span>`;
        var fakelabel = `<span style="color: var(--active-color);"> ${capitalizeFirstLetter(id)}</span>`;
        return (entity.state == 'on') ? fakestate + ': ' + fakelabel : null; ]]]

state_display responds to show_state:

Thank you very much for your explanation and suggested solution.
Works perfectly. Here is my final code:

Button:

                - type: 'custom:button-card'
                  entity: input_text.office_lights_current_scene
                  template: base_2x2_lights
                  name: 'Deckenlicht: Normal'
                  variables:
                    var_1: 'scene_1'
                  gridrow: 2
                  gridcol: 2 / 4

                  hold_action:
                    action: more-info
                    entity: light.deckenleuchte_buro

                  tap_action:
                    action: call-service
                    service: input_text.set_value
                    service_data:
                      value: '[[[ return variables.var_1 ]]]'
                      entity_id: input_text.variable_text_1

Template:

  base_2x2_lights:
    aspect_ratio: 1/1
    show_state: false
    show_icon: true
    icon: mdi:ceiling-light
    show_label: true
    label: 'Aus'
    variables:
      var_1: null
    size: 40%
    color: '#148eff'
    state:
      - value: '[[[ return variables.var_1 ]]]'
        label: 'An' 
    styles:
      name:
        - justify-self: start
        - top: 0%
        - left: 0%
        - line-height: 4em
        - padding-left: 10px
        - position: relative
      icon:
        - top: -20%
        - left: -30%
        - position: relative
      state:
        - top: 0%
        - left: -30%
        - line-height: 2em
        - position: relative
      label:
        - justify-self: start
        - padding-bottom: 10px
        - padding-left: 10px
      card:
        - font-family: Verdana
        - font-weight: 450
        - font-size: 1.4em
        - border-radius: 0.8em
        - transition: none
        - box-shadow: >
            [[[ return (entity.state === variables.var_1) ? '5px 10px 18px rgba(0, 0, 0, 0.8)' : '5px 10px 18px rgba(0, 0, 0, 0.0)'; ]]]
        - color: >
            [[[ return (entity.state === variables.var_1) ? 'rgba(0, 0, 0, 0.6)' : 'rgba(255, 255, 255, 0.2)'; ]]]
        - background-color: >
            [[[ return (entity.state === variables.var_1) ? 'rgba(255, 255, 255, 0.8)' : 'rgba(115, 115, 115, 0.2)'; ]]]
 

2 Likes

Hello Marius,
thank you very much for your suggestion. I used your solution for my blinds buttons.

Button:

                - type: 'custom:button-card'
                  entity: cover.tradfri_blind_office_window_left
                  template: base_2x2_blinds
                  name: 'Jalousien Offen'
                  show_state: true
                  variables:
                    var_1: 'scene_0'
                    var_min: 75
                    var_max: 100
                  gridrow: 2
                  gridcol: 2 / 4
                  
                  hold_action:
                    action: more-info
                    entity: group.window_blinds_office

                  tap_action:
                    action: call-service
                    service: input_text.set_value
                    service_data:
                      value: '[[[ return variables.var_1 ]]]'
                      entity_id: input_text.variable_text_2

                  state_display: '[[[ return entity.attributes.current_position + "%" ]]]' 

Template:

  base_2x2_blinds:
    aspect_ratio: 1/1
    show_state: false
    show_icon: true
    icon: mdi:blinds-open
    show_label: false
    variables:
      var_1: null
      var_min: 75
      var_max: 100
    size: 40%
    color: '#c4c4c4'
    state:
      - value: '[[[ return variables.var_1 ]]]'
        label: 'An' 
    styles:
      name:
        - justify-self: start
        - top: 0%
        - left: 0%
        - line-height: 4em
        - padding-left: 10px
        - position: relative
      icon:
        - top: -20%
        - left: -30%
        - position: relative
        - color: >
            [[[ return (entity.attributes.current_position <= variables.var_max && entity.attributes.current_position >= variables.var_min ) ? '#148eff' : 'rgba(255, 255, 255, 0.2)'; ]]]
      state:
        - top: 0%
        - left: -30%
        - line-height: 2em
        - position: relative
      label:
        - justify-self: start
        - padding-bottom: 10px
        - padding-left: 10px
      card:
        - font-family: Verdana
        - font-weight: 450
        - font-size: 1.4em
        - border-radius: 0.8em
        - transition: none
        - box-shadow: >
            [[[ return (entity.attributes.current_position <= variables.var_max && entity.attributes.current_position >= variables.var_min ) ? '5px 10px 18px rgba(0, 0, 0, 0.8)' : '5px 10px 18px rgba(0, 0, 0, 0.0)'; ]]]
        - color: >
            [[[ return (entity.attributes.current_position <= variables.var_max && entity.attributes.current_position >= variables.var_min ) ? 'rgba(0, 0, 0, 0.6)' : 'rgba(255, 255, 255, 0.2)'; ]]]
        - background-color: >
            [[[ return (entity.attributes.current_position <= variables.var_max && entity.attributes.current_position >= variables.var_min ) ? 'rgba(255, 255, 255, 0.8)' : 'rgba(115, 115, 115, 0.2)'; ]]]

1 Like

fwiw, here’s what they do in my config:
Schermafbeelding 2021-03-20 om 22.08.32

1 Like

Hi folks, a quick question. Different icon by state on/off works with no problem, but when I have different states “home” “work” it does not work. Any ideas?

state:
  - value: biocenter
    icon: 'mdi:biohazard'
  - value: away
    icon: 'mdi:account-question'
    styles:
      card:
        - background: Red
  - value: 'on'

The apostrof ' stays only with states off/on, the editor cuts off it when I need to specify the other states.

Thank you for your help. It is working now :partying_face:

The only thing I noticed is that it doesn’t reconnect to Home Assistant if I unplug the Air Purifier to move it into a different room… Did you experienced the same issue ? To fix it I have to restart Home Assistant…

Check the actual state in dev-tools…

not sure if you mean this, but you only need to quote these yaml values when they are booleans, see YAML - Home Assistant

of course, when using templates, additional quoting rules apply.

In any case, the icons don’t need quoting here.

Special locations/state of a tracker is case-sensitive. That’s the problem. Boolean on/off is not apparently.

For me, it keeps loosing the connection al the time. I guess that’s down to the bad implementation of Philips.

there’s an awful lot of haze in my head right now, I cant figure out how to get the deviceID to the frontend, showing it on a button-card… please help me out:

    name: |
      [[[ return 'UserID: ' + hass.user.id + "<br>" +
                 'Username: ' + hass.user.name + "<br>" +
                 'Owner: ' + hass.user.is_owner + "<br>" +
                 'Admin: ' + hass.user.is_admin + "<br>" +
                 'DeviceID: ' + ???;
      ]]]

I need it to be a template, and not a hard coded deviceID of a single device.

1 Like

DeviceID as in browser_mod device ID?
If so, you could try: 'DeviceID: ' + localStorage['lovelace-player-device-id']