Site Overlay

Home automation 4 – smart switch & rules

Next up on my home automation journey, is connecting a smart switch to a smart bulb. I went and bought a couple more items:

My goal here is to link both the bulb and the smart plug (which will be connected to my computer screen) to the switch, and if I turn off the light via the switch, both the light and the screen will be turned off. My computer screen (which is actually a TV) displays a horrible white "No signal" screen when my computer goes off, so I generally never turn it off. However that means that I have to turn off the light and the TV at night, so let us use home automation to make this easier! One switch for everything.

First we have to add the new Z-Wave switch and plug to openHAB. Just a matter of going back to Configuration > Things > (+) > Z-Wave Binding and then connecting the devices, same as we did in part 3 for the Z-Wave bulb.

OpenHAB has the concept of Items and Things. Things are physical devices which can be controlled (like a bulb) or can send control signals (like a switch). Items are like virtual things, and act as a gateway between Things. They can also be used to group things together, for example you could control one bulb from multiple switches, or multiple bulbs from one switch, etc.

There is also the concept of Channels, which are used to link things together.

In this case I have one switch and two targets (a bulb and a plug). So I created an Item called "Josh's Bedroom Lamp" (Josh_Bedroom_Lamp), and linked the switch's Dimmer channel to it using the Default profile. This means that when the dimmer switch is changed, the value of this Item called Josh_Bedroom_Lamp is going to be changed as well.

Next, I want it to set the bulb accordingly, so I went to the bulb and linked it's Dimmer channel to the same Item, but this time using the Follow profile, which means any changes to the Item will write that value to the bulb as well.

And it works! Now when I adjust the dimmer switch, the bulb is dimmed correctly.

Next is to link the wall plug connected to my TV to the same Item, so that when I adjust the dimmer switch all the way off, the TV is also turned off. This should simply be a matter of connecting the plug's Switch channel to the Josh_Bedroom_Lamp item, same as we did for the bulb (using the Follow profile).

And…… it doesn't work. Alright, time to debug. First things first, let's open the logs and see if there's anything in there. Since we don't have access to the console now that openHAB is running as a system service, we can follow this guide and connect via SSH. Enter ssh openhab@localhost -p 8101 into a console window, and use password habopen.

logging into openHAB via ssh

Once in, we can enter log:tail to view logs in realtime. Turning the light on and off a few times, there's no errors at all being printed…

so many logs, none that we want

Time to try another approach then. The only thing that comes to mind is that we've been linking a dimmer switch with a sliding value 0 to 100, to a plug with a binary value (either OFF or ON). Could it be that openHAB just doesn't support controlling a binary Thing with a dimmer value?

If that's the case, another approach we can try is to create a custom rule. Custom rules can be created as text files and saved to the <openhab>/conf/rules folder. After consulting the syntax, I came up with this and saved it as Josh's Bedroom - Link TV to Light.rules.

rule "Josh's Bedroom - Link TV to Light"
when
    Item Josh_Bedroom_Lamp changed
then
    if (previousState <= 0 && Josh_Bedroom_Lamp.state > 0) {
        Josh_TV.sendCommand(ON)
        logInfo("Home", "Turning Josh's TV on since the bedroom lamp was turned on.")
    } else if (previousState > 0 && Josh_Bedroom_Lamp.state <= 0) {
        Josh_TV.sendCommand(OFF)
        logInfo("Home", "Turning Josh's TV off since the bedroom lamp was turned off.")
    }
end 

This code will check if the previous value was above 0 and the new value is 0, turn the TV off. And vice versa. After saving the file (openHAB picks up rule changes automatically), I tried the switch and it works! Now if I turn my dimmer all the way off, the TV goes off too. And if I turn the dimmer on again, the TV comes on. Awesome!