Home Assistant and ESPhome in Docker

Home Assistant and ESPhome in Docker

Summary:

I will show step by step how to install and configure Home Assistant with ESPHome Server in Docker containers as the main service. I will install Home Assistant (no supervisor) as I don’t feel it is necessary to keep all the functionality I had in the Home Assistant Operating System on my Raspberry Pi 4. Also, it is one of the recommended methods in Home Assistant Alternative Installations . Important! Installing Home Assistant OS Supervisor on a container with an operating system running, for example: Ubuntu, is not currently recommended and would increase the resource consumption of our server unnecessarily. We must bear in mind that in a server we must be as efficient as possible for different reasons such as: energy efficiency, stability, durability, compatibility, among others.

Recommendations:

  1. Have knowledge of the operation of Home Assistant as in its configuration.
  2. Have knowledge in the use, configuration and operation of ESPHome.
  3. Have knowledge of Docker.
  4. Have knowledge in ssh, in bash. Well, basically everything you need to be able to manage a server and these specific services.
  5. I recommend having knowledge of online text editors such as Vim. But it is not essential. The essential thing is that you know how to communicate with your server and edit the files. This can be FTP server, Visual Studio Code over SSH, or whatever method you prefer.

Introduction:

By setting up a server at home I realized that I could unify all my services on a single computer using containers.

Why Home Assistant on a server?

Keep in mind that Raspberry Pi is an excellent solution for a myriad of projects. So much so that it is the standard and recommended method by Home Assistant and that I must recognize; It works perfectly on this board. In addition, and not a minor fact, it is a very, very efficient hardware.
But unfortunately it is not scalable and that makes it more susceptible to the passage of time. As is, for example, the use of flash memories. Although today they are getting better, they are still inferior to the technologies that we can apply to a tower. Because in my server tower, I have more power, resources are not an issue here. So much in processing, which translates into the speed of my assistant. As in storage that translates into the most stable and extensive databases.

Why Home Assistant in Docker?

First, because Docker is an open source project that automates the deployment of applications within the most popular software containers today. It is supported by Home Assistant and not least: there is a lot of documentation.
Also due to the characteristics of the use of containers, such as: potability, simplicity to upload, download, duplicate, support and experiment with services with low risk and high flexibility. Among many other benefits of the world of containers.

Think, in my case: On a single server I have Home Assistant, ESPHome, Unify Controller, Nextcloud and web’s among others.

Step by step Home Assistant:

Everything stated here refers to my experience to a server running Ubuntu Server with Docker.

ports:

It is important that you manage your firewall well and take into account which posts you must open. Let’s start with the minimum for main functionalities and that you should not forget.

sudo ufw allow 8123
sudo ufw allow 6052

install Home Assistant on docker:

As easy as creating a .yml file in a specific directory for example, let’s try one, keep in mind that my_user is your directory and you must have the necessary read and write permissions:

#in your working directory example  /home/my_user/
mkdir -p dockercompose_backups/homeassistant/ && mkdir -p dockercompose_backups/esphome/

We create a file for docker compose:

#in your working directory example /home/my_user/
vim /home/my_user/dockercompose_backups/homeassistant/docker-compose.yml

Note: You know that you can use the text editor of your choice.

Inside this file we paste:

version: '3'
services:
  homeassistant:
    container_name: homeassistant
    image: "ghcr.io/home-assistant/home-assistant:stable"
    volumes:
      - /PATH_TO_YOUR_CONFIG:/config
      - /etc/localtime:/etc/localtime:ro
    restart: unless-stopped
    privileged: true
    network_mode: host

/PATH_TO_YOUR_CONFIG is the directory where we must mount the root volume of our Home Assistant. This is where our setup will be. It is a directory that we should always back up with Rsync for example.

Then we save. For example suppose that in the docker-compose.yml we put the line in volumes: similar to this:

- /home/my_user/homeassistant/config:/config

There we will have to paste our Home Assistant files, the configuration that comes from our Raspberry Pi.

Note: We haven’t started the container.

Restore Home Assistant:

I am going to describe according to my experience, which was the fastest method to move my complete and quite extensive configuration from my Rasberry Pi to my Home Assistant Docker. Since to date the official image for Docker does not allow restoring with the classic steps, although it tentatively allows us to create snapshots.

Backup:

