Detecting mains for sonoff switch

TLDR:
What kind of electronics would I need to detect 220v mains and use this to trigger GPIO14 on a sonoff basic?

My goal is to have setup where the light switches works as they do now, bu the light is also controllable via mqtt.

In my house (in Denmark) i have light sockets that looks like this picture:
6888777 . From this socket I can get both uninterrupted 220v (from M2 and N) as well as a 220v line that is controlled by two switches (a 3-way switch for the americans). My idea is that I can connect the constant line to the In of the sonoff and somehow use the “switched” to toggle the sonoff.

I have managed to flash tasmota on a sonoff, connected it to HA, and I have configured it to toggle the relay when GPIO14 is connected and disconnected from the ground pin. So I am looking for a way to do something like that, but controlled by the 220v mains circuit.

I was wondering about something like this - https://www.ebay.com/itm/240V-220V-AC-Mains-Sensor-opto-isolator-optoisolator-optocoupler-5V-3-3V-Arduino/272619019157 ?

Search on here or even just Google or Youtube. Been answered many times over.

Basically, the Sonoff should be constantly powered and you connect the switch wires to the GPIO, no need for the extra hardware you’re suggesting.

Watch this guy

he go some alsum videos

Im in NZ which has 240v I have 10 Sonoff in my house and have more on the way

what I do is flash them with Tasmota put them in the wall to control the lights and with the switch to GPI0 14 and ground on the sonoff HA can turn them on/off and wife can turn them on/off at the switch and HA keeps track on there status

and those “240V 220V AC Mains Sensor opto-isolator” look very cool will look more into them

I might not be explaining myself clearly :flushed:

I have watched the videos by @DrZzs and others, but they don’t cover my exact situation.
I could follow his instructions, but that would require:

  • that i mess with both switches to install sonoffs
  • that my mqtt broken and HA instances in online for things to work.

What I’m imagining requires only one sonoff to be places by the lamp socket (so no messing with the switches) , and will work even when the wifi is down.

Since I have two mains circuits in the lamp socket, one that is constantly on and one that can be switched by the existing switches, all I need to figure out is how to use the switched AC circuit to toggle the GPIO14 pin on the sonoff.
I have ordered one of the opto-isolator devices to experiment with, so that might be a feasible way.

I was able to do this with several Sonoff Basics. I had a similar wall outlet that had both a constant and a switched socket. For the switched socket, I used a 120VAC-to-12VDC transformer (wall plug) connected to a 12V automotive relay to switch the GPIO on the Sonoff, It worked great!

1 Like

Sonoffs don’t need wifi to work directly, if you connect the switch to GPIO it works exactly like a normal switch if all is up or when there’s no wifi or HA is down.

There are a lot of variables depending on what your local wiring standards are, be they the weird US ones, the sensible UK ones or the could a mix of both that seems to occur in other countries. If you could elaborate on what the Danish standard are we could give you more specific help.

Now this is a good idea. I think I have one of those arduino relay boards laying around that I could drive from a phone charger.

I understand that sonoffs work without wifi. But I think the 3-way setup in the drZzs video does not. Two of the sonoffs just sends mqtt messages and then HA sends a message out to the one actually controlling the light.

With the relay idea from @bgehrich or something similar I hope to avoid this issue and use only a single sonoff (+ a phone charger and a relay board and whatever else).

That’s why I asked what the Danish wiring standards are, if they’re like the UK you can do it with one and it works exactly how I said because that’s what I have in my house and demonstrated in other thread on the topic.

Obviously if you’re determined to make it overly complicated without checking if it’s actually necessary carry on :grinning:

1 Like

Hi’
I’m new to all this Home Assistant, Sonoff/Tasmota etc., but learning every day :slightly_smiling_face:
As I’m quite reluctant to ‘implement’ Sonoff devices in my new house in/behind my wall switches (I’m in Denmark as well), I’m planning to implement in the same way as Pengman describes. Mainly due to the potential fire hazard with these devices behind the wall and covered in insulation :thinking: I’ve actually bought the same optocoupler as in the link to detect 230v presence and would like to implement the first devices ASAP at the loft for some outdoor downlight led fixtures in carport etc.

