Mike MacDonagh's Blog

Somewhere in the overlap between software development, process improvement and psychology

Howto: Controlling LightwaveRF lights with a Raspberry Pi, Flirc and Logitech Harmony One IR remote

I’ve recently got into home automation and so thought it would be fun to integrate my smart lighting with my media centre and remote control. When I watch TV or streaming video I want the main lights to go off and the side/back lights to come on in my living room. When I pause I want the main lights to come up a little and when I play I want main lights to turn off again 🙂

My setup

I use LightwaveRF devices to control my main lights (2 dimmers) and various side-lights and screen backlights, I’ve also got a LightwaveRF Link hub which allows the Lightwave’d lights to be controlled by app, but also by simple UDP packets.

I’ve got a Logitech Harmony One remote control, because it allows you to setup custom sequences and controls my TV, DVR box and sound bar.

My solution:

I decided to use a Raspberry Pi 3 (now with Wifi and Bluetooth built in!) to do the lightwave udp packets and  Flirc to interpret the IR signals and convert them into simple text commands. Then I wrote a little python program that listens to the incoming commands from the Flirc USB Infrared receiver. There are other ways of interpreting the IR commands, but this was a super simple one!

Harmony One to Lightwave RF integration

I have the Raspberry Pi setup as a device in MyHarmony using Brand: Raspberry Pi and Device: Kodi because it’s a simple profile that both the Harmony One and Flirc understand. The Flirc is also loaded with the kodi profile. This is a really an easy way to send number strings to the Pi because the number buttons/commands on the remote get turned by Flirc into number key presses and the Ok button of the remote gets translated to the Enter key. Unfortunately MyHarmony doesn’t let you assign custom sequences to a device setup  so I need to add my lighting sequences to my variouus Harmony  activities. I’ll just run through setting up one here, but the need to do this multiple times is one of the motivators for keeping it as simple as possible.

Using my LightwaveRF app I’ve setup a few “moods”:
* TV Time (dim side and back lights on, main lights off)
* Dim room (TV Time + Main lights on low dim settings)
* All on (Main lights at full brightness and side lights on)
* All off (yep)

My python script will listen for <number keys + enter> so I just need to map numbers to whatever I want the script to do. For my setup here’s my codes, there’s no need to use single digits but it’s quicker than really long ones:

0 – Exit Script
1 – Lights: TV Time
2 – Lights: All off
3 – Lights: All on
4 – Lights: Dim room
5 –  Set main lights to 5% without changing any others (for pause)
6 –  Turn main lights off without changing others (for play)
7 – Lock light changes (stop video events changing lights)
8 – Unlock light changes (re-enable video events changing lights)
999  – Reboot Rasberry Pi (script auto-starts on boot – see below)
(and some others that aren’t to do with lightwave)

Harmony Setup

I can then set up screen buttons for each activity to invoke those commands. I have an activity that already turns my TV+soundbar on, selects my DVR etc. To add lighting control to it I change the start up options and also the screen options:

a_harmony_sequence

Create simple command sequence (this one is Lights: TV, 1 + Enter)

b_activity_screen_buttons

Put it on the screen

Also, I want the lights to be set to “TV Time” whenever the various activities are started so I can customise the startup options too:

Customise activity start

Customise activity start

Finally I also want the main lights to dim up a little when I pause (or stop) and go off when I press play. To do that I customise the play and pause buttons for each activity to add a RPi command:

Add light control to pause button by making a custom sequence

Add light control to pause button by making a custom sequence

e_assign_play_dim_down_to_play

Assign the new sequence to the correct button

Raspberry Pi Setup

As I said earlier, I’ve got Flirc setup using the Kodi profile because it’s nice and easy and the Harmony remote sends <number(s)> + <Enter>. So I wrote a little script to listen for these numbers and do the various commands, then I need the script to run interactively everytime the Pi boots up. This is my very first python program, so the style may not be perfect but I decided to use Python for educational purposes (for me and my kids).

To control LightwaveRF via UDP:

If you ask the friendly support folks at LightwaveRF they’ll send you a spreadsheet of commands, and it’s on various websites but the simplest way is to simply set up your Lightwave kit in the Lightwave app on  android or iOS and then use a network sniffer like WireShark to see that happens when you press the various buttons. Oddly I’ve had trouble seeing the UDP packets that the Raspberry Pi is sending out with Wireshark, but they’re being sent because they’re controlling my lights! Either way the Lightwave commands are pretty simple text strings.

I can’t detail how to control it here because the API is subject to an NDA (I’ve no idea why, APIs should be open) but the Lightwave folks seem happy to let people use it. There’s lots of great help on the lightwaverf forums.

To send UDP commands in python 3:

LW_UDP_IP = "192.168.0.10"
LW_UDP_PORT = 9760

def send_udp(msg):
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Internet and UDP
    sock.sendto(msg.encode('utf-8'), (LW_UDP_IP, LW_UDP_PORT))
    return

To listen to inputs I created a simple while loop:

aCommand = ""
while aCommand != "0":
    aCommand = input("Command? ")
    if aCommand == "1":
        send_lightwave_command("R2FmP1","TV Time")
    etc. etc.

To make the script run interactively at startup:

Create a file called ir_commander.py in a sensible place: touch ~/source/python/ir_commander.py

Make sure it’ll be interpreted as a python script by making the first line: #!/usr/bin/python3

Make sure the file is executable: chmod + x ~/source/python/ir_commander.py

Add a file called ir_command.desktop to ~/.config/autostart with the following contents:

[Desktop Entry]
Exec=lxterminal -e "/home/pi/source/python/ir_commander.py"
Type=Application