To start we must have our backup image created in Home Assistant OS from Rasberry Pi. This will be a .tar file. Preferably when we make the snapshot we avoid including the extensions, since they will not be of any use to us in Docker. Note: To avoid errors, it is better to have our Home Assistant to the latest version on the Rasberry Pi. Then when we have our example backup: backup.tar on our personal computer. We unzip it.

  1. We look inside this and we will have a compressed file called: homeassistant.tar.gz
  2. We unzip this and inside we will have a directory called data. Ualá, there is all our configuration.
  3. We change the name of “data” to “config”.
  4. We create the directory where our volume will be. That is, the directory set in the “docker-compose.yml”. For the example it would be:
#in your working directory example /home/my_user/
mkdir homeassistant
  1. We send our brand new config folder inside the homeassistant directory we just created. That is, we must have the following: /home/my_user/homeassistant/config Where config will have all the content we had in data. Try to use a reliable transfer method. And don’t forget that inside config there are hidden directories and files that you should also transfer. I used FTP over TLS in FileZilla.

Start Home Assistant container:

Finally we start our container:

cd /dockercompose_backups/homeassistant/
docker-compose up -d
#It could also be depending on the version of docker compose.
docker compose up -d

Everything should be working if we do everything correctly. Home Assistantant will be at:

  • IP_server:8123

When launching the container with the configuration that we had on the Raspberry Pi, it could indicate an error in the logs that we will discuss at the end.

Step by step ESPHome:

Backup ESPhome:

In our Home Assistant OS on the Raspberry Pi and by using the app for example “File Editor” we download all the .yaml files inside the esphome folder. Each of these .yaml must represent a device. eg: my_node.yaml Let’s not forget to download the file: secrets.yaml and if we have a directory with fonts and/or images. We put everything inside a directory called config on our personal computer.

Now on our server we create a directory for the ESPHome configuration as we did with Home Assistant:

#in your working directory example /home/my_user/
mkdir esphome

As in the case of Home Assistant we must put the config folder that contains all our configuration files for each node, secrets and others inside the esphome directory on the server. That is, we must have the following:
/home/my_user/esphome/config

We start ESPHome container:

We create a file for docker, compose following the example:

#in your working directory example /home/my_user/
vim /home/my_user/dockercompose_backups/esphome/docker-compose.yml

Inside this file we paste:

version: '3'
services:
  esphome:
    container_name: esphome
    image: esphome/esphome
    volumes:
      - /path/to/esphome/config:/config
      - /etc/localtime:/etc/localtime:ro
    restart: always
    privileged: true
    network_mode: host

/path/to/esphome/config is the directory where we must mount the config volume of our ESPHOME. Now this will be where our configuration will always be.

Save the docker compose file.

source: Getting Started with the ESPHome Command Line

For example suppose that in the docker-compose.yml we put the line in volumes: similar to this:

- /home/my_user/esphome/config:/config

There we will have all the configuration that comes from our Raspberry Pi from our ESPHome.

Finally we start our second container:

cd /dockercompose_backups/esphome/
docker-compose up -d
#It could also be depending on the version of docker compose.
docker compose up -d

Update nodes:

Once our EspHome Server is installed and configured. We can find the dashboard at:

  • IP_server:6052

If everything went well there we should find our connected devices. And surely pending an update.

Bux fixies:

After starting my container with Home Assistant I found some errors in the log.

A resolution:

  1. Stop Home Assistant:
docker container stop homeassistant
  1. In the folder .storage, find the file: core.config_entries
    you have a backup the config files?
  2. Now delete de nexts lines:
      {
        "entry_id": "your_id",
        "version": 1,
        "domain": "raspberry_pi",
        "title": "Raspberry Pi",
        "data": {},
        "options": {},
        "pref_disable_new_entities": false,
        "pref_disable_polling": false,
        "source": "system",
        "unique_id": null,
        "disabled_by": null
      },
  1. Start the container
docker container start homeassistant

Great! that worked for me.

12 Likes

This is a good start for a Docker setup manual for running everything that is required for a docker setup.
Just a couple notes of differences that I did in my setup.

  1. I have a USB Zigbee unit and Bluetooth adapter, therefore I exposed the usb to the container with the compose file
devices:
  - /dev/ttyUSB0:/dev/ttyUSB0
  - /dev/ttyUSB1:/dev/ttyUSB1
1 Like

Hi this is a very good tuto. Why do you create a docker compose per docker ? On my side, I have a unique docker compose with all the dockers
Cheers
Phil

