Lovelace: Button card

Search box-shadow. Change on the state.

I’m not in a position to offer any code but I’ll send it when I’m back in front of my home computer.

Hi,
i have this on one of my buttons,
It is highlighted when on, and no background when off.

Here is the code:

type: 'custom:button-card'
color_type: icon
entity: light.dimmable_light_2
size: 55%
name: Light
state:
  - value: 'on'
    spin: false
    color: yellow
    icon: 'mdi:lightbulb'
    styles:
      state: 'on'
      card:
        - box-shadow: 0px 0px 10px 1px  cyan
      icon:
        - color: magenta
  - operator: default
    spin: false
    color: grey
    icon: 'mdi:lightbulb'

when active it looks like this:

image

when is inactive it looks like this:
image

Hope it helps
Bogdan

1 Like

It is possible if you check the path in the URL created by the state-switch card.
Something like:

styles:
  card:
    - background-color: | # or anything else
        [[[
          if (!this._monitoring) {
            this._monitoring = true;
            function hashHandler(elt = this) {
              elt.update();
            }
            window.addEventListener('hashchange', hashHandler, false);
          }
          return window.location.hash.slice(1) === "p1" ? COLOR_ACTIVE : null
        ]]]

I didn’t test it, but it should work. It’s bit hacky though :slight_smile:

Hi, so I’m interested in trying this as well (so I can move from input_select to hash). I can’t wrap my head around that code however. I need it to be color A by default and if the hash matches, it needs to be color B. But in your code I can’t seem to find a place where I could insert the color codes for either states. I figure I need to change null to my color I want it to be when matched right? But how can I set the color for when it’s not matched?

Could you point me in the right direction?

Change COLOR_ACTIVE to the color you want (eg 'rgb(255,0,0)' for matching hash and keep null or replace with another color for non-matching hash

Thanks! That part now works. Now I notice the button doesn’t update on action, only on reload of the page. I figure this is the case since I have no entity tied to it, so the card can update itself. Since it’s now hash and not input_select anymore, I don’t have an entity to use to update the card.

I read the part about triggers_update on the docs, but I didn’t find a solution there myself. Do you know what I could tie it to so it updates on tap_action? The important part is so it only updates on the device that presses it (which is why I want to refrain from input_select).

The part of the code you don’t understand should handle that so I’ll have to look further :grin:

1 Like

Hi Romrider,

Can you point me in the direction where to insert this code in my card config? :slight_smile:

Thx,

Jurgen

Hi Bogda,

This is not working for me, because I don’t handle active entities in my button.

Thx,

Jurgen

That goes as an option inside button-card. He wrote the exact code. All you need to do is copy and paste it and change colors by chaning:

Change COLOR_ACTIVE to the color you want (eg 'rgb(255,0,0)' for matching hash and keep null or replace with another color for non-matching hash

I highly advise you to read the documents first, like I advised you in a different post. Without reading the documentations, you have no idea why and what you are doing. Coping and pasting code will only get you so far.

custom-cards/button-card: :sparkle: Lovelace button-card for home assistant (github.com)

Btw, for now I would wait using this style. As you can read one post above you, it will not update the card when using tap_action right now.

Thanks! Would it also be possible to have something like this that (instead of hash) looks at the url (before hash)? For a different project (that doesn’t rely on state-switch), I want to reflect button styles based on the page itself. So I can use templates instead of copying the card to every dashboard.

yes, that’s very easy, and looks like the hash template above window.location.pathname.split('/')[2] (btw, you know you can check that in the inspector?)

:

using this button_template:

button_shortcut_menu:
  tooltip: >
    [[[ function capitalizeFirstLetter(string) {
              return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
              }
        return capitalizeFirstLetter(variables.tooltip.replace('_',' ')); ]]]
  variables:
#    dashboard: >
#      [[[ return window.location.pathname.split('/')[1]; ]]]
    view: >
      [[[ return window.location.pathname.split('/')[2]; ]]]
  size: 25px
  styles:
    tooltip:
      - color: var(--text-color-off)
      - font-size: 10px
      - background: var(--background-color-off)
#      - opacity: 0.5
      - transition: opacity 0.5s
      - padding: 5px
    icon:
      - color: var(--secondary-text-color)
    card:
      - box-shadow: none
      - background: >
          [[[ return variables.view == variables.path
              ? 'var(--secondary-background-color)' : 'var(--card-background-color)'; ]]]

I color several relevant parts in my menu buttons, but also set icons and navigation paths etc:

a button using that:

  - type: custom:button-card
    template: button_shortcut_menu
    variables:
       path: home
    tooltip: >
     [[[ return variables.view == variables.path
         ? 'Hassio' : 'Home'; ]]]
    icon: >
        [[[ return variables.view == variables.path
            ? 'mdi:home-assistant' : 'mdi:home'; ]]]
    tap_action:
      action: navigate
      navigation_path: >
        [[[ return variables.view == variables.path
            ? '/ui-data/home_assistant' : '/lovelace/home'; ]]]
    styles:
      icon:
        - color: >
            [[[ return (states['sensor.count_alerts_notifying'].state > 0)
                ? 'red': 'green'; ]]]
    state:
      - operator: template
        value: >
          [[[ return (states['sensor.count_alerts_notifying'].state > 0); ]]]
        spin: true

you can still combine the state_switch to make it a bit more complex ;-=)

  - type: custom:state-switch
    entity: group
    states:
      admin:
        type: custom:button-card
        template: button_shortcut_menu
        variables:
          path: settings
        tooltip: >
          [[[ return variables.view == variables.path
              ? 'Energy' : 'Settings'; ]]]
        icon: >
          [[[ return variables.view == variables.path
              ? 'mdi:flash' : 'mdi:tools'; ]]]
        tap_action:
          action: navigate
          navigation_path: >
            [[[ return variables.view == variables.path
                ? '/ui-data/home_energy' : '/ui-settings/settings'; ]]]

      user:
        type: custom:button-card
        template: button_shortcut_menu
        variables:
          path: help
        tooltip: Help
        icon: mdi:help
        tap_action:
          action: navigate
          navigation_path: /lovelace/help

3 Likes

That is very thorough and detailed piece of code :smiley: ! Thanks a lot, will check it all out and play with it.

When custom header was dropped a few months ago, I created my own version with button card inside a horizontal stack, using it at the bottom as a sticky and disbaling the original header. It works great, but I want to streamline the code, so it’s easier to maintane and change if needed.

Thanks for your code and suggestions!

same here. (btw, still using CH without a single hiccup …)

this is what it my shortcut menubar looks like

1 Like

I agree, I need to do more RFM! :slight_smile:
But in some cases it is not simple to read docs like these for a beginner!
And to the point examples are in some cases more use-full for beginners, because you can compare the code with the existing documentation!

But still, I understand your frustration! :wink:

Thx anyway!

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)