首页/AI 工程/home-assistant-best-practices
H

home-assistant-best-practices

by @homeassistant-aiv1.0.0
3.7(0)

Best practices for Home Assistant automations, helpers, scripts, device controls, and Lovelace dashboard configuration.

Home AssistantSmart HomeIoT AutomationHome AutomationBest PracticesGitHub
安装方式
npx skills add homeassistant-ai/skills --skill home-assistant-best-practices
compare_arrows

Before / After 效果对比

1
使用前
1编写冗长、难以理解的Home Assistant自动化脚本,可能包含重复逻辑或复杂的条件判断,导致维护成本高,且难以调试。
2例如,一个简单的灯光自动化,但逻辑分散:
3```yaml
4automation:
5  - alias: 'Turn on light if motion detected and it is dark'
6    trigger:
7      platform: state
8      entity_id: binary_sensor.motion_sensor
9      to: 'on'
10    condition:
11      condition: numeric_state
12      entity_id: sensor.light_level
13      below: 50
14    action:
15      service: light.turn_on
16      entity_id: light.living_room
17```
使用后
1遵循Home Assistant最佳实践,使用模板传感器、辅助器和更清晰的触发/条件/动作结构,使得自动化脚本更加简洁、高效和易于维护。
2例如,将光照判断封装为辅助器或模板传感器,使自动化更聚焦:
3```yaml
4# 假设已有一个辅助器 input_boolean.is_dark
5automation:
6  - alias: 'Turn on light if motion detected and it is dark (optimized)'
7    trigger:
8      platform: state
9      entity_id: binary_sensor.motion_sensor
10      to: 'on'
11    condition:
12      condition: state
13      entity_id: input_boolean.is_dark
14      state: 'on'
15    action:
16      service: light.turn_on
17      entity_id: light.living_room
18```

description SKILL.md


name: home-assistant-best-practices description: > Best practices for Home Assistant automations, helpers, scripts, device controls, and Lovelace dashboard configuration.

TRIGGER THIS SKILL WHEN:

  • Creating or editing HA automations, scripts, or scenes
  • Choosing between template sensors and built-in helpers
  • Writing or restructuring triggers, conditions, or automation modes
  • Setting up Zigbee button/remote automations (ZHA or Zigbee2MQTT)
  • Renaming entities or migrating device_id references to entity_id
  • Configuring Lovelace dashboard cards and selecting the right sensor or helper to feed them

SYMPTOMS THAT TRIGGER THIS SKILL:

  • Agent uses Jinja2 templates where native conditions, triggers, or helpers exist
  • Agent uses device_id instead of entity_id in triggers/actions
  • Agent modifies entity IDs or config objects without checking all consumers
  • Agent chooses wrong automation mode (e.g., single for motion lights)
  • Agent hard-codes min/max values or picks a raw sensor when a derived helper is more appropriate for a dashboard card metadata: version: "1.1.0"

Home Assistant Best Practices

Core principle: Use native Home Assistant constructs wherever possible. Templates bypass validation, fail silently at runtime, and make debugging opaque.

Decision Workflow

Follow this sequence when creating any automation:

0. Gate: modifying existing config?

If your change affects entity IDs or cross-component references — renaming entities, replacing template sensors with helpers, converting device triggers, or restructuring automations — read references/safe-refactoring.md first. That reference covers impact analysis, device-sibling discovery, and post-change verification. Complete its workflow before proceeding.

Steps 1-5 below apply to new config or pattern evaluation.

1. Check for native condition/trigger

Before writing any template, check references/automation-patterns.md for native alternatives.

