Normally the Sonoff switches don’t support MQTT but you can buy flashed devices that run the Tasmota firmware that does.
The Sonoff-Tasmota firmware provides three interfaces: MQTT, web and serial that can be used for controlling the switch.
In this tutorial I will cover basic set up of the Sonoff Tasmota Basic Switch and how to use MQTT to control and configure the switch.
There is just a single button that is used to control the switch.
It can be used to manually toggle the switch on/off or to place into various configuration mode depending on how often it is pressed.
This link here has an overview of the button options.
There are two steps to the configuration:
- Wi-fi Setup
- MQTT Client configuration
To configure the Tasmota switch you need to connect it to a Wi-Fi network.
The switch connects to a Wi-Fi network just like you mobile does and so s you will need to configure the SSID and password of that network.
There are several ways of doing this and the configuration methods are covered here under button usage
The easiest method is to use WPS followed by the android App.
If you use the manual method 4 short presses then note the following:
Note: newer firmware uses 6 short presses.
With this option the switch starts as a Wireless Access point. The SSID on mine was sonoffbasic-1122.
You then need to connect a computer to this access point and go to the IP address 192.168.4.1.
You can then enter the SSID and password of the Wi-Fi network you will be connecting the device to.
When you enter this information into the webpage the switch will restart, connect to the Wi-Fi network and acquire an IP address.
Now in order to configure it further you will need to know the IP address it has acquired.
This isn’t as easy as it sounds but the device name on the network seems to be set to the same as the access point.
So you could try to ping this network name which should give you the IP address.
On my network the BT Home Hub displays all connected devices and was able to find the device IP address there. (screenshot below).
Note: The device doesn’t have a default IP address
The switch will start as an MQTT client and also a web server.
Initial device configuration is done via the web server. To access the configuration page go to
Click on configure MQTT button and you should see the screen similar to the one below:
You need to enter:
- The IP address or domain name of the MQTT broker
- The Port (default 1883)
- Client – This is used as the client -id and needs to be unique
- Username – Optional
- Password – Optional
- Topic – The client will publish and subscribe on this topic. The name should be meaningful.
- Full Topic -Defaults to prefix/topic -prefix is stat,cmnd or tele see below under MQTT default topics.
When finished save the configuration and the switch should restart.
It is a good idea to use a name rather than IP address as it is more flexible. However to do this you need a local DNS sever like DNSMASQ.
Tasmota MQTT Topics
Sonoff will subscribe to topics to receive control commands . It will also publish status information to topics.
Deciding on the topic structure is an important part of the configuration.
Each Sonoff device has a unique name which you configure in the topic section ( shown above).
This name will reflect the use of the device and should be meaningful e.g
main-light, outside-light etc
The prefix is either cmnd, stat or tele and the choice is usually
prefix +topic (%prefix%/%topic%/) or topic+prefix (%topic%/%prefix%/).
tele/main-light/ or main-light/tele
tele/#, stat/# and cmnd/# options
Another option and my preferred option is to add your own prefix.
So if all the Sonoffs were used in your house you could use the topic hierarchy
house/prefix/topic or house/topic/prefix
now you can subscribe to all Sonoff devices using house/#
In this case I prefer house/topic/prefix.
To configure that use:
On start the Sonoff device will subscribe to several topics. The exact topics depend on how you have structured the topic hierarchy. Here we are using the structure house/topic/prefix:
- main-light=topic name
- main-light-1= the client name or fallback topic
- sonoffs is the group name.
It also publishes its status and a Last will message wth the retained message flag set..
- house/main-light/stat/POWER ON
- house/main-light/tele/LWT online
Below is a screen shot showing the messages sent by a sonoff device starting up.
The LWT topic can be checked to determine the online status of the device.
If the switch goes off line then the LWT topic is set to offline by the Last Will and Testament message.
Controlling the Switch
You can control the switch i.e turn it on/off using either http or MQTT.
Tasmota MQTT Commands
This is important to note as some systems may require http and if you have an existing control system that uses http then it might be easier to use that rather than going to MQTT.
This tutorial will mainly cover MQTT but I will also be covering http in another tutorial.
Turning on/off using mqtt
To turn it on/off you publish to the cmnd topic
The exact topic will depend on how you have set the topic structure. Below I use topic/prefix structure so to turn the device sonoff-light on and off I use the topic:
With message on or off (case insensitive)
In the example below show how to so it using the mosquitto_pub client.
Tasmota HTTP Commands
Turning on/off using http
You can use the curl command or easier still a web browser.
Enter the following
http://sonoff ip address/cm?cmnd=Power%20On
my ip address is 192.168.1.134 so I enter
Note: %20 is a space.
MQTT Default Topic Prefixes
The switch publishes and receives information on several topic prefixes, they are:
||PC,Smart Phone using an MQTT client||To control the Sonoff,set configuration and get status information|
||Sent by the Sonoff||Sent in response to a command|
||Status information||Report status information at set intervals|
The above topics are prefixed with the switch topic e.g.
MQTT Commands and Responses
Besides turning the switch on and off, commands can be used to set various settings like the MQTT broker, port retain flag etc and the get information about the switch.
The switch uses a command response structure. Each command is acknowledged with a response.
MQTT commands use the topic structure:
the result of the command uses the topic
The result is contained in the message payload.
The screen shot below shows the command response messages for setting the MQTT retain flag on the mqtt client.
The screen shot below show the status messages that the Sonoff sends at regular intervals,
Setting the Group Topic
A collection of devices can be controlled by setting them to use the same group topic.
If our devices used the group topic housesonoffs for example we could turn a device on or off using:
house/housesonoffs/cmnd/POWER1 with message on or off
To set the group topic for a device use:
topic house/sonoff-light/cmnd/grouptopic with message group name. As shown below:
You should note that setting a group topic causes the switch to restart as it needs to subscribe to the new topic.
Controlling using MQTT with a Python Demo Script
If you use python I have written a short demo script that you might find useful. You can download it below
Because changing certain parameters results in a reboot you can change several parameters in one command using the backlog topic see MQTT Features for more details.
Controlling Tasmota Devices Using Node-Red
You can also control tasmota devices using node-red and the MQTT or HTTP protocols.
- Initial Sonoff configuration
- MQTT Commands
- Tasmota MQTT Features
- Securing Sonoff
- Good video on flashing Sonoff and setup
- Introduction to Sonoff Switches
- How to Install Mosquitto on The Raspberry Pi
- Tasmota and Node-Red Project
- Using Tasmota With Home assistant
- Tasmota upgrade notes
- Setting Tasmota Timers with MQTT