Slider Button Card

repository·main·Indexed 19 days ago

https://github.com/mattieha/slider-button-card

A custom Home Assistant Lovelace card providing a button with an integrated slider for controlling entities such as lights, fans, covers, and media players. It supports various slider directions, background styles, and an optional action button. The card is compatible with domains including light, switch, fan, cover, input_boolean, media_player, climate, and lock, and can be installed via HACS or manually.

Tokens
4.5K
Snippets
13
Records
23
Agent score
65%

What's inside slider-button-card

  1. Entity-specific usage patterns

    main

    The card adapts its behavior based on the entity type:

    • Fans: The icon automatically rotates based on the fan speed.
    • Switches/Locks: Use slider.toggle_on_click: true to treat the slider as a simple toggle.
    • Covers: Typically use slider.direction: top-bottom and slider.background: striped.
    • Media Players: The slider controls volume. If the entity has a picture, it is used instead of the icon. Use action_button with mode: custom to implement play/pause.
    • Climate: The slider sets the target temperature but does not alter the state directly.
  2. Control Light and Cover Attributes via Slider

    main

    When using the attribute option in the slider configuration, you can control specific entity attributes.

    Note for Lights: Using attributes like color_temp, hue, or saturation may result in unexpected behavior (showing minimum values) if the entity's current color_mode does not match the selected attribute.

    Supported Attributes

    Light:

    • brightness_pct (default)
    • brightness
    • color_temp
    • hue
    • saturation

    Cover:

    • position (default)
    • tilt
  3. Customize Card Styles with Card Mod

    main

    You can use Card mod to override the card's appearance using CSS variables. Apply these within the style key of your card configuration.

    ```yaml
    style: |
      :host {
        --icon-color: red;
        --label-color-on: blue;
      }

    Available CSS Variables

    VariableDescriptionDefault
    --icon-colorColor of the icon when icon.use_state_color is falsevar(--paper-item-icon-color)
    --label-color-onColor of the label when state is onvar(--primary-text-color, white)
    --label-color-offColor of the label when state is offvar(--primary-text-color, white)
    --state-color-onColor of the state value when state is onvar(--label-badge-text-color, white)
    --state-color-offColor of the state value when state is offvar(--disabled-text-color)
    --action-icon-color-onColor of the action button icon when state is onvar(--paper-item-icon-color, black)
    --action-icon-color-offColor of the action button icon when state is offvar(--paper-item-icon-color, black)
    --action-spinner-colorColor of the spinner action buttonvar(--label-badge-text-color, white)
  4. Install Slider Button Card manually

    main

    If you are not using HACS, follow these steps to install the card manually:

    1. Download the slider-button-card.js file from the latest release.
    2. Place the file in your Home Assistant config/www folder.
    3. In Home Assistant, navigate to ConfigurationLovelace DashboardsResources.
    4. Click the Plus (+) button to add a new resource:
      • URL: /local/slider-button-card.js
      • Resource type: JavaScript Module
    5. Add custom:slider-button-card to your Lovelace UI using the editor or YAML.
    type: custom:slider-button-card
    entity: light.living_room
  5. Add a new translation to the Slider button card

    main

    To add support for a new language, you must create a JSON translation file, register it in the localization logic, and update the documentation.

    1. Create the JSON translation file

    Translation files are stored in src/localize/languages/.

    1. Copy src/localize/languages/en.json.
    2. Rename the copy to your two-character language code (e.g., xx.json).
    3. Translate only the values within the JSON file, keeping the keys intact.

    2. Register the language in the codebase

    You must make the new translation available to the card by modifying src/localize/localize.ts:

    1. Add an import statement for your new file: import * as xx from './languages/xx.json';
    2. Add the language to the languages object, ensuring it is sorted alphabetically:
    const languages: any = {
       en: en,
       he: he,
       nl: nl,
       pl: pl,
       ru: ru,
       xx: xx,
    };

    3. Update documentation

    Add your new language to the list under the languages section in README.md:

    - English
    - Hebrew
    - Nederlands (Dutch)
    - Polish (polski)
    - Russian
    - Your new language

    4. Submit changes

    Commit your changes and create a Pull Request to the repository.

  6. Configure the icon appearance and actions

    main

    The icon block allows you to customize the icon used in the card and define what happens when it is tapped.

    • icon: Overrides the default entity icon (e.g., mdi:lightbulb).
    • tap_action: Defines the Home Assistant action (e.g., more-info, toggle, call-service) when the icon is tapped.
    • use_state_color: (Boolean) If true, the icon uses the entity's state color.
    icon:
      icon: mdi:lightbulb
      use_state_color: true
      tap_action:
        action: more-info
  7. Use CSS Variables for Dynamic Styling

    main

    The card allows using CSS variables for colors, which can be resolved from the theme or specific values. If a value starts with var, the card attempts to resolve it from the element's computed style or the document root.

    Supported Variable Patterns:

    • var(--primary-color)
    • var(--slider-color)
    • var(--paper-item-icon-color)
  8. Configure the slider appearance and behavior

    main

    The slider block controls the visual style and interaction of the slider component.

    • direction: The orientation of the slider (left-right or top-bottom).
    • background: The visual style of the slider background (gradient, triangle, striped, solid, or custom).
    • use_state_color: (Boolean) Uses the entity's color or color temperature for the background.
    • show_track: (Boolean) Displays the slider track.
    • force_square: (Boolean) Forces the slider to maintain a square aspect ratio.
    • toggle_on_click: (Boolean) When true, clicking the slider acts as a toggle instead of sliding (useful for switches or locks).
    slider:
      direction: left-right
      background: triangle
      use_state_color: true
      show_track: true
      force_square: true
      toggle_on_click: true
  9. Configure Slider Options

    main

    The slider object controls the visual and functional aspects of the integrated slider. This is particularly useful for controlling attributes of light or cover entities.

    Slider Options

    NameTypeRequirementDescriptionDefault
    directionstringOptionalleft-right, top-bottom, bottom-topleft-right
    backgroundstringOptionalsolid, gradient, triangle, striped, customgradient
    use_state_colorbooleanOptionalUse state color for the slidertrue
    use_percentage_bg_opacitybooleanOptionalApply opacity to background based on percentagetrue
    show_trackbooleanOptionalShow track when state is onfalse
    force_squarebooleanOptionalForce the button to be squarefalse
    toggle_on_clickbooleanOptionalIf true, sliding is disabled and it acts as a togglefalse
    attributestringOptionalControl an attribute for light or cover-
    invertbooleanOptionalInvert calculation (useful for cover entities)false