home-assistant-best-practices
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseHome Assistant Best Practices
Home Assistant 最佳实践
Core principle: Use native Home Assistant constructs wherever possible. Templates bypass validation, fail silently at runtime, and make debugging opaque.
核心原则: 尽可能使用 Home Assistant 的原生功能。模板会绕过验证,在运行时静默失败,且调试难度大。
Decision Workflow
决策流程
Follow this sequence when creating any automation:
创建任何自动化时,请遵循以下步骤:
0. Gate: modifying existing config?
0. 前置检查:是否修改现有配置?
If your change affects entity IDs or cross-component references — renaming entities, replacing template sensors with helpers, converting device triggers, or restructuring automations — read first. That reference covers impact analysis, device-sibling discovery, and post-change verification. Complete its workflow before proceeding.
references/safe-refactoring.mdSteps 1-5 below apply to new config or pattern evaluation.
如果你的更改涉及实体 ID 或跨组件引用——例如重命名实体、用辅助工具替换模板传感器、转换设备触发器或重构自动化——请先阅读 。该文档涵盖了影响分析、设备关联项发现及变更后验证。完成该文档中的流程后再继续操作。
references/safe-refactoring.md以下步骤 1-5 适用于新配置或模式评估。
1. Check for native condition/trigger
1. 检查是否有原生条件/触发器
Before writing any template, check for native alternatives.
references/automation-patterns.mdCommon substitutions:
- →
{{ states('x') | float > 25 }}condition withnumeric_stateabove: 25 - →
{{ is_state('x', 'on') and is_state('y', 'on') }}with state conditionscondition: and - →
{{ now().hour >= 9 }}withcondition: timeafter: "09:00:00" - →
wait_template: "{{ is_state(...) }}"with state trigger (caveat: different behavior when state is already true — seewait_for_trigger)references/safe-refactoring.md#trigger-restructuring
在编写任何模板之前,请查看 寻找原生替代方案。
references/automation-patterns.md常见替代示例:
- → 带
{{ states('x') | float > 25 }}的above: 25条件numeric_state - → 包含状态条件的
{{ is_state('x', 'on') and is_state('y', 'on') }}condition: and - → 带
{{ now().hour >= 9 }}的after: "09:00:00"condition: time - → 带状态触发器的
wait_template: "{{ is_state(...) }}"(注意:当状态已为真时行为不同——详见wait_for_trigger)references/safe-refactoring.md#trigger-restructuring
2. Check for built-in helper
2. 检查是否有内置辅助工具
Before creating a template sensor, check .
references/helper-selection.mdCommon substitutions:
- Sum/average multiple sensors → integration
min_max - Binary any-on/all-on logic → helper
group - Rate of change → integration
derivative - Cross threshold detection → integration
threshold - Consumption tracking → helper
utility_meter
在创建模板传感器之前,请查看 。
references/helper-selection.md常见替代示例:
- 多个传感器求和/取平均值 → 集成
min_max - 任意开启/全部开启的二进制逻辑 → 辅助工具
group - 变化率计算 → 集成
derivative - 跨阈值检测 → 集成
threshold - 消耗量追踪 → 辅助工具
utility_meter
3. Select correct automation mode
3. 选择正确的自动化模式
Default mode is often wrong. See .
singlereferences/automation-patterns.md#automation-modes| Scenario | Mode |
|---|---|
| Motion light with timeout | |
| Sequential processing (door locks) | |
| Independent per-entity actions | |
| One-shot notifications | |
默认的 模式通常并不合适。详见 。
singlereferences/automation-patterns.md#automation-modes| 场景 | 模式 |
|---|---|
| 带超时的感应灯 | |
| 顺序处理(门锁) | |
| 独立的单实体操作 | |
| 一次性通知 | |
4. Use entity_id over device_id
4. 使用 entity_id 而非 device_id
device_idreferences/device-control.mdException: Zigbee2MQTT autodiscovered device triggers are acceptable.
device_idreferences/device-control.md例外情况: Zigbee2MQTT 自动发现的设备触发器是可接受的。
5. For Zigbee buttons/remotes
5. Zigbee 按钮/遥控器配置
- ZHA: Use trigger with
event(persistent)device_ieee - Z2M: Use trigger (autodiscovered) or
devicetriggermqtt
See .
references/device-control.md#zigbee-buttonremote-patterns- ZHA: 使用带 (持久化标识)的
device_ieee触发器event - Z2M: 使用自动发现的 触发器或
device触发器mqtt
详见 。
references/device-control.md#zigbee-buttonremote-patternsCritical Anti-Patterns
关键反模式
| Anti-pattern | Use instead | Why | Reference |
|---|---|---|---|
| | Validated at load, not runtime | |
| | Event-driven, not polling; waits for change (see | |
| | device_id breaks on re-add | |
| | Re-triggers must reset the timer | |
| Template sensor for sum/mean | | Declarative, handles unavailable states | |
| Template binary sensor with threshold | | Built-in hysteresis support | |
| Renaming entity IDs without impact analysis | Follow | Renames break dashboards, scripts, and scenes silently | |
| 反模式 | 替代方案 | 原因 | 参考文档 |
|---|---|---|---|
带 | | 在加载时验证,而非运行时 | |
| 带状态触发器的 | 事件驱动而非轮询;等待状态变更(语义差异详见 | |
触发器中使用 | | device_id 在设备重新添加后会失效 | |
感应灯使用 | | 重新触发时需要重置计时器 | |
| 用模板传感器计算求和/平均值 | | 声明式配置,可处理不可用状态 | |
| 用模板二进制传感器实现阈值检测 | | 内置迟滞支持 | |
| 未做影响分析就重命名实体 ID | 遵循 | 重命名会静默破坏仪表板、脚本和场景 | |
Reference Files
参考文档
Read these when you need detailed information:
| File | When to read | Key sections |
|---|---|---|
| Renaming entities, replacing helpers, restructuring automations, or any modification to existing config | |
| Writing triggers, conditions, waits, or choosing automation modes | |
| Deciding whether to use a built-in helper vs template sensor | |
| Confirming templates ARE appropriate for a use case | |
| Writing service calls, Zigbee button automations, or using target: | |
| Need compound examples combining multiple best practices | — |
需要详细信息时,请阅读以下文档:
| 文档 | 阅读场景 | 核心章节 |
|---|---|---|
| 重命名实体、替换辅助工具、重构自动化或任何现有配置修改 | |
| 编写触发器、条件、等待逻辑或选择自动化模式 | |
| 决定使用内置辅助工具还是模板传感器 | |
| 确认模板是否适用于当前场景 | |
| 编写服务调用、Zigbee 按钮自动化或使用 target: | |
| 需要结合多种最佳实践的复合示例 | — |