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!
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
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.
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.
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.
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).