MQTT - Help!

After many many hours learning the Home Assistant basics (and understanding some of it) I have Hass.io running and automating a few things on a Pi.

I have just bought a Sonoff and the associated hardware to flash it. I’ll figure this out as I go along. However, am I correct in thinking that MQTT is required to integrate it with Hass?

The problem is, I just can’t work out MQTT! I don’t know why I need it and what it does that Hass doesn’t.

There are only so many videos and and document you can read before getting utterly lost!

I would rather run something local (not cloud based).

Why is MQTT needed?

I understand that Mosquito is what most use, but the documentation says not to use as it’s not private (I don’t understand that?)

Should I just be using the embedded broker?

What is the embedded broker?

Is there any documentation to help me setting up the embedded broker in Hass.io

Even when it is set up successfully, I can’t work out what to do with it. Are there any (basic) videos that show how to use it rather than just setting it up?

1 Like

@Recneps
you need MQTT to communicate with your devices. suggest you install Mosquitto as it’s ready in Hassio.
See this video

Thanks, I’ve looked at that video many times and I’m still completely lost!

MQTT is the messaging protocol that connects your Sonoff to Home Assistant. Without MQTT there wouldn’t be a communication between the two.

You don’t have to use a cloud based service to use a MQTT broker as you can use either the embedded one or install the hass.io addon.

I don’t know where you’ve read Mosquitto isn’t private, but I assume the author meant demo services you can find online around the web.

You don’t have to set up anything. You can find the configuration details for the embedded one under the first link I’ve posted.

You don’t have to, but it may be easier in the beginning.

You just use the broker to host something called “topics”. Clients can subscribe and/or publish to these topics. For example Home Assistant will publish commands like “ON” or “OFF” to a topic a device like your Sonoff is subscribed to and -once read by that device- act according to that message.

You download Mosquitto MQTT onto your raspberry pi. You DO NOT have to use a cloud service. It is run locally on the pi. It is super easy.

What do you not get? I will try to explain as best I can. Although, I will say up front… I’m not an MQTT expert. I’ve used it for several applications outside of Home Assistant and love it.

What I don’t (or didn’t get) is what it is, what’s it for, why I need it, how does it work, how do I control it. I guess every one here has different levels of knowledge, mine a limited to what I have learnt over the last few weeks.

Following examples or videos generally help me, but I couldn’t find anything.

So this is what I have so far:

What is it: A program that sits in the background of Hass.io that has no user interface.

What’s it for: because some “things” can’t connect directly to Hass. They only connect to MQTT and then Hass connects to MQTT.

Why do I need it: Because it’s the only way to turn some “things” on and off.

How does it work: Not sure yet!

How do I control it: Not sure yet but I believe that MQTTLens is just a method to monitor what is happening in the background.

So I now have Mosquito working. I could send a message from Hass and read it in MQTTLens.

Then I added a username and password and changed - “anonymous” from true to false. MQTTLens has connected, but now does not receive any messages. When I change - “anonymous”: back to true, it does receive messages. Do I need to change another setting? (it’s not username / password related as MQTTLens has connected)

I’m still not sure how to control it. Will the Sonoff software subscribe to a particular topic in MQTT?

How does Hass subscribe to a topic?

Can I send a test message from MQTTLens back to Hass to test it?

Of course. You could configure a MQTT sensor like

sensor:
  - platform: mqtt
    state_topic: "home/bedroom/temperature"

in your configuration.yaml and send a message like

21.3

to that state_topic using MQTTLens. The value 21.3 should then be visible in hass.io’s web interface.

Thank you. That’s very helpful. I can now see the message in both direction.

However, I still can only get things to work by setting “anonymous” to true. As soon as I set it to false nothing works and the sensor that I just set up, on the overview page disappears.

I must be missing something simple (or simple to someone that understands all this!). Any ideas what I could be doing wrong?

You have to totally grasp the concept of what a broker is and does. Maybe think of it as a repository or Major Publishing company which publishes many different magazines. Each magazine a “/topic” and the articles “/topic/subtopic”. A Temp sensor would log into mqtt and publish temp readings something like “/home/upstairs/bedroom/temp” , “/home/upstairs/bedroom/humidity” .
Home assistant would subscribe to both topics to get and display the temp and humidity. I created a wifi outlet with a Wemos esp8266 module and relay. The Wemos logs into mqtt and subscribes to the topic “/home/downstairs/livingroom/light”. Home Assistant via configuration.yaml connects to mqtt and changes the state(Publishes on/off) and the light goes on/off.

With the default settings, when you install Home Assistant it shows a sun at the top of the panel. That changes from sunrise to sunset based on the location setup in HA. So you can program HA to turn on your porch light when it reaches sunset and turn off at sunrise. All via MQTT

It’s just me, and again, I’m no expert. But, I would go with mosquitto MQTT. It’s free and you run it locally on your system.

Install Mosquitto
a. sudo apt-get install mosquitto
b. sudo apt-get install mosquitto-clients
c. sudo apt-get update
d. sudo apt-get upgrade

Run Mosquitto at startup
e. sudo systemctl enable mosquitto
f. sudo systemctl start mosquitto

Running Mosquitto
mosquitto start

  1. mosquitto_sub –t ‘topic/subtopic’
  2. mosquitto_pub –t ‘topic/subtopic’ –m ‘message’
  3. mosquitto –v [verbose] think of it as a debug message, says what’s going on in the background. Like a syslog
    mosquitto_sub -v -V mqttv311 -t “#”
    Displays back all published MQTT topics

Finally got it. I needed to specify the username and password in the configuration.yaml file.

mqtt:
broker: core-mosquitto
username: #####
password: #####

This was not obvious to me based on the documentation.

Thanks to all for your help. I think I now have an understanding of how it works and how to use it.

I agree the doc for MQTT is really lacking and when reading I had the same experience as above. But the mention that mosquito is a cloud/public broker is incorrect. I believe its simply mentioning regarding the public test server/service they provide not Mosquitto in general but the formatting of the page lends it to be misread.