Sonoff, Tasmota and Alexa

Sonoff mains switchI always think it is worth periodically re-visiting things you’ve tried before, even when the first attempt led to tears. Things change. And so it was that I pulled out and old (original 4-pin connector) Sonoff Basic recently and gave Tasmota a go. It is some time since I looked at this. Off I went to the Tasmota site looking for a binary file for the Sonoff BASIC. Voila. Binary files for lots of boards including Sonoff Basic.

Next I had to remember how to flash this on my Windows PC. I tried and quickly realised this was not going to work on my PC without figuring out where Python is hiding. It just so happens I have esptool.exe on there, in my c:\espressif\utils folder.

I connected the Sonoff to the PC via a handy USB FTDI cable (4 pins inc. gnd, 3v3, serial in and serial out) and having checked which USB port the FTDI was connected to (thanks Arduino IDE) I then  went off here: and grabbed in my case the Win64 version of the single file ESPFLASHER.EXE, told it about the relevant COM port and where I’d put the sonoff BIN file (You put the file path under CONFIG). Making sure I’d started the Sonoff in FLASH mode (i.e. holding down the black button).. I pressed FLASH. A moment later, it was done.


For those who would rather use a command line –I have esptool.exe in my c:\espressif\utils folder. I opened a cmd.exe box, moved to the c:\espressif\utils folder and entered the following information:

esptool.exe -–port com15 write_flash –fs 8m –fm dout 0x0 d:\user\Peter\desktop\sonoff-basic.bin

Of course the com port and file location is unique to my situation and this way it all looks fiddly.

Before hitting ENTER to program, I disconnected the FTDI from the PC, held down the button on the Sonoff, restored the connection to the FTDI – and hit ENTER while holding the button for another couple of seconds – all with two hands. A short time later, the flashing process was complete. Easy.

I put the FTDI away, put the Sonoff board back in it’s box and connected mains to the board. I then altered the WIFI on my phone to use the Sonoff.. and set the phone browser to – the default address of the Sonoff. A menu came up and I set up the name of my normal WIFI router in the office along with password, also the name of a router elsewhere in the building along with it’s password. I also told the Sonoff about my Pi MQTT broker and password. finally I gave the unit a name – “sonoffA”.

All done, rebooted the Sonoff, reset the phone WIFI. Trust me, things don’t often go that smoothly.

From there, turning the output on and off in the Sonoff was simply a matter of looking up (remembering) the relevant Tasmota commands.

In Node-Red, I set up an INJECT node to fire stuff at an MQTT node:

topic: cmnd/sonoffA/POWER

string payload: ON

and another for OFF of course.

That worked. Next I tried first sending a PULSETIME command (as above but using PULSETIME instead of POWER). With a value of 50 I got 5 seconds. Nothing of course then happened until I sent the ON command (or pressed the button on the Sonoff) then the output came on for 5 seconds.  This is where I found the commands;

topic: stat/sonoffA/POWER

with no payload, injected into MQTT lets the Sonoff return the status of the output after an on or off command (by software or pressing the hardware button on the Sonoff) and when the timer times out.

All of that was easy. Last time I tried Alexa with this kind of setup, I almost took up woodworking. This time, thanks to:


it was a breeze. No https:, no other hassles, I installed the node, refreshed the screen added the Alexa node to Node-red – my test flow.

I told Alexa to find devices and it found sonoffA. I told Alexa to turn sonoffA ON. Nothing. It took me a minute to twig of course that the sonoffA I’d just set up has nothing to do with the real unit sonoffA. Duh. I fired the output of the Alexa node into MQTT via a FUNCTION NODE as follows:

msg.topic="cmnd/" + msg.device_name + "/POWER";
return msg;

I have so very far to go to get up to speed but this is a start. Lovely. Thankfully, the Tasmota software does not appear to be case-sensitive. Seriously, I don’t think I’ve missed anything out – it was THAT easy.