But could i wire the optocouplers output directly to the GPIO14 pin without additional pulldown resistor ?
I’ll drive the optocoupler with 3,3v from the Sonoff PCB, so output should be same voltage.

Hi

To be honest I never figured out how to use that octocoupler device, but I’m no electrician so there might be a way.
Below are my observations (from memory)

  • To function as a switch I needed to have a relay that switches connection from GPIO14 to GND, it might be possible to configure it otherwise, or to use a transistor as you suggest, but I never found an explanation that was at my level of understanding :wink:

  • When fed from 3.3v the “on” voltage from the octodevice was very low, too low to drive the relays I had then.

  • I ended out pursuing the idea proposed by bgehrich, but I haven’t finished it yet.

I’d like to hear if you have any success with your project.

I still have no idea what you two are trying to achieve but if it helps the Sonoffs also have 5v on the board before going down to 3.3v, I’m using the reverse of that and using a USB charger to power some of mine that are switching low voltage DC.

Before anyone asks, I’m using Basics rather SVs because they’re cheaper, have a case …and I had several sitting on a shelf doing nothing :slight_smile:

Take a look at the Shelly devices. They support tasmota and have 240/220v switch inputs. From reading your posts you want to place a wifi relay at the light socket in your roof and use the switch on the wall to control them with another wire.

What I would do if you cannot modify your old wall switches is to place a Sonoff in the roof at the light and use a Xiaomi button or lightswitch to switch the Sonoff through wifi. If your wifi, Hass or Mqtt goes down you can use the old wall switch by setting the Sonoff poweronstate to 1.

Hope this helps!

2 Likes

Thank you all for the replies :slightly_smiling_face:
Yes, as I live in a fairly new house (build 2012) I would prefer not mess with the switches if that could be avoided. But I agree that implementing the Sonoff Basics in/behind the switch, would be the easiest approach for a satisfying solution that would maintain use of original switch (also without Wifi) and additional HA/Wifi/RF switch control.

But I thought that the next best solution would be to use the 230v output from original wall switch as detection to switch the Sonoff Basic at the roof near the lights. I believe all main cables could be found under 50-60cm of insulation :slight_smile:

I’ve actually tested my setup and it works perfectly :sunglasses:

230v output from wall switch to Optocoupler. Sonoff Basic feeds the optocoupler with 3,3v, gnd and output from optocoupler directly to GPIO14. No other special settings in Tasmota besides setting Switchmode 2 (inverted FOLLOW (0 = On, 1 = Off)) and Switchtopic 0 (direct relay). Last to maintain use of original wall switch if wifi should be down.
I use the ‘single’ optocoupler version and the Sonoff Basic has no problems delivering enough power to operate the optocoupler and switch relays. That might not be the case with the 3 on 1 PCB optocoupler ??

I’m just a bit annoyed about the fact that the wall switch could be in a ‘wrong’ position if used to turn Sonoff on and Sonoff later is turned off again via wifi switch, HA etc. That could be fixed by adding a spring to my LK wall switches which would turn them into switch buttons instead. I actually believe that most LK wall switches are prepared for this ? But I’ll have to investigate this further.

Once again, thank you all for your input and suggestions :slight_smile:

Thats cool!

I guess I’ll try again now that I know its working.

Just an update to this topic. I’ve bought some Shelly one switches as well. As you know there’s not much room left behind danish wall switches and ceiling outlets. But a 'Tasmota’ed Shelly 1 actually fits behind standard LK Fuga switches. It’s a tight fit, but it can be done :slight_smile: I’ve also installed a Shelly in a ceiling outlet. This is impossible to do with a Sonoff Basic without serious modifications to the switch wall socket. The Shelly is also much saver to use in wall/behind outlets in my opinion. I’ll go for the Shelly’s for my future smart updates of switches in my home :slight_smile:

Thank you LavaGlass for getting my attention on the Shelly devices :sunglasses::+1: