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

Kmanoz is the one who started this whole thread. it’s supposed to be about the firmware he wrote as an easier alternative to the others.

Anyway I’m not sure how it could be the firmware. Its working ALMOST perfectly on two out of my four switches. I have one that’s pretty good. and one that’s pretty terrible. That is until the wifi goes down or the signal gets crappy (had a problem with a microwave once until i moved the sonoff further away) then they all act stupid.

Can I debug to the level that it will tell me the status of the GPIO?

So it does look like this firmware has debug but you could at the very least subscribe to the mqtt topics in a terminal and when it happens goes watch what was sent via mqtt. That might give you some insight into what’s happening. Other than that I can only suggest you use Tasmota firmware as it’s tried and true and works really well.

Ok thanks.

Maybe I will give tasmota a try.

I looked at it a little (along with several other recommended firmwares) before deciding on Kmanoz’s MUCH simpler firmware and couldn’t figure out how or where to set the wall switch support to standard a toggle switch.

It’s flexible but unfortunately with that comes complexity.

What switch are you using. Is it a proper wall switch used for lighting? As for firmware, this is being used by hundreds if not thousands of people and it’s not a firmware issue.

Yes, its a pretty much brand new standard wall toggle switch sold in the US at Home Depot. It was connected originally to my lighting circuit. I just disconnected it from the power coming in and hooked it up via wire nuts to GPIO14 & ground pins on the Sonoff via a female header I soldered wires to. The other two that work pretty well are hooked up exactly the same way.

And I agree that I don’t think it is a firmware issue. If it was a firmware issue it would never work on any Sonoff that I have it flashed to (4 total). The only reason I thought about changing firmware is for troubleshooting purposes. I’m not even sure if that will help tho.

The Sonoff I am having the most trouble with could very well be an interference issue with the wall switch wiring. It seems as though the wall switch pins are being triggered intermittently and nothing I’ve tried seems to be able to stabilize it.

But also the thing is that it doesn’t do it all the time. I can go several days and they all work perfectly. Then suddenly the trouble sonoff starts acting up. Then maybe after a few hours it will stop as suddenly as it starts.

And, again, they all seem to get flaky when either the wifi goes down or the MQTT broker crashes.

I’ve had to reinstall my MQTT broker on the Pi once a couple of weeks ago. It just randomly stopped working and the only way I could fix it was to purge then reinstall the MQTT package on the Pi. The reason I found out that the broker had crashed was because all of my Sonoffs were randomly firing on & off.

And it was the same when my main wifi router was off. The first indication was that the Sonoffs started firing randomly.

Don’t get me wrong, I would love to figure out how to get these to work if for nothing but the price. That’s why I’m posting here: To see if others have the same issue or if there is some way to troubleshoot the problem to come up with a fix.

Is there any way to troubleshoot these switches to see why they sometimes randomly fire to change state?

I have had the same issue with the Sonoff SV. I flashed Tasmota on it and used it for a garage door relay. I used a reed switch (commercial one) and had unreliable readings. Tried using pullup and pulldown and i finally got a stable input.

The other issue was if the Sonoff dc’d from the network, it would latch the relay.I found out that the mqtt retain really not be be of help since the state of the door doesn’t need to be remembered since the sensor should be realtime.
Mqtt stores the message even when retain was removed from Home-Assistant. Once I was able to clean any retain messages.

In your case, since your switch is not momentary, retain probably doesn’t need to be used.That might help in case of wifi disconnects or server drop. You need to debounce and filter. since I have a long lead wire to the reed switch it was picking up interference. The filter solved that issue.

And to calculate the values needed… http://protological.com/debounce-calaculator/

2 Likes

Thanks

I’ll give it a try when I get a chance

@HickHackerz

What values did you end up using for your filter circuit?

and what setting did you use (pullup/pulldown) to get stable input?

To add to the next part of the story:

I just added some T8 LED replacement bulbs for a 4ft fluorescent fixture. They are the ballast bypass type.

As soon as I turned them on my sonoff switches (the two that were typically pretty stable) started flashing on & off rapidly. If I turned the wall switch to the ‘on’ position the instability stops. I can then control the sonoffs thru HA perfectly fine.

I then reprogrammed one of them to ignore the wall switch and it no longer flashes no matter the wall switch position. the other one with the wall switch check still in the circuit still flashes. So that tells me the wall switch code/circuit is the problem. The LED’s must be feeding back some kind of high frequency noise onto the AC line that is now being picked up by the switch circuit.

I don’t recall the values but I had looked at debounce and then filtering since debounce was not the issue since I was not using it as a switch, but as a sensor.
This looks really familar:

There’s nothing wrong with the logic for the switch because I thoroughly tested before it was released and that logic was the same as the previous version but it but in saying that, these Sonoff’s were never really designed to do what we’re doing with them when it comes to an external switch. You are right, the LEDS are feeding back noise but there is nothing in the Sonoff from an electronics point of view to suppress or filter that so any back EMF or spikes will trigger the logic. The wire acts like a big antenna as you say and any minuscule amount of voltage on the cable is seen as a signal because it is directly connected to the the pin on the micro controller. Now better debounce logic could be written but I haven’t bothered because actual light switch designed for that purpose have debounce in them already otherwise you’d get arching across the contacts. My sister had to remove a certain brand of LED because when she installed them she could no longer watch TV because the rubbish switch mode supply in the LEDS caused so much noise back on the AC that everything connected to it that was sensitive and lacked proper filtering suffered. We want cheap electronics, well that comes at a price. :smiley:

