Home Automation Reference

As a project, I wanted to use a Raspberry Pi, Phillips Hue and Nanoleaf to create some pretty sweet lighting and a control panel as an alternative to pulling out my phone. This post contains some reference notes on my setup.

Hardware

Raspberry Pi - This runs openHAB and Homebridge.

Raspberry Pi Touch Display

Nanoleaf Light Panels + Rythm Module & Controller - These are gorgeous colored LED light panels that you connect together in whatever layout you like. More on these later.

Some Hue lights and a Phillips Hue Bridge to control all the Hue lights, this connects to my network via ethernet and connects to the devices via Zigbee.

I also have some Phillips Hue Tap Switchs, Phillips Hue Dimmer Switches, and Phillips Hue Motion Sensors in all the right places.

Software

About openHAB

I am currently using this to create a control panel for my smart home system on the Raspberry Pi. It is a super powerful system that I have only scratched the surface of.

The UIs I care about are PaperUI which is the web interface that is used for configuration, HABPanel which is the dashboard I use on my Raspberry Pi to control my home, and Karaf Console A command line interface that is useful to view the log and probably other stuff. To access the console you need to SSH into the Pi and then run ssh -p 8101 openhab@localhost, after which you can run log:tail to view the log.

In openHAB the entities I care about for my usage are Bindings which connects to Hue, Things these are all my devices, and Items which are how I interact with the devices.

And then on top of that we have:

Rules - These can be triggered based on status/property change (of items, groups, things, or the system) or time. They’re written in a Domain Specific Language.

Services: HABPanel is a “service” for some reason, so is the crappy voice interpreter.

Scripts: I Haven’t really explored this.

We can perform the basic functions, kile on off and change color on the Hue devices through the openHAB web interfaces, but to do more, we can get into the code.

Creating scenes programatically

In PaperUI, I created an item called Scenes with a type of String and then in HABPanel I created a button with an Action type of “Send a fixed command to an item” and selected Scenes as the openHAB item. The command value I chose to send for this button is “Excite”. On the Raspberry Pi I created the file /etc/openhab2/rules/scenes.rules with the following contents:

import org.openhab.core.library.types.HSBType

rule "Scenes"
when
    Item Scenes received command
then
   logInfo("Scenes", "state changed to: " + receivedCommand)
   switch(receivedCommand) {
      case "Excite": {
         val colorCarolinaBlue = HSBType.fromRGB(149, 182, 249)
         Lounge_Color.sendCommand(colorCarolinaBlue)
      }
   }
end

openHAB Nanoleaf Integration

In PaperUI, I created an item called Nanoleaf with a category of ColorLight and a type of Color. In HABPanel I created a Switch, Colorpicker, and Knob connected to this Item.

The Nanoleaf Open API Postman Docs were useful at this point.

To retrieve Auth token do a POST request to http://[[IP Address]]:16021/api/v1/new

File: /etc/openhab2/rules/nanoleaf.rules

rule "Nanoleaf"
when
    Item Nanoleaf received command
then
   logInfo("Nanoleaf", "state changed to: " + receivedCommand)

   var cmd = "/etc/openhab2/shellscripts/nanoleaf.sh"

   switch(receivedCommand) {
      case receivedCommand instanceof OnOffType: cmd = cmd + receivedCommand
      case receivedCommand instanceof HSBType: cmd = cmd + "COLOR " + receivedCommand.toString.replace(",", " ")
      case receivedCommand instanceof PercentType: cmd = cmd + "DIM " + receivedCommand
   }

   logInfo("nanoleaf", "cmd : " + cmd)
   val results = executeCommandLine(cmd, 1000)
//   logInfo("nanoleaf", "res: " + results)
end

File: /etc/openhab2/shellscripts/nanoleaf.sh

#!/bin/sh
IP_ADDRESS=[[IP Address goes here]]
AUTH_TOKEN=[[Auth token goes here]]

if [ "$1" = "ON" ]; then
    echo "switch on"
    curl -X PUT -H "Content-Type: application/json" -d '{"on" : {"value":true}}' "http://$IP_ADDRESS:16021/api/v1/$AUTH_TOKEN/state/"
elif [ "$1" = "OFF" ]; then
    echo "switch off"
    curl -X PUT -H "Content-Type: application/json" -d '{"on" : {"value":false}}' "http://$IP_ADDRESS:16021/api/v1/$AUTH_TOKEN/state/"
elif [ "$1" = "DIM" ]; then
    echo "dim to $2"
    curl -X PUT -H "Content-Type: application/json" -d '{"brightness" : {"value":'$2'}}' "http://$IP_ADDRESS:16021/api/v1/$AUTH_TOKEN/state/"
elif [ "$1" = "HUE" ]; then
    echo "hue to $2"
    curl -X PUT -H "Content-Type: application/json" -d '{"hue" : {"value":'$2'}}' "http://$IP_ADDRESS:16021/api/v1/$AUTH_TOKEN/state/"
elif [ "$1" = "SAT" ]; then
    echo "sat to $2"
    curl -X PUT -H "Content-Type: application/json" -d '{"sat" : {"value":'$2'}}' "http://$IP_ADDRESS:16021/api/v1/$AUTH_TOKEN/state/"
elif [ "$1" = "COLOR" ]; then
    echo "hue to $2"
    echo "sat to $3"
    echo "dim to $4"
    curl -X PUT -H "Content-Type: application/json" -d '{"sat" : {"value":'$3'}, "hue" : {"value":'$2'}, "brightness" : {"value":'$4'}}' "http://$IP_ADDRESS:16021/api/v1/$AUTH_TOKEN/state/"
fi