---
tags: Home Assistant
---
# Part 1 - How to run Home Assistant on Raspberry Pi 4 in a Docker container
##### `Internet of Things` `Raspberry Pi 4` `Home Assistant` `Mosquitto Broker` `zewelor/bt-mqtt-gateway`
> [name=Mr. Akashic] [time=Wed, Jun 2, 2021]
## Requirements
* **安裝佈署工具docker和docker-compose**
在Raspberry Pi 4安裝docker和docker-compose這兩個佈署工具。安裝方式可在Raspberry Pi 4建立一個副檔名為.sh的檔案,在將下列的安裝docker和docker-compose的步驟複製並貼上.sh檔案,儲存後執行.sh檔。
{%gist awlchang/71412d18a4e05414e6ffea222a805c6d %}
{%gist awlchang/af8c7c8571fa54a0130734b949cea65e %}
安裝完成後,重新開機Raspberry Pi 4。
* **建立工作目錄**
等一下會用docker建立4個服務,分別是Home Assistant、Hass-configurator、Mosquitto Broker和zewelor/bt-mqtt-gateway。其中,除了Home Assistant在第一次執行服務時會自動建立自己的工作目錄。其於三個依照下列指令建立工作目錄與設定目錄存取權限。
- 建立Hass-configurator工作目錄
```bash=
#建立configurator-config資料夾
sudo mkdir /opt/configurator-config
#設定資料夾configurator-config的使用權限可以存放檔案
sudo chmod 777 /opt/configurator-config
#切換到configurator-config資料夾
cd /opt/configurator-config/
#建立settings.conf檔案,檔案內容為雙引號內的字串
sudo echo -e "{ \n \"BASEPATH\": \"/hass-config/\" \n}" > settings.conf
```
- 建立Mosquitto Broker工作目錄
建立一個副檔名為.sh的檔案,將下列建立Mosquitto Broker工作目錄的步驟複製並貼上.sh檔案,儲存後執行.sh檔。
{%gist awlchang/c21dbaab4984c9b8573aadab692afdd0 %}
- 建立zewelor/bt-mqtt-gateway工作目錄
```bash=
#切換到opt資料夾
cd /opt
#下載設定檔
sudo git clone https://github.com/awlchang/custom-workers-for-zewelor-bt-mqtt-gateway.git
#修改custom-workers-for-zewelor-bt-mqtt-gateway資料夾名稱
sudo mv /opt/custom-workers-for-zewelor-bt-mqtt-gateway /opt/bt-mqtt-gateway
#設定權限為可編輯
sudo chmod 777 -R /opt/bt-mqtt-gateway/
```
## Implementation
```bash=
#切換到Desktop
cd /home/pi/Desktop
#下載docker-compose.yml以執行home assistant的服務
sudo git clone https://github.com/awlchang/homeassistant-docker-compose.git
#切換到homeassistant-docker-compose資料夾
cd homeassistant-docker-compose/
#執行docker-compose.yml內的services
sudo docker-compose up -d
```
看到Terminal顯示如下畫面表示所有服務啟動成功。
```
Starting mosquitto ... done
Creating homeassistant ... done
Creating bt-mqtt-gateway ... done
Creating hass-configurator ... done
```
開啟瀏覽器輸入http://192.168.0.112:8123 (IP位址改成自己樹莓派上的IP位置),就可看到如下畫面,成功執行Home Assistant服務,完成本篇實作。