@KmanOz

Sorry. I wasn’t trying to imply, per se, that there was a problem with the wall switch code. I was pointing out that the basic wall switch functionality was definitely the issue. And, I agree that there should be no need for any debounce code using a standard toggle switch.

I’m bringing these issues up in hopes of working this out so that these can possibly be a viable robust (as possible…) alternative for the more expensive zwave stuff that in fairness just seems to work pretty well. I just can’t believe that I’m the only one (or one of so few) that is having these issues. My situation is as about as standard as it gets in a lighting circuit.

The filter circuit definitely seems to be the solution to this. I’m an electrician by trade but I’m not knowledgeable enough to figure out a filter circuit. Again, I’m sure I’m not the only one in this predicament. I’ll be glad to do some more research and to post back anything else I can figure out.

And, KmanOz, your setup seems to be working well. Is there anything that you are doing differently at all, aside from using just your firmware, that stabilizes the wall switch input?

I don’t have any other models of Sonoff but I thought there was a Sonoff switch that had the external switch circuit access pre-built into it (i.e. it brings out GPIO14 & ground out to an external jack). If that’s the case then what is the difference between the two that allows it to be stable but not the basic sonoff switch?

Hi there,

I received my basic switch yesterday. At first i tested it with the recommended android software and a simple light bulb to make sure hardware is OK. Worked like a charm…

After soldering i managed to upload the sketch (with an UNO) to the switch. After plugging in to AC power: No results. Manual switch not working, wifi light not flashing. Checked SSID AND Password twice.

Did i do something wrong?

plug it back in via the header and use a serial monitor to see the console log.

@sholofly

What firmware did you use?

What was the procedure you used to upload the firmware?

I had the same problem a while back make sure you have DOUT set when doing the upload.

Hi moose, thanks for your reply. I tried to that with the arduino IDE but didn’t see any response in any of the baud rates. Is there any other option to monitor the serial output?

It looks like i’m using the old source code. I think I accidentally pulled the old repo… sorry. I will give it a new try this afternoon and come back with the results. I’m curious @cariboo, what is the DOUT option? I remember to see it in my search on Google but didn’t find out what it is…
@finity: I use the uno to upload. I skip the atmega by connecting the reset with the ground. Use the 3.3v ground and port 1 and 2 for comm. I had to swap jumpers 1 and 2, because at they work the or her way around on the uno. Uploading seems to go well this way…

I’m an MQTT newbie, so please be patient. I’m on hassio 0.58.1.

I’m using the default mqtt broker and my yaml looks like this:

mqtt:

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

I successfully flashed my S20 with sonOTA and I think it’s connecting to the mqtt server. If i look at the console in the web interface, I see:

22:17:47 MQT: Connect failed to 192.168.192.79:1883, rc -2. Retry in 10 sec
22:17:58 MQT: Attempting connection...
22:17:59 DNS: Query done. MQTT services found 0
22:18:03 MQT: Connected
22:18:03 MQT: ha/sonoff/LWT = Online (retained)
22:18:03 MQT: ha/sonoff/cmnd/POWER = 
22:21:11 MQT: ha/sonoff/STATE = {"Time":"2017-12-04T22:21:11", "Uptime":0, "Vcc":3.138, "POWER":"OFF", "Wifi":{"AP":1, "SSId":"TP-LINK_3F9F", "RSSI":78, "APMac":"EC:08:6B:F9:3F:9F"}}

If I look at the startup messages in home assistant, I see:

Timer got out of sync. Resetting
21:38 core.py (ERROR)
Setup of tts is taking over 10 seconds.
21:38 bootstrap.py (WARNING)
When targeting Google Home, listening port has to be port 80
21:38 components/emulated_hue/__init__.py (WARNING)
Listen IP address not specified, auto-detected address is 192.168.192.79
21:38 components/emulated_hue/__init__.py (WARNING)
Setup of sensor is taking over 10 seconds.
21:37 bootstrap.py (WARNING)
Setup of mqtt is taking over 10 seconds.
21:37 bootstrap.py (WARNING)

When I click the switch toggle on the main states page, the toggle moves for a second and the reverts. Nothing happens with the switch.

I can control the switch directly through the tasmota web interface.

What am I missing?

Thanks

It looks to me like the sonoff is communicating via topics ha/sonoff/LWT, ha/sonoff/cmnd/POWER and ha/sonoff/STATE
and HA is communicating on topics cmnd/sonoff/POWER and stat/sonoff/POWER so they are basically not talking to each other on the same topics.

Hi All, as promised i would get back to you with my results of the retry.

It works like a charm! Tried the new firmware first: no success, Resoldered the headers for the sonoff, just to be sure: Check! So: shame on me, it looks like it was my lack of solid soldering skills that seemed to be the reason. Thanks for your help anyway!

2 Likes