Control Philips Hue with a wireless keyboard

Last year I acquired a Philips Hue smart lighting system for my bedroom, kitchen and living room – a total of 8 bulbs. Overall the setup is great but there’s one drawback: as the control of the lights relies on the smart phone application that needs to be paired with the system, occasional visitors can’t easily control the lights. The standard solution for “app-less” control would be the Hue Tap, but the Tap only has four buttons so I’d need at least three of them to provide sufficient control. I wanted to have a more extendable solution for controlling the lights so I purchased a wireless Microsoft All-in-One Media Keyboard, hooked it up on a Raspberry Pi and wrote a simple Python script to control Philips Hue system over the Hue API. The initial version covers only selecting different lighting scenes, but the future versions could utilize the rest of the keyboard for e.g. adjusting individual lights.

Video of the system in action:

The hardware

The software

I use the phue Python library to interface with the Hue Bridge. It’s an easy-to-use library with no extra dependencies. I also used a getch() recipe from ActiveState to read the keyboard presses. The control script itself is very straightforward – some might say it has too many hard-coded parameters (keybindings, scene ids) but as the whole script fits in one screen, there isn’t much point to add unnecessary bloat to it.

from phue import Bridge
from getch import getch
from time import sleep

# Taken from phue Basic Usage guide
b = Bridge('') # IP of my Hue Bridge
# If the app is not registered and the button is not pressed,
# press the button and call connect() (this only needs to be run a single time)
# Get the bridge state (This returns the full dictionary that you can explore)
api = b.get_api()

# List all the Hue Scenes on the Bridge
scenes = api['scenes']
for k,v in scenes.iteritems():
    print "%s: %s" % (k, str(v))

# Flash the lights red to indicate that the script is running and connection to Bridge works.
# Comment this section out if you don't need visual indication when the script has loaded.
b.set_group(0, 'scene', '627ca62d9-on-0') # Custom scene that turns all lights red
b.set_group(0, 'scene', 'dd58cdb16-on-0') # Custom scene to turn all lights off
print 'Hue keyboard controller initiliazed.'

bindings = {
    '1': 'eb399f66c-on-0', # OFF @ Livin groom
    '2': '77c7142e3-on-0', # Dim warm light @ Living room
    '3': 'b224e1b01-on-0', # Bright cold light @ Living room
    'q': 'ec54cdb06-on-0', # OFF @ Kitchen
    'w': 'b641c6ba3-on-0', # Dim warm light @ Kitchen
    'e': '2cd81a233-on-0', # Bright warm light @ Kitchen
    'r': '8f3e94927-on-0', # Bright cold light @ Kitchen
    'a': '4379442bb-on-0', # OFF @ Bedroom
    's': 'a0b3c961a-on-0', # Very dim warm light @ Bedroom
    'd': '617a8be9a-on-0', # Reading spotlight @ Bedroom

    keypress = getch()
    # Exit script on . press
    if keypress == '.':
        scene_id = bindings[keypress]
        b.set_group(0, 'scene', scene_id)

print 'Hue keyboard controller stopping.'

Usage to control the Philips Hue lights

The script must be registered to the Hue Bridge. The procedure is the same as with any other Hue app – press the button on top of the Bridge and run the script. The script will first print all the available scenes on the Bridge. As the scene ids are system specific, the script needs to be updated accordingly. Simply terminate the script by pressing “.” key after it has listed the scenes on the Bridge.
root@ *** # python
eb399f66c-on-0: {u'active': True, u'lights': [u'6', u'7', u'8'], u'name': u'Living room OFF on 0'}
77c7142e3-on-0: {u'active': True, u'lights': [u'6', u'7', u'8'], u'name': u'Dim living room on 0'}
Hue keyboard controller initiliazed.
Hue keyboard controller stopping.
root@*** #

Let’s say I want to bind key ‘1’ to turning off the living room lights. I take the corresponding scene id eb399f66c-on-0 and assign it as the value for '1' in the bindings dictionary -> Pressing 1 will turn the living room lights off.

After configuring all the desired scenes to their corresponding key bindings, relaunch the script and you’re good to go.

Leave a Reply

Your email address will not be published. Required fields are marked *