Sonoff-HomeAssistant (Alternative firmware for Sonoff Switches for use with mqtt/HA)

I am using 7 different versions of Sonoff switches with a total of 20 ckts. working in my Home Assistant system running on a RP3 Raspbian Stretch Desktop using Mosquitto MQTT server.

These have all been flashed with https://github.com/arendst/Sonoff-Tasmota/tree/development/sonoff developed by Theo Arends. This firmware works great and also his github describes in detail how to flash his firmware onto the Sonoff switches also you don’t have to solder anything, just hold the 4 pin connector in the 4 holes on the board while you are doing the flash.

The only issue I have encountered is failure of two of the S20 Smart Sockets. With a bit of trouble shooting I determined that the only part of the unit that failed was the AC to DC bridge rectifier. With a few modifications and the addition of a Gikfun FT232RL 3.3V 5.5V FTDI USB to TTL Serial Adapter Module for Arduino Mini Port set at 5.5V, I have a HA controllable dry contact which I have placed in series with a remote garage thermostat which is controlled by a automation in HA using the output from a http://sonoff.itead.cc/en/products/sonoff/sonoff-th to determine the temp in my remote garage, also flashed with Theo Arends firmware.

image

My opinion on the firmware is why reinvent the wheel when you already have a perfect wheel?

1 Like

Why reinvent the wheel? Because sometimes (read a lot of times) people overcomplicate a simple task. I am a minimalist and designed the firmware accordingly. The only issues with this firmware is generally to do with the individual who is trying to install it and mistakes that they make. Because of it’s minimalistic approach, it’s bulletproof and has been running on switches that I have had for 2 years without any intervention. That’s why… but thanks for your opinion. It’s duly noted :smiley:

1 Like

this is my working config with my Sonoff S20 flashed with SonOTA based on Tasmota firmware

configuration.yaml

mqtt:
  broker: 192.168.1.x
  port: 8883
  client_id: home-assistant-1
  keepalive: 60
  username: xxxxxx
  password: xxxxx
  protocol: 3.1
  birth_message:
    topic: "tele/sonoff/LWT"
    payload: "Online"
    qos: 1
    retain: true
  will_message:
    topic: "tele/sonoff/LWT"
    payload: "Offline"
    qos: 1
    retain: true

switch.yaml

- platform: mqtt
  name: "Sonoff"
  state_topic: "stat/sonoff/POWER"
  command_topic: "cmnd/sonoff/POWER"
  qos: 1
  payload_on: "ON"
  payload_off: "OFF"
  retain: true
1 Like

Actually it wasn’t that I was making some kind of “mistake”.

The firmware wouldn’t work properly with the wall switch under some (common, IMHO) conditions.

After modifying the “retain” statement to false I no longer have ANY issues with flickering using the wall switch function. I still may find in the future I might need to add a filter circuit but for now my setup is working pretty good.

So I guess I was wrong…It was your firmware after all and, therefore, not completely “bulletproof”. That’s why I & others have been adding solutions to the problems we have been having.

But I still like it better than the others I’ve tried because of its simplicity. And I thank you for putting it out there for everyone to use.

However, you need to learn to take a little constructive criticism. :wink:

I’m not sure why you you’ve been triggered and taking this personally because I wasn’t referring to you. However, I am glad you like it and figured out the exact setup for your situation. Sorry that I didn’t give you specific instructions for your particular setup and helped you more but I do this in my spare time for the betterment of the community as a whole and it’s hard to work out individual situations especially when I’m not there. You’re saying you changed a firmware option and it’s working? That’s great to hear. Part of doing what you are doing is called learning. That’s why you built your own system vs buying a ready made solution. The stability of the firmware though once installed and working is like I said bulletproof, mainly because of it’s simplicity. Again, most issues have to do with the installer and the fact that they are learning on the job. Enjoy HA, it’s an amazing system and it’s all free. By all means if you discover a runtime bug in the firmware that I am unaware if, please feel free to post in here or in the Github page and I can have a look at it. Cheers

I honestly didn’t think you were talking specifically about me.

It was a pretty general statement about others “mistakes” and that it wasn’t your firmware at fault at all. I was just pointing out the probable overstatement of those facts.

By posting this for peoples benefit you kind of take on the mantle of “expert”. When something doesn’t go as expected people will look to you for help. It is your firmware after all.

But it’s all good. No worries…

Sorry maybe I misunderstood. Have you in fact made modifications to the firmware to get it going or just adjusted user definable settings?

“user definable” is kind of broad. Everything in the firmware is “user definable”.

I put my own wall switch functionality in your original Sonoff basic firmware that diudn’t originally include it. But yours now includes it and it is equivalent to the code I have in mine.

They “kind of” worked. Apparently there was either interference on the wall switch wiring and/or the signaling from the wifi was being interrupted and causing occasional relay chatter. That is until I put in those LED bulbs then they just were literally not functional until I set the “retain” to false. Now they work (tentatively) perfect.

However, the simplicity of the firmware was a positive in allowing me to figure out what it was “supposed” to do and modify it to finally be able to get it to work. Along with a lot of suggestions by others on this thread.

Thanks for all of that.

Actually I had omitted the code from the switch for the very reason you mention, it wasn’t an accident it was deliberate. Let me explain a little.

