New Tasmota Firmware for Sonoff 6.4.x and Home Assistant Discovery

sonoff s22 switch mqtt firmware

This post is my latest in a series of posts about using Sonoff devices in Home Assistant over MQTT. The main reason for yet another post is that the latest version 6.4.x firmware for Tasmota has changed the way it interacts with Home Assistant MQTT Discovery and there are also some other gotchas with the Arduino Core version used. OK I’ll stop talking jargon real soon I promise and hopefully that will make everything more clear.

First though, I’m just going to recap and post links to all my articles on the Sonoff Switches and Tasmota already posted on this site. To date, all my Sonoff switches are S22’s and in this post I’m going to introduce my use of the Sonoff SV. I’m going to go through how to build your own firmware and the reasons why you might want to do that as well. But first the recap.

21st April 2018How to use a Sonoff Switch with Home AssistantThis was my original integration post which covered using the original Itead firmware with the eWeLink app and IFTTT as well as initially flashing a switch with Tasmota and using MQTT
21st May 2018Home Assistant May 2018 updates & Tasmota UpdateVersion 0.70.0 and paid cloud option for Home Assistant.
This was also the first time I used an OTA (over-the-air flash of new Tasmota Firmware)
14th November 2018Sonoff Switches with Tasmota and new MQTT Addon Home AssistantThis is where I switched to using MQTT Discovery and the new MQTT v4 addon and integration in Home Assistant. The latter part of that post 'Flashing Sonoff Switches with Tasmota' contained wiring schematic as well as a partial 'flashing' video and also a video about fixing the power retain settings.

Compiling your own custom version of Tasmota

I guess the obvious way to start this is to ask WHY anyone would want to do this right? Well I have a couple of reasons.

  1. All new version 6.x.x Tasmota firmware is too large to flash to a device over-the-air (OTA) in one ‘flash’. The flash chip in the devices mostly being used is a 1mb flash. When you flash OTA, the device has to be able to hold 2 versions of the firmware in it’s flash memory at the same time. This is because it will not wipe one version until it knows it has a new working version. In practice this means that the device firmware maximum size is around 510kb. All of the pre-compiled firmwares available on the Tasmota Github are larger than this. To circumvent this limitation, Tasmota also compile a ‘minimal’ version of their firmware so then they can use a 2 step flashing process… first flash the minimal version and then flash the full version (after a reboot)
    One of the advantages of compiling your own firmware is that you can disable any options you do not need and then when you compile the firmware it will be under 510kb and can be flashed in one step.
    Given flashing firmware can be ‘hazardous’ and that sometimes switches might not be all that accessible, flashing once instead of twice can have advantages.
  2. A more compelling reason for me though was because I was having WiFi disconnects (a LOT of them) when I flashed version 6.4.0 of Tasmota whereas before with version 6.3.x and earlier I didn’t have any! Upon investigating this on the Tasmota Wiki I discovered that version 6.4.x was using a different version or Arduino Core than previously (See Troubleshooting section of Wiki).
    Interestingly, Arduino Core 2.4.2 has known problems with Mesh Networks, FritzBox Routers and Auto-WiFi Channel assignments so I was 3 from 3 there. Arduino core 2.5.0 is fine with mesh and Fritz but still no auto-channel so I switched the router to a fixed channel and decided to use core 2.5.0. I could have gone back to core 2.3.0 but it has it’s own issues anyway. Version 2.5.0 has very few dropouts and I can live with those.

There are pre-compiled binaries for core 2.3.0, 2.4.2 and 2.5.0 on the Tasmota Github. But where’s the fun in that I ask!!

Now just in case you start shaking at the thought of compiling your own Tasmota and flashing your device(s)… if you have ever flashed Tasmota to a device you have most probably already done this without even realising it!

I have previously mentioned how to do this but I am going to cover it here again now anyway.