Oh yes, nearly forgot, last night i set the emulation mode to 1 (see the Emulation entry below) and set a friendly name for the same board before renaming it and without that Node-Red node, as it turns out,  I didn’t do that with sonoffA. So right now I can ask Alexa to turn “my board” on directly – ie without that node or I can talk via the node and a function…. so here, as the light in my head turns on – is a summary – I can tell Alexa to turn “my boad” on” then turn “sonoffA” off – same thing – same real board. If I tell Alexa to turn “madness” on, hte output ends up in the debug node, but there is no real device called “madness”.  Indeed, do I actually need the Alexa node? It appears not for Sonoff – but for devices that don’t support Alexa, sure.

Here it all is – and yes I am using the Node-Red dark theme which Mr Shark pointed me to..


More here as I get more ambitious. I’ve one or two Sonoff BASIC devices floating around.

For those who like to get their Sonoffs from Amazon, here’s a link.
Price: normal £9.9, 15% off with link i.e. £8.42 after discount, available from 2018.12.26 to 2019.01.31

And yes, using the Web interface after setting up the Sonoff, I did try the OTA command to ensure I have the latest ROM – it worked a treat ALSO. If only life was always this easy.

In case you’re wondering how to turn stuff up and down when the payload is “on”. Check out the “madness” node- I told Alexa to set madness to 5 and looking at the whole message, not just the payload, we see..


Theo Arends deserves a medal for the work he’s done on Tasmota, as (to a lesser extent) does the Singapore-based team who made node-red-contrib-alexa-local.

I now know how to get the APP to forget my previous naming attempts – as I give the Sonoffs actual useful names. I’ve called one “my desk light” (friendly name for Alexa) with a hostname of “my-desk-light” to which Windows adds “.broadband” – hence I can access the unit from my PC browser as http://my-desk-light.broadband (for simplicity I also make the mqtt topic my-desk-light.

And as my infatuation with Sonoff + Tasmota continues, I’ve just flashed a Sonoff 4CH PRO. To do that I used the same code, and again used the “NODEMCU Firmware Programmer” in Windows, this time in SPI mode DOUT and assuming 1MB of FLASH (well, it worked – under ADVANCED).  I had to temporarily hardwire GPIO0 to ground and once flashed I seemed to be short of Alexa instructions so I immediately ran OTA. After that I had access to all 4 outputs and their buttons – as I’d called the unit “sonoff-four” I gave friendly names for Alexa of “sonoff-four-one”, “sonoff-four-two” etc. Worked a treat. I tested all 4 buttons and outputs and noted all 4 LED indicators worked too.

Sonoff 4CH

Sonoff Dual R2And flushed with that success – today I got my first Sonoff DUAL (R2). Here it is..


