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
services: [...] homeassistant: [...] depends_on: - mariadb - influxdb - mosquitto mosquitto: container_name: mosquitto image: eclipse-mosquitto restart: unless-stopped ports: - "1883:1883/tcp" environment: - TZ=Europe/Brussels volumes: - /opt/mosquitto/config:/mosquitto/config - /opt/mosquitto/data:/mosquitto/data - /opt/mosquitto/log:/mosquitto/log stdin_open: true tty: true
We added the
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 https://raw.githubusercontent.com/eclipse/mosquitto/master/mosquitto.conf
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 names
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 homeassistant:$7$101$q7VtJJ/E*******7$1I******************************************b/G**************************************A==
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
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.
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.
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 192.168.10.106:43153 on port 1883. 1662414198: New client connected from 192.168.10.106:43153 as 4atvQWWEyf2XpG3yy0kgOW (p1, c1, k60, u'homeassistant'). 1662414198: Client 4atvQWWEyf2XpG3yy0kgOW disconnected. 1662414198: New connection from 192.168.10.106:39605 on port 1883. 1662414198: New client connected from 192.168.10.106:39605 as 5gNbahjfE5DulEufquYcaa (p2, c1, k60, u'homeassistant').
In the next article, we’ll add Node-RED to our setup so we can make some visually appealing automations.