
Home Assistant 卡片分享(五)环境监测与耗材管理卡片
本文将详细介绍如何在 Home Assistant 中使用 Mushroom Chips Card 创建环境监测与耗材管理卡片。这个方案包括四个环境传感器(温度、湿度、PM10粉尘、甲醛)和一个耗材监控群组,能够直观地展示环境数据和耗材状态。
最终效果展示
耗材充足时:
text
🌡️ 23.5°C 💧 45% 🌀 12.5μg/m³ ☣️ 0.023mg/m³ ✅ 耗材充足
耗材不足时:
text
🌡️ 23.5°C 💧 45% 🌀 12.5μg/m³ ☣️ 0.023mg/m³ 🔴 净化器滤芯, 门锁电池 需更换
⚠️ 净化器滤芯 (15%)
⚠️ 门锁电池 (10%)
完整配置步骤
1. 创建耗材监控群组
首先需要在 Home Assistant 的配置文件中创建耗材监控群组。打开 configuration.yaml
文件,添加以下内容:
yaml
# 耗材监控群组配置
group:
hao_cai:
name: 耗材监控
entities:
- sensor.xiaomi_cn_845289709_va3_filter_life_level_p_4_1 # 净化器滤芯剩余寿命
- sensor.yeelink_cn_blt_3_1kq920if8ck00_contrl_battery_level_p_2_1003 # 双键无线开关电池电量
- sensor.xiaomi_cn_822494120_lx40_filter_life_level_p_3_1 # 净水器复合滤芯剩余寿命
- sensor.xiaomi_cn_822494120_lx40_filter_life_level_p_5_1 # 净水器RO反渗透滤芯剩余寿命
- sensor.xiaomi_cn_821056281_ymv8_battery_level_p_6_1 # 燃气灶电池电量
- sensor.lumi_cn_blt_3_1l1mgh9k8c800_bmcn01_battery_level_p_3_1 # 水浸卫士电池电量
- sensor.miaomiaoc_cn_blt_3_1kpg1kpmckc01_t9_battery_level_p_2_1003 # 温湿度计电池电量
- sensor.loock_cn_1142402576_t2pv1_battery_level_p_4_1003 # 门锁电池电量
- sensor.loock_cn_1142402576_t2pv1_battery_level_p_10_1 # 猫眼电池电量
注意: 请将上述实体ID替换为您自己设备对应的实体ID。保存文件后,重启Home Assistant使配置生效。
2. Mushroom Chips Card 完整配置
将以下配置添加到您的Lovelace仪表盘中:
yaml
type: custom:mushroom-chips-card
chips:
# 温度传感器芯片
- type: template
entity: sensor.miaomiaoc_cn_blt_3_1kpg1kpmckc01_t9_temperature_p_3_1001
icon: mdi:thermometer
icon_color: |
{% set temp = states(entity) | float(default=0) %}
{% if temp < 18 %} blue
{% elif temp < 26 %} green
{% else %} red
{% endif %}
content: |
{{ states(entity) | float | round(1) }}°C
tap_action:
action: more-info
hold_action:
action: more-info
# 湿度传感器芯片
- type: template
entity: sensor.miaomiaoc_cn_blt_3_1kpg1kpmckc01_t9_relative_humidity_p_3_1002
icon: mdi:water-percent
icon_color: |
{% set humi = states(entity) | float(default=0) %}
{% if humi < 30 %} orange
{% elif humi < 70 %} green
{% else %} purple
{% endif %}
content: |
{{ states(entity) | float | round(0) | int }}%
tap_action:
action: more-info
hold_action:
action: more-info
# PM10粉尘传感器芯片
- type: template
entity: sensor.xiaomi_cn_845289709_va3_pm10_density_p_3_5
icon: mdi:air-filter
icon_color: |
{% set pm10 = states(entity) | float(default=0) %}
{% if pm10 <= 50 %} green
{% elif pm10 <= 150 %} yellow
{% else %} deep-orange
{% endif %}
content: |
{{ states(entity) | float | round(1) }} μg/m³
tap_action:
action: more-info
hold_action:
action: more-info
# 甲醛传感器芯片
- type: template
entity: sensor.xiaomi_cn_845289709_va3_hcho_density_p_3_11
icon: mdi:chemical-weapon
icon_color: |
{% set hcho = states(entity) | float(default=0) %}
{% if hcho <= 0.08 %} green
{% elif hcho <= 0.2 %} yellow
{% else %} deep-orange
{% endif %}
content: |
{{ states(entity) | float | round(3) }} mg/m³
tap_action:
action: more-info
hold_action:
action: more-info
# 耗材汇总芯片(核心部分)
- type: template
entity: group.hao_cai
icon: mdi:check-circle
icon_color: green
content: 耗材充足
tap_action:
action: more-info
hold_action:
action: more-info
# 当有耗材不足时,覆盖默认显示
conditions:
- entity: group.hao_cai
state: "on"
icon: mdi:alert-circle
icon_color: red
content: |
{% set low_supplies = [] %}
{% for entity_id in state_attr('group.hao_cai', 'entity_id') | default([]) %}
{% if state_exists(entity_id) and states(entity_id) | int(default=100) < 20 %}
{% set device_name = state_attr(entity_id, 'friendly_name') | default(entity_id) %}
{% set low_supplies = low_supplies + [device_name] %}
{% endif %}
{% endfor %}
{{ low_supplies | join(', ') }} 需更换
condition: >
{% set entities = state_attr('group.hao_cai', 'entity_id') | default([]) %}
{% for entity_id in entities %}
{% if state_exists(entity_id) and states(entity_id) | int(default=100) < 20 %}
true
{% endif %}
{% endfor %}
false
# 以下为每个耗材的单独芯片(仅在低于20%时显示)
# 净化器滤芯
- type: conditional
conditions:
- entity: sensor.xiaomi_cn_845289709_va3_filter_life_level_p_4_1
state: "<20"
chip:
type: template
entity: sensor.xiaomi_cn_845289709_va3_filter_life_level_p_4_1
icon: mdi:air-filter
icon_color: red
content: |
{% set state = states(entity) | int %}
净化器滤芯 ({{ state }}%)
tap_action:
action: more-info
# 双键无线开关电池
- type: conditional
conditions:
- entity: sensor.yeelink_cn_blt_3_1kq920if8ck00_contrl_battery_level_p_2_1003
state: "<20"
chip:
type: template
entity: sensor.yeelink_cn_blt_3_1kq920if8ck00_contrl_battery_level_p_2_1003
icon: mdi:battery-alert
icon_color: red
content: |
{% set state = states(entity) | int %}
双键无线开关电池 ({{ state }}%)
tap_action:
action: more-info
# 净水器复合滤芯
- type: conditional
conditions:
- entity: sensor.xiaomi_cn_822494120_lx40_filter_life_level_p_3_1
state: "<20"
chip:
type: template
entity: sensor.xiaomi_cn_822494120_lx40_filter_life_level_p_3_1
icon: mdi:water-filter
icon_color: red
content: |
{% set state = states(entity) | int %}
净水器复合滤芯 ({{ state }}%)
tap_action:
action: more-info
# 净水器RO反渗透滤芯
- type: conditional
conditions:
- entity: sensor.xiaomi_cn_822494120_lx40_filter_life_level_p_5_1
state: "<20"
chip:
type: template
entity: sensor.xiaomi_cn_822494120_lx40_filter_life_level_p_5_1
icon: mdi:water-filter
icon_color: red
content: |
{% set state = states(entity) | int %}
净水器RO反渗透滤芯 ({{ state }}%)
tap_action:
action: more-info
# 燃气灶电池
- type: conditional
conditions:
- entity: sensor.xiaomi_cn_821056281_ymv8_battery_level_p_6_1
state: "<20"
chip:
type: template
entity: sensor.xiaomi_cn_821056281_ymv8_battery_level_p_6_1
icon: mdi:stove
icon_color: red
content: |
{% set state = states(entity) | int %}
燃气灶电池 ({{ state }}%)
tap_action:
action: more-info
# 水浸卫士电池
- type: conditional
conditions:
- entity: sensor.lumi_cn_blt_3_1l1mgh9k8c800_bmcn01_battery_level_p_3_1
state: "<20"
chip:
type: template
entity: sensor.lumi_cn_blt_3_1l1mgh9k8c800_bmcn01_battery_level_p_3_1
icon: mdi:water-alert
icon_color: red
content: |
{% set state = states(entity) | int %}
水浸卫士电池 ({{ state }}%)
tap_action:
action: more-info
# 温湿度计电池
- type: conditional
conditions:
- entity: sensor.miaomiaoc_cn_blt_3_1kpg1kpmckc01_t9_battery_level_p_2_1003
state: "<20"
chip:
type: template
entity: sensor.miaomiaoc_cn_blt_3_1kpg1kpmckc01_t9_battery_level_p_2_1003
icon: mdi:thermometer
icon_color: red
content: |
{% set state = states(entity) | int %}
温湿度计电池 ({{ state }}%)
tap_action:
action: more-info
# 门锁电池
- type: conditional
conditions:
- entity: sensor.loock_cn_1142402576_t2pv1_battery_level_p_4_1003
state: "<20"
chip:
type: template
entity: sensor.loock_cn_1142402576_t2pv1_battery_level_p_4_1003
icon: mdi:lock
icon_color: red
content: |
{% set state = states(entity) | int %}
门锁电池 ({{ state }}%)
tap_action:
action: more-info
# 猫眼电池
- type: conditional
conditions:
- entity: sensor.loock_cn_1142402576_t2pv1_battery_level_p_10_1
state: "<20"
chip:
type: template
entity: sensor.loock_cn_1142402576_t2pv1_battery_level_p_10_1
icon: mdi:eye
icon_color: red
content: |
{% set state = states(entity) | int %}
猫眼电池 ({{ state }}%)
tap_action:
action: more-info
# 卡片样式微调
style: |
mushroom-shape-chip {
margin-right: 8px;
margin-bottom: 8px;
}
配置说明
1. 环境传感器配置
环境传感器部分包含四个芯片:
温度传感器:显示当前温度,颜色根据温度范围变化(蓝/绿/红)
湿度传感器:显示当前湿度,颜色根据湿度范围变化(橙/绿/紫)
PM10粉尘传感器:显示当前粉尘浓度,颜色根据浓度变化(绿/黄/橙)
甲醛传感器:显示当前甲醛浓度,颜色根据浓度变化(绿/黄/橙)
每个芯片都配置了:
对应的实体ID
适当的图标
颜色逻辑(根据数值变化)
显示内容格式
点击动作(查看详细信息)
2. 耗材监控核心逻辑
耗材监控部分采用条件覆盖方案:
默认状态:显示绿色对勾图标和"耗材充足"文字
条件覆盖:当检测到有耗材低于20%时,覆盖为红色叹号图标和需要更换的耗材名称列表
条件检测:遍历群组中的所有耗材实体,检查是否有低于20%的耗材
3. 单个耗材芯片
为每个耗材单独配置了一个条件芯片:
仅当耗材低于20%时显示
显示耗材名称和当前百分比
使用红色图标突出显示
点击可查看该耗材的详细信息
后期添加新耗材的方法
步骤1:将新耗材添加到群组
编辑 configuration.yaml
文件中的 hao_cai
群组,在 entities
列表中添加新的传感器实体ID:
yaml
group:
hao_cai:
name: 耗材监控
entities:
# 原有实体...
- sensor.new_supply_entity_id # 添加新的耗材传感器
保存文件后,重启Home Assistant使群组更新生效。
步骤2:添加新耗材的单独显示芯片
在卡片配置的"单个耗材芯片"部分(即最后一个芯片之后),添加一个新的条件芯片:
yaml
# 新耗材(示例)
- type: conditional
conditions:
- entity: sensor.new_supply_entity_id # 替换为你的实体ID
state: "<20" # 阈值设置
chip:
type: template
entity: sensor.new_supply_entity_id
icon: mdi:icon-name # 选择合适的图标
icon_color: |
{% set level = states(entity) | int %}
{% if level < 10 %} red
{% elif level < 20 %} orange
{% else %} green
{% endif %}
content: |
{% set state = states(entity) | int %}
耗材名称 ({{ state }}%) # 替换为你的耗材名称
tap_action:
action: more-info
步骤3:调整阈值(可选)
如果需要调整耗材不足的阈值(默认为20%),需要修改两处:
在群组条件覆盖部分的
<20
修改为你的新阈值在每个耗材芯片的条件部分修改阈值
高级定制选项
1. 添加多级颜色警告
修改单个耗材芯片的 icon_color
部分,实现多级警告:
yaml
icon_color: |
{% set level = states(entity) | int %}
{% if level < 10 %} red # 严重警告
{% elif level < 20 %} orange # 警告
{% elif level < 30 %} yellow # 注意
{% else %} green # 正常
{% endif %}
2. 添加健康状态指示器
在耗材芯片内容中添加健康状态指示:
yaml
content: |
{% set level = states(entity) | int %}
{{ state_attr(entity, 'friendly_name') }} ({{ level }}%)
{% if level < 10 %} ⚠️{% endif %} # 严重不足时显示警告符号
3. 添加最后更新时间
在耗材芯片内容中显示最后更新时间:
yaml
content: |
{% set level = states(entity) | int %}
{% set last_updated = state_attr(entity, 'last_updated') %}
{{ state_attr(entity, 'friendly_name') }} ({{ level }}%)
<br><small>{{ last_updated | as_datetime | default('未知') }}</small>
常见问题解决
实体ID不匹配:
确保所有实体ID与您系统中的实际ID一致
在开发者工具 > 状态中检查实体ID
图标不显示:
确保使用的图标名称是有效的Material Design图标
访问 https://materialdesignicons.com/ 查找合适的图标
耗材状态不更新:
检查耗材传感器的更新频率
在卡片配置中添加
update_interval: 30
(30秒更新一次)
群组不显示:
确保群组配置正确且在重启后生效
在开发者工具 > 状态中检查
group.hao_cai
是否存在
总结
这个环境监测与耗材管理卡片方案具有以下优点:
直观显示:环境数据和耗材状态一目了然
智能预警:耗材不足时自动突出显示
交互性强:点击可查看详细信息
高度可定制:可根据需要调整阈值、图标和显示内容
易于扩展:添加新耗材简单快捷
通过这个方案,您可以轻松监控家庭环境和设备耗材状态,及时了解需要更换的耗材,确保智能家居设备正常运行。