MQTT Sonoff Smart Switch

I flashed my first Sonoff Smart Switch with Sonoff-Tasmota and all went well. Found the IP Address of the switch and on the Sonoff Basic Module GUI I can turn the switch ON.OFF. Adding MQTT to Home Assistant I modified the configuration file to add the switch. On my HA dashboard I can see the switch turn it ON/OFF but nothing happens on the switch, watching it on the Sonoff GUI that does not change state also. Comments are welcomed.

# MQTT Broker Information
mqtt:
  broker: XXX.XXX.X.X
  port: 1883
  username: !secret mqqt_username
  password: !secret mqqt_password

# Coffee Room Floor Light XXX.XXX.X.X
switch:
  platform: mqtt
  name: "Coffee Room Floor Light"
  command_topic: "homeassistant/coffeeroom/floorlight/power"
  state_topic: "homeassistant/coffeeroom/floorlight/power"
  qos: 1
  payload_on: "on"
  payload_off: "off"
  retain: true

On the Home Assistant it shows that the cmd was set ON and then OFF.

  payload_on: "ON"
  payload_off: "OFF"

It is case sensitive.

Thank you but I made that change, no change in the light, now I am wondering if MQTT is installed correctly, all indications say it is.

Any other suggestions?

For a standard sonoff configuration I don’t think your command and state topics are correct. They should be something like this:

  state_topic: "stat/your_device_topic_here/POWER"
  command_topic: "cmnd/your_device_topic_here/POWER"
  availability_topic: "tele/your_device_topic_here/LWT"

Replace your_device_topic_here with the topic you set in the Tasmota web interface mqtt configuration page. Again, note the case of the letters.

I would recommend the mosquito mqtt broker over the HA built in one. If you use this you can check the addon log to see if your device is authenticating correctly.

Setting retain true is also going to cause you problems once you get this sorted. Set it false and check out this video as to why and how to fix it (later - get control from HA working first).

Also you can install an mqtt client like mqttfx on your PC and subscribe to topics to see what is going on.

Here is the information from the Sonoff GUI:

00:00:00 Project sonoff Coffee Room Floor Lamp Version 6.4.1(sonoff)-2_4_2
00:00:00 WIF: Connecting to AP1 MTP Office in mode 11N as coffeeroomfloorlight-7614…
00:00:06 WIF: Connected
00:00:06 DNS: Initialized
00:00:06 HTP: Web server active on coffeeroomfloorlight-7614.local with IP address 192.168.1.8
19:38:23 MQT: Attempting connection…
19:38:23 MQT: Connect failed to 192.168.1.4:1883, rc 5. Retry in 10 sec
19:38:34 MQT: Attempting connection…
19:38:34 MQT: Connect failed to 192.168.1.4:1883, rc 5. Retry in 10 sec

Do I have the Broker Wrong?

Yes. It is failing to connect to the broker for some reason. Check your host, user and password settings in the sonoff.

I am very discussed with Home Assistant, after a month of learning the program and watching hours of videos, it crashed again, this is the third time. Can Node Red run as a stand alone program for automation. I just do not have the time and patience to deal with this. All I am looking for a program that I can use to setup an alarm system and the Tasmota Sonoff Devices. At my wits end.

I don’t know anything about Node Red sorry.

I understand your frustration. I have been at the point of giving up many times. HA can be an infuriating system to debug.

Regular crashes are usually due to one of three things:

  1. bad SD card
  2. insufficent power supply
  3. invalid config, not restarting correctly
  4. faulty platform/component (see below)

On point three (and I am not accusing you of doing this - just FYI) best practice is as follows:

  • write some code
  • do a configuration check from the configuration / general page
  • if it passes, restart HA from the configuration general page (or reload platform automation, groups, scripts, etc…)
  • never pull the power without shutting down the host system first (hassio / system page).

On point four, there is another reason your system could be crashing. A memory leak in the embedded mqtt broker. Read the puke green coloured warning paragraph.

If you feel like giving it one more try I would recommend deleting the embedded broker and using the mosquitto hassio addon broker.

Thank you for your kind words. I thought I was get it down and moving forward it crashed after deleting MQTT from HA. I just want to move forward and not taking five steps backward.

I thought it was the Pi, then went to a VM Machine and that got even worse.

Thanks again.

Ah there I go assuming host platforms again.

As you are using a VM cant you just restore the VM from a backup?

I’m not sure what VM you are using but using Oracle box this is dead easy.

Well after four-hours and recreating my HA on the Pi, it is backup and working, Thanks for the challenge @tom_l. All my switches worked and I am now back on the mountain. Installed a backup system so if this happens again it will not be so bad.

Today I restarted HA and noticed that my switches on the dashboard are “Not Available” but still working. What kind of hole am I in now? LOL.

At least I am laughing.

Any one have any ideas?

Most likely a problem with your availability topic configuration:

  availability_topic: "tele/your_sonoff_topic_here/LWT"
  payload_available: "Online"
  payload_not_available: "Offline"

There is also a new recommended config depending on the version of Tasmota you are using. What version are you using, 6.4.1?

Version 6.4.1

Right. According to the wiki your config should now look like this:

Replace /sonoff/ with your topic.

Added five more switches and everyone came up “Not Available”.

Post one of your switch configs and a screen shot of the tasmota mqtt configuration page for the device and Ill try to see what is going on tomorrow morning. Off to bed now.

Here is the information:

# MQTT Broker Information
mqtt:
  broker: core-mosquitto
  port: 1883

# Coffee Room Floor Light 192.168.1.8
switch 1:
  platform: mqtt
  name: "Coffee Room Floor Light"
  command_topic: "cmnd/coffeeroomfloorlight/POWER"
  state_topic: "stat/coffeeroomfloorlight/POWER"
  availability_topic: "tele/coffeeroomfloorlight/LWT"
  qos: 2
  payload_on: "Online"
  payload_off: "Offline"
  retain: false

Unknown

Try this instead:

- platform: mqtt
  name: "Coffee Room Floor Light"
  state_topic: "tele/coffeeroomfloorlight/STATE"
  value_template: "{{ value_json.POWER }}"
  command_topic: "cmnd/coffeeroomfloorlight/POWER"
  availability_topic: "tele/coffeeroomfloorlight/LWT"
  qos: 1
  payload_on: "ON"
  payload_off: "OFF"
  payload_available: "Online"
  payload_not_available: "Offline"
  retain: false

Also you do not need this in your config, remove it:

# MQTT Broker Information
mqtt:
  broker: core-mosquitto
  port: 1883

Set mqtt up in the configuration / integrations page.

Well we are getting there, I can see all of my switches with the exception of one that is still unavailable. I had to keep the switch in the configuration file to eliminate errors.

# MQTT Broker Information
switch:
  - platform: mqtt
    name: "Coffee Room Floor Light"
    state_topic: "tele/coffeeroomfloorlight/STATE"
    value_template: "{{ value_json.POWER }}"
    command_topic: "cmnd/coffeeroomfloorlight/POWER"
    availability_topic: "tele/coffeeroomfloorlight/LWT"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"
    retain: false

  - platform: mqtt
    name: "Office Floor Light"
    state_topic: "tele/officefloorlight/STATE"
    value_template: "{{ value_json.POWER }}"
    command_topic: "cmnd/officefloorlight/POWER"
    availability_topic: "tele/officefloorlight/LWT"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"
    retain: false

  - platform: mqtt
    name: "Master Bed Room TV"
    state_topic: "tele/masterbedroomtv/STATE"
    value_template: "{{ value_json.POWER }}"
    command_topic: "cmnd/masterbedroomtv/POWER"
    availability_topic: "tele/masterbedroomtv/LWT"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"
    retain: false

Coffee Room Switch I have to move the slider switch a half of dozen time for it to come ON once, then it goes OFF in 2 seconds. on the sonoff GUI I can turn it ON and also goes OFF in 2 seconds.

Office Switch is unavailable. On the sonoff GUI I can turn it ON and OFF.

Master Bed Room TV does not come on at all. On the sonoff GUI I can turn it ON and OFF.

I hate to ask you if you have any more suggestions, just tell me to go blow smoke, lol.

When I am on the sonoff GUI for the Coffee Room Switch in the console I turned the switch ON and it went off in 2 seconds but never showed it was on.

03:48:41 MQT: Connected
03:48:41 MQT: tele/coffeeroomfloorlight/LWT = Online (retained)
03:48:41 MQT: cmnd/coffeeroomfloorlight/POWER = 
03:48:42 MQT: stat/coffeeroomfloorlight/RESULT = {"POWER":"OFF"}
03:48:42 MQT: stat/coffeeroomfloorlight/POWER = OFF

Here is the Office Light from sonoff GUI, noticed it does not say connect:

03:49:51 MQT: stat/officefloorlight/RESULT = {"POWER":"ON"}
03:49:51 MQT: stat/officefloorlight/POWER = ON
03:49:55 MQT: stat/officefloorlight/RESULT = {"POWER":"OFF"}
03:49:55 MQT: stat/officefloorlight/POWER = OFF

Here is the sonoff GUI for the Bed Room:

03:52:45 MQT: tele/masterbedroomtv/LWT = Online (retained)
03:52:45 MQT: cmnd/masterbedroomtv/POWER = 
03:52:48 MQT: Attempting connection...
03:52:49 MQT: Connected
03:52:49 MQT: tele/masterbedroomtv/LWT = Online (retained)
03:52:49 MQT: cmnd/masterbedroomtv/POWER = 

This switch is trying to connect like the other one.