Setting up a Night Light Motion Sensor in Home-Assistant and Node-red

Motion Detected Night Light

Today I am going to give you a quick run through on how to set up a night light that’s fully automated and only comes on after dark. The idea is that if either my girlfriend or I need to get up in the night neither of us has to switch a light on and wake the other up. It was very cheap and easy to do, I’ll list through all the steps and show all the flows to accomplish this in node-red.

Motion Sensor: $10-12 on aliexpress – Xiaomi Aqara Zigbee Motion Sensor

This has been paired to a USB CC2531 zigbee stick which cost me $4. It’s running zigbee2mqtt. There are more expensive USB options like Conbee and Nortek as well as different zigbee software you could potentially use. Depending on the size of your home it might be necessary to look at alternative solutions, but my CC2531 has been working perfectly fine for over a year already. The sensor is in home-assistant and you can read more about how I flashed the usb stick and integrated the sensors here. The main thing here is that I didn’t want to use a xiaomi gateway. Everything here is under local control with none of the sensors able to ring back to the mother ship! The sensor is tiny and sits under the bed.

Xiaomi Aqara Motion Sensor
Xiaomi Aqara Motion Sensor

Xiaomi Aqara Motion Sensor

Zigbee USB CC2531 (suggest get the one with the cable – if you need the cable separately it’s here Debug Cable )

Only aliexpress links above as that most sellers offering on Amazon have ridiculous markups and still have 2-3 week delivery times.

If you wanted to get the more expensive and (allegedly more powerful zigbee unit here are some more options for you).

Nortek HUSBZB zigbee/Z-wave combo  Amazon US

Dresden Elektronik Conbee II  Amazon (DE)  Amazon (US)  Amazon (UK)

 

Smart Plug: Teckin (EU spec).

Cheap and well made smart plug. Originally with TUYA software and worked under the smartlife app. I immediately removed this and flashed the device with Tasmota. You can read about how to do that here. You essentially don’t need to solder. You put the plug into a flashable mode by holding down the button, then you connect to it using your phone which is sat on a special wifi network and you send over the new firmware. I’m simplifying here, but you can check the link above to go through the full details. I can write a post if you guys really need, just let me know in the comments. Once flashed with Tasmota (or ESPhome – a different variant),  you can control the smart plug either with MQTT directly in node-red, or you can integrate the smartplug into Home-Assistant and use service call commands to execute on/off/toggle from there. I’ve tried both ways and both are immediate and don’t suffer from latency issues. I like the ability to have two ways to interact in case I want to remove home-assistant in the future and switch to some other platform. Node-red and the automations can come with me.

You can find the plugs I used here (affiliate links):

Amazon US  Amazon UK  Amazon DE

 

aliexpress

Blitzwolf SHP5
Blitzwolf SHP5

Note I’ve selected a slightly different plug on aliexpress as that plug also had two usb charging ports attached to it. Now the ports are only 2.1a, but they seem to charge my ipad pro much quicker than other “more powerful” chargers I’ve used. If you’re interested in flashing one of these, you can read here how to set that up correctly so everything works in MQTT.

 

Lights: Cheap fairy light LED string.

We opted for these as we had some left over from our Xmas tree, and as we’re in rented accomodation, I didn’t want to go through the hassle of buying and fixing LED strips to the bottom of the bed. These were cheap and worked well, and I got them from a local discount center at the grand cost of $4.00.

 

 

The Flow:

This one is very simple. In essence, if motion is detected AND the time is between sunset and 8.30am then the under bed light comes on for one minute. If the light is already on and further motion is detected i.e. theoretically after 50 seconds. Then the light will stay on for a further minute and repeat the process. I beleive the xiaomi aqara motion sensors aren’t able to detect motion after the initial trigger for 30-45 seconds or thereabouts so i set the trigger to be one minute. If the time is between 8.30am and sunset, then nothing happens. I still get notification in home-assistant that there’s motion, but the light stays off.

Motion Detected Night Light
Motion Detected Night Light

So I take the motion sensor feed from home-assistant, although there are separate xiaomi nodes you can integrate into Node-red, i prefer doing it this way. The node looks as follows:

Movement Detected
Movement Detected

 

When motion is detected it sends a payload to the time node which acts as a gate only allowing between the times (note am using the bottom output for that). You need to enter your coordinates in order for the system to calculate sunset etc, as opposed to just using straight times. This means you don’t need to mess around in winter changing times etc.

time node
time node

From there we go into a simple switch node:

switch node
switch node

The idea in it’s simplest form is that if it’s off, it’s turned on, and then you wait 1minute until you turn it off. If it’s already on, you wait 1minute until the trigger kicks in to switch it off.

Here’s the node to switch on:

Switch on
Switch on

Here’s the trigger node and the switch off command:

Trigger Node
Trigger Node

 

switch off
switch off

Technically speaking, i could have removed the connection from the OFF of the switch node as if the light is already off then there’s no need to trigger the delay and then the switch off command. I’ve left it there as I was originally using a debug node to check the flows. I could remove it, but remember the old adage… if it’s not broke!

Happy to answer any questions you might have. I know there are several ways to accomplish the same result. The main thing for me is that my flows are nicely laid out and most importantly easy to debug. The more simple the flow, the easier it will be to amend or adjust when something goes wrong or you want to add complexity to it.

Cheers.

 

Leave a Reply