HomeAssistant 教程

注意
本文最后更新于 2025-09-14,文中内容可能已过时。

安装HomeAssistant

1.安装UTM虚拟机

下载:https://mac.getutm.app/

/images/guides/HomeAssistant教程/1.png
(图1)

2.部署Debain镜像

下载:https://cdimage.debian.org/cdimage/archive/12.8.0/arm64/iso-cd/

/images/guides/HomeAssistant教程/2.png
(图2)

安装过程中所做的任何设置均可在安装完成后根据需要自行修改,因此请勿将安装阶段的配置视为最终的永久设置。

/images/guides/HomeAssistant教程/3.png
(图3)
/images/guides/HomeAssistant教程/4.png
(图4)
/images/guides/HomeAssistant教程/5.png
(图5)
/images/guides/HomeAssistant教程/6.png
(图6)
/images/guides/HomeAssistant教程/7.png
(图7)
/images/guides/HomeAssistant教程/8.png
(图8)
/images/guides/HomeAssistant教程/9.png
(图9)
/images/guides/HomeAssistant教程/10.png
(图10)
/images/guides/HomeAssistant教程/11.png
(图11)
/images/guides/HomeAssistant教程/12.png
(图12)
/images/guides/HomeAssistant教程/13.png
(图13)
/images/guides/HomeAssistant教程/14.png
(图14)
/images/guides/HomeAssistant教程/15.png
(图15)

设置主机 hostname,本文设置为 debian,可根据自身习惯或喜好进行设置。

/images/guides/HomeAssistant教程/16.png
(图16)

可以跳过 domain 设置

/images/guides/HomeAssistant教程/17.png
(图17)

设置 root 用户密码

/images/guides/HomeAssistant教程/18.png
(图18)

再次输入确认密码

/images/guides/HomeAssistant教程/19.png
(图19)

创建普通用户,本文设置为 piliqiu

/images/guides/HomeAssistant教程/20.png
(图20)

/images/guides/HomeAssistant教程/21.png
(图21)

设置普通用户密码

/images/guides/HomeAssistant教程/22.png
(图22)

确认普通用户密码

/images/guides/HomeAssistant教程/23.png
(图23)

选择时区,选择 Eastern,系统时区和时间可以在系统安装完毕后自行设置。

/images/guides/HomeAssistant教程/24.png
(图24)

进行硬盘分区,如果自身没有特殊需求默认即可。

/images/guides/HomeAssistant教程/25.png
(图25)

/images/guides/HomeAssistant教程/26.png
(图26)
/images/guides/HomeAssistant教程/27.png
(图27)
/images/guides/HomeAssistant教程/28.png
(图28)
/images/guides/HomeAssistant教程/29.png
(图29)
/images/guides/HomeAssistant教程/30.png
(图30)
/images/guides/HomeAssistant教程/31.png
(图31)
/images/guides/HomeAssistant教程/32.png
(图32)
/images/guides/HomeAssistant教程/33.png
(图33)

不打算安装图形界面,选择最后一项即可,为了远程方便同时选择了 SSH 组件

/images/guides/HomeAssistant教程/34.png
(图34)

安装完成重启

/images/guides/HomeAssistant教程/35.png
(图35)

清除 usb 驱动器,不再使用 usb 中的系统启动,使用已经安装好的系统启动。

/images/guides/HomeAssistant教程/36.png
(图36)

/images/guides/HomeAssistant教程/37.png
(图37)

网络改为桥接,接口每个都试一下,那个可以上网,就用那个接口。

/images/guides/HomeAssistant教程/38.png
(图38)


MacOS 系统中,无法将复制的内容粘贴到通过 UTM 虚拟机运行的 Debian 系统内。目前使用 WindTerm 通过 SSH 连接至 Debian 系统。

通过在 Debian 中执行 ip addr show 命令获取其 IPv4 地址,再使用 SSH 工具进行远程连接。

3.安装Docker环境

通过 DockerAPT 仓库安装

在开始前,先设置代理

1
2
3
# 只在本次会话生效
export http_proxy=http://IP:端口
export https_proxy=http://IP:端口

