Searching: Working example Exec-Binding 2 with a Switch running Bash Script on Raspi

Hi there,

I was reading a lot of threads here, but I am still not able to get it working.
I use OH2 with Exec-Binding 2 on my Raspberry Pi and I want to create a Switch within the Sitemap using an Item with two states (on / off), which is based on a Thing using the Exec-Binding2.
The Exec-Binding should run a Bash-Script (.sh) on the RPi incl. the parameter “ON” or “OFF”.

But nothing happens - I’ve added a line to write a log-file out of my Bash-Script, but even that does not appear after trying almost all ideas, I’d found here.

Has anybody a working example on OH2 with Exec-Binding2 for that use case?


From your post I cannot make up if you need a script to control a switch or have a switch to control a script. Below is an example of a script that controls a switch item.

The basic idea is to have the script echo “ON” or “OFF” and have that output mapped to a switch example.

This useless example sets a switch item if today is Sunday

if [ `date '+%u'` -eq 7 ]; then echo 'ON'; else echo 'OFF'; fi


Thing exec:command:sunday [command="/path/to/", interval=60, timeout=5] // run every minute


Switch IsSunday "Today is Sunday" <switch> { channel="exec:command:sunday:output" }

Thanks a lot. OH2 with the Exec-Binding 2 has to control the Bash Script. It should control either power plugs by an attached 433 MHz sender and some power sockets which can be turned off / on via USB cable and an external library.

So e.g. the Bash script for controlling the 433 MHz Power Plugs is quite easy:

if [ "$1" = "off" ]
  sudo /home/pi/raspberry-remote/send 10011 1 0
  sudo /home/pi/raspberry-remote/send 10011 1 1

It is stored as /home/pi/ and made executable and also available for everybody (chmod 777).
And it works, when starting from shell.

So, getting back to your example, creating a thing like that:

Thing exec:command:sunday [command="/home/pi/ %2$s"]

And an Item like that…

Switch IsSunday "Today is Sunday" <switch> { channel="exec:command:sunday:input" }

…I can add a Switch “Today is Sunday” on my Sitemap, e.g. like that:

Switch item=IsSunday mappings=[ "ON"="ON", "OFF"="OFF" ]

But when using it, nothing happens.
Hence, I’ve added some “log” into it, by a final line of code in the Bash-Script:

sudo echo "$1" >> /home/pi/log.txt

If I start if from Shell, again all fine - pressing the switch ion OH2 again nothing happens (also no entry in log.txt).


I am still not getting a working result, but it must be something about access rights.

  1. Running the script as User “pi” from Bash works
  2. It does not do anything, when triggering it from OpenHab2 using Exec-Binding2

But, when I change

Thing exec:command:GB01_switch_control  [command="sudo /etc/openhab2/scripts/ %2$s", transform="REGEX((.*?))", interval=0, autorun=true]


Thing exec:command:GB01_switch_control  [command="cp /etc/openhab2/scripts/log.txt /etc/openhab2/scripts/log2.txt ", interval=0, autorun=true]

The copy command is executed.


  1. OH can access the folder
  2. OH cannot execute my sh-Script

Some problem, e.g. running that

Thing exec:command:2an [command="/usr/local/bin/sispmctl -o 2"]

Nothing happens…

What to do?

So, I am one step further, but still I am fighting against some kind of access rights, I do not understand. Hopefully someone can give me the final idea.

OH2 was installed on a Raspberry Pi with apt-get, I have the latest Oracle Java 1.8.0_121, but still problems to run some commands by openhab.

Finally, because openhab was not able to execute sh-scripts, I granted a lot of access rights (which is maybe not the best idea, but now I am one step further):

sudo visudo

# Allow members of group sudo to execute any command

and in addition to that

sudo usermod -aG root openhab
sudo adduser openhab sudo

So what is the current result?

My Thing is as follows:

Thing exec:command:GB01_switch_control  [command="/etc/openhab2/scripts/ %2$s", transform="REGEX((.*?))", interval=0, autorun=true]


String GB_01_01 { channel="exec:command:GB01_switch_control:input", autoupdate="false" }


Switch item=GB_01_01  label="Power"  mappings=[ON="ON", OFF="OFF"]

This is my sh-Script:


echo $1 >>  /etc/openhab2/scripts/log.txt

if [ "$1" = "OFF" ]
  sudo /etc/openhab2/scripts/raspberry-remote/send 10011 1 0
  sudo /etc/openhab2/scripts/raspberry-remote/send 10011 1 1

So what works an what not?
works: When pressing the Switch in OpenHab, it now creates new lines with “ON” or “OFF” values in the log.txt file.
works not: the send command (so the part within the if-condition) is not run by openhab

Of course, it works, when doing it as user pi from shell.

So I assume that there are still user permission problems. Maybe those different colors are an indicator for it:

You might try to send output of your sudo script to your log file too. Send &1 and &2 to the file and see if you can catch a permission denied error

You could also try to use a rule triggered by received command first that uses executeCommandLine until you have the script actually running and then move back to the exec binding.

Did it - result is:

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

sudo: no tty present and no askpass program specified

So I did

sudo adduser openhab tty

…restartet openhab service and tried it again. Same result. :frowning:

Next change was again

sudo visudo

and adding the commands to be executed at the end of that file

openhab ALL = NOPASSWD: /etc/openhab2/scripts/raspberry-remote/send, /usr/local/bin/sispmctl

Restart of openhab service --> WORKS :slight_smile:

YEAH, we made it!