Skip to main content

ha-mqtt

Install MQTT broker mosquitto as a Docker container

docker-compose.yml
# ... others

mosquitto:
image: eclipse-mosquitto
container_name: mosquitto
volumes:
- <path-to-the-local-store>/opt/mosquitto:/mosquitto
- <path-to-the-local-store>/opt/mosquitto/data:/mosquitto/data
- <path-to-the-local-store>/opt/mosquitto/log:/mosquitto/log
ports:
- 1883:1883
- 9001:9001

Create mosquitto.conf under <path-to-the-local-store>/opt/mosquitto/config:

mosquitto.conf
persistence true
persistence_location /mosquitto/data/
log_dest file /mosquitto/log/mosquitto.log
listener 1883

## Authentication ##
# allow_anonymous true

Then run sudo docker compose up -d to start the container.

Access the container shell:

sudo docker exec -it mosquitto /bin/sh
danger

mosquitto container does not support bash shell, use /bin/sh instead.

Reference: stakeoverflow

mosquitto_passwd -c /mosquitto/config/password.txt mosquitto@hass
Output
mosquitto_passwd -c /mosquitto/config/password.txt mosquitto@hass
Password:
Reenter password:
Warning: File /mosquitto/config/password.txt group is not root. Future versions will refuse to load this file./ #

Than we will see a file password.txt created under /mosquitto/config folder.

Now add the following line to mosquitto.conf:


allow_anonymous false
password_file /mosquitto/config/password.txt

Restart the container sudo docker restart mosquitto.

Now the MQTT broker is ready to be used in the server ip under port 1883.

The next step is to configure the Home Assistant to use this MQTT broker.

Add MQTT broker to Home Assistant integration

Go to Settings -> Devices & Integrations -> + Add Integration

Search for MQTT and click on it.

Fill the form with the following information:

  • Broker: server-ip
  • Port: 1883
  • Username: mosquitto@hass
  • Password: <password-set-on-prev-step>

Now Home Assistant is able to use the MQTT broker.