OpenHab2 Rules to Control Roku

One of the most recent features I've discovered with OpenHab2 is its ability to execute shell commands. With this ability, you can easily send commands to Roku. If you don't know, Roku can be issued commands via HTTP (using their External Control Protocol). So, if you'd like to open apps, hit play/pause or go home, you can simply use curl or wget to issue these commands. You can control this by adding these commands to a rule that corresponds to a switch item. If you make these items "Switchable", then you can control them via Google Assistant.

I had to do a little more work with Google routines so that I could trigger these switches with various different commands. This allows a more natural conversation.

In order to get this working, you need to create the Items and the Rules.

The /etc/openhab2/items/home.items should include these switches. (If you would like to expand your list, just add the switches here.)

Switch          roku_home               "Roku Home"             ["Switchable"]
Switch          roku_playPause          "Roku PlayPause"        ["Switchable"]
Switch          roku_netflix            "Roku Netflix"          ["Switchable"]
Switch          roku_plex               "Roku Plex"             ["Switchable"]
Switch          roku_hbo                "Roku HBO"              ["Switchable"]
Switch          roku_amazon             "Roku Amazon"           ["Switchable"]
Switch          roku_hulu               "Roku Hulu"             ["Switchable"]

At the bottom of your /etc/openhab2/rules/home.rules file, add these rules. Make sure they correspond with the appropriate Item or you will have random things opening.

rule "Roku On"
        when
                Item    roku_home       received command ON
        then
                executeCommandLine("curl -d '' http://192.168.1.111:8060/keypress/Home")
        end

rule "Roku PlayPause"
        when
                Item roku_playPause     received command ON
        then
                executeCommandLine("curl -d '' http://192.168.1.111:8060/keypress/play")
        end

rule "Roku Netflix"
        when
                Item roku_netflix       received command ON
        then
                executeCommandLine("curl -d '' http://192.168.1.111:8060/launch/12")
        end

rule "Roku Amazon"
        when
                Item roku_amazon        received command ON
        then
                executeCommandLine("curl -d '' http://192.168.1.111:8060/launch/13")
        end

rule "Roku Plex"
        when
                Item roku_plex  received command ON
        then
                executeCommandLine("curl -d '' http://192.168.1.111:8060/launch/13535")
        end

rule "Roku Hulu"
        when
                Item roku_hulu  received command ON
        then
                executeCommandLine("curl -d '' http://192.168.1.111:8060/launch/2285")
        end

rule "Roku HBO"
        when
                Item roku_hbo   received command ON
        then
                executeCommandLine("curl -d '' http://192.168.1.111:8060/launch/8378")
        end

For the list of links, there are two ways to obtain the app ID that you are trying to launch; You can either return the information with curl or wget, or just trust the internet. As stated on Roku's ECP documentation, using the following curl command in a shell will render the app ID. (note: the id attribute that corresponds to the app Title. id 12 is Netflix).

$ curl http://192.168.1.134:8060/query/active-app
<active-app\>  
<app id="12" type="appl" version="4.1.218">Netflix</app>
</active-app>

The last thing I did was make this easier for me to talk to Google. If you have the Google Home app installed on any of your devices, then you can create routines. Custom routines allow you to add custom commands that trigger events. Below are some screenshots of example routines that I've created. You will have to make sure all of the applicable Google Home/Assistant setup and integrations are in place to utilize this feature. Setting up Google Assistant is a fairly simple process; follow these instructions.

After you've completed this, then you just need to ask Google to "sync my devices" and you are all set. I'd restart the Openhab service to make sure that all changes stick.

September 29, 2019