Configuring MQTT on Hass.io

I’ve followed Bruh Automation’s vids. However, he doesn’t cover the MQTT for Hassio he only covered it for Hassbian.

Yes Bruh Automation was one of the 20 or so I watched on Sunday about HA, Hassio and MQTT. I think the problem for me is that I expected MQTT principals to be easy to understand in plain terms, and on the surface they look simple (any MQTT network diagram looks very simple on screen) , but as I know very little of Python or any scripting languages at all I have struggled to understand the odd terms I see dotted around the configuration at the clients and the broker.
I consider myself fortunate to muddle through the Raspberry Pi HA and Sonoff S20 firmware installs successfully at all really given this is the first Pi thing I’ve ever done.

Today I found these tutorials and I understand better some of the names and terms others found ‘easy’. It seems to me with the info I found today I need to start configuring at the publishing client end first, maybe that will make more sense…
https://www.hivemq.com/mqtt-essentials/

1 Like

I avoided MQTT for ages because I didn’t understand it, then decided to have a go and just take everything at face value and then discovered how stupidly easy it was to set up and use, particularly thanks to the way it’s been built in to HA so you don’t even have to see any of the workings.

Don’t bother to try and understand, just copy and paste from the likes of Bruh and others, tweak to your needs and then you’ll see why people say it’s easy.

The problem is you stopped following the guide before you had it working, the Sonoff has no idea what it’s talking to and I’ve already got you going with the differences in hass.io so if you stuck with it you’d be fine. You’ve started ad-libbing with the names and topics with no idea what they related to is what’s messed you up.

Go back to basics and paste this into your config…

switch:  
  - platform: mqtt
    name: "Sonoff"
    command_topic: "cmnd/sonoff/power"
    state_topic: "stat/sonoff/POWER"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: true

If you followed Bruh’s guide without messing your Sonoff should already be subscribed to the topic ‘sonoff’ and have the MQTT settings sorted with your Pi’s IP and username/password if you set one up in the MQTT add-on, restart HA and it should magically work, if you’ve messed with anything else it may not!

When you have it working you can start changing things to suit your set up. Essentially that would just be the ‘name:’ which is whatever you want to refer to your device as elsewhere in HA and ‘sonoff’ in both of the topics to something else if you have more than one, then just change the ‘topic’ in the MQTT set up to match. Most people just add a number on the end as all the guides say. Leave everything else alone!

Got to love the ‘magic’. At that point I’d not messed with anything related to MQTT, however that changed now…

I tried your suggested switch: device block above, did a copy and paste into configuration.yaml and restarted HA via browser.
This stops HA from working after a restart (and a long long wait for that restart to finish), so I have to rip it out of the config again to get back to where I was. Tried three times to be sure of it.
(SAMBA is still running so I do have access via Windows File Explorer thankfully).
Not sure why your block fails, syntax seems OK to me.

Doing more reading here about the Sonoffs:

Of the many things that puzzle me is host and broker ‘domus1’, as I don’t or didn’t see that as a setting in hassio config for addon MQTT.

I have two Sonoffs S20’s BTW, not one.

If you’d followed Bruh’s guide you would’ve replaced domus1 in the Tasmota MQTT config with your Pi’s IP before flashing!

Regarding your errors in your config yaml, obvious thing to try would be type the correct topics over the top of your incorrect ones, then you’d guarantee the context is OK.

If you’re going to follow a guide it’s a good idea to actually follow it so then it should be quite easy to track down what’s wrong if it doesn’t work :slight_smile:

In my defence I think I used Bruh’s guide to hass.io installation and then adding MQTT and I think I then used Tasmota guide to the Sonoff firmware. I reckon I watched Bens guide to MQTT and Owntracks on Sunday but don’t recall ever seeing him talk about Sonoff firmware in relation to MQTT and what the critical settings to match in both are (have you seen the array of videos he’s produced, the list is overwhelming for a somebody on day one or two of home automation creation).

Anyway, see how I go as these little bits of info fall into place…

Easy to miss :slight_smile:

Yep missed that one, was skimming all over for HA and MQTT tutorials or vids at the time and had been at it for about six hours…

Will back-track using the info here and see how I go. Thanks.

No probs, should hopefully fill in the basics you were missing.

As I’ve said to others, it’s best to take everything at face value with this until you’re up and running, then it should become clear what’s going on and you can adapt accordingly. Saves all the pain :slight_smile:

Hey, I’ve read about 10 different forum messages and still haven’t been able to figure out why my HA doesn’t see that a sonoff switch is on (I can go from off to on, and after a second it’ll jump back to off even though the switch is on)

Here’s my config:

