# Flexible Lighting Automation with OpenHAB



## jimbodude (Jul 26, 2011)

Thought I'd give a little summary of a project I've been working on that has been keeping me away from the forum. It's long - executive summary: in addition to controlling movie playback, my lights go into movie scene when I press play on my IR remote, and back to white when I hit pause or stop.

For Christmas, my fiance got me some Philips Hue color changing bulbs. They're LED bulbs that can be set to color and brightness by a phone/tablet app, on the website, or directly by other devices. For those who are a bit more technical - the bulbs are ZigBee LightLink nodes controlled by the Philips Hue Controller. The Hue Controller is effectively an HTTP to ZigBee bridge - it publishes a pretty slick REST API.

The bulbs are a bit special compared to regular bulbs in that they control their own brightness, power state, and color. What this means is they need full power to them at all times - no switches or dimmers - in order to work. Of course, that has some impact on how you use them, since you have made your existing switches get in the way. Once nice feature, the bulbs turn on to a normal white every time you power them - so if there is a switch, you can always get to that white state by flicking it off then on.

Now, I don't know about you folks, but controlling lights from a phone app is not super useful to me. "Hey, let me turn on the lights... now where is my phone, I can't see in the dark... ok, let's fire up the app - hold on... Ok, great, they're on." I want the lights to respond to my remote or the wall switch. And I didn't want to spend a pile of cash on proprietary stuff that will never serve any other purpose.

Parts list:
- Raspberry Pi (very inexpensive and low-power computer) running Raspbian (Linux)
- USB-UIRT (USB IR transmitter / receiver - I had one lying around, you could get something cheaper)
- Logitech Harmony One remote

Summary of what I did:
1) Install OpenHAB on the Pi. This allows me to define scenes and a bit more advanced control once I introduce Z-Wave switches (more on that later... once it works) as well as lots of more automation stuff - like turning on the porch light when I get home if it is dark.
2) Configure OpenHAB to talk the Hue bridge. Pretty easy - just followed the instructions in OpenHAB.
3) Added Items to OpenHAB, one for each of the 3 bulbs, and 2 counters to control scene/mode settings
4) Added a set of Rules to define room scenes and control the 2 front lights separate from the one in back when the scene counter items change value.

At this point, everything was working in OpenHAB. That's all well and good, but I'm still controlling things with an app on a phone (not efficient) or by typing commands into an OSGi terminal (really really not efficient). Time to make the remote work.

5) Install LIRC on the Pi. For those that haven't used LIRC - it provides an interface for triggering actions when IR signals are received.
6) Configure LIRC to talk to the UIRT. This is a pain every time I do it, I could not hope to document the process... I think it is easier with other devices.
7) Capture IR codes from the remote to make an lirc.conf file. lirc.conf holds the IR codes and maps them to simpler names. I picked some random lighting device on the Harmony and used LIRC's irrecord to record them into an lirc.conf.

Great - now LIRC receives IR codes. But what do we do with them?

8) Build some .sh scripts to tell OpenHAB to do things. This sounds really hard, but it isn't. You can control OpenHAB by sending little web requests to the REST API, so each of these is a really simple curl command to send something like "1" or "2" to a specific URL. I have scripts for "allLightsOn", "allLightsOff", "movieScene", etc. They are one line each, and the only thing that makes them long lines is the OpenHAB URL.

Cool - now we can set the lights from scripts. We just need to trigger them with IR pulses.

9) Configure the global lircrc file. This file tells LIRC what to do when it sees an IR code that it knows. There is a great option called "irexec" which will run those scripts for us. So for each code, we associate it with a script.
10) Update the programming on the Harmony to put the light controls in useful places. I use the Up/Down rocker to turn the lights on/off in all Harmony Activities. I also added some "additional buttons" (the ones on the little screen) to do more advanced things. Now - the really cool one - you can define sequences of commands and put those on a button. For my PS3 I have the play button set to send the "movie scene" light command followed by play, and for pause I have it set to send pause then "lights dim white".

The effect is pretty awesome and very professional looking.

