Introduction

In the world of smart home technology, connectivity is key. One protocol that plays a vital role in connecting remote devices with minimal network bandwidth is MQTT. Designed specifically for the Internet of Things (IoT), MQTT is a lightweight messaging transport layer that simplifies communication in smart homes. In this guide, we’ll walk you through setting up an MQTT broker in Home Assistant and configuring a device to send messages over MQTT.

  1. Understanding MQTT: MQTT, short for Message Queuing Telemetry Transport, is a protocol that facilitates efficient communication between devices in an IoT ecosystem. Unlike traditional protocols, MQTT is lightweight and requires minimal network bandwidth. This makes it an ideal choice for smart homes, which often have numerous interconnected devices.
  2. Setting Up an MQTT Broker: To get started with MQTT in your smart home, you’ll need to set up an MQTT broker. An MQTT broker acts as a central hub for message communication. Home Assistant, a popular home automation platform, offers a built-in MQTT broker that can be easily configured. Simply follow the instructions provided by Home Assistant to set up and enable the MQTT broker.
  3. Configuring a Device for MQTT: Once you have the MQTT broker up and running, it’s time to configure a device to send messages over MQTT. This can be any smart device that supports MQTT communication, such as a smart thermostat or a motion sensor. In the device’s settings or companion app, look for MQTT configuration options. Enter the required details, including the MQTT broker address, port, and authentication credentials, if applicable.
  4. Publishing and Subscribing to MQTT Topics: In MQTT, devices communicate by publishing and subscribing to topics. A topic acts as a channel through which messages are exchanged. To send messages from your device, you’ll need to publish to a specific topic. Likewise, you can receive messages by subscribing to a topic. In your device’s settings, specify the MQTT topic to publish or subscribe to, ensuring it aligns with your home automation needs.
  5. Leveraging MQTT in Home Automation: Now that your device is configured for MQTT, you can take advantage of its capabilities in home automation. For example, you can set up automation rules in Home Assistant that trigger certain actions based on MQTT messages. This allows you to create sophisticated routines and control your smart home devices seamlessly.
  6. Monitoring and Troubleshooting: As you delve deeper into MQTT-based smart home automation, it’s important to monitor and troubleshoot any potential issues. Most MQTT brokers provide dashboards or logs to help you track message activity and diagnose problems. Additionally, familiarize yourself with MQTT quality of service (QoS) levels, which determine the reliability of message delivery.

Conclusion

MQTT is a powerful protocol that simplifies communication in smart home environments. By setting up an MQTT broker in Home Assistant and configuring devices to send messages over MQTT, you can establish a seamless and efficient connection between your smart home devices. Embrace the potential of MQTT in your home automation journey, and enjoy the benefits of a truly interconnected and intelligent home.

Install Mosquitto MQTT Broker on Home Assistant

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 Settings menu
  2. Click Add-ons
  3. Click ADD-ON STORE in the lower right corner
  4. Go to the Search add-ons box
  5. Type Mosquitto broker
  6. Click on Mosquitto broker from the list
  7. Click INSTALL and wait for the Mosquitto broker to be installed
  8. Enable Start on boot and Watchdog
  9. Click START to start the broker

Install MQTT Broker via Home Assistant add-on

Now that the MQTT Broker has been installed, Home Assistant itself has to communicate to the MQTT Broker. To do this install the MQTT Integration.

  1. Within your Home Assistant frontend/home navigate to the Settings menu
  2. Click on Devices & Services
  3. Make sure you are the Integrations part (shown on the upper side of the screen)
  4. MQTT should appear as a discovered integration at the top of the page
  5. Click on CONFIGURE on the MQTT integration
  6. Click SUBMIT when asked if you want to configure Home Assistant to connect to the MQTT broker provided by the add-on Mosquitto broker
  7. Click FINISH

MQTT Authentication

When you completed the installation of the Mosquitto MQTT Broker add-on and the MQTT Integration within Home Assistant you are almost ready. Now your devices need to authenticate to the MQTT Broker. The MQTT Broker automatically will use all users within Home Assistant for authentication. So you could use your own user account for this. This is not really secure. Best practice is to create a user account for mqtt which you can use on all your devices.

  1. Within your Home Assistant frontend/home navigate to the Settings menu
  2. Click on People
  3. Click on ADD PERSION in the lower right corner
  4. Enter a name, in this example we will use mosquitto
  5. Enable Allow person to login
  6. enter the password and confirm it
  7. Select if the user can only log in from the local network
  8. Disable Administrator
  9. Click CREATE
  10. Click Create again

Final check

To check if the MQTT broker is installed you can use MQTT Explorer. MQTT Explorer is a comprehensive MQTT client that provides a structured overview of your MQTT topics and makes working with devices/services on your broker dead-simple.

MQTT Explorer can be downloaded here

  1. Download and install MQTT Explorer
  2. Click on the + sign to add a new connection
  3. Enter the following details:
    1. Name: Home Assistant
    2. Protocol mqtt://
    3. Host: ip address of your home assistant server
    4. Port 1883
    5. Username: mosquitto
    6. Password: enter the password for the created mqtt user
    7. disable Validate certificate
    8. disable Encryption (tls)
    9. Click SAVE
    10. Click CONNECT

If everything is working you should be able to see the statistics of your Mosquitto MQTT Broker. Click on the arrows before each topic to expand.

Congratulations, you are now ready to connect MQTT Clients to Home Assistant!

Leave a Reply

Your email address will not be published. Required fields are marked *