Config Template Card 使用教程
1. 项目介绍
Config Template Card 是一个用于 Home Assistant 的 Lovelace UI 插件,允许用户在配置中使用 JavaScript 模板。通过这个插件,用户可以动态地生成和更新 Home Assistant 的 UI 元素,从而实现更灵活和个性化的界面配置。
2. 项目快速启动
安装
首先,确保你已经安装了 Home Assistant 并且版本在 0.110.0 或更高。然后,你可以通过 HACS(Home Assistant Community Store)来安装 Config Template Card。
- 打开 Home Assistant 的 HACS 界面。
- 导航到“前端”部分。
- 搜索“Config Template Card”并点击安装。
- 安装完成后,重启 Home Assistant。
配置
安装完成后,你可以在 Lovelace 配置中使用 Config Template Card。以下是一个简单的配置示例:
type: entities
entities:
- type: 'custom:config-template-card'
variables:
- states['light.bed_light'].state
entities:
- light.bed_light
row:
type: section
label: "$[vars[0] === 'on' ? 'Light On' : 'Light Off']"
- entity: light.bed_light
在这个示例中,我们使用了一个模板来动态显示灯的状态。
3. 应用案例和最佳实践
动态显示状态
Config Template Card 最常见的用途是动态显示实体的状态。例如,你可以根据传感器的状态来显示不同的图标或文本。
type: picture-elements
image: http://hs-sbcounty.gov/CN/Photo%20Gallery/_t/Sample%20Picture%20-%20Koala_jpg.jpg
elements:
- type: 'custom:config-template-card'
variables:
- states['light.bed_light'].state
entities:
- light.bed_light
- sensor.light_icon_color
element:
type: icon
icon: "$[vars[0] === 'on' ? 'mdi:home' : 'mdi:circle']"
style:
'--paper-item-icon-color': "$[states['sensor.light_icon_color'].state]"
style:
top: 47%
left: 75%
全局函数
如果你需要在多个地方重复使用相同的逻辑,可以在变量中定义全局函数,然后在模板中调用这些函数。
type: 'custom:config-template-card'
variables:
setTempMessage: |
temp => {
if (temp <= 19) {
return 'Quick, get a blanket!';
} else if (temp >= 20 && temp <= 22) {
return 'Cozy';
}
return 'It's getting hot in here!';
}
currentTemp: states['climate.ecobee'].attributes.current_temperature
entities:
- climate.ecobee
card:
type: entities
entities:
- entity: climate.ecobee
name: "$[setTempMessage(currentTemp)]"
4. 典型生态项目
Config Template Card 通常与其他 Home Assistant 插件和组件一起使用,以实现更复杂的自动化和 UI 定制。以下是一些常见的生态项目:
- Home Assistant: 作为 Config Template Card 的基础平台,Home Assistant 提供了丰富的自动化和集成功能。
- HACS (Home Assistant Community Store): 用于管理和安装社区开发的插件,包括 Config Template Card。
- Lovelace UI: Home Assistant 的默认用户界面,Config Template Card 可以在这里进行配置和展示。
通过结合这些项目,用户可以创建高度定制化和动态的 Home Assistant 界面。