Lovelace: Button card

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)

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.