Hi Andreas,
Welcome to the community!
The most up to date documentation about the exec binding can be found here. Unfortunately, it seems that the rule example contains an error and as a result the sample does not work (the trigger execution should be before the while-loop).
I have stripped down the demo example to the bare minimum:
Content of exec.things
(you can name it anything you like, as long as it has the things
file extension):
Thing exec:command:yourcommand [ command="/tmp/exec-test.sh %2$s", interval=0, autorun=false ]
Here you define what command needs to be run, in this case a the /tmp/exec-test.sh
shell script. The %2%s
will be replaced by the (optional) command line arguments.
Content of exec.items
(you can name it anything you like, as long as it has the items
file extension):
Switch YourTrigger
// state of the execution, is running or finished
Switch yourcommand {channel="exec:command:yourcommand:run"}
// Arguments to be placed for '%2$s' in command line
String yourcommand_Args {channel="exec:command:yourcommand:input"}
Here we have defined the YourTrigger
switch. By toggling this switch, you will trigger a rule which will then trigger the execution of your shell script.
The yourcommand
and yourcommand_Args
items are used in the rule. You don’t use them yourself to manually control the thing.
Content of exec.sitemap
(you can name it anything you like, as long as it has the sitemap
file extension AND the sitemap
keyword is followed by the name of the sitemap file without the file extension):
sitemap exec label="Your Value"
{
Frame {
Switch item=YourTrigger
}
}
You can display the sitemap in your browser by opening the following URL: http://<your openhab server ip>:8080/basicui/app?sitemap=exec
(If you’ve named your sitemap differently, then replace exec
by the name of your sitemap)
The sitemap file is also used to determine the layout of the mobile apps.
Content of exec.rules
(you can name it anything you like, as long as it has the rules
file extension):
rule "Your Execution"
when
Item YourTrigger changed
then
if(YourTrigger.state == ON){
yourcommand_Args.sendCommand("ON")
}else{
yourcommand_Args.sendCommand("OFF")
}
// Optional: give the event bus time to update the item
// Thread.sleep(250)
// Trigger execution
yourcommand.sendCommand(ON)
end
When the YourTrigger
item is changed by toggling the switch, the rule is triggered. When the state is ON
then the yourcommand_Args
item is set to ON
. Since yourcommand_Args
is an Item
, we need to use sendCommand to update its state. The new state is then sent to the openHAB eventbus from where it is picked up again and assigned to the item. Note that this is asynchronous so you may need to slow down the rule a bit by adding a Thread.sleep(...)
.
So you cannot do the following in openHAB to assign a state to an item: yourcommand_Args = "ON"
.
The youcommand_Args will contain the command arguments. So in this case when you switch the switch on, then the command line argument will be be ON
and when you switch the switch off then the command line argument will be OFF
.
To demonstrate this, I have created a little shell script which will save the command line argument to the /tmp/exec-test.out
file.
Content of the /tmp/exec-test.sh
script that will be executed by the exec binding. Note that the openhab
user will execute this script so it must be executable by the openhab
user.
#!/bin/sh
echo $* > /tmp/exec-test.out
When you toggle the switch, the command line argument will be stored in the /tmp/exec-test.out
file.
Hope this helps!