HomeHome AutomationCreating a smarter light switch

Creating a smarter light switch

This is how I manage my lights in the bedroom!

I decided to change the light switch in my bedroom. It’s a single gang, traditional flip switch, wired to the UK standards. You are probably right expecting a plot twist in this project, as replacing this switch with a one gang smart equivalent wouldn’t be interesting enough. After a week of testing, I’m ready to share why you should consider the famous n+1 formula for calculating the gangs in your room.

More is not always better unless you have a plan

The burning question is: why n+1? Why bother with extra gangs? The answer is as usual, complicated but can boil down to: “because I can” and that the home automation suppose to be tailored and personal. Plus the extra gang usually cost you $2-3.

Armed with “everything you should know about smart light switches” and a Zemismart WiFi smart light switch (no neutral, touch, 2 gangs) I proceed to reimagine the bedroom lighting system. I’m not going to stop on adding voice controls, I want to add something fancier.

After all, my bedroom is home to the cinema system build around Xiaomi DLP projector (review) linked with Röth&Meyer amp (review) and smart motorised curtains from Zemismart (review) already.

The master (bedroom) plan

I’m already running NodeRED server with Zigbee2MQTT integration, which opens my door to IKEA range. The smart light switch doesn’t support dimming, but I have other plans for that. Here are the options I came up with.

  • 1st gang – control main lights
  • 2nd gang – control bedside lamp
  • Automatic Cinema Mode – when the projector is in use
  • Close curtains – when the projector is in use (pending)
  • IKEA Dimmer – control main & bedside light
  • IKEA Dimmer – override cinema mode
  • IKEA Dimmer – timed switch off
  • Alexa, Google Assistant integration

That’s more bullet points that I initially imagined, but there is plenty you can do with smart light switches… as long as you are willing to hack it! There are plenty of switches out there and most of them will be either WiFi or Zigbee based. I have these:

Hackmaster 3000

That’s the catch. The WiFi light switch has to be hacked to play nice with NodeRED. Thankfully, the smart toggle is ESP8266 based and thanks to Tasmotizer, hacking this baby is as easy as identifying the pins on the chip, and carefully connecting wires with your excellent soldering skills.

Don’t panic, which this guide and reasonably small soldering iron (take a look at this MiniDSO TS80review) you will get it done in 30 min! If this is your 1st time, you will learn something as you go along too!

Accessing the switch is easy, as the plate meant to be open. pull out the control board and marvel at the design for a moment, until you locate the ESP in question. If you have a similar, but not an exact light switch, identify the ESP model number and look up the datasheet to identify all pins we are going to use (GPIO00, RX, TX, Vcc, GND). Map other GPIOs too as you will need it later on.

FTD1232Light Switch
Vcc 3.3VVcc
GNDGND
RXTX
TXRX
GNDGPIO00

I looked up the ESP8266 (TYWE3S or ESP W602) that is soldered to my Designator 3 (3000 sounds better) board and marked the pins in use. I will be hunting for these later.

Careful – image to the right shows the backside!

Carefully solder dupont cables to the pins required. Make sure the cables do not touch the metal shielding, as this would cause a short. Once you are happy, use Tasmotizer and this guide to backup (important), erase and flash new tasmota-lite.bin (there is no need for sensors).

Pin Hunter

This is where you get to play a detective. I have asked you to note all connected pins with traces. These will operate the relays, keep the LEDs on and let you use buttons. Unless you have the exact version of the WiFi switch as mine, your pinout may vary.

There are no shortcuts here. You have to test each pin and see what it does. Each GPIO can perform one of 3 functions: relay, button, LED. Select “generic” option in Tasmota configuration and try setting your GPIOs to these functions. Take notes, so you don’t go in circles.