The full code of ir_commander.py is on github if you want it. I’ll probably add a time element to it so that the lights don’t change during daylight hours, or maybe a light sensor to do it automatically! Also my remote can’t turn on my PS4 so I’m planning to get the Pi to turn it on over the network as part of a remote control sequence.

Advertisements

13 responses to “Howto: Controlling LightwaveRF lights with a Raspberry Pi, Flirc and Logitech Harmony One IR remote

  1. 60milesmile (@60milesmile) August 8, 2016 at 11:07 am

    Thanks for this Mike, I’m going to give it a try too.

    60. (found via lightwaverf forum)

  2. surreyaudiovisual October 30, 2016 at 11:43 pm

    This is amazing! And just what I was looking for…

    With Amazon echo recently adding Logitech harmony support (for hub based remotes like the elite) with your help hopefully I can get alexa to draw my curtains…dim my lights…drop projector screen…fire up projector/AV receiver/TiVo etc all with an ‘Alexa turn on the tv’ voice command…

    Amazing…thank you so much!

    • mikemacd October 31, 2016 at 12:59 am

      I’m glad you liked it. Another idea I’ve got is to develop an Echo “skill” to talk to the pi, that way it can integrate to lightwave or anything else that goes through my pi hub 🙂

  3. Luke A. November 7, 2016 at 4:28 pm

    Hi Mike,

    Great post. This is just what I am trying to setup for my father in law. We have been and purchased a Rasberry Pi and Flirc. I wondered whether you might be able to give us some guidence regarding putting the python script on the Pi please?

    The Pi is something new to both of us, I believe that I have added the Kodi profile to the Pi and Flirc (I can see Kodi when I view it through a HDMI cable anyway)?

    Thanks in advance.

    Luke

    • mikemacd November 8, 2016 at 2:36 pm

      Sure Luke, what exactly is the problem? Have you created the script on a different computer or on the pi itself?

      • Luke A November 8, 2016 at 2:52 pm

        Hi Mike,

        Excellent thank you.

        I have created it on the pi itself. (I have rasbian installed, and have used Python 3). I am a little unsure where to place the file now I have created it, and how to get the pi to launch it on startup?

        • mikemacd November 8, 2016 at 3:50 pm

          You can save it wherever you like. I created a “source” folder in my home areas “~/source” and called the script “ir_commander.py”. The bottom of the blog shows the steps required under “To make the script run interactively”.

          • Luke A November 8, 2016 at 4:31 pm

            Hi Mike,

            Okay. I think I have saved this successfully now. I am trying to run the module from within python to check it. I have used your code as a template, when I run the module I get the “Command?”. I am guessing it is at this point I should be able to type in 1,2,3 etc, or use the app to send the instruction through the flicr.

            Sorry if this all seems simplistic, as I said this is our first experience using the Pi.

            Thanks again,

            Luke

            • mikemacd November 8, 2016 at 4:53 pm

              Great. At this point once it says “Command?” you can either type the number (e.g. 123) or just point your remote and press one of your custom commands (which equates to e.g. 123). Flirc doesn’t need to be running. You can test it’s working simply by typing a “0” it should then say “Finished!”

              Remember to get this as an executable script, outside of python, you should add “#!/usr/bin/python3” to the top of the file and make sure it’s got execute permissions “chmod +x ./ir_commander.py”

              • lukestatham November 8, 2016 at 6:20 pm

                Hi Mike,

                Thanks for your help. I now have it running successfully on startup receiving commands from either the keyboard or Harmony Remote (although I can’t quite figure out how to press enter). It isn’t actually controlling the lights yet but I guess just needs a little tinkering now with the commands themselves. I’ll keep you informed.

                Thanks again!

                • mikemacd November 8, 2016 at 6:25 pm

                  Fantastic 🙂

                  For pressing if you’re using the same flirc profile as me then the key does an enter. If you look at my harmony screenshots above you’ll see they all send after the commands:
                  example harmony command

  4. Sam May 29, 2017 at 12:20 pm

    Hi Mike, I have a good overall understanding of computer science but I am not a programmer at all. I am already using a lightwave relay in my home theatre room and am thinking of investing in the Harmony Elite and Hub. Would there be any way to control the lights using RF or WiFi instead of IR ? That way there would be no need for line of sight. However, I realise that the Hub could talk to the IR based FLIRC device.

    I wish you could provide a ready made product that would work with Lightwave dim-able or regular relay switches. Would that be possible at a fee ?

    • mikemacd May 30, 2017 at 10:37 am

      Hi Sam. Harmony doesn’t do RF so that’s not an option. My solution is basically a way of getting Harmony to do this over wifi by doing [IR from harmony] -> [IR into RPi] -> [RPi UDP (maybe via wifi) to Lightwave].

      There are some other options though:

      – you can use a custom RF device (complicated and expensive). Or a consumer RF device such as a Broadlink, but that requires running an Android app permanently (and paying for it yearly). I think that’s a messy solution but I know a lot of people who think they’re great.

      – I have got a simple php page that can be set up on a server that sends commands to Lightwave. If you set that up on a server in your network (and register that machine with LWRF by pressing the button on the hub) then you can change your lights simply by calling a URL. The downsides are: needing a server running all the time (an RPi?) and actually calling a URL from a device… easy from a phone, or something like an OmniRemote app, but not apparently possible from normal remotes or Harmony. I actually have this running side by side with my IR script on my Pi.

      – IFTT, if you can trigger that somehow from a button press/device then you can get it to connect to LWRF – although it can be a bit laggy since it’s a round trip through the internet instead of local.

      Long story short, if you wanted me to build you a version of what I’ve done using Harmony and Lightwave, specifically for your needs, I could do that since I run a small software company. If you’re interested in discussing that, let me know and I’ll email you directly (I can see your email addy from the comment info).

What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: