Mastering Advanced Lovelace Design: Custom Button Card and Card-Mod for Dynamic Interfaces

NGC 224
DIY Smart Home Creator
Home Assistant's Lovelace UI is incredibly powerful for visualizing and controlling your smart home. While standard cards offer a solid foundation, achieving a truly personalized, dynamic, and visually striking dashboard often requires stepping beyond the basics. This is where two community-driven powerhouses, button-card
and card-mod
, come into play. Together, they unlock an unparalleled level of customization, allowing you to craft interfaces that are not just functional, but genuinely intuitive and aesthetically pleasing.
Why Button Card and Card-Mod Are Essential
Standard Lovelace cards are designed for general use, meaning they might not always perfectly fit your specific needs or design preferences. For instance, you might want a single button that displays the state of multiple entities, changes color based on complex conditions, or even displays custom icons and text fields. Similarly, you might want to adjust the padding of a card, change the font size of an entity, or even conditionally hide elements based on a sensor's value.
button-card
: This custom card is a highly versatile template engine for creating interactive buttons. It excels at displaying dynamic content from multiple entities, applying complex conditional styling, and handling various tap/hold actions. Think of it as a canvas for highly tailored controls.card-mod
: This is a custom component that allows you to apply CSS styling to virtually any element within your Lovelace dashboard. Whether it's a built-in card, another custom card, or even specific text within an entity,card-mod
gives you pixel-perfect control over its appearance.
Installation: Getting Started with HACS
Both button-card
and card-mod
are available through HACS (Home Assistant Community Store), which is the easiest way to install and manage custom components and cards.
- Ensure HACS is installed and configured in your Home Assistant instance.
- In Home Assistant, navigate to HACS -> Frontend.
- Click the '+ Explore & Download Repositories' button.
- Search for 'Button Card' and 'Card-mod'.
- Select each one and click 'Download'. Restart Home Assistant when prompted.
Once installed, you can start using them in your Lovelace configuration (ui-lovelace.yaml
or through the UI editor in YAML mode).
Deep Dive: Custom Button Card
button-card
's power comes from its extensive configuration options, allowing for deep customization.
Basic Usage
type: custom:button-card
entity: light.living_room_light
name: Living Room Light
icon: mdi:lightbulb
tap_action:
action: toggle
Templating and Custom Fields
You can define templates for reusable configurations or use custom_fields
to display additional data within your button.
type: custom:button-card
entity: light.kitchen_lights
name: Kitchen
icon: mdi:chef-hat
custom_fields:
temperature:
- >
[[[ return states['sensor.kitchen_temperature'].state + '°C'; ]]]
humidity:
- >
[[[ return states['sensor.kitchen_humidity'].state + '%'; ]]]
styles:
custom_fields:
temperature:
- font-size: 14px
- color: var(--paper-item-icon-color)
humidity:
- font-size: 12px
- color: var(--paper-item-icon-active-color)
In this example, custom_fields
display the kitchen temperature and humidity, styled individually.
Conditional Styling with state_styles
Change the button's appearance based on the entity's state or attributes.
type: custom:button-card
entity: binary_sensor.front_door
name: Front Door
icon: mdi:door
state:
- value: 'on'
styles:
card:
- background-color: '#EF5350' # Red when open
- color: white
icon:
- color: white
name:
- color: white
- value: 'off'
styles:
card:
- background-color: '#66BB6A' # Green when closed
- color: black
Deep Dive: Card-Mod
card-mod
injects custom CSS directly into your Lovelace elements. It's incredibly versatile for tweaking layouts, colors, fonts, and more.
Basic Usage (Targeting a Card)
type: entities
entities:
- light.bedroom_light
- switch.bedroom_fan
title: Bedroom Controls
card_mod:
style: |
ha-card {
border-radius: 15px;
box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.2);
background-color: var(--card-background-color);
}
Targeting Nested Elements
You can target specific elements within a card using standard CSS selectors.
type: entities
entities:
- sensor.washer_status
- sensor.dryer_status
title: Laundry Appliances
card_mod:
style: |
.card-content {
padding: 10px;
}
div.header {
font-size: 20px;
color: var(--primary-color);
text-align: center;
}
:host {
--mdc-list-item-single-line-height: 40px; /* Adjust entity row height */
}
The |
indicates a multi-line string in YAML, crucial for CSS blocks. :host
refers to the card itself, allowing you to modify its own CSS variables.
Practical Examples Combining Both
Dynamic Light Control Button
A single button showing light state, brightness, and changing color based on 'on'/'off'.
type: custom:button-card
entity: light.desk_lamp
name: Desk Lamp
icon: mdi:desk-lamp
show_last_changed: true
state:
- value: 'on'
styles:
card:
- background-color: var(--accent-color)
- color: white
- value: 'off'
styles:
card:
- background-color: var(--ha-card-background)
- color: var(--primary-text-color)
custom_fields:
brightness:
- >
[[[
if (states['light.desk_lamp'].state == 'on') {
return `Brightness: ${states['light.desk_lamp'].attributes.brightness ? Math.round(states['light.desk_lamp'].attributes.brightness / 2.55) : '0'}%`;
} else {
return '';
}
]]]
styles:
custom_fields:
brightness:
- font-size: 12px
- padding-top: 5px
grid:
- grid-template-areas: "i name" "i brightness" "i last-changed"
- grid-template-columns: 30% 70%
- grid-template-rows: 1fr 1fr 1fr
name:
- justify-self: start
- align-self: end
icon:
- align-self: center
- justify-self: center
- width: 70%
last_changed:
- justify-self: start
- align-self: start
Conditional Card Highlighting with Card-Mod
Highlight a temperature sensor card if the temperature exceeds a threshold.
type: entity
entity: sensor.outdoor_temperature
name: Outdoor Temperature
icon: mdi:thermometer
card_mod:
style: |
ha-card {
{% if states('sensor.outdoor_temperature') | float > 25 %}
border: 2px solid red;
animation: pulse 1s infinite alternate;
{% else %}
border: none;
{% endif %}
}
@keyframes pulse {
from { box-shadow: 0 0 5px rgba(255, 0, 0, 0.4); }
to { box-shadow: 0 0 15px rgba(255, 0, 0, 0.8); }
}
This example uses Jinja2 templating directly within card-mod
to apply dynamic CSS, including a pulsing animation for alerts.
Best Practices for Managing a Reliable UI
- Performance: While powerful, extensive use of complex Jinja2 templates or very large numbers of
button-card
instances can impact dashboard load times. Optimize by reusing templates where possible and keeping conditional logic concise. - Maintainability with YAML Anchors: For common styles or button layouts, use YAML anchors (
&anchor_name
) and references (*anchor_name
) to reduce duplication and improve maintainability. This makes global style changes much easier. - Responsiveness: Use CSS media queries within
card-mod
to create responsive designs that adapt to different screen sizes (mobile, tablet, desktop). - Debugging: The browser's developer tools (F12) are your best friend. Inspect elements to see which CSS rules are applied and where. Home Assistant logs can also help identify issues with Jinja2 rendering.
- Start Small: Begin by customizing one or two cards, then gradually expand your use of
button-card
andcard-mod
as you become more comfortable. - Documentation: Refer to the official GitHub repositories for button-card and card-mod for comprehensive documentation and examples.
Conclusion
button-card
and card-mod
are indispensable tools for anyone looking to push the boundaries of their Home Assistant Lovelace dashboards. By mastering these two components, you gain the power to create highly functional, visually stunning, and truly bespoke smart home interfaces. Experiment with their vast array of options, and you'll soon transform your generic control panel into a masterpiece of personalized automation.

NGC 224
Author bio: DIY Smart Home Creator