custom:button-card

repository·master·Indexed 25 days ago

https://github.com/custom-cards/button-card

A highly customizable Lovelace card for Home Assistant (version 7.0.1) that enables the creation of state-aware UI elements. It features JavaScript templating, advanced interaction triggers (tap, hold, double click), and deep styling options for colors, icons, and layouts. Key capabilities include config templates for reusability, custom fields for embedding HTML or other cards, and flexible dimension control via aspect ratios.

Tokens
32.4K
Snippets
68
Records
128
Agent score
80%

What's inside button-card

  1. What is custom:button-card?

    master
    The custom:button-card is a highly configurable Lovelace custom card for Home Assistant designed to work with any entity. It provides extensive customization for state display, colors, icons, layouts, and animations. It supports advanced interaction patterns including JavaScript templates, custom CSS, and complex action sequences.
  2. Overview of custom:button-card features

    master

    custom:button-card is a highly customizable button designed for Home Assistant Lovelace interfaces. It allows for deep integration with any entity and provides extensive control over styling, actions, and animations.

    Key Capabilities:

    • Entity Integration: Works with any Home Assistant entity.
    • Flexible Actions: Supports 6 standard actions (none, toggle, more-info, navigate, url, assist, and call-service) on tap, hold, or double click.
    • Granular Interaction:
      • Icon Tap Action: Define a specific action for clicking the icon that takes precedence over the main card action.
      • Momentary Actions: Use press_action and release_action to replace default tap behaviors.
      • Custom Actions: Supports javascript, multi-actions, and toast actions.
      • Security: Includes password/PIN protection for sensitive actions and a locking mechanism.
    • Advanced Styling:
      • Color Management: Supports icon color type (applies to icon only) and card color type (applies to card and automatically adjusts font color).
      • Dynamic Styling: Custom colors based on light RGB values or temperature, and custom state definitions (color, icon, and style).
      • Layout & Sizing: Supports custom icon sizes, width/height, aspect ratio, and multiple layout modes.
      • CSS: Full support for custom CSS styling.
    • Animations & Feedback:
      • Native blink animation support.
      • Icon rotation animation support.
      • Haptic feedback support for the iOS companion app.
    • Organization:
      • Blank cards and label cards for UI organization.
      • Ability to redefine or hide sensor units.
  3. Configure separate actions for the icon

    master

    You can define specific actions that only trigger when the icon part of the card is clicked using icon_tap_action, icon_hold_action, etc.

    Note: If any icon_*_action is defined, the icon area captures all interactions for that area. If you want the icon to perform the same action as the main button on a tap, you must explicitly define icon_tap_action to match the tap_action.

    - type: 'custom:button-card'
      entity: light.living_room
      name: Living Room Light
      tap_action:
        action: toggle
      icon_tap_action:
        action: more-info
  4. Handle nested templates in custom_fields

    master

    When nesting a custom:button-card inside the custom_fields of another custom:button-card, you must use quadruple brackets [[[[ ... ]]]] if you want the inner card to evaluate the template itself.

    If you use triple brackets [[[ ... ]]] in a nested configuration, the outer card will evaluate the template and pass the resulting value (a string) to the inner card. If you use quadruple brackets [[[[ ... ]]]], the outer card will strip one pair of brackets and pass the template code to the inner card, allowing the inner card to evaluate the logic dynamically.

    type: custom:button-card
    entity: sensor.skylight
    custom_fields:
      nested:
        card:
          type: custom:button-card
          entity: light.bed_light
          name: |
            [[[[
              return entity?.state === 'on' ? 'Light On' : 'Light Off';
            ]]]]
    styles:
      grid:
        - grid-template-areas: '"nested"'
  5. Configure color application types in custom:button-card

    master

    When defining colors, you can specify how the color settings are applied using the color_type concept. This determines whether the color affects the icon or the entire card background.

    • icon: Applies color settings to the icon only.
    • card: Applies color settings to the card background. When card is selected, the card automatically adjusts the font color for readability.
  6. Use `action: multi-actions` to run a sequence of actions

    master

    The multi-actions action type allows you to execute multiple actions in a sequence with optional delays.

    Important Limitations:

    • This runs exclusively in the browser. If you navigate away from the page while actions are queued, they will not execute.
    • Actions are fired back-to-back without waiting for the previous action to finish (unless using wait_completion).
    • Nested actions do not support repeat, repeat_limit, sound, confirmation, protect, or haptic properties; these will be ignored.

    Special Entries in the actions array:

    • delay: Accepts a string (natural language like 3s or 1min) or a number (milliseconds). Supports templating.
    • wait_completion: Accepts a JS template that must return true or false. The sequence waits (checking every 0.5s) until the template returns true. You can specify a timeout (same format as delay).
    type: 'custom:button-card'
    icon: mdi:console
    name: multi-actions
    tap_action:
      action: multi-actions
      actions:
        - action: call-service
          service: light.toggle
          service_data:
            entity_id: light.test_light
        - delay: '3s'
        - action: javascript
          javascript: '[[[ helpers.toastMessage(`Done!`); ]]]'
  7. Configure button-card actions and interactions

    master

    The card supports various interaction triggers including tap, hold, and double click.

    Available Actions

    You can assign the following actions to triggers:

    • none: No action.
    • toggle: Toggles the entity state.
    • more-info: Opens the entity's more-info dialog.
    • navigate: Navigates to a specific dashboard view.
    • url: Opens an external URL.
    • assist: Triggers Home Assistant Assist.
    • call-service: Calls a specific Home Assistant service.

    Advanced Interaction Features

    • Icon Tap Action: You can define a separate action specifically for clicking the icon, which takes precedence over the main card actions.
    • Momentary Actions: Use press_action and release_action to define behavior for the duration of a press. Note that using these replaces the default tap/hold/double-click actions.
    • Custom Actions: Supports javascript, multi-actions, and toast actions.
    • Security: Supports confirmation popups for sensitive items and password/PIN protection for specific actions.
  8. Customize button-card appearance and styling

    master

    The custom:button-card offers deep styling capabilities:

    Color and Icon Styling

    • Color Types:
      • icon: Applies color settings only to the icon.
      • card: Applies color settings to the card itself (includes automatic font color adjustment).
    • State-based Styling: Define custom state definitions with unique colors, icons, and styles.
    • Custom Icons: Supports custom icon sets and rotation animations.

    Layout and Dimensions

    • Sizing: Control icon size, card width, and card height.
    • Aspect Ratio: Supports defined aspect ratios for the card.
    • Layouts: Supports multiple built-in layouts and custom layout configurations.
    • Units: Ability to redefine or hide units for sensor entities.

    Animations and Feedback

    • Blink Animation: Native support for blinking states.
    • Icon Rotation: Support for rotating icons.
    • Haptics: Supports haptic feedback for the iOS companion app.
  9. Merge states using the `id` parameter

    master

    When using templates, you can merge specific state configurations together if they share the same id. If a state in a template or a child template has an id that matches an id in a parent template, the properties (like styles, color, or operator) are merged instead of the state being appended to the list. States without an id are always appended to the state array.

    This is useful for defining a base set of states and then selectively overriding or extending specific ones in specialized templates.

    button_card_templates:
      sensor:
        state:
          - color: orange
            value: 75
            id: my_id
    
      sensor_test:
        template: sensor
        state:
          - color: pink
            id: my_id
            operator: '>'
            value: 75
            styles:
              name:
                - color: '#ff0000'
  10. Customize layout using CSS Grid

    master

    The grid style member allows you to control the card layout using CSS Grid. The button is composed of several HTML elements that map to specific grid-area values by default:

    • img_cell $\rightarrow$ grid-area: i (icon)
    • name $\rightarrow$ grid-area: n
    • state $\rightarrow$ grid-area: s
    • label $\rightarrow$ grid-area: l

    You can redefine the layout using grid-template-areas. For example, to place the label on top of the icon, name, and state, use:

    styles:
      grid:
        - grid-template-areas: '"l" "i" "n" "s"'
        - grid-template-rows: min-content 1fr min-content min-content
        - grid-template-columns: 1fr
    styles:
      grid:
        - grid-template-areas: '"i n s" "i n s" "i n l"'
  11. Define and use variables in JS Templates

    master

    Variables allow you to reuse logic and values across multiple fields. They can be any type (string, number, object, function).

    Note: Variables are only evaluated if they are used. If a variable is defined but never referenced, it will not be evaluated.

    Simple Usage

    You can define a variable once and reference it in multiple style or name fields.

    type: custom:button-card
    entity: switch.skylight
    variables:
      color_on: green
      color_off: red
      is_on: '[[[ return entity.state === "on"; ]]]'
      color: |
        [[[ 
          if (variables.is_on) return variables.color_on; 
          else return variables.color_off; 
        ]]]
    styles:
      icon:
        - color: '[[[ return variables.color; ]]]'

    Using Variables as Functions

    You can store functions in variables to perform complex logic.

    type: custom:button-card
    variables:
      myFunc: |
        [[[ 
          return (str) => { return `${str} from myFunc()`; } 
        ]]]
    name: '[[[ return variables.myFunc("Nice Name"); ]]]'

    Variable Dependencies

    Variables can depend on other variables, but circular dependencies (loops) are prohibited and will cause failure.

    Advanced: Forcing Variable Evaluation

    By default, variables are only evaluated if used. To force a variable to evaluate every time the card updates (useful for advanced JS logic), use the force_eval: true property:

    variables:
      varName:
        force_eval: true
        value: '[[[ ... ]]]'
  12. Build the button-card project

    master

    To build the project for production, run the build command. The compiled output will be placed in the dist folder.

    If you are using the provided VS Code devcontainer, you can also trigger a build and start Home Assistant simultaneously by using the VS Code debug tool. This process runs npm run watch to automatically watch for changes and rebuild the code.

    npm run build