75 thoughts on “Sonoff, Tasmota and Alexa

  1. Peter, it can be even easier: go in tasmota - configuration - extra - enable belkin wemo emulation
    then tell Alexa to discover new devices, and that's it 🙂

    even flashing tasmota is easier, just use the FlashESP8266.exe tool you can find in any EspEasy firmware download zip file, choose com port, bin file, that's it, never missed a single flash with that and no command to remember... more info:

    and you can use Termite to configure them, pasting something similar in console (single line):
    Backlog SSID1 xxxxxx; Password1 yyyyyy; MqttHost brokerIP; MqttUser xxxxxxx; MqttPassword yyyyyyy; Hostname sonoff_name; MqttClient unique_sonoff_name; Topic sonoff_name; FriendlyName1 Sonoff-Name

    1. Erm that first point - on my Sonoff BASIC - configuration - the option is OTHER - and it did that without me thinking..

      Belkin WeMo single device
      Hue Bridge multi device

      What's the benefit of HUE BRIDGE as against WEMO (Im sure it is obvious but bear with me)

      1. belkin wemo are single switch devices, while hue bridge are multi device and dimmable...
        so, for a normal sonoff acting as an on/off switch, belkin wemo is just enough, while if you have a sonoff dual or you need dimming, go for hue

          1. yup, for example if you set 3 gpios (setting the tasmota as GENERIC device, last in the list) to


            and 3 other gpios to


            that's it,you'll have automatic connection between button1 and relay1, and so on... no other config needed, and you have a multirelay board, controllable via alexa

            a trick many use with tasmota is to setup any gpio to relay, then in main interface you click them 1 by 1 and you see where a led is connected or a relay and so on, when they turn on... then you put those unneeded relay back to NOTHING, of course

  2. While on the subject of Tasmota I wondered if you noticed how they get round the OTA update of an image that takes up more than half the flash memory, They call it OTA magic and its a 2 stage process. If the system detects that the image is too large it uploads a minimal image which then leaves more than half the flash free into which it then uploads the full image.

        1. Theo is moving to esp sdk 2.5.0 afaik, as 2.3.0 is now limited for his future plans... the suggested solution in case of problems with wifi on 6.4.0 was to go back to 6.3.x or go to 6.4.1 with sdk 2.3.0

          standard production binaries:
          daily binaries:
          sdk 2.3 binaries:
          sdk 2.5 binaries:

          usually, going for the 2.4.x ones (1st link above) is the recommended path, use the others only in case of issues

    1. You know, in all the time I've been using OTA I never thought of that. The downside is, if the OTA fails you have only mini-firmware in your Sonoff, not the original - oh, well. I'm happy to use this on limited memory devices, eapecially now he has 2 access points - just like ESP-GO. Very good. I find two WIFI access points essential, especially as I usually set these up in my (home) office but deploy in the house.

      When I tell Alexa to turn something on or off, it happily obeys but the "OK" feedback is useless, the words "on" and "off" are far too similar for my liking, do we yet have a way to get Alexa to say "I have turned madness on" etc or are we then back to complex skills?

      1. i think that you can do that via custom routines in the alexa app, i never did that... on my home assistant i setup alexa and i can send text to its tts to have it says whatever i want via nodered custom nodes

    1. I don't know where you buy your stuff Garry but 9.90 each compared to £4 or less each doesn't seem like a good alternative to me. Oh, - yes, well, that's a "think of a price and double it" price for people who don't like to buy from "overseas". Itead themselves (who charge too much for post) end up as around £7 inc post for a single Sonoff Basic.

      AliExpress if you scour through the (tedious) options you will find £3.95 and free postage. That will likely only work up to 3 or 4 units so place multiple orders if you want more (that is typical for all such companies).

      Banggood £3.91 and free post. I just checked. For reasons beyond me the Banggood price at least seems to have no upper qty limits - I tried 12.

      1. yes, shellys are more expensive, but smaller, have dry contacts and native mqtt support (as far as you enable that, you loose shelly cloud service, though)... so, they have their uses, without reflashing needed/due...

          1. The Shelly 1 is worth the premium imo, as mentioned it has dry relay contacts and a very capable stock fw - it also runs on 24VDC-60VDC as well as 240VAC. This combined with the very small size makes it great for hacking devices like doorbells and garage door openers etc.

            It also exposes pre-soldered headers for flashing and GPIO... Very impressive product by a small company that is blowing up with orders - I just they don't get bought out by a big player who changes their ethos.

            1. Oops, meant to say runs on 12VDC and 24VDC-60VDC, as well as 240V.

              The ability to read 240VAC on GPIO is the game changer though as you can put them behind standard light switches for ultimate wife acceptance factor 🙂

          1. Oh yes, not going down that road again. The reason I was looking for alternatives is because I have had a Sonoff basic and a TH16 fail with stuck relays after moderate use, within the last 6 months, which I found pretty poor.

    1. You could connect node-red to iobroker ( There is an adapter that lets Alexa say whatever you want, and it can be connected to mqtt. So you could set up a node-red flow that switches the Sonoff as well as sends the appropriate commands to your Alexa(s). Unfortunately, their Alexa adapter does not work locally, so you have to connect iobroker with your Amazon account.

        1. Now you need to install the iobroker software on, for example, your raspberry pi. Then you can install and setup the Cloud adapter (that's what the app key is for; it will let you create devices that Alexa will be able to control, or rather make these devices actually discoverable by alexa) , the Alexa2 adapter (will let you have Alexa say whatever you want upon any trigger, for instance an mqtt payload), and the mqtt client (not the broker, it will not work for what you want to do even though it has an integrated client as well).

          I switched from using Node-red only to a combination of node-red and iobroker. The flows in node-red feel so much more natural than the awful iobroker blockly scripts, but it can do some things that will not work with node-red - or would be a hassle.

          I can saY "Alexa, set living room to 23 degrees" and iobroker will send 23 to an mqtt topic with which node-red can work and set me thermostate accordingly. You could easily connect your neato vacuum robot or landroid lawnmower, which I had trouble with in node-red. Node-red still does all the actual logic, but passes commands via mqtt and lets iobroker execute them.

          I attempted and quit using this software several times, but once I understood the workflow it was such a big help. I am one vacation until the 20th,but once back home I could take some screenshots on how to set this up if needed.

    2. go in app, menu, routines, set one from there for your device, with 2 actions, 1 for smart home and select device, 1 for saying what you want, save, done

      if you just want a sound, with no OK, go in settings, alexa account, voice answers (don't know the actual item, i've in italian), and activate SHORT ANSWERS

  3. in these videos you can see some advanced use cases of tasmota, that make them interact between themselves without any controller (nodered or any else) or mqtt broker, using the integrated RULES engine (directly extracted from espeasy, as far as i can see looking at both...):

  4. Hi Peter, having played around with that neat Alexa node and having to delete ones I don't need, you can do this from the Alexa app itself in the settings for smart devices. I find it quicker to remove all and then ask Alexa to scan for devices. 🙂

      1. it's there, search "s20" in that page and you'll get:
        08 Sonoff S2X Sonoff S20/S26 Wifi Smart Socket

        but the firmware is always the same, once installed you get it like it's a sonoff basic, you just need to go in configuration, module, and select the correct one (in 6.4.x it was added the new sonoff basic, too)

        look here for the differences in binaries:
        Provided Binary Downloads
        The following binary downloads have been compiled with ESP8266/Arduino library core version 2.4.2 patched with the Alexa fix.

        sonoff-minimal.bin = The Minimal version allows intermediate OTA uploads to support larger versions and does NOT change any persistent parameter. This version should NOT be used for initial installation.

        sonoff-classic.bin = The Classic version allows initial installation using either WifiManager, Wps or SmartConfig.

        sonoff.bin = The Sonoff version without Wps and SmartConfig configuration but adds more sensors.

        sonoff-BG.bin to sonoff-TW.bin = The Sonoff version without Wps and SmartConfig configuration in different languages.

        sonoff-sensors.bin = The Sensors version without Wps and SmartConfig configuration but adds even more useful sensors.

        sonoff-display.bin = The Display version without Wps and SmartConfig configuration but adds display support.

        sonoff-knx.bin = The Knx version without Wps and SmartConfig configuration and some other features but adds KNX support.

        and it's all there, for which feature is enabled in which firmware... usually you just use the sonoff.bin file, i use the sonoff-IT.bin for italian

  5. I suggest that you leave the original Sonoff firmware alone. The Alexa eWeLink Smart Home skill now works great with Sonoff. It also works great with Google Home Assistant. Once you burn Tasmota or ESPeasy firmware to a Sonoff device, you can never go back to the stock firmware. Try the stock firmware & app first before you make the irrevocable firmware change.

    Use Sonoff S20 sockets, rather than the Sonoff Basic. Keeping the original firmware means you don't have to worry about installing headers, or a clamp-on device, or cleaning out the header holes for newer devices, if they have been filled with solder, and jumping through a lot of other hoops.

    If you do decide to change the Sonoff firmware, because you must have MQTT, the DrZzs video mentioned at the top of the comments is the easiest method that I have found. You can use the latest versions of all the software & Tasmota source code mentioned, and everything will work just fine.

    I also suggest that, after updating the Sonoff firmware, you disconnect the FTDI, then reconnect it, and do all your Sonoff configuration through the USB/serial port, rather than over your network. It is faster and much more reliable. You get virtually instant response from the Sonoff.

    I've followed these instructions for both WeMo and Philips Hue clones, and they sort of work OK. There is a limit of one WeMo, but several (16 maybe?) Hue devices. There is no need to install either skill.

    The problem is that Alexa doesn't always immediately recognize the clone devices, no matter how many times you try to get them discovered, though they do just start work after some period of time. They often get lost to Alexa/Google if the power to Alexa/Google or the Sonoff gets interrupted. This is not a problem when staying with the stock Sonoff firmware.

    1. Hi there Arthur. I intend to do some tests with the original software as I have just received a set of Sonoff Basics. I am however not too enthusiastic about relying on their cloud server - just another thing that can go wrong. I used the original firmware for some time and occasionally had to reboot the S26 - I can't be having that. My own ESP-GO softeare and Tasmota as far as I can tell, never dies. I use the S26 units here in the UK but they are much more expensive than Sonoff Basic. I only need direct control and Alexa - using the Node-Red-contrib-alexa-local node as we're not into the Google Home or Apple products here (been there, done that). I got an extra 2nd generation Alexa Dot in the Black Friday sales in the USA recently for $19 - should have bought more as there are no such bargains in the UK. I've not lost anything yet using Tasmota despite changing device names several times and today adding hostnames so Windows can access the devices by name (for setup). More on that in the blog.

        1. You make a good point Antoino but more importantly - if you are sensible, Alexa is a secondary means of control so there is always the likes of Blynk or NR Dashboard as a fallback. Any control using the Itead cloud requires that to be working. One reason I dedveloped ESP-GO initially was that I needed 100% reliability. At the time, The alternatives had issues. Today, at least for Sonoff, it does look like Arendst has cracked it. Ive only checked on and off, noted level control and set timeouts. I've also noted the output states survice power out as they do in ESP-GO. I'm happy to give Tasmota a good trial - I'd be happier if case-dependency was consistent but that's just me.

          Re: Sonoff firmware backups- are you sure?

        2. trust Google, that made me smile, they have just been fined 50 million euro in France for breaches of data protection laws (GPDR) . However I agree that they are probably a safer bet than some of the Cn clouds. at least there is some scrutiny in Eurpoe.

  6. sonoff backups:
    Theo's guide:

    Xose's guide:

    1st link on Google's guide:

    case sensitivity in topics: "Tasmota uses trim to eliminate spaces and uppercase to process all parameters, so that behaviour is by design."

    p.s.: Blynk only if you have a local server, otherwise back to cloud chains...

    1. That Theos guide looks good to me.

      As for Blynk - you are right - I'm using their cloud. More than a year ago I was using local server.. but this time around they were having trouble with Java 8.. so I stayed with their server - ten forgot all about it. Is it now back to being easy to put the local Blynk server on the PI ????

      1. use my script, i've not got issues reports lately on blynk community, so it should still work:

        sudo wget --no-check-certificate -O /etc/init.d/blynk

        sudo chmod 755 /etc/init.d/blynk && sudo update-rc.d blynk defaults

        sudo /etc/init.d/blynk install

        to update it, from time to time:

        sudo /etc/init.d/blynk update

      2. Ok, ok, I'll do it... - good grief - she's beautiful - and intelligent. I followed her instructions to check the Java version and I have 1.8.0_65 runtime environment already. This led me to a long conversation with Antonio, only to realise all I had to do was copy the up to date server.jar over my existing but long since broken one - then QRCODE copy my projects from the Blynk servers to local - easy. I've nothing new to learn from the young lady in the video but thanks anyway.

  7. First, thanks Peter for this very rich blog.
    I use tasmota for several months, here is some information that I collected and may be useful
    - Tasmota led status :
    - SonOTA : a script to update a Sonoff device from the stock firmware to Sonoff-Tasmota purely over WiFi : and
    -Tasmota wiki commands :
    In wiki command, I found how to fix IP address, here an example :
    IP address : http://[dhcp IP]/cm?cmnd=IPAddress1 [fixed IP]
    Gateway : http://[dhcp IP]/cm?cmnd=IPAddress2 [gw IP]
    Mask : http://[dhcp IP]/cm?cmnd=IPAddress3
    DNS : http://[dhcp IP]/cm?cmnd=IPAddress4 [DNS IP]

    - A Node-RED node, to control Sonoff switches running Sonoff-Tasmota firmware :

    1. sonota is a bit complicated and not always working with recent firmwares... way easier to attach 4 pins, honestly...
      ip addresses can be changed as soon as you flash tasmota, opening Termite on the serial com port and giving commands from there, instead of doing it via web and loosing connection...
      nodered node, useless as it just recalls mqtt settings you can do with just mqtt node

  8. you can also detach the link between physical buttons and relays, and with leds, too, in tasmota...
    so for example having the buttons trigger anything else, via mqtt or what you want... or "enhance" them, giving them single, double and long press behaviour, just look at "SetOption" in the wiki 🙂
    and the leds, too, you can have them display what you want, not relay related

    1. Since the ESP8266 (esp12) first appeared I've been stressing that GPIO2 should have a pullup and be used as an input. I have many board using GPIO2 and GPIO14 with the likes of Dallas temperature sensors. I have no idea why they didn't just bring all the pins to pads.

      1. new sonoffs have esp8285 instead of 8266, so only 1mb ram but embedded in main package (so cheaper manufacturing), and less pins broken out... and btw, those are devices meant for 1 thing, why break out every single gpio?

          1. yes, but in 8285 is embedded in soc, not a separated package, cheaper for them and no more hacks like yours of years ago, for changing the winbond IC...

            about gpio, all started because on the pcb gpio14 was exposed (and serial connection, too) and someone started creating alternative firmwares, indeed, but in any case they want to mantain their own product as intact as possible, hacking was never in their minds... unlike shelly that exposes full connection externally (always a few gpios, though), with pins just there...

    1. I have done some work with the PZEM004T and also with a basic CT suitably conditioned fed to an Arduino or an ESP8266. I got good results with both apart from when I tried to monitor a 50W LED light. The Ac waveform gets somewhat distorted from a true sine wave and so the resultant current reading is out.
      On another subject an excellent way to connect mains carrying wires quickly and safely is to use WAGO 221 series connectors, these can connect both rigid and flexible wires over a good range of sizes. I use them in preference to crimps or 'chocolate' blocks. Wago 221 are available in various connector widths on Ebay.

        1. They look like inferior Far Eastern copies of the genuine German Wago items. The genuine items are not that expensive so that's what I use. I use them in my business too., which is convenient!

  9. Hi Peter. Switching a sonoff basic with tasmotta is my first node-red project. I have managed to flah tasmotta onto a sonoff basic and the mqtt commands are working within the tasmotta console. In node-red it would appear that the mqqt nodes are connected to my mqqt server (installed on a Pi as per your Script) but I can't get a working connection to the sonoff. Any suggestions?

    1. Are you putting in your mqtt credentials - ie user and pass...I use MQTT )on the pi as per script) and Tasmota - indeed, with the IP problems (ghost network) I had the other day, had it not been for the working MQTT connection to the Tasmotas I would have given up and taken up joinery instead.

  10. Hello Brian Dunward

    if you are sure mqtt is working and you have not changed tasmota topic
    try sending via node-red inject node
    Topic = cmnd/sonoff/POWER
    msg.payload = on
    your basic sonoff should switch on
    you can see iwhat the sonoff receives in the console sonoff s IP XXX.XXX.XXX.XXX/cs


Leave a Reply

Your e-mail address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.