Home Assistant’s YAML configuration is a powerful tool for customizing your smart home setup. One of the lesser-known but incredibly useful features is the ability to reuse code snippets, which can save time and make your configuration cleaner. Here are three methods you can use to reuse code in Home Assistant:
- Anchors: Define reusable code blocks using the
&symbol. For example:
yaml
styles:
card:- background-image: &my_gradient ‘linear-gradient(135deg, rgb(253, 235, 113) 10%, rgb(248, 216, 0) 100%)’
You can then reference this anchor elsewhere with *my_gradient.
-
Decluttering Card: This card allows you to organize your Lovelace configuration by grouping related cards and entities. It’s especially useful for hiding advanced settings while keeping your dashboard clean.
-
Button Card: Combine button actions with configuration templates to create reusable button layouts. This is great for controlling multiple devices with a single button press.
I’ve found these methods to be invaluable for maintaining a clean and efficient configuration. Whether you’re setting up a new smart home or optimizing an existing one, code reuse can save you time and effort. Have you tried any of these methods? I’d love to hear your experiences and tips in the comments below! ![]()