3.1 查看 Codename

1
2
3
4
5
6
7
8
# 查看你的 Debian 版本代号
lsb_release -a

# 输出示例
Distributor ID: Debian
Description:    Debian GNU/Linux 12 (bookworm)
Release:        12
Codename:       bookworm

📌 关键点:你需要记住 Codename(如 bookworm, bullseye),这是添加 Docker 软件源时要用到的。

常见 Debian 版本代号对照表

Debian 版本 代号(Codename)
Debian 12 bookworm
Debian 11 bullseye
Debian 10 buster

3.2 安装必要依赖工具

1
sudo apt install -y ca-certificates curl gnupg lsb-release

解释:

  • ca-certificates: 用于 HTTPS 证书验证
  • curl: 下载文件
  • gnupg: 导入 GPG 密钥
  • lsb-release: 获取发行版信息

3.3 Docker官方GPG公钥

创建密钥环目录并导入密钥:

1
2
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

3.4 添加Docker的APT软件源

使用前面查到的 Codename(比如 bookworm)来构建源地址。

执行以下命令(自动获取 codename):

1
2
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

如果你是 Debian 12 (bookworm),这条命令就会写入:

1
deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian bookworm stable

3.5 再次更新APT包索引

1
sudo apt update

因为我们刚刚添加了新的软件源,必须重新加载。


3.6 安装Docker引擎及相关组件

1
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

各组件说明:

组件 作用
docker-ce Docker 社区版主程序
docker-ce-cli Docker 命令行工具
containerd.io 容器运行时
docker-buildx-plugin 支持多平台构建
docker-compose-plugin 提供 docker compose 命令(v2)

3.7 验证Docker是否安装成功

1
2
3
4
5
# 检查版本
sudo docker --version

# 正常输出类似
Docker version 25.0.5, build xxxxxxx

4.安装Homeassistant

1
2
3
4
5
6
7
8
9
docker run -d \
  --name homeassistant \
  --privileged \
  --restart=unless-stopped \
  -e TZ=Asia/Shanghai \
  -v /PATH_TO_YOUR_CONFIG:/config \
  -v /run/dbus:/run/dbus:ro \
  --network=host \
  ghcr.io/home-assistant/home-assistant:stable

📌 记得替换 /PATH_TO_YOUR_CONFIG 为你的实际配置路径,例如:

1
-v /home/piliqiu/homeassistant/config:/config

安装后,访问 http://ip:8123 打开 HomeAssistant 初始化页面

如果用 Chrome 浏览器打不开,换成 Safari 浏览器就可以

解决:设置 -> 隐私与安全性 -> 本地网络 -> Google Chrome

HomeAssistant插件

国区应用商店(了解)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# 下载地址
https://get.hassbox.cn/hassbox_store.zip

# 进到 custom_components 目录下
cd homeassistant/config/custom_components/

# 创建文件夹
mkdir hassbox_store

# 进入目录
cd hassbox_store

# 上传压缩包解压
unzip hassbox_store.zip

# 重启 HA 服务生效

/images/guides/HomeAssistant教程/39.png
(图39)
/images/guides/HomeAssistant教程/40.png
(图40)

安装HACS

我映射的目录是 /home/piliqiu/homeassistant/config

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 1. 进入 config 目录
cd /home/piliqiu/homeassistant/config

# 2. 创建 custom_components(如果还没有)
mkdir -p custom_components/hacs

# 3. 进入 hacs 目录
cd custom_components/hacs

# 4. 下载 HACS 最新版本
curl -sL https://github.com/hacs/integration/releases/latest/download/hacs.zip -o hacs.zip

# 5. 安装 unzip(如果未安装)
sudo apt update && sudo apt install unzip -y

# 6. 解压并清理
unzip hacs.zip
rm hacs.zip