Common substitutions:

  • {{ states('x') | float > 25 }}numeric_state condition with above: 25
  • {{ is_state('x', 'on') and is_state('y', 'on') }}condition: and with state conditions
  • {{ now().hour >= 9 }}condition: time with after: "09:00:00"
  • wait_template: "{{ is_state(...) }}"wait_for_trigger with state trigger (caveat: different behavior when state is already true — see references/safe-refactoring.md#trigger-restructuring)

2. Check for built-in helper or Template Helper

Before creating a template sensor, check references/helper-selection.md.

Common substitutions:

  • Sum/average multiple sensors → min_max integration
  • Binary any-on/all-on logic → group helper
  • Rate of change → derivative integration
  • Cross threshold detection → threshold integration
  • Consumption tracking → utility_meter helper

If no built-in helper fits, use a Template Helper — not YAML. Create it via the HA config flow (MCP tool or API) or via the UI: Settings → Devices & Services → Helpers → Create Helper → Template. Only write template: YAML if explicitly requested or if neither path is available.

3. Select correct automation mode

Default single mode is often wrong. See references/automation-patterns.md#automation-modes.

ScenarioMode
Motion light with timeoutrestart
Sequential processing (door locks)queued
Independent per-entity actionsparallel
One-shot notificationssingle

4. Use entity_id over device_id

device_id breaks when devices are re-added. See references/device-control.md.

Exception: Zigbee2MQTT autodiscovered device triggers are acceptable.

5. For Zigbee buttons/remotes

  • ZHA: Use event trigger with device_ieee (persistent)
  • Z2M: Use device trigger (autodiscovered) or mqtt trigger

See references/device-control.md#zigbee-buttonremote-patterns.


Critical Anti-Patterns

Anti-patternUse insteadWhyReference
condition: template with float > 25condition: numeric_stateValidated at load, not runtimereferences/automation-patterns.md#native-conditions
wait_template: "{{ is_state(...) }}"wait_for_trigger with state triggerEvent-driven, not polling; waits for change (see references/safe-refactoring.md#trigger-restructuring for semantic differences)references/automation-patterns.md#wait-actions
device_id in triggersentity_id (or device_ieee for ZHA)device_id breaks on re-addreferences/device-control.md#entity-id-vs-device-id
mode: single for motion lightsmode: restartRe-triggers must reset the timerreferences/automation-patterns.md#automation-modes
Template sensor for sum/meanmin_max helperDeclarative, handles unavailable statesreferences/helper-selection.md#numeric-aggregation
Template binary sensor with thresholdthreshold helperBuilt-in hysteresis supportreferences/helper-selection.md#threshold
Renaming entity IDs without impact analysisFollow references/safe-refactoring.md workflowRenames break dashboards, scripts, and scenes silentlyreferences/safe-refactoring.md#entity-renames
template: sensor/binary sensor in YAMLTemplate Helper (UI or config flow API)Requires file edit and config reload; harder to managereferences/template-guidelines.md

Reference Files

Read these when you need detailed information:

FileWhen to readKey sections
references/safe-refactoring.mdRenaming entities, replacing helpers, restructuring automations, or any modification to existing config#universal-workflow, #entity-renames, #helper-replacements, #trigger-restructuring
references/automation-patterns.mdWriting triggers, conditions, waits, or choosing automation modes#native-conditions, #trigger-types, #wait-actions, #automation-modes, #ifthen-vs-choose, #trigger-ids
references/helper-selection.mdDeciding whether to use a built-in helper vs template sensor#numeric-aggregation, #rate-and-change, #time-based-tracking, #counting-and-timing, #scheduling, #entity-grouping, #decision-matrix
references/template-guidelines.mdConfirming templates ARE appropriate for a use case#when-templates-are-appropriate, #when-to-avoid-templates, #template-sensor-best-practices, #common-patterns, #error-handling
references/device-control.mdWriting service calls, Zigbee button automations, or using target:#entity-id-vs-device-id, #service-calls-best-practices, #zigbee-buttonremote-patterns, #domain-specific-patterns
references/examples.yamlNeed compound examples combining multiple best practices

forum用户评价 (0)

发表评价

效果
易用性
文档
兼容性

暂无评价,来写第一条吧

统计数据

安装量926
评分3.7 / 5.0
版本1.0.0
更新日期2026年3月16日
对比案例1 组

用户评分

3.7(0)
5
0%
4
0%
3
0%
2
0%
1
0%

为此 Skill 评分

0.0

兼容平台

🔧Claude Code

时间线

创建2026年3月16日
最后更新2026年3月16日