Execute command on switch OFF?

In openhab 1 I had a switch to control my mediacenter. ON connected to the OpenWRT router and issued a command for etherwake (mu), OFF connected to the mediacenter and issued a shutdown command:

Switch BH_Mediacenter_Power_Active   "Mediacenter Power" (gMediacenter) { autoupdate="false", nh="", exec=">[ON:ssh sshcommander@ sudo mu] >[OFF:ssh sshcommander@ sudo systemctl suspend -i]"}

As far as I understand the documentation for the 2.0 bindung, I have to define a thing now

Thing exec:command:mediacenter_up [command="ssh sshcommander@ sudo mu"]

and an Item that uses the run channel?

Switch BH_Mediacenter_Power_Active { channel="exec:command:mediacenter_up:run", autoupdate="false", nh="" }

but how do I create the OFF functionality?

As far as I know, if you want to send different commands through exec, you would have to use channel exec:command:yourthing:input and define the thing like

Thing exec:command:yourthing [command="./scripts/myscript.sh %2$s"]

Now you can write a script myscript.sh, which uses the input to decide what to do. Be aware that ON and OFF are sent as 0 and 1 to the script.

I feared that would be the case.
So now I have this:

Thing exec:command:mediacenteronoff [command="/etc/openhab2/exec/mediacenter.sh %2$s", timeout=20, autorun=false]


Switch BH_Mediacenter_Power_Active   "Mediacenter Power" (gMediacenter) { channel="exec:command:mediacenteronoff:run"}


echo x$1
if [[ "x$1" == "x1" ]]; then
    echo $(date) " Switching on" >> /tmp/mediacenter
    ssh sshcommander@ sudo mu
elif [[ "x$1" == "x0" ]]; then
    echo $(date) " Switching off" >> /tmp/mediacenter
    ssh sshcommander@ sudo systemctl suspend -i
    echo $(date) " Unknown parameter\"$1\"" >> /tmp/mediacenter
exit 0

Apparently, nothing happens when I operate the switch. /tmp/mediacenter stays empty. The script works and can be run on the shell. (Permissions are correct.)
As usual, there is not the slightest hint of anything helpful in the logs. (There wasn’t even when I accidentally had an uneven number of quotation marks in the item line … I am seriously irritated by OpenHABs error handling …)


Switch BH_Mediacenter_Power_Active   "Mediacenter Power" (gMediacenter) { channel="exec:command:mediacenteronoff:input"}

instead :wink:

1 Like

That executes only for ON apparently, but only when I remove the %2$s from the thing. I get this error in the log:

22:05:32.318 [ERROR] [hab.binding.exec.handler.ExecHandler] - An exception occurred while formatting the command line with the current time and input values : 'Format specifier '%2$s''

As soon as it is there, nothing gets executed. I also tried to remove the commas from the thing definition, with no change.

I seriously wonder how other people work with openHAB. I am constantly frustrated because there doesn’t seem to be a reasonable way find to errors.