Install and adding MQTT server to the Home Assistant

Chelaru Adrian
5 min readDec 20, 2023

If you have a Home Assistant instance, then Mosquitto might come handy in connecting different IoT devices that support this protocol, like these that can run OSS firmware, Tasmota, ESPHome, OpenBeken, etc.

I use Home Assistant on Raspberry Pi, so I’ll show you how easy it is to set it up in a few steps. If you don’t have HASS installed, then check this article.

Install MQTT server

This process is straightforward, with a simple line in Terminal:

sudo apt-get install mosquitto mosquitto-clients 

And that’s it 🐧.

Now it’s important to secure access to the Mosquitto broker.

Create and configure personal cloud using Nextcloud

11 stories

Start the MQTT and add at boot

You can start MQTT now, and after the device boot up, using the built-in service:

sudo systemctl enable mosquitto
sudo systemctl start mosquitto

Create a user and a password

You can create a new username and password using the mosquitto_passwd. Replace the YOUR_MQTT_USER with the user you want to use.

sudo mosquitto_passwd -c /etc/mosquitto/passw YOUR_MQTT_USER

Set a password and remember it.

Now you should disable anonymous users:

echo -e "allow_anonymous false\npassword_file /etc/mosquitto/passw" | sudo tee -a sudo nano /etc/mosquitto/mosquitto.conf

Now restart the Mosquitto broker:

sudo systemctl restart mosquitto

Great, now let's configure Home Assistant!

Home Assistant MQTT set up

You can connect to the MQTT broker using built-in HASS integration with ease. Navigate to “Settings” -> “Devices & services” -> “+ Add integration” and find MQTT in the list. Use the MQTT broker details to connect.

Connect to MQTT broker using Home Assistant integration

Now you can use Mosquitto compatible devices with Home Assistant 👌.

Listen to outside of localhost

If you plan to use MQTT outside the Raspberry Pi to set up the IoT devices on your LAN or internet, then you should set Mosquitto broker to listen for outbound connection request:

echo -e "listener 1883" | sudo tee -a sudo nano /etc/mosquitto/mosquitto.conf

Example with Tasmota

If you have a device that is running Tasmota, you can control it using MQTT from Home Assistant. Configure MQTT on the Tasmota by navigating to “Configuration” -> “Configure MQTT” then add your MQTT broker data.

Replace YOUR_MQTT_DEVICE_IP with your Raspberry Pi local network IP, and of course YOUR_MQTT_USER and YOUR_PASSWORD with what you used above.

Configure Tasmota MQTT parameters

After this, the Tasmota entity should appear automatically inside the Home Assistant

Tasmota sending MQTT data in left, Tasmota entity on the Home Assistant

If not, press the “+ Add integration” and add it. Verify the Tasmota “Console” to see if MQTT connection working properly.

Now you can add buttons to the dashboard, knowing the Tasmota entities IDs inside that integration.

Manual integration

Other IoT devices needs manual configuration and might appear inside the MQTT integration, or you can use MQTT service to send and receive data inside scripts and automations.

Example of manual integration using MQTT Publish service

There are both Publish and Receive MQTT service that you can use in your script and let you add buttons on your HASS interface.

Test with MQTT client

You can use MQTTX to test the Mosquitto broker using the Windows / Linux or another machine. Get it from here.

Use your device data. Replace the 192.168.0.190 with your Raspberry Pi IP.

MQTTX Mosquitto client for Windows, Linux, Mac, right is message received from Tasmota status topic

Super!

So now you know how to configure MQTT with your device and Home Assistant.

⚠️ Keep in mind if you want to expose this MQTT server directly over the internet, it is not TLS / SSL secured, so a bad actor can read commands your login credentials. If you use different username and password than usual, you are kind of safe, but even so, a bad actor can get control of your device and who knows what malicious code can inject to get access to your entire network. Always keep your IoT devices updated!

You can add TLS / SSL to the MQTT quite fast with certbot but it’s not compatible with all IoT devices, so better keep it local and don’t expose MQTT port to the internet.

Does this mean I can’t use Home Assistant to control the MQTT device over the internet? No! Your HASS instance is separate from the Mosquitto broker. The broker will handle the IoT devices in your local network only and interface them with Home Assistant that can be exposed over the internet with encrypted connection.

You can find more related articles in this list:

Configuring Home Assistant and custom integrations

5 stories

--

--

Chelaru Adrian

💻 Hey, I'm here to share my passion for tech! 🚩 Follow me on Medium for exciting new ideas and innovative solutions.