GPIOFunction (comp #)
GPIO04Relay 2 (22)
GPIO12Button 1 (17)
GPIO13Relay 1 (21)
GPIO14Button 2 (18)
GPIO16LED1i (56)

In Tasmota, you can apply reverted settings as well, so feel free to experiment with your component assignment. You can start with my template and modify to your needs:

{"NAME":"Generic","GPIO":[0,0,0,0,22,0,0,0,17,21,18,0,56],"FLAG":0,"BASE":18}

Wonderwall

IMPORTANT! You are playing with HIGH VOLTAGE, take extra care, in doubt consult professional

Take a look at this post to get yourself familiar with wiring standards. My WiFi light switch needs a capacitor across the bulb to work. I powered down the light circuit for safe installation and wired the switch inside the wall. Before connecting, identify the L wire in your wall (if you are using no Neutral version) and secure the terminals.

Turn on the power supply. If everything is configured, you should be able to toggle the lights from the switch. It’s time to open up NodeRED and add special features.

NodeRED magic!

Time to make the whole system special. Feel free to pick the features you want, discard the ones that you find useless and add yours that will make your master bedroom shine (oh that pun!). I will walk you through the features I added to the project.

The spare gang sends updates to the MQTT server every time the status changes, so I can use this in NodeRED to control any NodeRED enabled device.

2nd Gang – bedside lamp

2nd Gang – bedside lamp

Inside the WiFi light switch, there is a 2nd relay which is useless. Thanks to Tasmota, the relay is reporting back to my MQTT broker. NodeRED uses this info to control a bedside lamp connected via smart socket.

The smart socket in question is the Koogeek Smart Socket (review). It’s one of my favourite smart sockets due to UK plug size. Most of the smart sockets simply take way too much space. This socket is also hackable and flashed with Tasmota – see the tutorial here. You can pick other smart sockets that can be hacked with Tasmota (Sonoff Smart Socket, Oittm Smart Socket) or pick another compatible NodeRED device.

Note, that I’m actually using updates from the smart light switch changes to control the smart socket. I don’t control the bedside lamp directly. This way smart light switch always displays correct lights.

To make things uniform, I used a switch node to change the values to boolean and store it in flow variables There is one for each source of light.

I have a whole article showing you how to use IKEA Trafri dimmer in NodeRED. Once connected you can map 4 actions of the dimmer to anything you want. In this section, I will create 2 toggles to turn the lights independently.

  • on (1) – toggle lamp
  • off (0) – toggle main

I already have the current status of the lights stored in the flow variables, I can use a simple function node script to check the status of the light and return the opposite value. I need to update the status change as well.

FUNCTION NODE: Lamp Toggle
var x = flow.get("bedroom_lamp");

if(x === false){
    msg.payload = true;
    flow.set("bedroom_lamp", true);
    flow.set("bedroom_lamp_override", true);
    return msg;
}
else{
    msg.payload = false;
    flow.set("bedroom_lamp", false);
    flow.set("bedroom_lamp_override", true);
    return msg;
}

Instead of sending the payload to MQTT out directed at the lamp, I update the smart light switch instead and let the previous chapter handle the rest. In the same way, script the main lights.

FUNCTION NODE: Main Toggle
var x = flow.get("bedroom_main");

if(x === false){
    msg.payload = true;
    flow.set("bedroom_main", true);
    flow.set("bedroom_lamp_override", true);
    return msg;
}
else{
    msg.payload = false;
    flow.set("bedroom_main", false);
    flow.set("bedroom_lamp_override", true);
    return msg;
}
This image has an empty alt attribute; its file name is Xiaomi-DLP-Projector-5-2.jpg

Since I got the Xiaomi DLP projector (review), I get to enjoy a home cinema in bed. The films are best watched in the comfort of the bed, so I made automatic lights off profile to showcase my laziness!

I used a ping node (node-red-contrib-ping) to check if the projector is online. The moment it connects, I can send the signal to turn the lights off. I check the ping values every 7 sec, and if the value is an int>0 I send a msg.payload = false to both my light sources.

Without further modifications, this profile will turn the lights each time they are turned on, as long as the projector is connected. To remedy this, I created an override profile.

Buy Xiaomi DLP Projector

Buy it using these links to support NotEnoughTech.

A long press of the IKEA Tradfri button sends a brightness_up message. I use this to enable the bedside lamp, and stop the ping messages from turning the lights off.

This way, if I want to have the bedside lamp on while the projector is running, I can do that with a single press of the button. To suppress the messages coming from ping node – I have added a switch override which is controlled by the bedroom_lamp_override flow variable.

I have 4 actions that I can map, so the last action brightness_down is used to start a timer. You can specify the number of minutes after which lights in use will turn off.

Press and hold the 0 button to capture the time in ms and use the timeout value to set an alarm.

FUNCTION NODE: Lamp Timer
var seconds = Date.now();
var timeout = flow.get("timeout");
var timer = (timeout * 60000) + seconds;
flow.set("timer_state", true);
flow.set("timer", timer);

return msg;

Setting the alarm will change the value of the timer to time in seconds and the timer_state to true. These are monitored every minute by the inject node paired with a function node: watch timer.

FUNCTION NODE: Watch Timer
var timer = flow.get("timer_state");
var now = msg.payload;

if(timer === true){
    if(now > timer){
        msg.payload = false;
        flow.set("timer", 0);
        flow.set("timer_state", false);
        flow.set("bedroom_lamp_override", true);
        return msg;
    }
}

The timer triggers with the msg.payload = false, when the now > timer, triggering lights shut down. I’m sending this message to every light, so it doesn’t matter which light is on.

It wouldn’t be true automation without voice controls, so I have added both assistants to NodeRED.

Alexa

I talked about Alexa in NodeRED before, it’s the simplest way to add your devices to the Alexa ecosystem. Log in to your Alexa Bridge account and add a device for each light you want to use.

Configure the node, and select the correct devices from the dropdown list.

Google Assistant

I decided to give NORA (node-red-contrib-nora) a go, since the gBridge won’t be working after 15th March 2020 (alternatives). The premise is very similar to Alexa in NodeRED. Create your account and add the NORA skill to Google Home. The names of the devices will correspond with the node names.

Both nodes send true/false based on your voice command, so the hookup is very simple. Just name the lights accordingly and assign it correct rooms.

Conclusion

I will consider adding smart curtains to the mix, but first I need to have a closer look at the WiFi controller that comes with it. I will be tempted to add the 3rd gang in my smart light switch so I could control all 3 light sources: main, lamp & curtains. Let me know what would you like to automate with the n+1 rule? Leave a comment in this Reddit thread.

Project Download

Download project files here. Bear in mind that Patreon supporters have early access to project files and videos.

PayPal

Nothing says "Thank you" better than keeping my coffee jar topped up!

Patreon

Support me on Patreon and get an early access to tutorial files and videos.

image/svg+xml

Bitcoin (BTC)

Use this QR to keep me caffeinated with BTC: 1FwFqqh71mUTENcRe9q4s9AWFgoc8BA9ZU

Smart Ideas with

Automate your space in with these ecosystems and integrate it with other automation services

client-image
client-image
client-image
client-image
client-image
client-image
client-image
client-image
client-image

Learn NodeRED

NodeRED for beginners: 1. Why do you need a NodeRED server?

0
To server or not to server? That's a very silly question!

Best Automation Projects

Tuya SDK for beginners: Intro to Tuya Cloud API

0
Working with Tuya Cloud API. A guide to Cloud automation for beginners, get started with REST!

NEST your old thermostat under $5

0
Nest-ing up your older thermostat under $5

Nora – Google Assistant in NodeRED

0
Integrate Google Assistant with NodeRED thanks to Nora - NodeRED home automation

Sonoff Zigbee Bridge – review

0
Sonoff line up will soon include Sonoff Zigbee Bridge and more Zigbee sensors - here is the first look

DIY Smart Washing Machine – for about 15 bucks!

0
Learn how to add washing machine notifications to your Google Home on the cheap

Smart Home

We’ve seen this before: SwitchBot K10+ PRO

0
This is an odd one. Building on the success of SwitchBot K10+ they released SwitchBot K10+ PRO - but is the experience actually better?

Is this the smart panel we have waited for?

0
ITEAD has released a new smart panel: Sonoff NSPanel Pro 120 - have they learned the lesson from the terrible launch of the original Pro? Let's see what's new.

Aqara FP1E detects motionless humans

0
This isn't exactly a new device, it's an interaction of the original Aqara presence sensor. Aqara FP1E brings Matter, ZigBee and new triggers to your smart home

SwitchBot S10: cleaning re-imagined!

0
SwitchBot S10 promises unattended vacuuming and mopping so you can focus on things you love and care for. Does it deliver?

Sonoff for households and gardens

0
ITEAD released two ZigBee devices to manage water in your household and gardens. Now you can monitor the flow and dispense water with Sonoff SWV and prevent water damage using the Sonoff Water Leak sensor with a twist.