switch:
    - platform: mqtt
      name: "Sonoff One"
      state_topic: "stat/sonoff1/POWER"
      command_topic: "cmnd/sonoff1/POWER"
      qos: 1
      payload_on: "ON"
      payload_off: "OFF"
      value_template: '{{ json_value.POWER }}'
      retain: true

sonoff1 is the name I gave the sonoff in the web ui.
Here’s some samples from its console:

01:48:37 MQT: tele/sonoff1/LWT = Online (retained)
01:48:37 MQT: cmnd/sonoff1/POWER = 
01:48:55 MQT: stat/sonoff1/RESULT = {"POWER":"ON"}
01:48:55 MQT: stat/sonoff1/POWER = ON (retained)
01:49:06 CMD: stat/sonoff1/POWER 0
01:49:06 MQT: stat/sonoff1/RESULT = {"POWER":"OFF"}
01:49:06 MQT: stat/sonoff1/POWER = OFF (retained)
01:51:37 CMD: cmnd/sonoff1/POWER
01:51:37 MQT: stat/sonoff1/RESULT = {"POWER":"OFF"}
01:51:37 MQT: stat/sonoff1/POWER = OFF (retained)
01:51:42 CMD: cmnd/sonoff1/POWER
01:51:42 MQT: stat/sonoff1/RESULT = {"POWER":"OFF"}
01:51:42 MQT: stat/sonoff1/POWER = OFF (retained)
01:51:46 CMD: cmnd/sonoff1/POWER 1
01:51:46 MQT: stat/sonoff1/RESULT = {"POWER":"ON"}
01:51:46 MQT: stat/sonoff1/POWER = ON (retained)
01:51:48 CMD: cmnd/sonoff1/POWER 0
01:51:48 MQT: stat/sonoff1/RESULT = {"POWER":"OFF"}
01:51:48 MQT: stat/sonoff1/POWER = OFF (retained)
01:52:10 MQT: tele/sonoff1/STATE = {"Time":"2017-12-07T01:52:10", "Uptime":0, "Vcc":3.153, "POWER":"OFF", "Wifi":{"AP":1, "SSId":"WubbaLubbaDubDub-2.4", "RSSI":88, "APMac":"18:A6:F7:25:C5:4F"}}

(I tried with/without the value_template)

Thanks

The obvious answer would be HA isn’t receiving the state message back again but don’t know why.

Is your console example just from turning it on and off from HA as mine doesn’t show any CMD messages, this is one turn on and then off from the HA front end on mine…

10:22:54 MQT: stat/sonoff2/RESULT = {"POWER":"ON"}
10:22:54 MQT: stat/sonoff2/POWER = ON (retained)
10:22:58 MQT: stat/sonoff2/RESULT = {"POWER":"OFF"}
10:22:58 MQT: stat/sonoff2/POWER = OFF (retained)

Yeah I can’t see why it’s not getting the state back.
HA’s log isn’t showing anything related to MQTT responses…

I can’t really turn it off from HA because the switch keeps jumping back to the off position. The cmnd entries you see are my manual entries in the console to turn it off

(Edited, can’t see why, not “can”)

Found another thread describing my issue exactly: Sonoff with embedded mqtt

My switch config in case it helps. My sonoff switches are working now (my trouble now is in automations…).
switch:

  • platform: mqtt
    name: “Sonoff switch1”
    command_topic: “cmnd/sonoff1/power”
    state_topic: “stat/sonoff1/POWER”
    qos: 1
    payload_on: “ON”
    payload_off: “OFF”
    retain: true

Also the first MQTT broker config I tried was wrong (at face value).
mqtt:
broker: core-mosquitto
username: yourusername
password: yourpassword

Worked when I set Broker: to my Pi ip and ignored passwords (for now).
mqtt:
broker: 192.168.0.50
client_id: home-assistant-pi

When I configure Hassio like this, the log of the mosquito broker says

starting version 3.2.2
1512735275: mosquitto version 1.4.12 (build date 2017-06-01 13:03:48+0000) starting
1512735275: Config loaded from /etc/mosquitto.conf.
1512735275: Opening ipv4 listen socket on port 1883.
1512735275: Opening ipv6 listen socket on port 1883.
1512735275: Warning: Mosquitto should not be run as root/administrator.
1512735461: New connection from 172.30.32.1 on port 1883.
1512735461: Socket error on client , disconnecting.
1512735830: New connection from 172.30.32.1 on port 1883.
1512735830: Socket error on client , disconnecting.

What do I do wrong?

When I had that “socket error on client” this week (while learning stuff) it was when I had a user/password mismatch between sonoff and MQTT broker…

Interesting, core-mosquitto is the recommended, and working for most people, broker using the hass.io add on rather than the built in mqtt component.

Be sure you’re using the username/password you set up in the broker not an overall HA one.

As @mr.sneezy did, try removing them from the equation altogether to be sure everything then works and it’s not misdirecting you away from a different fault.