Flashing Tasmota

    1. Download and install Visual Studio Code from the Microsoft Store in Windows 10. (I am only providing Windows instructions here. I’m sure it’s possible on a Mac but I have never done it! I only instruct what I do myself) So go ahead and open the windows store from the Windows icon on your computer.
      visual studio code download microsoft for tasmotaClick on the search in the top right and type in visual and you will see a link for
      Visual Studio. Click on that and it will open up a web page where you can download Visual Studio.This is the one you want... not the Visual Studio 2017 download. Click on Free Download and download and install it on your computer.
      Once downloaded and installed, run Visual Studio Code.
      platformio ide for visual studio code tasmotaNext click on the extensions button (Red circle with arror pointing up at it) and type in PlatformIO IDE and add that extension to Visual Studio Code.
      Mine is showing this way because it is already installed. If you have not installed it you will see an install button.
    2. The next thing to do now is to go to the Tasmota Github Page and download the latest Tasmota.sonoff tasmota downloadOn this page note: On the left I have selected the Development Branch. You should select the ‘master’ branch unless you want to use a development version. Second, take note of the commit version.. in this case fa5711e – I use that commit ‘version’ as the name of the folder and zip file when I download the firmware.
      Go ahead now and click on Clone of download and then click on Download ZIP. Download the zip file to somewhere you will be able to find it later and I add the commit number to the filename.
      save sonoff tasmota firmware zip fileHere you can see I have a Sonoff-Tasmota folder in my Documents as well as a couple of firmware versions and a zip file I downloaded a few days ago.
    3. There are 2 files that contain settings we need to confirm and enter before we flash Tasmota. In the folder you created (above it is called Sonoff-Tasmota-development 4b741cd) there is a folder structure like this:
      sonoff firmware file structureHere you see a file platformio.ini This file contains settings for PlatformIO.
      There are 3 parts of this file we are interested in.
      First is the section up the top that looks like this:

      src_dir = sonoff
      ; *** Uncomment one of the lines below to build/upload only one environment
      env_default = sonoff
      ;env_default = sonoff-minimal
      ;env_default = sonoff-basic
      ;env_default = sonoff-classic
      ;env_default = sonoff-knx

      As per the instructions I have removed the semi colon and have set the env_default  = sonoff. If you don’t do that, compiling the firmware will compile all variants of the firmware.
      Secondly, we need to specify the Arduino Core we wish to compile the firmware with. As per point 2 above, the default is version 2.4.2 however I want to use version 2.5 To do this, we need to change this following section so it looks like this:

      ; Select one core set for platform and build_flags
      ;platform                  = ${core_2_3_0.platform}
      ;build_flags               = ${core_2_3_0.build_flags}
      ;platform                  = ${core_2_4_2.platform}
      ;build_flags               = ${core_2_4_2.build_flags}
      platform                  = ${core_2_5_0.platform}
      build_flags               = ${core_2_5_0.build_flags}
      ;platform                  = ${core_stage.platform}
      ;build_flags               = ${core_stage.build_flags}

      This sets core to version 2.5.0
      The last section we want to change is the com port used to upload the firmware:

      upload_port               = COM4

      To get the right com port number, you will need to load the device manager and identify the com port used for your USB-Serial adapter here:
      com port used for usb to serial adapter flash tasmota
      So mine is on COM4 which matches the upload port in platformio.ini

    4. The other file to change is in the Sonoff folder.
      sonoff configuration file tasmota flashing
      The file my_user_config.h contains all settings.
      There is so much in this file that is changed that I am not going to post it here. Instead you can download my file from here.
      Line 64 & 65 enter your SSID and password.
      Line 89 MQTT Broker IP address
      Line 93 & 94 your MQTT Username and password
      Line 138 your NTP Server
      Lines 143 – 160 define Time Zone parameters and latitude/longitude so local times will be displayed in the console. (Current settings are Australian Sydney Time)
      All of these changes are in Section 1 of the file.To assist you, you can get my copy of platformio.ini here (Note you can right click on link and save as platformio.ini)
      Here is my_user_config.h You will have to edit this file to enter your settings.
      My config file also sets tasmota to use a topic of sonoff, turns on discovery and makes all the other settings I recommended in my previous post here.
    5. Big TIP for editing platformio.ini and my_user_config.h
      Every time you download a new version of firmware, you will end up with a ‘fresh’ original copy of these files. Sometimes (frequently) other new options will be added to the files so you can’t really just copy over the one you edited. However, what I do is I use a free download called Winmerge which highlights differences in files side-by-side and it’s simple to copy changes from one file to the other so you can use your ‘master’ edited file and update settings in the new downloaded one and can also update the ‘master’ for any structural changes.


Leave a Reply