Docker - Home Assistant Container Setup with MariaDB and InfluxDB 2.x


Here I describe my setup of the Home Assistant container with MariaDB and InfluxDB 2.x.

Dependencies

This Home Assistant installation uses a MariaDB database to store recorder data and an InfluxDB 2.x bucket to store history data.

  1. Make sure you have MariaDB running, for example by installing the Docker container. A database must be created for Home Assistant
  2. Make sure you have InfluxDB 2.x running, for example by installing the Docker container. A bucket must be created for Home Assistant

Installation

I run this Docker container on a Ubuntu VM that runs via [[ Proxmox VE ]]. I perform the following on the CLI.

Docker pull command:

docker pull homeassistant/home-assistant

Within my home folder I have created a Docker folder where I create a subfolder for each container:

cd ~
mkdir -p docker/homeassistant
cd docker/homeassistant

Also create a folder where the Home Assistant configuration files can be stored:

mkdir config

I use the Nano text editor to create a shell script:

sudo nano homeassistant_run.sh

With this shell script we are going to create the container. Copy the following into homeassistant_run.sh:

docker run -d \
 --name=homeassistant \
 --hostname=homeassistant \
 --privileged \
 --network=host \
 -v $PWD/config:/config \
 -v /var/run/docker.sock:/var/run/docker.sock \
 -v /home/USER/docker/influxdb/data:/influxdb_data \
 -e TZ=Europe/Amsterdam \
 -e "MYSQL_USER=USR" \
 -e "MYSQL_PASSWORD=PASS" \
 -e "MYSQL_DATABASE=homeassistant" \
 -e "MYSQL_HOST=192.168.x.xx" \
 --restart unless-stopped \
 homeassistant/home-assistant

If needed replace the following:

-v $PWD/config:/config
Choose the location for the Home Assistant configuration. In this example it is the config map we created

-v /var/run/docker.sock:/var/run/docker.sock
OPTIONAL: I have this volume set up because I’m using the Docker Monitor integration to monitor some Docker containers via HA. So it is not necessary to set up this volume if you do not use this integration.

-v /home/USER/docker/influxdb/data:/influxdb_data
OPTIONAL: Replace /home/USER/docker/influxdb/data with the full path to the InfluxDB data directory so you can monitor the size of the database with a sensor. This will only work if InfluxDB is on the same machine as your Home Assistant instance. So it is not necessary to set up this volume if you do not use this sensor.

-e TZ=Europe/Amsterdam
Pick the right timezone

-e “MYSQL_USER=USR”
Replace USR with your MariaDB username

-e “MYSQL_PASSWORD=PASS”
Replace PASS with your MariaDB password

-e “MYSQL_DATABASE=homeassistant”
Replace homeassistant with the name of your Home Assistant MariaDB database

-e “MYSQL_HOST=192.168.x.xx”
Replace 192.168.x.xx with the IP address of your MariaDB host

Exit Nano (CTRL-X) and save the changes.

Now create the container:

sudo sh homeassistant_run.sh

Check if the container is running properly.

See also my notes about updating containers with Portainer or via the CLI. With Synology’s Docker Application, updating a container is also very easy.

Setup Recorder and History

Now we are going to tell the recorder to use the MariaDB database and for the history we will use InfluxDB. Go to the config directory:

cd config

Create secrets.yaml:

sudo nano secrets.yaml

Add the MariaDB url and InfluxDB token:

db_url: mysql://USER:PASS@192.168.x.xx:3306/homeassistant?charset=utf8mb4
influxdb_token: TOKEN

Replace the following:

db_url: mysql://USER:PASS@192.168.x.xx:3306/DATABASE?charset=utf8mb4
Replace with your MariaDB configuration (USER, PASS, 192.168.x.xx and DATABASE)

influxdb_token: TOKEN
Replace with your token. The token can be easily obtained through InfluxDB’s web interface

Exit Nano (CTRL-X) and save the changes.

Edit configuration.yaml:

sudo nano configuration.yaml

Add the recorder and history configuration:

recorder:
  purge_keep_days: 30
  auto_purge: true
  db_url: !secret db_url
  exclude:
    entities:
      - person.myname

history:

influxdb:
  api_version: 2
  ssl: false
  host: 192.168.x.xx
  port: 3004
  token: !secret influxdb_token
  organization: ubuntuvm
  bucket: homeassistant
  tags:
    source: HomeAssistant
  tags_attributes:
    - friendly_name
  default_measurement: units
  exclude:
    entities:
      - person.myname

If needed, replace the following:

purge_keep_days: 30
Replace 30 with the number of history days to keep in the recorder database after a purge

person.myname (mentioned twice in the configuration!)
This is an example of an entity that does not need to be saved. Replace myname with the Home Assistant user or remove the exclude part

host: 192.168.x.xx
Replace 192.168.x.xx with your InfluxDB host IP address

port: 3004
Replace 3004 with your InfluxDB host port

organization: ubuntuvm
Replace ubuntuvm with your InfluxDB organization name

bucket: homeassistant
Replace homeassistant with your InfluxDB bucket name

Exit Nano (CTRL-X) and save the changes.

Optionally you can setup sensors to monitor the database size of MariaDB and InfluxDB2

Using Home Assistant

Now restart the Docker container:

docker restart homeassistant

If everything went well, Home Assistant will now use MariaDB for the recorder and InfluxDB for the history.

Home Assistant can now be accessed via the following URL:

http://<IP DOCKER HOST>:8123/

Optionally you can install Visual Studio Code Server to edit the yaml configuration files via the browser

Optionally you can install Node-RED to assist in setting up automation!

Now make the necessary settings and have fun using Home Assistant.


Read other notes

Comments

    No comments found for this note.

    Join the discussion for this note on this ticket. Comments appear on this page instantly.

    Tags


    Notes mentioning this note


    Notes Graph