If you've gotten this far, you probably have questions. I'm happy to go into more detail on any step and guide people to documentation on any of the tricky bits.

I'm currently working on getting a Z-Wave scene controller added to the mix so I can also control the lights from the wall without this strange switching situation that is going on now. More on that when I get it to work.


----------



## ALMFamily (Oct 19, 2011)

That sounds really cool Jim - we want pictures!


----------



## MrAngles (May 1, 2012)

Very cool. Does OpenHAB just control lights, or with a Z-Wave controller will it also do other automation such as thermostats?


----------



## jimbodude (Jul 26, 2011)

I'll try to get a picture or video or something.

OpenHAB does tons of stuff. It supports Z-Wave. I'm not certain about Z-Wave thermostats, but I don't see why not. Check out the list of bindings for supported equipment / protocols.
https://github.com/openhab/openhab/wiki

I'm using it as sort of a bridge between IR / ZigBee / Z-Wave and to centralize some of the automation logic in something that is easy (for me) to work with. I'm a software guy, so these rule scripts really fit my way of thinking.


----------



## MrAngles (May 1, 2012)

jimbodude said:


> I'm using it as sort of a bridge between IR / ZigBee / Z-Wave and to centralize some of the automation logic in something that is easy (for me) to work with. I'm a software guy, so these rule scripts really fit my way of thinking.


Yeah I can appreciate that. Plus I like the idea of adding/replacing individual components to an automation system when something fails or a newer version is needed, rather than replacing an entire expensive all-in-one controller unit.


----------



## MrAngles (May 1, 2012)

I see a lot of bindings for IP based controls and various RF protocols, is there an IR command option as well? I'd love to be able to turn all my A/V equipment off from upstairs, or even from outside of the home.


----------



## jimbodude (Jul 26, 2011)

There are two problems with the big proprietary systems, for me - 1) Price - I don't have an extra $10k to drop in a lighting system 2) Selection - each system is only good at some things (lights, sensors, HVAC, pretty switches, etc), but I want all the things and no vendor lock-in... So I think bridges are the only rational answer today. There are a couple of interesting bridges on the market for 200-300USD, but they don't have IR.

I have not seen any good IR to home automation bridge products. Not to say that there aren't any, but I have looked a fair bit. You can get IR repeaters and such, sure, or boxes you put on your network where you can tell it what IR command to send and it sends it to a specific place (see Global Cache IR), but to get nice clean IR in/out and bridge that to a HA system seems to be a bit of a challenge. Look at all the hoops I had to go through... I've seen a few IR controlled light switches, but they were extremely limited functionality and quite expensive (and ugly...), but I don't remember who made them.

A while back, I used LinuxMCE, which actually has a very advanced IR send capability. That was probably the best IR device controller I've seen. It was smart enough to route the IR signals to the right blasters (across any number of rooms) and whatnot, and it had all the codes mapped back to commands. It has Z-Wave support and some other protocols, and some basic media management capabilities. There were plenty of other problems with that system, including stability, complexity, and hardware requirements, so I don't use it anymore.

The Harmony is pretty much where I'm at for IR control, though I am pushing the limits on what it can be programmed to do. It's also completely useless for more than one room. For outside, I've been looking at getting an RF-to-IR adapter and just use an RF remote to control that one isolated system.


----------



## MrAngles (May 1, 2012)

I have Lutron Maestro IR dimmers which look and work great, as long as you don't need to control more than one. They all use the same non-adjustable IR codes, so I currently control two zones by having one dimmer in my equipment rack (outside the theater room) and one on the wall in the room, with the harmony 900 set up to control one via RF extender and one via direct IR. Beyond that you need the Lutron Spacer system, which gets close to the same price range as a Grafik Eye or Z-Wave or similar system anyway.

As far as a RF to IR adapter goes, there's the Harmony smartphone app, but it's programmed just like a regular harmony remote, where it assumes it's the only remote you're ever using for the equipment, and gets confused if anything is already on. I would love an IP-based IR controller that would just let me send Off commands to all of my equipment without having to be in an "activity."


----------