## Introduction
本篇文章說明如何使用Docker佈署Home Assistant服務在Raspberry Pi 4上執行。Home Assistant是一個整合多家廠牌的物聯網設備平台,透過該平台可以提供使用者三個主要的功能,觀察(with sensor)、控制(with actuator)和自動化(with rule)。
Home Assistant為不同的開發板或作業系統提供多種不同的安裝方式([詳細資訊](https://www.home-assistant.io/installation/)),本篇希望能夠快速佈署Home Assistant的服務,因此選用Docker的安裝方式,但以此方式安裝的Home Assistant只提供部分的功能服務,所以會在使用Docker佈署其他的服務與Home Assistant整合,且能夠提供物聯網情境運行時所需要的元素,如下圖所示。

其中,Mosquitto Broker、hass-configurator和bt-mqtt-gateway說明如下:
* Mosquitto Broker: 扮演MQTT Server的角色,使Home Assistant可以透過MQTT通訊的方式觀察感測器感測的資料或控制actuator。
* hass-configurator: 一個提供檔案管理的web app,主要用來線上修改Home Assistant的配置檔。
* bt-mqtt-gateway: 在藍牙和Wi-Fi(MQTT)設備之間轉傳訊息,讓客製化的藍牙設備經過bt-mqtt-gateway後,再以Wi-Fi的通訊方式讓Home Assistant實現設備的觀察、控制和自動化。
## Appendix
<span class="dark_orange">docker ps -a</span> 查看正在運行的Docker容器大小的指令,執行結果如下。
```
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ad4e3af562a7 homeassistant/raspberrypi4-homeassistant:stable "/init" 19 hours ago Up 19 hours homeassistant
6705dd20c38b causticlab/hass-configurator-docker:latest "/app/run.sh" 19 hours ago Up 19 hours 0.0.0.0:3218->3218/tcp, :::3218->3218/tcp hass-configurator
92f81b0a11f4 eclipse-mosquitto:2.0.10 "/docker-entrypoint.…" 47 hours ago Up 47 hours 0.0.0.0:1883->1883/tcp, :::1883->1883/tcp, 0.0.0.0:9001->9001/tcp, :::9001->9001/tcp mosquitto
31759ae8fb66 zewelor/bt-mqtt-gateway:latest "/bin/sh -c /start.sh" 2 days ago Up 2 days bt-mqtt-gateway
```
<span class="dark_orange">docker inspect [container_id or container_name]</span> 查看指定的Docker容器的詳細資訊,執行結果如下。
<span class="dark_orange">docker-compose exec [container_name] /bin/sh -c "cat /etc/*-release"</span> 查看指定的Docker容器的詳細資訊,執行結果如下。
check whether your distribution
```
[
{
"Id": "92f81b0a11f46ab531fcf834b75d4347010272a17721209c9a757a0184b106ab",
"Created": "2021-06-02T07:18:07.957298991Z",
"Path": "/docker-entrypoint.sh",
"Args": [
"/usr/sbin/mosquitto",
"-c",
"/mosquitto/config/mosquitto.conf"
],
......more
```
<span class="dark_orange">docker-compose logs --tail=[number] [container_id or container_name]</span> 查看指定的Docker容器的log,tail參數可設定只顯示最新n筆log,執行結果如下。
```
Attaching to hass-configurator
hass-configurator | INFO:2021-06-03 11:07:48,757:hass_configurator.configurator:192.168.0.113 - "GET /flUhRq6tzZclQEJ-Vdg-IuiaDsNc.woff2 HTTP/1.1" 200 -
hass-configurator | INFO:2021-06-03 11:07:48,855:hass_configurator.configurator:192.168.0.113 - "GET /api/file?filename=/hass-config/configuration.yaml HTTP/1.1" 200 -
hass-configurator | INFO:2021-06-03 11:07:48,949:hass_configurator.configurator:192.168.0.113 - "GET /api/listdir?path=. HTTP/1.1" 200 -
```
<span class="dark_orange">docker exec -it [container_id or container_name] sh</span> 進入已在運行的指定Docker容器裡面,略執行結果。
<span class="dark_orange">docker-compose exec [container_id or container_name] /bin/sh -c "cat /etc/*-release"</span> 執行指令查看Linux distribution,執行結果如下。
```
3.12.6
NAME="Alpine Linux"
ID=alpine
VERSION_ID=3.12.6
PRETTY_NAME="Alpine Linux v3.12"
HOME_URL="https://alpinelinux.org/"
BUG_REPORT_URL="https://bugs.alpinelinux.org/"
```
## Acknowledgements
1. [Home Assistant](https://www.home-assistant.io/)
2. [Docker and Docker Compose on Raspberry Pi OS](https://withblue.ink/2020/06/24/docker-and-docker-compose-on-raspberry-pi-os.html)
<style>
.dark_orange {
color: #FF8C00;
background:#F6F6F6;
border-radius:4px;
padding-right:6px;
padding-left:6px;
}
.sub_title {
font-size: 25px;
}
.blockquote {
background:#F6F6F6;
}
</style>