CAUTION: I am not an electrician, the information below should not be considered advise. Please consult a qualified electrician before attempting to replicate any of the below.
I just wanted to share a project I have been working on for the main lighting in our house (i.e. the stuff that’s in the ceiling).
If anything this will be a good record for me if I need to add to it in the future .
Ambition
I was looking for a (cheap) system that would allow my ceiling lights to be controlled via Home Assistant but also (and crucially) via the normal switch. As I’m sure we’ve all found out, launching an app to turn a light on is not convenient .
I also didn’t want to go down the route of “Smart Bulbs” as many of our fittings take pretty specialist bulbs or there are just so many of them it would be too expensive (ceiling spots). Smart Bulbs also tend to require the switch to be in the ‘on’ position to allow for wireless control.
Solution
My solution was to use an Wemos D1 Mini / NodeMCU (both ESP8266 based) micro controller controlled (from Home Assistant) by MQTT.
The basic premise is this. A normal wiring circuit for a ceiling light (in the UK at least) has a live feed running through the switch. With a bit of swapping around we can set it up so that:
- The micro controller switches the live feed for the light(s) using a relay.
- The switch(es) in the wall simply connect to the micro controller. The only voltage over the switch cables is 3.3v DC.
It may differ depending on how the house is wired but in our case the installation was very simple, no new cables needed to be run and everything is easily undone if we move. All that was needed was to cut the live feed to and from the switch, put the relay on the live side and plugged the switch wires (now not connected to AC at all) into the mircro controller.
Here’s a simple diagram showing how everything is wired:
Note: If wired correctly this even works where there are 2 switches controlling the same light
Micro Controller Software
Using some great libraries I cobbled together some code to run on the micro controllers. You can find a copy on my GitHub page.
Note: By no means am I a developer, I’m very happy with the result but it was very much trial and error and I’m sure there are things that could be improved.
Main Features:
- Works well with Home Assistant.
- Basic control using simple MQTT commands (e.g. “1” to turn on, “0” to turn off).
- Trigger a Home Assistant automation / script with a double toggle of the switch. Some examples of how I use this:
- Double click the kitchen lights to toggle the under cupboard kitchen lights.
- Double click the landing switch to toggle all of the downstairs lights (good for when going to bed).
- Restart the ESP with 6 toggles of the switch. A useful backup but not something I’'ve ever had to use.
- Plug in as many switches and relays as your micro controller will support.
- Utilizes a non-blocking reconnect function allowing switch control to continue if MQTT or WiFi connection is lost. Good for girlfriend acceptance.
- Automatic Recovery and Offline Mode (see below). Again good for girlfriend acceptance.
- Retained Statuses - The state is published as a retained message allowing your hub (e.g. Home Assistant) to grab the current state if it is restarted. Also used for recovery if the micro controller is restarted.
- Smart Debounce - Should the ESP detect a switch toggle it will monitor the switch for a further 50ms to ensure it was genuine.
The real winning feature for me is the Automatic Recovery and Offline Mode. I have been running 3 sets of lights with this code for over 6 months now and never had an issue. They have recovered from router restarts, network outages, power cuts etc… When designing this there were a couple of scenarios that needed to be worked around:
Short-Term Issues - The ESP8266 can be tricky when trying to detect WiFi disconnections. In a lot of cases my device believed it was connected to WiFi and so was trying to connect to the MQTT broker, in fact it had lost it’s WiFi conection and so was stuck.
Long-Term Issues - We also want to be able to carry on using our lights without any WiFi / MQTT connection. I’m not sure my girlfriend would be happy if a router / Raspberry Pi issue meant no lights
Useful for both scenarios:
- The switch control continues to operate whilst the ESP is trying to reconnect to either the WiFi or MQTT broker.
- When the ESP is first started it grabs the retained state message for each relay and uses it to recover it’s last state.
Useful for short-term issues:
- A restart of the ESP is triggered if 2 minutes has elapsed without a connection (see below). This solves most short-term issues.
Useful for long-term issues:
- The 2 minute restart is not triggered if no connection has been made since the ESP started. In this scenario the ESP sits in an Offline Mode until a connection is made. This avoids the lights turning off every 2 minutes during a long-term issue.
There are further details on how to deploy the code and Home Assistant YAML on my GitHub page. If anyone has any questions I’d be happy to try and answer them.
Hopefully this is useful for someone.
Edit
09/09/2017 - There is now a new fork of the above repo to be used on a Sonoff device. Further details in the repos README.md.