We’re continuing where we left off after Part 3: MariaDB and InfluxDB. If you don’t know what this is all about, make sure you check the previous posts.

In this Part 4, we’ll be adding our first add-on. The Mosquitto MQTT broker will allow us to connect with a wide range of devices and I highly recommend you install it as well.

Install Mosquttio MQTT broker


As has become our habit so far, we start off by adding some more config to our docker-compose.yaml.

      - mariadb
      - influxdb
      - mosquitto
    container_name: mosquitto
    image: eclipse-mosquitto
    restart: unless-stopped
      - "1883:1883/tcp"
      - TZ=Europe/Brussels
      - /opt/mosquitto/config:/mosquitto/config
      - /opt/mosquitto/data:/mosquitto/data
      - /opt/mosquitto/log:/mosquitto/log
    stdin_open: true
    tty: true

We added the stdin_open and tty lines so we can later connect to the terminal of the container to run some commands.

Note that we also added a dependency on the mosquitto container for our Home Assistant container. This way our broker is online before Home Assistant starts and we can be confident our connection won’t fail.


When we now launch the Mosquitto container using docker-compose up-d the logs will fill with errors about being Unable to open config file /mosquitto/config/mosquitto.conf. To fix this, we can download the default config file and store it in the newly created config directory.

cd /opt/mosquitto/config/
sudo wget

We’ll make a few changes however. You can add the following lines at the end of the config file as all settings are commented out (default setting) by default.

sudo nano mosquitto.conf
# add the following lines at the end

# Listen on port 1883 on all IPv4 interfaces
listener 1883
socket_domain ipv4
# save the in-memory database to disk
persistence true
persistence_location /mosquitto/data/
# Log to stderr and logfile
log_dest stderr
log_dest file /mosquitto/log/mosquitto.log
# Require authentication
allow_anonymous false
password_file /mosquitto/config/mqttuser

Mosquitto users

Next we’ll create a user for our Home Assistant instance to connect with the broker.

Ideally we later create a new user for every device (family). For example, a user account for our Shelly devices, one for Zigbee2MQTT, etc.

Run the following command to create a user named homeassistant. Note that the -c will create a new password file and will overwrite any existing file. So drop this parameter when you want to create a second account.

docker exec -it mosquitto mosquitto_passwd -c /mosquitto/config/mqttuser homeassistant

If you now check the content of the newly created mqttuser file, you’ll find your username and hashed password.

cat /opt/mosquitto/config/mqttuser

Restart Mosquitto

We can now restart the Mosquitto container so it loads our changes.

docker compose restart mosquitto

Our log file should show the Mosquitto container has started.

sudo cat /opt/mosquitto/log/mosquitto.log
1662413136: mosquitto version 2.0.15 starting
1662413136: Config loaded from /mosquitto/config/mosquitto.conf.
1662413136: Opening ipv4 listen socket on port 1883.
1662413136: mosquitto version 2.0.15 running
Screenshot of Portainer showing our running containers
Mosquitto container up and running

Configure MQTT in Home Assistant

We can now connect Home Assistant to our MQTT broker so it can start subscribing to topics and send messages. Since there are no other MQTT clients connected yet, Home Assistant won’t be addin new devices yet.

Set up a new integration - MQTT
Add an MQTT integration

We add a new MQTT integration to Home Assistant and enter the credentials we set up a minute ago to authenticate. Make sure you enter the IP address of your Docker system.

Enter MQTT credentials
Enter the creds of our HA MQTT user

The logs of our MQTT container confirm Home Assistant has successfully connected.

sudo cat /opt/mosquitto/log/mosquitto.log
1662413136: mosquitto version 2.0.15 running
1662414198: New connection from on port 1883.
1662414198: New client connected from as 4atvQWWEyf2XpG3yy0kgOW (p1, c1, k60, u'homeassistant').
1662414198: Client 4atvQWWEyf2XpG3yy0kgOW disconnected.
1662414198: New connection from on port 1883.
1662414198: New client connected from as 5gNbahjfE5DulEufquYcaa (p2, c1, k60, u'homeassistant').
MQTT integration installed
Our MQTT integration is setup

We can now add smart devices that communicate over MQTT like my favourite Shelly devices, or our Ginlong Solis sensor.

In the next article, we’ll add Node-RED to our setup so we can make some visually appealing automations.