# 7. 修复权限,确保容器能读取
chmod -R 777 /home/piliqiu/homeassistant/config/*

# 8. 安装后需要重启容器

/images/guides/HomeAssistant教程/41.png
(图41)
/images/guides/HomeAssistant教程/42.png
(图42)
/images/guides/HomeAssistant教程/43.png
(图43)
/images/guides/HomeAssistant教程/44.png
(图44)
/images/guides/HomeAssistant教程/45.png
(图45)
/images/guides/HomeAssistant教程/46.png
(图46)

彩云天气

HACS 中搜索 彩云天气,安装 彩云天气Colorfulclouds Weather Card

/images/guides/HomeAssistant教程/47.png
(图47)

申请 彩云天气key

/images/guides/HomeAssistant教程/48.png
(图48)
/images/guides/HomeAssistant教程/49.png
(图49)
/images/guides/HomeAssistant教程/50.png
(图50)
/images/guides/HomeAssistant教程/51.png
(图51)
/images/guides/HomeAssistant教程/52.png
(图52)

免费用户是 1000 次,修改数据刷新间隔,延长使用时间,全部配置后,重启 homeassistant

/images/guides/HomeAssistant教程/53.png
(图53)

新增网页卡片

/images/guides/HomeAssistant教程/54.png
(图54)

/images/guides/HomeAssistant教程/55.png
(图55)
/images/guides/HomeAssistant教程/56.png
(图56)

翻页时钟

下载:https://github.com/liaoliao007/leoha/

之前 homeassistant 映射的目录是 /home/piliqiu/homeassistant/config

将仓库中 www 目录下的 clock 文件夹放到 /home/piliqiu/homeassistant/config/www 目录下

/images/guides/HomeAssistant教程/57.png
(图57)
1
2
3
4
5
6
7
8
/home/piliqiu/homeassistant/config/
├── ...(其他文件)
└── www/
    └── clock/
        ├── __init__.py
        ├── index.html
        ├── script.js
        └── style.css

/local/clock/index.html 中的 local 表示 www

/images/guides/HomeAssistant教程/58.png
(图58)

Windy天气

/images/guides/HomeAssistant教程/59.png
(图59)
/images/guides/HomeAssistant教程/60.png
(图60)

复制网页嵌入代码

/images/guides/HomeAssistant教程/61.png
(图61)

/images/guides/HomeAssistant教程/62.png
(图62)

从之前的嵌入代码中复制 src 中的内容粘贴到这里

/images/guides/HomeAssistant教程/63.png
(图63)

/images/guides/HomeAssistant教程/64.png
(图64)

TP-Link摄像头接入Frigate

安装MQTT

1
2
3
4
5
mqtt-server/
├── docker-compose.yml
├── mosquitto/
│   ├── mosquitto.conf
│   └── passwd       # 用户密码文件(可选)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
version: '3.8'

services:
  mosquitto:
    image: eclipse-mosquitto:2.0
    container_name: mosquitto
    restart: unless-stopped
    ports:
      - "1883:1883"    # MQTT 协议
      - "9001:9001"    # WebSocket 支持
      # - "8883:8883"  # TLS(可选,需配置证书)
    volumes:
      - ./mosquitto/mosquitto.conf:/mosquitto/config/mosquitto.conf
      # - ./mosquitto/passwd:/mosquitto/config/passwd  # 认证文件
      - mosquitto_data:/mosquitto/data
      - mosquitto_log:/mosquitto/log

    # 可选:设置时区
    environment:
      - TZ=Asia/Shanghai
    # 网络模式(可选)
    # network_mode: host

volumes:
  mosquitto_data:
  mosquitto_log:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 监听端口
listener 1883
protocol mqtt

# WebSocket 支持
listener 9001
protocol websockets

# 持久化设置
persistence true
persistence_location /mosquitto/data/

# 日志
log_dest file /mosquitto/log/mosquitto.log
log_type all
# 关闭时间戳(Docker 日志已有)
log_timestamp false

# ==================================================
# 🔐 认证配置(可选:开启用户名密码)
# ==================================================
allow_anonymous true    # 允许匿名访问
# password_file /mosquitto/config/passwd

# ==================================================
# 🔐 TLS 配置(可选:开启 HTTPS/WebSocket Secure)
# ==================================================
# listener 8883
# protocol mqtt
# cafile /mosquitto/config/certs/ca.crt
# certfile /mosquitto/config/certs/server.crt
# keyfile /mosquitto/config/certs/server.key
# require_certificate false

MQTT 开启用户名密码认证(可选)

步骤 1:生成密码文件

1
2
3
4
5
6
7
8
9
# 进入 mosquitto 目录
cd mqtt-server/mosquitto

# 创建 passwd 文件(第一次用 -c)
mosquitto_passwd -c passwd your_username

# 输入密码
# 再添加其他用户用(去掉 -c)
# mosquitto_passwd passwd another_user

步骤 2:取消 mosquitto.conf 中的认证注释

1
2
allow_anonymous false
password_file /mosquitto/config/passwd

开启 TLS 加密(可选)

如果你有证书,放入 mosquitto/certs/ 目录,并取消 mosquitto.confTLS 部分注释。

端口映射加上:

1
2
ports:
  - "8883:8883"

部署Frigate

1
2
3
4
frigate/
├── docker-compose.yml
├── config/
│   └── config.yml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
services:
  frigate:
    container_name: frigate
    restart: unless-stopped
    stop_grace_period: 30s
    image: docker.cnb.cool/frigate-cn/frigate:stable
    volumes:
      - ./config:/config
      - ./storage:/media/frigate
      - type: tmpfs # 可选:1GB内存,减少SSD/SD卡损耗
        target: /tmp/cache
        tmpfs:
          size: 1000000000
    ports:
      - "8971:8971"
      - "8554:8554" # RTSP 视频流
      - "5050:5000" # Web UI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
mqtt:
  enabled: true
  host: mosquitto
  port: 1883
  user: # 没有可以删除
  password: # 没有可以删除

# 录制配置,保留7天的陆续变动
record:
  enabled: true
  retain:
    days: 7
    mode: motion
  alerts:
    retain:
      days: 30
  detections:
    retain:
      days: 30
snapshots:
  enabled: true
  retain:
    default: 30


cameras:
  camera_name: # <--- 此处改为你摄像头的名称,仅支持英文数字下划线与连接符
    enabled: true
    ffmpeg:
      inputs:
        - path: rtsp://账号:密码@摄像头IP:554/stream1 # <--- 此处改为你摄像头的rtsp地址
          roles:
            - detect
            - record
    detect:
      enabled: true
      width: 3840 # <- 可选,默认Frigate会尝试自动检测分辨率
      height: 2160 # <- 可选,默认Frigate会尝试自动检测分辨率
    objects:
      filters:
        person:
          mask: 0.005,0.008,0.421,0.009,0.452,0.988,0.007,0.995
    motion:
      mask: 0.007,0.008,0.421,0.009,0.452,0.988,0.007,0.995

go2rtc:
  streams:
    tplink_dw: ## <- 这里的back为摄像头名称,根据你的实际情况进行调整
      - "ffmpeg:rtsp://账号:密码@摄像头IP:554/stream1/cam/realmonitor?channel=1&subtype=2#video=copy#audio=copy#audio=aac#hardware"

  
version: 0.16-0

rtsp://账号:密码@摄像头IP:554/stream1

  • 账号:密码:在 Frigate 部署后,访问 UI 平台,账号默认是 admin,密码自己设置
  • 摄像头IPTP-LINK物联 APP -> 摄像头 -> 更多设置 -> 关于此摄像机 -> IP地址

添加Frigate

1.安装 Advanced Camera CardFrigate

/images/guides/HomeAssistant教程/65.png
(图1)

2.添加 MQTT

/images/guides/HomeAssistant教程/66.png
(图2)

3.添加 Frigate

/images/guides/HomeAssistant教程/67.png
(图3)

4.地址填写 FrigateWeb UI 地址

/images/guides/HomeAssistant教程/68.png
(图4)

5.添加卡片

/images/guides/HomeAssistant教程/69.png
(图5)

6.首页效果

/images/guides/HomeAssistant教程/70.png
(图6)


0%