ESP8266 / Sonoff Controlled Ceiling Lights

I’d be interested to see your code, please.
Also, as a fellow UK resident, I’d be interested to see your wiring diagram with the sonoff and wall switch.
I’ll be honest that my knowledge of UK lighting ring topologies is not particularly good… although a cursory inspection revealed the presence of brown and blue wires behind some of my switches, so my working assumption is that I have both live and neutral.
Caution suggests I dont necessarily rely on wire sleeve colours though :slight_smile:

No problems, I’ll try and publish my code and some pointers on wiring over the weekend :smiley:.

New repo added for use on a Sonoff device. The in the repo explains how to use it and includes a wiring diagram.

Hopefully this is useful to someone :slight_smile:.

1 Like

Thanks for sharing.
I have a couple of Sonoff basics on their way.
I think I have L and N connections at (at least some of) my light switches.
I’m going to try to incorporate a pot or rotary encoder to act as switch and also to allow soft on/off and dimming via mqtt of smart bulbs.

I’ve spent some time on my SwitchedSonoff repository and now you can configure all of the WiFi, MQTT and Other settings over WiFi and you no longer need to make any changes to the code. This should make it easier for those who don’t want to get into Arduino / C++. :smiley:

1 Like

I really love all this info. I’ve been very keen to find a way to make my kitchen light accessible both manually via the wall switch and also via HA automations.

However, has anyone had any experience or luck with the idea of having a wifi bulb and doing something similar. In the sense that it could be triggered off by a wall switch and controlled in HA as well. I would assume the wall switch would be completely detached from the wiring for the actual light socket and simply be a trigger to tell HA to turn it off and on.

That way scenes and dimming of the light could be used as well as turning it off and on.

Of course the desire to do this is based on the better half stating there is no way the kitchen light can only be turned on via an app or even controlled by a motion sensor.

I created a soft switch that looks like a normal uk 1gang dimmer. Rather than dimming directly it sends mqtt messages to HA, which then in turn controls smart bulbs etc

There are many other such projects…

But doesn’t the MQTT message either say to HA for the light to be on or off? With a smart wifi bulb you don’t want to turn the power off, you want it to control it’s on and off state at the bulb. My current LIFX bulb in the loungeroom requires that the wall switch be left constantly on and the bulb itself does all the work.

I’m just thinking about a way to have a physical wall switch not wired to the bulb in any way that is almost like a mini version of the HA UI for just that one light. Perhaps a touchscreen is the way to go.

I don’t know for sure but I can imagine hijinx’s project has a HA automation listening out for the correct MQTT message. This in theory could trigger any action including the toggling of a wifi bulb.

I actually have a similar use case that I will be working on shortly. Here’s my current setup:

I have 2 lights in my bathroom, the ceiling lights and the lights in the mirror. Both are connected separately to HA using MQTT. The light switch is plugged into the micro-controller looking after the main lights and turns them on and off…then an HA automation triggered by the main light turning on and off turns the mirror lights on and off in sync.

There is also a motion sensor that will turn on the mirror lights if anyone walks in the bathroom at night (kind of a night light). The trouble is, when the night light is on and the main lights are off I would like to be able to turn the night light out by switching the switch (instead of waiting for the motion sensor to time out). At the moment this will turn the main lights on.

What I plan to do is, in the case of the main bathroom lights, instead of the switch controlling the light and then updating HA, I want it to tell HA that the switch has been switched and then HA can make a decision on what to do. In my case, if either of the lights are on, turn them all off, if both are off, turn them all on.

This could be used in your use case to toggle the wifi bulb instead.

I’m sure there are plenty of projects out there that do this already, hijinx’s project for example does a great job if you need dimming functionality. If you just need a on / off switch and can’t find anything else then I’ll let you know when I’ve updated my repo.

I hope this helps.

1 Like

So many great project to be inspired by and fork :smiley:

You’re quite correct. The on/off/brightness is sent from the switch via mqtt, picked up by ha and then drives automation to tell a philips hue bulb to turn on/off or set brightness.

If you only want on/off then you could use the same setup but with a conventional light switch wired to the esp8266 rather than the rotary encoder + small change to espeasy config (rocker switch instead of the momentary in the rotary encoder).

If you’re using smart bulbs though I guess they can be brightness controlled so why not consider dimming too?

1 Like

This is a crude sequence diagram of how it works:

1 Like

ok, this is all making sense. I had gotten a bit confused by the Sonoff switches and MQTT thinking I could only really trigger off and on.

What I plan to do is, in the case of the main bathroom lights, instead of the switch controlling the light and then updating HA, I want it to tell HA that the switch has been switched and then HA can make a decision on what to do. In my case, if either of the lights are on, turn them all off, if both are off, turn them all on.

Perfect, just what I want to do. I can trigger it to make HA run an automation instead then all is good as I know how to make that run for each situation (light already on or off to know what to change it to).

I won’t try to add motion to this situation though as our kitchen is centrally located and we end up walking through it far too often when I would not want lights going on per se.

Thanks for your thoughts on this

Exactly what I’m trying to do. Now I just need to figure out how I will power the ESP button. Battery perhaps as if it is only used a couple of times a day it should last quite some time before needing a re-charge

ESPEasy maintains a constant wifi connection I think, so you’d definitely need to go with custom firmware option.
Also accessing a battery behind switch plate is awkward.
The biq question is whether you have live and neutral connections at the switch.
If you have L+N at the switch then :tada: you can power your switch from mains - for example I use a sonoff basic as you get esp8266 + PSU and case for ~£5
If no L+N then you need to search out other solutions like @Dullage project in this topic, where he’s placing the ESP8266 at the light fitting where there is always neutral (uk wiring). In this case maybe a battery will work, but by not having a persistent connection to wifi your switch will likely have latency as it needs to connect to wifi then publish mqtt topic.

I do this with LIFX bulbs and Flic Bluetooth switches.

The good thing about the Flics is they have a single
click, double click and hold action so you can setup three different light levels or scenes.

I have the baby’s room light setup to be 5% on a single click, 20% on a double click and 100% on a hold. Single clicking when the light is on turns it off.

I 3D printed a mock face place that sits over the top of the wall plate so the bulb is always on.

Thanks madpilot.

With the Flic though, you can only setup those 3 options correct? Or do you do that through the Lifx bulb?

The flic offers the three click types - and HA makes the decision on what to do with those click types.

Though: Full disclosure, I’m actually using node-red to do the logic - but you could do the same thing using HA’s automations rules.

Another option similar to this would be the Xiaomi Switches. They’re mega cheap and integrate with HA nicely.

You do need a Xiaomi gateway but they also fairly cheap and getting one means you can use all the Xiaomi stuff.


My sonoffs have just arrived. I was planning to update them OTA using this method in the first instance:

I’m very new to Arduino IDE, som startuing from basics. Can I replace the .bin file that updates the firmware with the tasmota .bin file with a bin of this firmware? I very much appreciate Dullage linked out to google for the flashing instructions, but the ability to push a bin file OTA seems easier than soldering/ connecting to the Sonoff pins.

Really appreciate any help!



To update firmware via OTA, you can just follow instructions on README from github. Be sure to check Wiki also for the supported firmware versions before starting the update process.