MQTT - can't switch ON/OFF

I have flashed a sonoff basic R2 wifi switch with tasmota. I can toggle it on / off using the
the tasmota main menu “toggle” button.
I can also switch it on off from mycodo using the shell script module.
I must have missed something when setting up MQTT.

Here some screen shots:



What are you asking? Your title and text are contradictory.

Looks like you are using the wrong topic in your Mycodo setup…

That topic needs to be the “cmnd” topic.
Are you using a GUI MQTT client like MQTT Explorer to look at the exact topic names that your Tasmota device is using? There are a lot of mqtt topics that are used by Tasmota in the background that aren’t always displayed in the setup. Some of those topics can be found on the Tasmota Information page (Tasmota Main Menu > Information).
image

You really need a MQTT GUI client to be able to view everything that Tasmota is sending to your broker.

Did you setup your MQTT Broker to use a user name & password? If so, your Tasmota setup needs to have that user name and password entered and saved.

Also, it looks like you changed the “Full Topic” name in the Tasmota setup? That topic name should not be changed unless you really understand what you are doing. I would reset that topic name back to the default setting (%prefix%/%topic%/). You can change the “Topic” name, but you should leave the “Full Topic” setting as the default. You can find the details about how that Full Topic name works here…

2 Likes

Yes, sorry. I can not switch it using the MQTT on/off output in Mycodo.
The shell scrip one works.

Lucid3y3’s answer is spot on but I thought I’d supplement it with an example.
The following screenshots are from a Tasmotized WyzePlug I have which has 2 outlets. I don’t know what you’re using but you may have to specify which outlet you are trying to control (in my example “/POWER1”).
I do usually use a device name for the topic when setting up MQTT in Tazmota but leave the “Full Topic” alone.
MQTT Explorer is highly recommended to see what is happening. As you can see, Tasmota creates it’s own topics for “tele, cmnd, stat”, and others. “cmnd” is the one needed to control your device. It may not show up in MQTT Explorer until you turn it on or off from the Tasmota page. When you click on the topic you need, MQTT Explorer gives you the option to copy it (upper right) to be able to paste it where needed.
I don’t usually set up Mosquitto to use a username and password if I’m just using it locally on my network but this time I did. If you are, be sure to tick the “Use Login” box on the Mycodo output setup screen as well as your credentials.



3 Likes

Hi , thanks for the replies.
I have installed mqtt explorer but it does not show any devices it seems.

Finally I got it to work! Yay!
Thank you guys!

2 Likes

Awesome!
Make sure you disable retained MQTT messages on your Tasmota device.
Go to the Tasmota Console and enter the following command…
SetOption104 1 - Disable MQTT retained messages (some brokers don’t support them)
0 = retained messages enabled (default)
1 = retained messages disabled

Here are some other Tasmota settings you may want to use…

SetOption8 1 - show temp in F

SetOption36 0 - disable boot loop control

SetOption53 1 - show hostname and IP adress in gui

SetOption55 1 - allow mDNS hostnames to resolve in network searches

SetOption65 1 - disable device settings reset on multi-reboot, prevents reset to factory settings if rebooted too many times too quick.

SetOption146 1 - enable display of ESP32 internal temperature

ipaddress1 <xxx.xxx.xxx.xxx> - If you want a static IP, enter this command followed by your desired static IP for the esp32
ipaddress1 0.0.0.0 removes static IP and switches back to DHCP assigned IP

For Timers and Rules to function the following 3 settings MUST be set correctly!
Timezone < value> - sets the ESP time to UTC timezone plus or minus the <#value>
Latitude <xx.xx> - set this to your home latitude so Tasmota knows sunrise and sunset times.
Longitude <xxx.xx> - set this to your home longitude so Tasmota knows sunrise and sunset times.

2 Likes

Thank you Lucid, very useful information!

1 Like

Today, without having made any changes, the switch stopped working.
Can’t switch it anymore from Mycodo. It still works when using the toggle button on the tasmota page.
When I open the console I see those lines. Looks like it can’t connect to the broker.
In MQTT explorer I can see the topic posted when I press the button in Mycodo.
But the sonoff switch does not show up there. I powered it on and off a few times but nothing happens.
Any ideas? How can this happen without any changed made by me?

Yes, your Tasmota device is not connecting to your broker.
That’s what the “rc -2” means…

When you set your password on your Tasmota device, did you check the box before you entered your password? Did you click the save button at the bottom of the mqtt settings page?
image

Next you need to check the configuration of your MQTT broker to make sure you have the user and password setup correctly and then you need to restart the broker.

Also make sure that your CLIENT name on the Tasmota device is NOT the same as the client name in your Mycodo settings.

If you did not set the following options to disabled, then you may have reset your Tasmota device to default settings by rebooting it too many times too fast…
SetOption36 0 - disable boot loop control
SetOption65 1 - disable device settings reset on multi-reboot, prevents reset to factory settings if rebooted too many times too quick.

1 Like

Hi Lucid,
believe it or not. The thing works again normally today. I have not changed anything.
This seems strange to me. Image this switch was installed already in our growing space.
How can one make sure that such error does not occur again?

^ Does this mean when I set static IP that the IP assigned will be the same after a router reboot?
If a power cut triggers the router to reboot will this cause a problem if the all the tasmota devices have different IP addresses assigned or will the system continue to work as long as the broker (The Pi) still has the same IP as before?

Any time your router reboots, any of your clients that are not assigned a static IP could be assigned a new IP address by DHCP. Often automatic DHCP IP addresses are associated to a client device’s MAC address, so usually DHCP will assign the same IP addresses to the associated MAC addresses, but this may not always be the case.
If you have “critical” machines on your network that need to always have the same IP address, like your MQTT broker or any of your IoT devices like your Sonoff power strip, you should assign them static IPs so they always get assigned the same IP after the router or the client is rebooted.
I believe it is better to setup a static IP on the client machines themselves whenever possible rather than force the static IPs from the router (some routers just don’t have very good static IP support). This way the client is telling the router what IP it needs rather than the router telling the client what IP it gets. This way, if you decide to move your SD card to a different Pi, it will still have the same IP address regardless of what MAC address that hardware has.

1 Like

Maybe the problem is a weak WiFi connection from the Tasmota device? Go into the Tasmota main menu and then click on the Information button and there you should be able to see what your WiFi signal quality looks like for that Tasmota device.

1 Like

Now that makes some, the router is two floors below.
The signal is only: RSSI 48%, -76 dBm

1 Like

Thanks Lucid, been wondering about this for a while. I’m gonna set static IP’s for all devices and the Pi!

1 Like