Configuring MQTT on Hass.io

Hi,

I am new to the Home Assistant community but I am excited with all its capabilities! However, I am at a little hiccup.

I have a Pi with Hass.io installed. I also have 2 sonoff switches that I flashed to Tasmota. However I can’t get the Sonoff Switches to work on Hass.io. Every time I add the config for the switches on the configuration.yaml file the Pi crashes. I’ve tried using many different configurations to add to the switches on Home Assistant.

Based on what I’ve read throughout many forums I assume it has something to do with MQTT not being used correctly. I’ve installed MQTT though Hass.io and added "mqtt:"on the config.yaml file since I am only hosting the MQTT locally.

Any tips on what I am doing wrong? Any tips would be awesome!

can you post your configuration to see what is wrong

I’ve literally just lost my MQTT virginity and found it’s far simpler than you’d expect it to be :slight_smile:

In the add-on, just add username and password if you want them…

{
  "plain": true,
  "ssl": false,
  "anonymous": true,
  "logins": [
    {
      "username": "yourusername",
      "password": "yourpassword"
    }
  ],
  "customize": {
    "active": false,
    "folder": "mosquitto"
  },
  "certfile": "fullchain.pem",
  "keyfile": "privkey.pem"
}

In config add…

mqtt:
  broker: core-mosquitto
  username: yourusername
  password: yourpassword

…and you’re away.

Awesome! I made those changes but I an still unable to add my sonoff switches, When adding the sonoff info into the config file I get this when I reboot Home Assistant

"Invalid Config;

The following components and platforms could not be set up:
state_topic
platform
command_topic:
payload_off: “OFF”
optimistic
name

Please check your config"

Any thoughts on what is wrong?

Show links to resources in log and frontend
introduction:

Enables the frontend
frontend:

Enables configuration UI
config:

http:
Uncomment this to add a password (recommended!)
api_password: PASSWORD
Uncomment this if you are using SSL/TLS, running in Docker container, etc.
base_url: example.duckdns.org:8123

Checks for available updates
Note: This component will send some information about your system to
the developers to assist with development of Home Assistant.
For more information, please see:
https://home-assistant.io/blog/2016/10/25/explaining-the-updater/
updater:
Optional, allows Home Assistant developers to focus on popular components.
include_used_components: true

Discover some devices automatically
discovery:

Allows you to issue voice commands from the frontend in enabled browsers
conversation:

Enables support for tracking state changes over time
history:

View all events in a logbook
logbook:

Track the sun
sun:

Weather prediction
sensor:

  • platform: yr

Text to speech
tts:

  • platform: google

mqtt:
broker: core-mosquitto
username: yourusername
password: yourpassword

platform: mqtt
name: “AIG”
state_topic: “room/AIG1/status”
command_topic: “room/AIG1/switch”
payload_on: “ON”
payload_off: “OFF”
optimistic: false

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml

You haven’t added it as a device, you need to put it under switch: or light: as is your preference.

try this

switch:
  - platform: mqtt
    name: "AIG"
    command_topic: "room/AIG1/status"
    state_topic:  "room/AIG1/switch"
    qos: 1
    payload_: "ON"
    payload_off: "OFF"    
    retain: true

Ok so that’s for the Hassio Config and MQTT broker add-on parameters, but what would we need to do in the actual switch device config (a Sonoff S20 in my case) ?

The switch has these MQTT settings available via it’s web interface.

S20 Socket Module
Sonoff_Switch#2
MQTT parameters

Host (domus1)

Port (1883)

Client (DVES_3B41C4)

User (DVES_USER)

Password

Topic = %topic% (sonoff)

Full Topic (%prefix%/%topic%/)

Save

With the greatest respect, which guide/vid did you follow as this is covered by every one of them that I’ve seen.

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