2026年3月3日
Home Assistant 智能家居 - 开源自动化平台指南
Home Assistant 是一款开源的智能家居自动化平台,可以整合各种智能设备,实现统一管理和自动化控制。
Home Assistant 简介
什么是 Home Assistant
Home Assistant 是一个免费开源的智能家居自动化平台,特点:
- 本地运行:数据隐私安全
- 设备整合:支持 1000+ 设备
- 自动化:强大的自动化引擎
- 移动端:iOS/Android 应用
- 社区活跃:丰富的插件和主题
支持的设备
| 类型 | 示例 |
|---|---|
| 灯光 | Yeelight、Philips Hue |
| 开关 | Sonoff、TP-Link |
| 传感器 | 温湿度、门窗传感器 |
| 摄像头 | 海康威视、萤石 |
| 音响 | 小爱同学、HomePod |
| 空调 | 米家、格力 |
安装方式
硬件要求
| 项目 | 最低配置 | 推荐配置 |
|---|---|---|
| CPU | 树莓派 3 | 树莓派 4 / NUC |
| 内存 | 1GB | 4GB+ |
| 存储 | 16GB SD | 64GB SSD |
安装方式对比
| 方式 | 难度 | 功能 |
|---|---|---|
| Home Assistant OS | 简单 | 完整 |
| Docker | 中等 | 核心 |
| Python 虚拟环境 | 复杂 | 核心 |
Home Assistant OS 安装
树莓派安装
- 下载镜像
https://www.home-assistant.io/installation/raspberrypi
- 烧录到 SD 卡
- 使用 Raspberry Pi Imager
- 或 Etcher
- 启动并访问
http://homeassistant.local:8123
Docker 安装
docker run -d \
--name homeassistant \
--privileged \
--restart=unless-stopped \
-e TZ=Asia/Shanghai \
-v /path/to/config:/config \
-v /run/dbus:/run/dbus:ro \
--network=host \
ghcr.io/home-assistant/home-assistant:stable
Docker Compose 安装
version: '3'
services:
homeassistant:
container_name: homeassistant
image: ghcr.io/home-assistant/home-assistant:stable
privileged: true
restart: unless-stopped
environment:
- TZ=Asia/Shanghai
volumes:
- ./config:/config
- /run/dbus:/run/dbus:ro
network_mode: host
初始配置
首次启动
- 访问
http://IP:8123 - 创建管理员账户
- 设置位置信息
- 自动发现设备
基础设置
修改时区
# configuration.yaml
homeassistant:
time_zone: Asia/Shanghai
设置单位
homeassistant:
unit_system: metric
temperature_unit: C
设备集成
添加集成
- 设置 -> 设备与服务 -> 添加集成
- 搜索设备品牌
- 按提示配置
常用集成
米家集成
# 配置小米米家设备
# 添加集成 -> Xiaomi Miio
# 输入小米账号密码
Yeelight
# 添加集成 -> Yeelight
# 自动发现或手动添加 IP
MQTT
# 添加集成 -> MQTT
# 配置 MQTT 服务器地址
手动配置设备
# configuration.yaml
light:
- platform: yeelight
devices:
192.168.1.100:
name: 客厅灯
transition: 1000
sensor:
- platform: mqtt
name: "温度"
state_topic: "home/sensor/temperature"
unit_of_measurement: "°C"
自动化配置
自动化基础
# automations.yaml
- alias: "晚上开灯"
trigger:
- platform: sun
event: sunset
action:
- service: light.turn_on
target:
entity_id: light.living_room
触发器类型
| 触发器 | 说明 |
|---|---|
| sun | 日出日落 |
| time | 定时触发 |
| state | 状态变化 |
| numeric_state | 数值条件 |
| template | 模板条件 |
| mqtt | MQTT 消息 |
| webhook | Webhook 调用 |
条件判断
- alias: "回家开灯"
trigger:
- platform: state
entity_id: device_tracker.phone
to: "home"
condition:
- condition: sun
before: sunset
action:
- service: light.turn_on
target:
entity_id: light.entrance
动作类型
action:
# 调用服务
- service: light.turn_on
target:
entity_id: light.living_room
data:
brightness: 255
# 延迟
- delay: "00:05:00"
# 条件动作
- choose:
- conditions:
- condition: state
entity_id: binary_sensor.night
state: "on"
sequence:
- service: light.turn_on
target:
entity_id: light.bedroom
自动化示例
离家模式
- alias: "离家模式"
trigger:
- platform: state
entity_id: group.family
to: "not_home"
action:
- service: light.turn_off
target:
entity_id: all
- service: switch.turn_off
target:
entity_id: group.all_switches
温度控制
- alias: "温度过高开空调"
trigger:
- platform: numeric_state
entity_id: sensor.temperature
above: 28
action:
- service: climate.set_temperature
target:
entity_id: climate.ac
data:
temperature: 26
hvac_mode: cool
仪表盘配置
Lovelace 界面
# ui-lovelace.yaml
title: 我的家
views:
- title: 首页
cards:
- type: entities
title: 客厅
entities:
- light.living_room
- switch.tv
- sensor.temperature
常用卡片
| 卡片 | 说明 |
|---|---|
| entities | 实体列表 |
| glance | 状态概览 |
| picture | 图片卡片 |
| weather | 天气卡片 |
| button | 按钮卡片 |
| gauge | 仪表盘 |
| history-graph | 历史图表 |
自定义卡片
# 安装 HACS 获取更多卡片
# 主题 -> HACS -> 前端 -> 浏览
语音控制
小爱同学集成
# 使用 Xiaomi Miot Auto 集成
# 或通过 MQTT 桥接
Google Assistant
# 配置 Google Assistant 集成
# 设置 -> 语音助手 -> Google Assistant
本地语音
# 使用 Wyoming 协议
# 安装 whisper + piper 实现本地语音识别
移动端配置
安装应用
- iOS:App Store 搜索 “Home Assistant”
- Android:Google Play 或 GitHub 下载
配置通知
# configuration.yaml
notify:
- name: mobile_app
platform: mobile_app
发送通知
- service: notify.mobile_app
data:
title: "提醒"
message: "门已打开"
进阶功能
脚本
# scripts.yaml
good_night:
alias: "晚安模式"
sequence:
- service: light.turn_off
target:
entity_id: all
- service: climate.set_temperature
target:
entity_id: climate.ac
data:
temperature: 25
场景
# scenes.yaml
- name: "电影模式"
entities:
light.living_room:
state: on
brightness: 50
light.bedroom:
state: off
switch.tv:
state: on
模板传感器
sensor:
- platform: template
sensors:
battery_status:
friendly_name: "电池状态"
value_template: >
{% if states('sensor.battery') | int > 80 %}
充足
{% elif states('sensor.battery') | int > 50 %}
正常
{% else %}
低电量
{% endif %}
备份与恢复
手动备份
- 设置 -> 系统 -> 备份
- 创建备份
- 下载备份文件
自动备份
# configuration.yaml
automation:
- alias: "每周备份"
trigger:
- platform: time
at: "03:00:00"
condition:
- condition: time
weekday:
- sun
action:
- service: backup.create
恢复备份
- 全新安装 Home Assistant
- 访问
http://IP:8123 - 选择恢复备份
- 上传备份文件
常见问题
Q: 设备无法发现?
检查:
- 设备和 HA 在同一网络
- 设备是否支持本地控制
- 防火墙设置
Q: 自动化不执行?
检查:
- 触发条件是否满足
- 条件判断是否正确
- 查看自动化追踪
Q: 系统变慢?
优化:
- 减少历史记录保留时间
- 禁用不需要的集成
- 使用更快的存储
总结
Home Assistant 是构建智能家居的优秀选择:
- 开源免费
- 隐私安全
- 功能强大
- 社区活跃
建议:
- 从简单设备开始
- 逐步添加自动化
- 定期备份配置
- 参与社区交流
智能家居是一个持续优化的过程,享受 DIY 的乐趣!