2 Likes

That exposing the USBs is very good, I didn’t include it in the tutorial since I haven’t tried it yet for my ESPhome service. But it is useful for setting up new ESPboards. I will surely include it. Thank you for your comment and your contribution!

I hope I understand your question well and I find it very interesting.

Having everything in a single docker compose file is a good option.

But, I use a docker file for each service because I like to manually update my containers without dropping the other. For example, I only want to update ESPHome without stopping Home Assistant. It’s just a habit. Thank you for your question and your contribution!

I do make upgrade on any docker container without stopping any
This is true for esphome, Mariadb, influx, etc ….
Sometimes, I just have some log messages in the HA traces but without any issue
Phil

2 Likes

@Juan_Diego_Tortul - A great guide indeed. Could you pls make a guide for installing HA on Android for 2022? I tried following this guide but got stuck at a point. Would be really helpful to many users who want to run it on their old phones.
Thanks

1 Like

If true, over time I will have more optimal and summarized upgrade steps. I am still studying the method that would be comfortable and safe for me. And so build an update protocol.
What I most wanted to show in the ‘new topic’ is how you can combine the official installation methods and how to backup your previous settings. Without complicating too much (simple to read).
It is for a good start.
At first I had some difficulty porting my HA and ESPHome setup. I’m just looking for others to simplify the concept of moving. Once again, it is only a first step.
On a server there is always a lot to do and improve.
Thank you for all your contributions.
You can share your method or your docker compose. Everything is well received by me!

I wish I could help you, but I’m not familiar with using Android as a server. What step failed you?

Pls check my post here

Would really like someone to help me out as I think I m 70% done.

1 Like

Be patient and at some point you will achieve it or you will have new ideas. Luck!

can u give the method a try on ur Android phone?

Sorry to jump on an old thread, but you can actually manage specific services inside of your compose by specifying them in the command (ex: docker-compose down homeassistant). And bringing up all services with docker-compose upwill only restart those which have changed :slight_smile:

4 Likes

Indeed, there are many ways to manage Docker containers. For example, you can back up data, manage volumes, consumption and images. In this introductory guide to Home Assistant Docker containers, I have tried to use the simplest possible commands based on my experience.

The world of Docker is vast and each user must explore it and adapt it to their needs.
Therefore, it has not been considered appropriate to continue adding commands in this guide.
The decision to have a home automation server in Docker is a great responsibility that requires knowledge, as Home Assistant and ESPHome are very important server.
Each user must assess their situation and deepen their knowledge of container use according to their needs.

Hi,

When you have multiple USB they might swap places.
So I recommend the USB device ID and not the assigned USB port (USBx) because when there are more than 1 USB devices attached on a system reboot assigned USB ports can (and will) change. UDEV can be used also but requires extra work outside the container. Command to show USB devices by ID:

sudo ls -la /dev/serial/by-id

For your docker-container use a logical USB name.

devices:
- /dev/serial/by-id/usb-FTDI_FT232R_USB_UART_AK2NEAM6-if00-port0:/dev/ttyUSB_DSMR

Good advice. I have both USB devices exposed as I have both a coral for frigate and a TI zigbee cordinator for ZHA. Can I assume this method will work for both devices?

First question would be “Did you experience it yourself?”

swapping USB port will not happen much, the first time it happened to me last summer I was off course on holiday. Likely my Linux-settings with automatic updates is the root cause why it happened because this process can also initiate a reboot.

I think this is a or mayby the solution to avoid it, so try it out. Your Coral for frigate will be a good “sandbox” and despite you need to recreate this container if you do not change the second part (likely :/dev/ttyUSB2), you do not need to change any Frigate settings. You can always change this later to a more meaningful and non-confusing name.

BTW About

privileged: true

There are topics which advise you not to use this. If I understand it correctly with my NOOBS/DUMMY limited Linix/Docker knowledge your container with this setting will have access to more(/everything?) than the container itself.

This is one of those settings I have had for as long as I can remember, but I don’t remember why I had it enabled. I am in the process of updating this.

Where do I put the fonts required for ESPHome? In my case the Docker data is stored in appdata on UnRaid, but do I store the fonts in the ESPHome folder, the Home Assistant folder or a Font folder in the parent directory?

The ESP32 device tutorial talks about using the File Editor in Home Assistant from Settings/Add-ons to set this up, but I don’t see an add-ons option under settings?

EDIT: Ok, all sorted