Machine Learning for the Phillips Hue

Posted on February 16, 2016 in docker, machine learning, technology, node, python, hue

Machine learning is being used in almost everything we use today. From Netflix recommendations to Nest devices that predict and change the temperature of your home, it's pretty incredible how we're applying the technology in a diverse manner.

I wanted to build something for the Phillips Hue that would learn my usage patterns and make predictions based on the observed behavior.

Meet Hubert

Hubert is a machine-learning application that records, learns, and predicts your light usage patterns. Over time, Hubert will make predictions on the current light states and change any non-regular lights automatically. This can be especially useful when you've left to walk the dog but forgot to shut off the lights. Hubert can even learn when you arrive back home and start to make these changes for you automatically.

How It Works

Hubert is built and run using Docker. A series of containers are run in parallel to analyze, record, learn, and make changes to your light usage patterns.

There are four main Docker images that make up Hubert.

  1. hubert - The main processor; Sends updates to Cassandra container; Processes RabbitMQ messages to alter the state of the lights
  2. learn - Machine learning algorithm; Learns from usage and sends messages to RabbitMQ for processing
  3. rabbitmq - A RabbitMQ instance used for publish-subscribe
  4. cassandra - A Cassandra instance used for data collection

Recording Behavior

First, we need a way to log and store behavior of the lights. The Hue Bridge provides us with a robust API that we can use to fetch information about the state of our lights such as bri, sat, hue, on, reachable, and so much more. Almost all of this information is important to us and so we record it to a Cassandra database every 10 seconds via a daemon written in Node.js.

Learning Behavior

Rather than stick to one language that could do this job fairly well, Hubert uses proven tools such as Python and scikit-learn to build a ridge regression model that can predict future states of our lights.

The first few weeks of data collection are to train the ridge regression model. Hubert does not start altering the state of your lights until enough data has been collected and the mean squared error is within an acceptable limit.

Hubert's algorithm is simple in that it loops over a range of polynomials (0-9) and a range of alphas (0.0, 1e-8, 1e-5, 1e-1) to determine the fit that best minimizes the test error. There are plans to implement k-fold cross-validation or something similar to best fit smaller amounts of data.

Processing Predictions

Hubert's machine-learning algorithm will alter the state of your Hue lights if:

  • enough data has been collected; and
  • our RSS is less than 0.1 (we're right 90% of the time)

When both of these conditions are met, the learn image will send a message via RabbitMQ. The hubert image in turn picks up this message and updates the Hue lights via the API.


You can find all the source for Hubert on Github


I openly welcome feedback. If you find a bug or issue, you can open an issue or submit a pull request on Github.

Cheers, Tom

Found Hubert to be useful? Please let me know!

Thomas Lackemann :)


Tom is the founder of Astral TableTop. He's a homebrewer, hiker, and has an about page. Follow @tlackemann on Twitter for more discussions like this.