Brighten up your day! How to automate your DIY smart lighting system – part 3

Published by Oliver on

You built your own smart lighting system? But it really is not smart yet. Lets add some smartness to the system. You don’t want to be pressing buttons all the time anymore, right? Here are some ways to make you light setup smart(er).

Rules in Openhab

I am controlling my setup with OpenHab. So the way to add “smartness” to my light is by using OpenHab rules. In this post I will use this special syntax but I will also explain what I am doing. Every major smart home controller out there has a similar rule engine, be it HomeAssistant, Domoticz or anything else. You just have to change code details to use it with your system.

Unfortunately AI is really still in its infancy so (to my knowledge) there is not great AI controlled smart home software out there yet. This means that we have to build rules that work with a similar concept: if something happens under certain conditions do that, under other conditions do something else. The popular IFTTT service has it summarized as If This Then That.

OpenHab keeps all its rules in *.rules files in the rules folder of the configuration (unless you are using the new experimental rule engine). I like it this way, as text based rules can be easily managed and versioned by git. OpenHab rules look like this:

rule "Rule name and description"
when
    something happened
then
    do something
end

Of course these rules can get very complex and include states from various items and sources as well as do complex things, including web calls. Sometimes they can also be very simple though. Here is a rule that send a command to my smart robot vacuum to start cleaning when the corresponding item turns on.

rule "vacuum on"
when
    Item jarvisRunning received command ON
then
    actionControl.sendCommand("vacuum")
end

If written well they can be quite easy to read even if you are not familiar with the syntax.

How and when to use rules

These rules can control everything your OpenHab instance controls. Does that mean you should automate everything? The simple answer is yes!

The more complex answer is: yes if it works and makes sense. This can be a very fine line. In my lessons about smart home I said “let the smart home control itself”. Whenever you find yourself doing the same (set of) simple task(s) again and again you should automate it. You need to turn off all lights one by one? Instead build one button to turn them all off.

Sometimes the automations stop working after a certain point though. My advice is to keep them small and adjustable. For example I had all my lights controlled by motion sensors only to notice that some of them are triggered when I am trying to fall asleep in my bed. Afterwards I added a manual and a time triggered switch to disable this rule during the night.

Other rules have turned out to be useless or even annoying in the long run. I disabled those again quickly. In most cases (at least after some tweaking) an automated smart home is awesome. A smart vacuum robot that cleans automatically? Not having to turn on or off any lights anymore? An automated fan during the summer? You get used to all of this really fast!

Overall it is more of a learning system to me. Pretty much no rules you write will every be perfect on the first try. Keep making them better. The definition of perfect also always changes so don’t be afraid to change the rules. Also do not over-complicate them. Simplicity is king.

Oh and if you are using OpenHab I suggest to install Visual Studio Code with the OpenHab plugin. It add syntax highlighting and more for a much enjoyable experience working with rules.

visual studio code open with code for my smart light automation
Visual Studio Code used to edit my smart light automations

My automations

Now that the value of rules is clear, what am I using? Most of my rules are centered around light. Lights are easily replaced by smart lights (see the first two parts of this guide) and noone really enjoys turning on or off the lights. Yet it is something we have to do all the time. Having it done automatically for you is awesome.

Other automations are focused on adapting my home to different circumstances. Slowly dimming up the lights in the morning, turning on/off the fan based on the heat or transforming one room into a home theater. Lets go through the most important ones one by one.

Simple motion sensors

The single most used automation, yet maybe the simplest, is coupling my lights with motion sensors. I have placed small and cheap Aqara motion sensors (see part 2) in each room. The trick is to place them in a corner or on a wall where they are able to detect motion whenever someone is actually moving in the room, without picking up motion from somewhere else.

Small Aqara Zigbee motion (left) and temperature (right) sensors I am using

