Rules File Help for ADB FireStick

I am currently attempting to connect my Amazon Fire TV Stick to OpenHAB. I am in need of some help (most likely with .rules files).

Here is what is working so far.
Currently, I have the FireTV Stick correctly working on the network using ADB. I have set up batch files (OpenHAB is in a Windows environment) that control the different functions of the TV Stick.
I have set up a string type item in HABPanel that, when checking the item’s state in the terminal, correctly updates the state of the item.

What I need is the commands being sent by HABPanel to trigger the scripts (batch files) that I have created. If I am correct, that is done through a .rules file. I have attached the file that I made below. It doesn’t work, and any help you could give would be greatly appreciated.

import org.openhab.core.library.types.*

rule "FTVBack"
when

        Item fireTVStick received command [back]
then
	executeCommandLine("C:\openHAB2\customScripts\pressFireTV\FTVBack.bat")

end

rule "FTVDown"
when
	Item fireTVStick received command [down]
then
	executeCommandLine("C:\openHAB2\customScripts\pressFireTV\FTVDown.bat")

end

rule "FTVEnter"
when
	Item fireTVStick received command [enter]
then
	executeCommandLine("C:\openHAB2\customScripts\pressFireTV\FTVEnter.bat")

end

rule "FTVHome"
when
	Item fireTVStick received command [home]
then
	executeCommandLine("C:\openHAB2\customScripts\pressFireTV\FTVHome.bat")

end

rule "FTVLeft"
when
	Item fireTVStick received command [left]
then
	executeCommandLine("C:\openHAB2\customScripts\pressFireTV\FTVLeft.bat")

end

rule "FTVMenu"
when
	Item fireTVStick received command [menu]
then
	executeCommandLine("C:\openHAB2\customScripts\pressFireTV\FTVMenu.bat")

end

rule "FTVNext"
when
	Item fireTVStick received command [next]
then
	executeCommandLine("C:\openHAB2\customScripts\pressFireTV\FTVNext.bat")

end

rule "FTVPlayPause"
when
	Item fireTVStick received command [playpause]
then
	executeCommandLine("C:\openHAB2\customScripts\pressFireTV\FTVPlayPause.bat")

end

rule "FTVPrevious"
when
	Item fireTVStick received command [previous]
then
	executeCommandLine("C:\openHAB2\customScripts\pressFireTV\FTVPrevious.bat")

end

rule "FTVRight"
when
	Item fireTVStick received command [right]
then
	executeCommandLine("C:\openHAB2\customScripts\pressFireTV\FTVRight.bat")

end

rule "FTVUp"
when
	Item fireTVStick received command [up]
then
	executeCommandLine("C:\openHAB2\customScripts\pressFireTV\FTVUp.bat")

end

rule "FTVInitializeServer"
when
	Item fireTVStick received command [initializeserver]
then
	executeCommandLine("C:\openHAB2\customScripts\pressFireTV\FTVInitializeServer.bat")

end

So HABPanel is sending commands to the fireTVStick?

Are these commands literally “[back]” and such with the square brackets?

You do not nor should you import anything from org.openhab in a Rules in OH 2.x.

Currently the received command X triggers only works for the enumerated types (i.e. ON/OFF, OPEN/CLOSED, etc) and do not work with Numbers and Strings.

So, assuming that HABPanel is successfully sending commands to fireTVStrick as Strings you need a rule that looks something like:

rule "FTV Commands"
when
    Item fireTVStick received command
