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.
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 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.
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.
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.
Fantastic project and I have programmed it to my Wemos D1 on a breadboard!
I have a slightly different spin on the project, I am using DC under lighting in my kitchen and 98% of this is perfect for my use, however I would like to swap the latching switch to a momentary switch, I was hoping it would be an easy swap in the code, could you help me out?
For anyone looking to use a Sonoff for this, they do fit in a normal receptical box if you remove the plastic casing. I myself am using the Tasmota firmware which gives access to all the GPIOās on the sonoff. I have a push button wired, as well as an LED that illuminates when the switch is OFF to be found in the dark.
Iāve designed and printed decora-style switches that fit nicely behind a switch plate. I just recently re-designed the push button yesterday for better illumination, I will be posting this later on.
Hi Gavster, the part that handles the switch checking is in the file SwitchedRelayClass.cpp. The function is called switchedRelay::switchCheck() and starts on line 56.
You may be able to get away with simply deleting everything after line 56 and replacing it with:
void switchedRelay::switchCheck() {
// Lap the timer
switchIntervalFinish = millis();
// Calculate the elapsed time, set to 0 if it's the first press
if (switchCount == 0) {
switchIntervalElapsed = 0;
}
else {
switchIntervalElapsed = switchIntervalFinish - switchIntervalStart;
}
// Check for a change in state, if changed loop for the deboucneDelay to check it's real
switchState = digitalRead(switchPin);
if (switchState != prevSwitchState) {
unsigned long loopStart = millis();
int supportive = 0;
int unsupportive = 0;
// Loop
while ((millis() - loopStart) < debounceDelay) {
switchState = digitalRead(switchPin);
if (switchState != prevSwitchState) {
supportive++;
}
else {
unsupportive++;
}
}
// Calculate the findings and report
if (supportive > unsupportive) {
switchIntervalStart = millis();
lastStateChange = millis();
switchCount += 1;
prevSwitchState = switchState;
}
}
// If the elapsed time is greater that the timeout and the count is higher than 0, do stuff and reset the count
if (switchIntervalElapsed > switchIntervalTimeout && switchCount > 0) {
// If the count is greater than 2
if (switchCount > 2) {
if (relayState == 0) {
switchDevice(1);
}
else {
switchDevice(0);
}
// Reset the count
switchCount = 0;
}
}
I havenāt tested this and thereās lots of redundant code left in there but it might get you in the right direction. This also removes any alternative uses for the switch e.g. the double toggle special function or toggle 6 times to reset.
Also ensure you set the switchIntervalTimeout_ in step 1 to 0.
I am no expert at writing code and I figured it would be in the SwitchedRelayClass.cpp but I just couldnāt work out what changed the states on the switch!
This will be a great help and I will report back if it works, I ended up using the original code and swapping to toggle switches for my current project but it would be neat to use the push switches!
The restart code shouldnāt be as necessary as where I am using these they are on DC power supplies with easy access to power down/restart.
I am just waiting on another 5 Wemos to arrive once they do I will report back.
OK so I need to tweak it slightly I think as it is a little slow to react, I assume this is a debounce issue so will have another look over the code, the main thing is that is works and works well!
I now have some Sonoffs and I am struggling a little, When I lose power they donāt seem to save the AP details so I lose internet connection, the weird part is that I have 1 unit that has saved the details, I am sure it has something to do with SPIFFS but I donāt know enough about them to debug!
I have run the SPIFFS.format(); code included in the sketch but that still doesnāt work!
The odd thing is that it does save my MQTT details? Could it be something to do with WiFiManager?
Iāve had issues like this before. Itās worth checking your board settings. If youāre using the Arduino IDE these are the settings Iāve found work best for me:
If you can plug the Sonoff into a Linux machine thereās also a handy tool to help erase the flash:
I have played a bit more, I had a eureka moment, originally I had updated Tasmota on to the working Sonoff, I thought it would be worth a go at trying to upload Tasmota to one of the boards that wasnāt working.
The first board I tried wouldnāt connect on Tasmota or on your firmware, the second and third board however have now worked after using Tasmota first then flashing to Dullage firmware!
No idea if it is a bug?
I have 2 more boards to try, I will try flashing Dullage first and if it doesnāt work I will go with the Tasmota then Dullage firmware.
Hi Dullage, love the code but this is also wrecking my head. This WiFi problem happens with me also and I have tried these settings you recommend but no luck.
I had a look at the code, now Iām no coder but could it be that the SSID and password isnāt saved in the saveConfig class but is stored in ram so canāt be recalled after a powercut.
Iāve switched to your switched relay sketch for my sonoffs works perfectly and thanks for sharing your good work.
Hi Conor, Iāve recently had a lot of wifi issues with the SwitchedSonoff firmware and the issues seem to stem from the use of WiFiManager (or at least my implementation of it).
I have since re-worked the code to replace WiFiManager with the standard ESP8266WiFi. This removes the ability to remotely configure / re-configure the devices but has been solidly running my lights for a while now. In fact itās very similar to the SwitchedRelay code only simplified as there can only be one relay with a Sonoff.
Iāll upload it to GitHub and let you know when itās up.