I then use the motion detection event to turn on the lights. The will stay on until the sensor stops detecting motion (which only happens after a timeout.

rule "Switch kitchen lights depending on motion sensor"
when
    Item motionZigbee1 received update
then 
	sendCommand(H801kuecheObenDimmer, motionZigbee1.state.toString)	
	sendCommand(H801kuecheUntenDimmer, motionZigbee1.state.toString)	
end

As all my rules this one grew over time. I added some debug statements for testing and some very important exceptions. The first one: only automatically control the lights during the night. I really do not want these lights to turn on all the time during the day. I could have used the brightness values in my motion sensor for this.

Instead I decided to base the light control on a generic “day time” script I found in the OpenHab forum that I also used for my smart fan control. I also added a generic switch to activate or deactivate all my motion sensors (in software only). MotionSensorsActivated will turn off for my home cinema for example.

rule "Switch kitchen lights depending on motion sensor"
when
    Item motionZigbee1 received update
then 
	logDebug("Rule", "Motion in kitchen detected")
	if (timeOfDay != "DAY" && MotionSensorsActivated.state == ON) { // is night & motionsensors are activated
		logDebug("Rule", "Switching lights in kitchen to " + motionZigbee1.state.toString)
    	sendCommand(H801kuecheObenDimmer, motionZigbee1.state.toString)	
        sendCommand(H801kuecheUntenDimmer, motionZigbee1.state.toString)	
	}
end

Most lights in my apartment are controlled this way. Some without daylight (bathroom and hallway) are except from the daytime rule as they always need light.

Very simple rules but they work perfectly.

Smart switches

Of course automating lights is great but in some cases a manual trigger is still needed. I have a couple of scenes set up that I can trigger via OpenHab but in some cases a good old light switch works better. Of course in this case it is not an old one but a wireless smart switch.

One such switch is used as a more or less normal light switch to control different lights in my kitchen. It has two buttons where each one controls a LED strip. I was also able to add more functionality though.

rule "Add functionality to the double button in the kitchen"
when
	Item kitchenSwitch received update
then
	logDebug("Rule", "Kitchen button state is {}", kitchenSwitch.state)

	val org.eclipse.xtext.xbase.lib.Procedures$Procedure1<GenericItem> toggle = [
	switchItem |
		if (switchItem.state != ON && switchItem.state != 100) {
			switchItem.sendCommand(ON);
		} else {
			switchItem.sendCommand(OFF);
		}
	]

	if (kitchenSwitch.state == "left") {
		toggle.apply(H801kuecheUntenDimmer);
	}

    if (kitchenSwitch.state == "right") {
		toggle.apply(H801kuecheObenDimmer);
	}

    if (kitchenSwitch.state == "left_double") {
		toggle.apply(H801kuecheUntenDimmer);
        toggle.apply(H801kuecheObenDimmer);
	}

    if (kitchenSwitch.state == "both") {
		toggle.apply(Bulb_Kitchen_dimmer);
	}

    if (kitchenSwitch.state == "both_long") {
		sendCommand(H801kuecheObenDimmer, OFF);
        sendCommand(H801kuecheUntenDimmer, OFF);
        sendCommand(Bulb_Kitchen_dimmer, OFF);
	}

end

The button also supports double- and long clicks as well as pressing both buttons. I used those to automate actions I use quite often: toggling both LED strips at the same time, toggling the main light and maybe most important turn off all three lights at the same time.

The great thing about these smart switches and automations is that they do not only need to be used for simple switching tasks or even just for things in the same room. I have added another button next to my apartment’s door.

While this button can be used with a single click to control the lights there it mainly acts as a “turn everything off” button. One long click here turns off any lights in the apartment as well as the fan. This sometimes saves me a lot of time when I need to leave in a hurry.

rule "Add functionality to the main button in the doorway"
when
	Item mainSwitch received update
then
	logDebug("Rule", "Main button state is {}", mainSwitch.state)

	val org.eclipse.xtext.xbase.lib.Procedures$Procedure1<GenericItem> toggle = [
	switchItem |
		if (switchItem.state != ON && switchItem.state != 100) {
			switchItem.sendCommand(ON);
		} else {
			switchItem.sendCommand(OFF);
		}
	]

	if (mainSwitch.state == "single") {
		toggle.apply(BulbHallway_dimmer);
	}

	if (mainSwitch.state == "double") {
		toggle.apply(grLightWZ);
	}

	if (mainSwitch.state == "hold") {
		sendCommand(grLight, OFF);
		sendCommand(XiaomiSmartFan1C_Status, OFF);
	}
end

The applications of these smart switches are pretty much endless. Right now I am using battery powered ones, so the real ones need to be kept on all the time. If you are able to you can also always replace the real ones with smart ones and get even more out of it.

Alarm clock

Another automation I am constantly using is my alarm based light. I have described this in depth in the original article. Basically if activated on my lights will start to slowly dim up 10 minutes before I have to get up. I am trying to simulate a sunrise here.

I am sure there are more ideas like this. Once you have the lights connected to your smart home controller you can build pretty much anything you can think of. I am for example planning to use ward white/cold white adjustable LEDs in the future. Right now I have RGBW LEDs but I rarely ever use the colored light. Instead having the light tone automatically change during the day might be a great future addition to my smart home setup.

Home theater

As I already described I have a couple of different scenes set up. One of them is used to set the stage for my smart home theater setup. While it does a couple of things, including starting my receiver it also controls the lights. With just one click all the lights in the room turn off and the motion sensors are deactivated. Before I did that even small movements would turn the lights back on. Another rules with small but important incremental improvements.

What else can be done?

I am pretty satisfied already with my current smart lighting setup. It gives me a great hands-free automated light experience. The smart switches give me some awesome extra capabilities (compared to normal ones) and scenes can be super helpful too.

So what else can be done? Of course more ways of controlling the lights can be added. In my case I can control them via the smart switches, my phone, my PC and via Alexa (or like this). I am thinking about adding even more automation though in the form of automatic brightness/color changes based on the time of the day (or measured brightness).

I also added lighting for less utilitarian reasons. I have built a head phone holder with awesome RGBW animated LEDs and upgrade my wardrobe with another set of RGB lights that automatically turn on when opening it. Oh and it also supports animations.

My custom headphone holder with RGBW lights

If you missed part 1 or part 2 of this post check them out. I describe how to set up a light system in general and what hardware to get. If you are looking for more projects head over to my blog and subscribe to the newsletter and/or follow me on Twitter.

Categories: smart lightAutomation