The power supply in the Sonoff is a switch mode supply. Inherently they aren’t the most clean form of DC but with a few filter caps on the DC side they get the job done. They are under rather than over designed. Because they’re not fully isolated any noise on the AC side can produce spikes on the DC side because little filter capacitors just aren’t big enough. They were only really deigned for clean AC coming in not dirty.

LEDS are notorious for noise because their supplies aren’t designed to be clean at all. All they’re doing is just powering an LED so it’s not very important as say a logic circuit.

If a device like the Sonoff is on the same circuit (sometimes not even) that noise can travel down the mains and find it’s way into the Sonoff supply which does a shit job off isolating it. That noise is appears across the whole circuit board because it’s not isolated and is just dropped down AC being rectified into DC. The end result is what you are seeing. Add an extra long cable that acts as a form of crude antenna connected straight to a low level logic pin and there you have it, low level logic with spikes throughout the whole supply rails being misread as logic level changes. There are NO filters on the Sonoff because quite frankly it really is misusing the pin for something unintended.

Now why the original Sonoff is less tolerant to noise than the TH series is anyones guess but probably because of better board layout or just luck. That and the fact that it’s quite difficult to connect into the little pins right in the middle of the Sonoff original board made me decide to leave the switch logic out of the original Sonoff. You then went back into the code and put it back in without really taking any of that into account. Now I understand, in your specific environment, the retain option didn’t help but you are using the old firmware version where it was not user configurable or not easily at least. That is not there case in the latest version.

Speaking of criticism, you modified the code and just added back what was essentially missing and available in the other versions of my code, how can you criticise it? Had you written a smart software debounce routine that catches transient spikes that somehow help with the situation I would understand totally, but if you had, why not share it with everyone else considering as you say this is a major oversight in the code.

Anyway enjoy the switches. Glad they’re working for you.

I didn’t say it was " a major oversight in the code".

I didn’t criticize the wall switch code functionality in and of itself. The code works fine to allow a wall switch to function to turn on & off the relay.

Yes, I added it back in not taking into account that the original Sonoff is more susceptible to AC noise. I didn’t know that it was when I did.

I didn’t write any “debounce” code so there was nothing to share.

I’ve pretty much posted EVERY SINGLE THING I’ve had any issues with and tried to post EVERY SINGLE THING I’ve found/done to successfully overcome those issues in hopes that my experiences might help somebody else having the same issues.

If those posts helped anybody then that’s great, if not then just ignore them.

Sorry this has turned into such a kerfuffle.

Obviously it’s your choice and I respect that but would it not be better to make it optional with those warnings upfront and just point people to them if they don’t bother to read them and have problems as it only occurs in a few specific circumstances.

I say this because the most popular use for new Sonoff Basic users on here at the moment is being able use original light switches and you’re instantly making the use of your firmware not an option. I run five like this using Tasmota with up to 8” wires to the switch and no issues whatsoever.

Kman
I am following your experiences with sonoff
sorry about your equipment
I wanted to ask what if a basic switch sits in happily in the wall as you put it
is there any concerns about security
can it melt,
does it has some protection
power cut anything to prevent short circuit and fire

thanks !

KmanOZ

When trying to flash the sonoff I am getting the error msg “no matching function for call to 'PubSubClient::PubSubClient(WiFiClient&, const char [12], int)”

Is this because of the pubsub file issue that danichispa brought up?
Do I need to replace my pubsub with a different version, I’m not sure I understand.
I have saved the pubsub from your instructions and renamed the folder PubSubClientKmanOZ and in the sketch have included the following line instead of the usual PubSub.
#include <PubSubClientKmanOZ.h>

It doesn’t work and tells me “Invalid library found” etc.etc.
I feel like I’m struggling to understand a relatively basic concept here… any help or direction would be much appreciated

The switch are basically fireproof but yes they melt under extreme conditions

However i have faced reliability issue with two of my switches conking off within 10 - 15 days of installation and they were not even used 24/7

Hi actually i have faced the issue you have described i had two switches connected to LED lights stopped working in the last week, is there an isolator or some electrical device i can add to protect the sonoffs kindly guide

No not really or at least not really without making the whole installation messy. It’s the price you pay for cheap electronics unfortunately.

so its because of the quality of LED fixtures i am using if i upgrade them better brand the issue should be solved

hi Samir
Is that 2 swithces of 2 you have or 2 of many
reliability in general is really important and
is that smallest ones or bigger for 4 lights?
thanks

hi

its 2 out of 6 and both are connected to LED fixtures infact i have one connected to a fan which almost run 24/7 while the one which stopped working only worked for 5 - 6 hours a day.

Hence i am pretty sure there are some external reasons for them going bust rather then the switches itself

1 Like

Yes some LED manufactures do try to produce a product that is relatively noise free by using a better circuit for the switch mode supply and better filtering etc. Here in Australia the government was offering free LEDS for households. My sister ended up installing them and every time she turned the lights on in the kitchen (those free LEDS) she couldn’t watch television on a brand new 4K OLED screen because they just fed too much noise back into the AC. Like I said earlier, the price we pay for cheaper and cheaper products. Something has to give somewhere. Just try to find a brand that minimises the noise and you should be OK.

1 Like