then
    switch receivedCommand.toString {
        case "[back]": executeCommandLine("C:\...
        case "[down]": executeCommandLine("C:\...
        ...
    }
end

Yes, the commands are literally strings such as [back], [down], etc… I just kept it simple. I did not know that the received command X only worked with enumerated types. Good to know, thank you.

The switch looks simple enough, thank you for that!

Note this is a bug and there is an issue open for it. No idea when/if it will get fixed though. :frowning:

I tried creating that .rules file, but with no success. I know this isn’t a “technical support” area, but I am lost as to what I am doing incorrectly. Do the .rules files need to be manually called from another part of OH2 - sorry, I’m very new to rules and openHAB in general.

I have HABPanel successfully pushing strings to update the status of my item (fireTVStick), and have the batch files successfully controlling my fireTVStick itself over ADB. The only problem is getting the state changes to execute the batch files - which is what the rules file (that I was kindly helped with) is for. I will include it below for reference, but not sure what I’m missing…again, any help would be greatly appreciated.

THE BELOW SCRIPT IS OUTDATED. SEE UPDATED SCRIPT FURTHER IN THREAD

rule "FTV Commands"
when
    Item fireTVStick received command
then
    switch receivedCommand.toString {
        case "[back]": executeCommandLine("C:\openHAB2\customScripts\pressFireTV\FTVBack.bat")
	case "[down]": executeCommandLine("C:\openHAB2\customScripts\pressFireTV\FTVDown.bat")
        case "[enter]": executeCommandLine("C:\openHAB2\customScripts\pressFireTV\FTVEnter.bat")
        case "[home]": executeCommandLine("C:\openHAB2\customScripts\pressFireTV\FTVHome.bat")
        case "[initializeserver]": executeCommandLine("C:\openHAB2\customScripts\pressFireTV\FTVInitializeServer.bat")
        case "[left]": executeCommandLine("C:\openHAB2\customScripts\pressFireTV\FTVLeft.bat")
        case "[menu]": executeCommandLine("C:\openHAB2\customScripts\pressFireTV\FTVMenu.bat")
        case "[next]": executeCommandLine("C:\openHAB2\customScripts\pressFireTV\FTVNext.bat")
        case "[playpause]": executeCommandLine("C:\openHAB2\customScripts\pressFireTV\FTVPlayPause.bat")
        case "[previous]": executeCommandLine("C:\openHAB2\customScripts\pressFireTV\FTVPrevious.bat")
        case "[right]": executeCommandLine("C:\openHAB2\customScripts\pressFireTV\FTVRight.bat")
        case "[up]": executeCommandLine("C:\openHAB2\customScripts\pressFireTV\FTVUp.bat")
    }
end

Are you sure that is the item that is being updated by HABpanel?

Add a logInfo(“test”, “some message”) to the top of the rule.

See

To debug the executeCommandLine calls themselves.

Are the brackets optional? afaik switch would be like

switch (value) {
    case a : { }
    case b : { }
    ...
}

The brackets are optional if the case statement consists of only one line, similar to how they are optional for if statements with only one line.

When checking the logs, the state of my item is being updated by HABPanel. Sample log entry:

2017-08-26 15:19:52.895 [ItemCommandEvent          ] - Item 'ftv1' received command down
2017-08-26 15:19:52.895 [ItemStateChangedEvent     ] - ftv1 changed from next to down

I have a Command type thing that’s input is bound to the item 'ftv1’
Is the ‘command’ field where I should be calling the .rules file? I called the batch files directly from here, and it worked, but obviously since the buttons in HABPanel only have one state, I couldn’t set up a bunch of commands to use as button pushes.

But you wrote the rule to trigger on changes to fireTVStick, not ftv1.

The trigger is the event that causes the role to execute.

I’m sorry, I should have included the new rules file. I updated to 2.1 and did a little bit of housekeeping on the names of my items and things. I changed the rule to ‘ftv1’

rule "FTV Commands"

when
    Item ftv1 received command
then
    switch receivedCommand.toString {
        case "[back]": executeCommandLine("C:\AutoDI\customScripts\FTVCommands\FTVBack.bat")
	case "[down]": executeCommandLine("C:\AutoDI\customScripts\FTVCommands\FTVDown.bat")
        case "[enter]": executeCommandLine("C:\AutoDI\customScripts\FTVCommands\FTVEnter.bat")
        case "[home]": executeCommandLine("C:\AutoDI\customScripts\FTVCommands\FTVHome.bat")
        case "[initializeserver]": executeCommandLine("C:\AutoDI\FTVCommands\pressFireTV\FTVInitializeServer.bat")
        case "[left]": executeCommandLine("C:\AutoDI\customScripts\FTVCommands\FTVLeft.bat")
        case "[menu]": executeCommandLine("C:\AutoDI\customScripts\FTVCommands\FTVMenu.bat")
        case "[next]": executeCommandLine("C:\AutoDI\customScripts\FTVCommands\FTVNext.bat")
        case "[playpause]": executeCommandLine("C:\AutoDI\customScripts\FTVCommands\FTVPlayPause.bat")
        case "[previous]": executeCommandLine("C:\AutoDI\customScripts\FTVCommands\FTVPrevious.bat")
        case "[right]": executeCommandLine("C:\AutoDI\customScripts\FTVCommands\FTVRight.bat")
        case "[up]": executeCommandLine("C:\AutoDI\customScripts\FTVCommands\FTVUp.bat")
    }
end

I am now gettinga new error message when I look at the logs:

19:37:42.782 [ERROR] [hab.binding.exec.handler.ExecHandler] - An exception occurred while executing 'C:\AutoDI\conf\rules\ftvCommands.rules' : 'Cannot run program "C:\AutoDI\conf\rules\ftvCommands.rules": CreateProcess error=193, %1 is not a valid Win32 application'

I don’t have any spaces in the file path that would cause an issue.

You need to escape the \ in the paths perhaps

E.g.

C:\\AutoDI\\customScripts\\FTVCommands\\FTVBack.bat

According to this thread, CreateProcess requires an executable.

A .rules file is not an executable.

Interesting. When I had the Command “thing” directly call and execute the batch files, it worked. I guess I would be able to make a “thing” for each command (each batch file), but would need a way to set the state of the item (used to trigger the command) back to NULL immediately after each trigger.

Use the express binding or use a rule for this.

aspinwalld,

would you share one of you bat files you are using with your firestick. I would love to see an example. Also one of your items.

Sure! I haven’t had time to work on this in a week or so, so don’t have the items bound yet, but the working batch files look something like this:

Initializes ADB: I just run this every night at 3am, and have a HABPanel button that can manually run if needbe.

adb kill-server
adb start-server
adb connect 10.0.88.xxx
adb disconnect

The button push files are like this (this one triggers ‘home’):

adb connect 10.0.88.xxx
adb shell input keyevent 3
adb disconnect

Great thanks a lot. I need to implement something like this as well. Will you also give an example of your items file. I’m sure I can figure it out but for those that want to copy you can you give an example.

I’m still working out what the best logical way to handle items will be. Once I have this fully implemented and working I will create a new topic with step-by-step instructions on getting it working in openHAB and HABPanel (and will link that here).