Setting Up MQTT & Mosquitto in Home Assistant

Cyan Automation
4 min readSep 13, 2020

--

Photo by israel palacio on Unsplash

MQTT is a useful protocol for smart home technology, being designed for the Internet of Things (IoT). It’s a lightweight messaging transport layer that is ideal for connecting remote devices with a minimal network bandwidth — this last part is helpful, as our smart homes are likely to have many devices. With this guide, we’ll set up a MQTT broker in Home Assistant and then configure a device to send messages over MQTT.

Getting Started

To communicate over MQTT, we need an MQTT broker to manage, store and action the messages. Thankfully, Home Assistant has an OOTB integration, called Mosquitto (by Eclipse), which is a well-performing broker.

To locate and install Mosquitto, follow these steps:

  1. Within your Home Assistant frontend/home navigate to the Supervisor menu
  2. Select the Add-on Store, and search for Mosquitto
  3. Click the INSTALL command

For the remainder of the steps, I will share the official instructions (from the Add-On itself). To get the add-on running:

  1. Start the add-on, by clicking START
  2. Have some patience and wait a couple of minutes
  3. Check the add-on Log output to see the result
  4. Create a new user for MQTT via the ConfigurationUsers. Remember this username and password, as we’ll need it later. For this guide, I’ve created a user named mosquitto, but this could be anything.

Note: This name cannot be homeassistant or addon, those are reserved usernames

Then some final steps and validation:

  1. Navigate in your Home Assistant frontend to Configuration -> Integrations.
  2. MQTT should appear as a discovered integration at the top of the page
  3. Select it and check the box to Enable MQTT Discovery if desired, and hit SUBMIT.

Finally, you should see Mosquitto fully set up, like this:

Making It Work

Now with the MQTT broker set up, let’s configure a device to use it. I will use a Shelly 1, but you can use any MQTT device to do this. The steps will differ per device, so refer to that device’s manual. It’s important to know your HA/Mosquitto Broker’s IP address, as well as the username & password you created when we set up Mosquitto, above.

For the Shelly, I head to Internet & Security, and under the ADVANCED — DEVELOPER SETTINGS menu, click the checkbox to select Enable Action Execution via MQTT.

Note: with Shelly, If you enable MQTT, actions via Cloud connection will be disabled

Now, enter the username and password you created when setting up Mosquitto, then enter the Server details; being the IP address of your Home Assistant installation. Remember that the port suffix needs to be :1883. There’s no need to Use Customer MQTT Prefix, but this option is there is you need it.

Leave the Reconnect configurations as default, then ensure Clean Session is selected whilst Retain is unchecked for switches and lights. Lastly, Max QoS being equal to 2 is highly recommended for smart home automation.

Now save settings, and your device is all set up to use MQTT! Remember to check out Defining & Customizing a Shelly Switch via MQTT in Home Assistant, for making the best use of your Mosquitto set up.

--

--

Cyan Automation

Marc’s tips on building your Smart Home with Home Assistant, Shelly and ESPHome.