早上起床,窗帘自动拉开、咖啡机开始煮咖啡;下班回家,空调提前打开、灯光自动亮起;晚上睡觉,所有电器自动关闭、安防系统启动。这不是科幻电影,这是 Home Assistant 能给你的真实生活。本文手把手教你搭建一套稳定、强大的智能家居系统。
一、Home Assistant 是什么
Home Assistant(简称 HA)是一个开源、本地优先的智能家居平台。
核心特性
- 本地运行:不依赖云端,断网也能用,隐私安全
- 跨品牌:支持米家、HomeKit、Aqara、Tuya、Philips Hue 等上千种设备
- 强自动化:基于事件、条件、动作的自动化引擎
- 可扩展:3000+ 集成、5000+ 插件
- 可视化:Web 仪表盘、移动 App
与其他平台对比
| 平台 | 本地运行 | 跨品牌 | 自动化 | 隐私 |
|---|---|---|---|---|
| Home Assistant | ✅ | ✅ | ⭐⭐⭐⭐⭐ | ✅ |
| 米家 | ❌ | ⚠️ | ⭐⭐⭐ | ⚠️ |
| HomeKit | ⚠️ | ⚠️ | ⭐⭐ | ✅ |
| Alexa / Google Home | ❌ | ⚠️ | ⭐⭐⭐ | ❌ |
二、硬件准备
主机选择
| 方案 | 优势 | 劣势 | 推荐 |
|---|---|---|---|
| Raspberry Pi 4/5 | 功耗低、社区活跃 | 性能一般 | ⭐⭐⭐⭐ |
| 小米路由器/迷你主机 | 性价比高 | 配置低 | ⭐⭐⭐ |
| NUC/i3 软路由 | 性能强 | 功耗高 | ⭐⭐⭐⭐⭐ |
| 旧笔记本 | 零成本 | 体积大 | ⭐⭐⭐ |
| NAS(群晖/威联通) | 24h 运行 | 占用资源 | ⭐⭐⭐⭐ |
推荐:Raspberry Pi 4 (4GB) 或 NUC(i3/i5),前者 300 元左右,后者 800-1500。
必需配件
- Zigbee 网关:Zigbee 设备需要(推荐 SONOFF Zigbee 3.0 USB Dongle Plus,80 元)
- 红外遥控器:控制空调、电视(Broadlink RM Mini 3)
- 存储:32GB TF 卡(树莓派)或 SSD
- 网络:稳定 Wi-Fi
推荐设备清单(入门套件)
- 智能开关:Aqara 墙壁开关(86 型)
- 智能灯泡:Yeelight LED 彩光灯泡
- 人体传感器:Aqara 人体传感器
- 门窗传感器:Aqara 门窗磁
- 温湿度传感器:Aqara 温湿度
- 智能插座:米家智能插座
总价约 1000-1500 元可以覆盖客厅 + 卧室基础场景。
三、安装 Home Assistant
方案 1:树莓派 + HAOS(推荐新手)
- 下载 HAOS 镜像
- 用 Raspberry Pi Imager 烧录到 TF 卡
- 启动后自动运行,浏览器访问
http://homeassistant.local:8123
方案 2:Docker 部署(推荐进阶用户)
# 创建目录
mkdir -p /opt/homeassistant/config
# 创建 docker-compose.yml
cat > /opt/homeassistant/docker-compose.yml << 'EOF'
version: '3.8'
services:
homeassistant:
container_name: homeassistant
image: homeassistant/home-assistant:stable
restart: unless-stopped
network_mode: host
volumes:
- /opt/homeassistant/config:/config
- /etc/localtime:/etc/localtime:ro
devices:
- /dev/ttyUSB0:/dev/ttyUSB0 # Zigbee 网关
EOF
cd /opt/homeassistant
docker-compose up -d浏览器访问 http://IP:8123。
方案 3:群晖 NAS
套件中心直接搜索安装,最省事。
方案 4:Python venv(开发用)
sudo apt install -y python3 python3-dev python3-venv
python3 -m venv /opt/ha
source /opt/ha/bin/activate
pip install homeassistant
hass四、初始配置
1. 创建账户
首次访问 http://IP:8123,创建管理员账户。
2. 配置网络发现
设置 → 设备与服务 → 添加集成,HA 会自动发现局域网内的设备。
3. 配置 Zigbee
# configuration.yaml
zha:
usb_path: /dev/ttyUSB0
database_path: /config/zigbee.db把 Zigbee 适配器插入主机,HA 会自动识别。
4. 添加设备
设置 → 设备与服务 → 添加集成 → 选择品牌(米家/Aqara/Philips 等) → 输入账号 → 自动同步设备。
五、核心概念
1. 实体(Entity)
HA 里每个设备都是一个实体,比如:
light.bedroom(卧室灯)switch.kitchen_plug(厨房插座)sensor.living_room_temperature(客厅温度)
2. 状态(State)
每个实体有当前状态:
- 灯:on / off / 亮度 / 色温
- 传感器:温度数值、湿度数值
- 开关:on / off
3. 服务(Service)
调用服务改变状态:
light.turn_onswitch.toggleclimate.set_temperature
4. 自动化(Automation)
trigger: # 触发条件
condition: # 附加条件
action: # 动作六、自动化配置
1. 回家自动开灯(基础场景)
# configuration.yaml 或 UI 自动化
automation:
- alias: "回家自动开灯"
trigger:
- platform: state
entity_id: person.zhang_san
to: 'home'
action:
- service: light.turn_on
target:
entity_id: light.living_room2. 早晨唤醒(进阶)
automation:
- alias: "工作日早晨"
trigger:
- platform: time
at: "07:00:00"
condition:
- condition: time
weekday:
- mon
- tue
- wed
- thu
- fri
action:
- service: cover.open_cover # 拉开窗帘
target:
entity_id: cover.bedroom_curtain
- delay: "00:01:00"
- service: light.turn_on # 灯缓缓亮起
target:
entity_id: light.bedroom
data:
brightness_pct: 30
transition: 60
- delay: "00:05:00"
- service: light.turn_on # 灯全亮
target:
entity_id: light.bedroom
data:
brightness_pct: 100
- service: media_player.play_media # 播放新闻
target:
entity_id: media_player.bedroom_speaker
data:
media_content_id: "http://example.com/news.mp3"
media_content_type: "music"3. 离家模式
automation:
- alias: "全屋关闭"
trigger:
- platform: state
entity_id: person.zhang_san
to: 'not_home'
for: "00:05:00"
action:
- service: light.turn_off
target:
entity_id: all
- service: switch.turn_off
target:
entity_id: all
- service: climate.turn_off
target:
entity_id: all
- service: alarm_control_panel.alarm_arm_away # 启动安防
target:
entity_id: alarm_control_panel.home4. 联动控制(传感器触发)
automation:
- alias: "夜间起夜"
trigger:
- platform: state
entity_id: binary_sensor.bedroom_motion
to: 'on'
condition:
- condition: sun
after: sunset
before: sunrise
- condition: state
entity_id: input_boolean.sleep_mode
state: 'on'
action:
- service: light.turn_on
target:
entity_id: light.hallway
data:
brightness_pct: 5
color_temp_kelvin: 2700
- delay: "00:02:00"
- service: light.turn_off
target:
entity_id: light.hallway七、自定义仪表盘(LoveUI / Mushroom)
1. 安装 Lovelace
HA 默认就有,无需安装。
2. 推荐卡片:Mushroom
HACS 安装 Mushroom Cards,极简风格,响应式布局。
type: vertical-stack
cards:
- type: weather-forecast
entity: weather.home
- type: glance
entities:
- entity: light.living_room
- entity: switch.kitchen_plug
- entity: climate.bedroom_ac
- entity: sensor.living_room_temperature
- type: entities
entities:
- entity: person.zhang_san
- entity: sun.sun3. 自定义主题
# themes.yaml
midnight:
primary-color: "#5294E2"
accent-color: "#E45A65"
text-primary-color: "#FFFFFF"
card-background-color: "#383C45"
background-color: "#101216"configuration.yaml:
frontend:
themes:
midnight: !include_dir_merge_list themes/八、Node-RED 集成(可视化自动化)
安装 Node-RED
通过 HACS 或 Add-on 安装。
示例:复杂条件判断
Node-RED 用拖拽方式写自动化,比 YAML 更直观:
[时间触发] → [是工作日吗] → [是] → [有人在吗] → [是] → [开灯]
↓ 否 ↓ 否
跳过 跳过适合复杂场景:早上起床、离家、回家、睡眠 等大流程编排。
九、能源管理(高级玩法)
1. 太阳能监控
sensor:
- platform: integration
source: sensor.solar_power
name: solar_energy_total
unit_prefix: k
round: 22. 用电分析
energy:
# 实时功率
- source: sensor.grid_power
# 智能插座累计
- source: sensor.plug_kitchen3. 智能调度
automation:
- alias: "电价低谷充电"
trigger:
- platform: time
at: "00:00:00"
action:
- service: switch.turn_on
target:
entity_id: switch.ev_charger
- delay: "06:00:00"
- service: switch.turn_off
target:
entity_id: switch.ev_charger十、远程访问
1. Nabu Casa(官方,5$/月)
设置 → Home Assistant Cloud → 启用,一键远程。
2. 自建 FRP / Cloudflare Tunnel
# cloudflared tunnel
cloudflare:
- hostname: ha.example.com
service: http://localhost:81233. 内网穿透(FRP)
# frpc.ini
[homeassistant]
type = http
local_ip = 127.0.0.1
local_port = 8123
custom_domains = ha.example.com十一、语音助手集成
方案 1:小爱同学(米家设备联动)
通过 Xiaomi Miot Auto 集成,小爱可以直接控制 HA 设备。
方案 2:小度、天猫精灵
类似方案,各有集成包。
方案 3:HomePod / Alexa
原生支持 HA,Siri 可以控制 HA 设备。
方案 4:自建语音(ESP32 + Snowboy)
进阶玩法,完全本地语音识别。
十二、安全与备份
1. 定期备份
设置 → 系统 → 备份 → 自动备份,推荐每周一次,保留到云端。
2. 启用双因素认证
设置 → 个人资料 → 多重身份验证。
3. 暴露公网要谨慎
- 改默认端口
- 启用 HTTPS
- 配置 fail2ban
- 用 Cloudflare 代理
十三、常见问题
1. Zigbee 设备掉线
- 增加 Zigbee 路由器(智能插座、墙壁开关都是)
- 检查电池电量
- 排查 2.4G Wi-Fi 干扰
2. 米家设备延迟高
- 用 Aqara 替代米家(Aqara 走 Zigbee,本地通信)
- 把米家设备添加为”局域网控制”模式
3. 自动化不执行
检查自动化是否启用、追踪日志、查看触发条件是否满足。
4. 系统卡顿
- 减少 YAML 自动化,用 Node-RED
- 用 MariaDB 替代默认 SQLite
- 限制历史数据保留时间
十四、进阶玩法
1. 接入摄像头(RTSP)
camera:
- platform: generic
name: 前门摄像头
stream_source: rtsp://user:pass@192.168.1.100:554/stream配合 Frigate 做人脸识别、车牌识别。
2. 接入电视
控制小米电视、Apple TV,实现”看片场景”一键关灯、关窗帘。
3. 集成 ChatGPT
让 HA 可以通过语音问答天气、播报新闻。
4. 自动化场景模板
- 观影场景:关主灯、开氛围灯、关窗帘、降幕布
- 晚餐场景:餐厅主灯、暖色温、BGM 播放
- 派对场景:全屋彩光、动感音乐、随机颜色变换
- 睡眠场景:关灯、关电器、启动安防、调节空调
十五、推荐资源
社区
视频
- B 站搜”Home Assistant”有大量教程
- YouTube:Everything Smart Home 频道
- HA 官方 YouTube
书籍
- 《Home Assistant 智能家居实战》(中文社区编写)
- 《智能家居系统搭建从入门到精通》
总结
Home Assistant 是一个一入深似海的项目,从几十块的温湿度传感器到几千块的门锁摄像头,都能整合到同一个平台。
入门建议:
- 从 Docker 部署开始,熟悉配置
- 买一套基础设备(灯 + 传感器 + 网关)
- 从一个简单自动化开始(回家开灯)
- 逐步扩展(场景、语音、远程)
真正的智能不是堆设备,而是让设备消失在场景里。当你不再掏出手机操作,生活自动按照你的习惯运转,这就是智能家居的最高境界。
愿你早日享受”未来已来”的生活 ✨