Skip to content

Home Assistant container setup with MariaDB and InfluxDB 2.x

Introduction

Home Assistant is open source home automation that puts local control and privacy first. Powered by a worldwide community of tinkerers and DIY enthusiasts. Perfect to run on a Raspberry Pi or a local server.

Requirements

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.

Setup

  1. Create the folders needed by the container

    Run the following commands inside your home folder:

    # Open your terminal application
    cd ~
    mkdir -p homeassistant/config
    cd homeassistant
  2. Create the script needed to run the container

    Save the following script as homeassistant_run.sh:

    homeassistant_run.sh
    # To create this script use your text editor application, for example Nano
    docker run -d \
    --name=homeassistant \
    --hostname=homeassistant \
    --privileged \
    --net=host \
    -v $PWD/config:/config \
    -v /home/<linux-username>/influxdb/data:/influxdb_data \
    -e "MYSQL_USER=<mariadb-username>" \
    -e "MYSQL_PASSWORD=<mariadb-password>" \
    -e "MYSQL_DATABASE=<database>" \
    -e "MYSQL_HOST=<mariadb-host-IP-address>" \
    -e TZ=Europe/Amsterdam \
    --restart unless-stopped \
    homeassistant/home-assistant
    # IMPORTANT: Please read the instructions below
    Instructions:
    • Optional Replace docker with podman if needed
    • Optional Replace --net=host with ports to pass through if you want to expose ports. Here is described how to check if ports are available
    • Optional Replace $PWD/config with the location of your config if needed. This can be a fileserver mount
    • Required Replace <linux-username> with your own username if you want to monitor the size of the database with a sensor. This will only work if InfluxDB is running on the same machine as your Home Assistant instance. So you can remove the whole line if you do not use this sensor
    • Required Replace <mariadb-username> with your MariaDB username
    • Required Replace <mariadb-password> with your MariaDB password
    • Required Replace <database> with your MariaDB database. For example homeassistant
    • Required Replace <mariadb-host-IP-address> with the IP address of your MariaDB host
    • Required Replace Europe/Amsterdam with your own timezone
  3. Run the script to create the container

    Run the following command:

    # Open your terminal application
    sudo sh homeassistant_run.sh

    The image home-assistant is automatically pulled and the container is created.

  4. Recorder and History setup

    Now we are going to tell the recorder to use the MariaDB database and for the history we will use InfluxDB. Create secrets.yaml and add the MariaDB url and InfluxDB token:

    homeassistant/config/secrets.yaml
    # To create this script use your text editor application, for example Nano
    db_url: mysql://<mariadb-username>:<mariadb-password>@<mariadb-host-IP-address>:3306/<database>?charset=utf8mb4
    influxdb_token: <influxdb-token>
    # IMPORTANT: Please read the instructions below
    Instructions:
    • Required Replace <mariadb-username> with your MariaDB username
    • Required Replace <mariadb-password> with your MariaDB password
    • Required Replace <database> with your MariaDB database. For example homeassistant
    • Required Replace <mariadb-host-IP-address> with the IP address of your MariaDB host
    • Required Replace <influxdb-token> with the InfluxDB token. The token can be easily obtained through InfluxDB’s web interface

    Edit configuration.yaml and add the recorder and history:

    homeassistant/config/configuration.yaml
    # To edit use your text editor application, for example Nano
    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: <influxdb-host-IP-address>
    port: <3004>
    token: !secret influxdb_token
    organization: <organization>
    bucket: <bucket>
    tags:
    source: HomeAssistant
    tags_attributes:
    - friendly_name
    default_measurement: units
    exclude:
    entities:
    - person.myname
    # IMPORTANT: Please read the instructions below
    Instructions:
    • Optional Replace <30> with the number of history days to keep in the recorder database after a purge
    • Required Replace myname (twice) with your Home Assistant user or remove the exclude part. This is an example of an entity that does not need to be saved to the recorder or history
    • Required Replace <influxdb-host-IP-address> with the IP address of your InfluxDB host
    • Required Replace <3004> with the port of your MariaDB host
    • Required Replace <organization> with your own organization name. I used the name of my virtual machine for example
    • Required Replace <bucket> with your own bucket name. I used homeassistant for example
  5. Check the results

    Restart the homeassistant container. Run the following command:

    # Open your terminal application
    sudo docker restart homeassistant

    If needed you can check if the container is running properly. Home Assistant will now use MariaDB for the recorder and InfluxDB for the history.

    Now you can use Home Assistant by opening a web browser and going to: http://localhost:8123. Replace localhost with the relevant IP address or FQDN if needed, and adjust the port if you changed it earlier.

Favorites

Comments

    No comments found for this note.

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

    Copyright 2021- Fiction Becomes Fact