OH2: Echo in exec binding not working?

since I’m failing to get the intertechno binding running in OH2, I was looking for a workaround sending the data for switching my lights directly to CUL via command line.

In Linux, this works fine:
echo is000000000fff > /dev/ttyACM0 for turning on the light
echo is000000000ff0 > /dev/ttyACM0 for turning off the light

But as soon as I try to add this to a item…

Switch CUL_WZ_Licht_TV          "Licht TV"                       { exec=">[ON:echo is000000000fff > /dev/ttyACM0] >[OFF:echo is000000000ff0 > /dev/ttyACM0]"}

or even as a rule…

rule TEST
          Item CUL_WZ_Licht_TV received command
          if ((receivedCommand)== ON) 
                  val String results = executeCommandLine("echo is000000000fff > /dev/ttyACM0", 5000)
                  logInfo("RULE:TEST-ON", results)
          else if ((receivedCommand)== OFF)        
                  val String results = executeCommandLine("echo is000000000ff0 > /dev/ttyACM0", 5000)
                  logInfo("RULE:TEST-OFF", results)

nothing will be really executed. It seems like the exec binding executes the echo directly - and as I result I get the following in the logfile:

22:08:18.696 [INFO ] [smarthome.event.ItemCommandEvent    ] - Item 'CUL_WZ_Licht_TV' received command ON
22:08:18.708 [INFO ] [ab.binding.exec.internal.ExecBinding] - executed commandLine 'echo is000000000fff > /dev/ttyACM0'
22:08:18.781 [INFO ] [.smarthome.model.script.RULE:TEST-ON] - is000000000fff > /dev/ttyACM0

Means: I get the part after the “echo” as printout, but it won’t be executed on OS level.

Do some of you have an idea how to make this working?

Thank you :smiley:

Found a workaround myself: I just created a tiny bash script which looks this way:

echo $1 > /dev/ttyACM0

My rule now looks this way:

rule TEST
          Item CUL_WZ_Licht_TV received command
          if ((receivedCommand)== ON) 
                  val String results = executeCommandLine("sudo sh /etc/openhab2/scripts/sendtoCUL.sh is000000000fff", 5000)
                  logInfo("RULE:TEST-ON", results)
          else if ((receivedCommand)== OFF)        
                  val String results = executeCommandLine("sudo sh /etc/openhab2/scripts/sendtoCUL.sh is000000000ff0", 5000)
                  logInfo("RULE:TEST-OFF", results)

This way even works without rules as the following item definition:

Switch CUL_WZ_Licht_TV          "Licht TV"                       { exec=">[ON:sudo sh /etc/openhab2/scripts/sendtoCUL.sh is000000000fff] >[OFF:sudo sh /etc/openhab2/scripts/sendtoCUL.sh is000000000ff0]"}

Not very elegant, but at least it works and I can move on with other things.
If you have ideas for improvement or an explanation why echo seems to be diverted into openhab instead of doing what it does on OS level, I’d be grateful.

I don’t know the specifics in this case, but my advice when dealing with the exec binding or executeCommandLine is if it doesn’t work the first time just do what you did and write a script.

The problem is when openHAB calls these command line scripts it is running as another non-privileged user without a shell. Certain commands, like echo, behave differently in different shells because it depends upon the shell to indicate where what is is supported to echo starts and stops. Since openHAB is not using a shell, apparently the echo command is being told that the whole string is to be echoed.

Also, I believe the ‘>’ operator is implemented by the shell which wouldn’t work in this case because OH doesn’t have a shell.

1 Like


I struggle (again) with executeCommandline.
Obviously the handling changed from OH1 to OH2.
I used flawlessly the following command in OH1:

var delete_log_tmp = executeCommandLine("/etc/openhab2/scripts/cleanABUSlogs.sh", 10000)

and the script:

RESULT=`sudo /bin/rm /var/www/upload/abus/*.log`
echo $RESULT

for visudo I have the same settings in OH1 and OH2.

However I get in the logs of OH2:

Execution failed (Exit value: -559038737. Caused by java.io.IOException: Cannot run program "/etc/openhab2/scripts/cleanABUSlogs.sh" (in directory "."): error=2, No such file or directory)

Any suggestion?

Start with the basics.

  • Does the script exist at /etc/openhab2/scripts/cleanABUSlogs.sh
  • Is the script executable? ls -l should show at least rwxr-xr-x
  • Can you run it from the command line using your usual login?

The error implies the script either doesn’t exist, the openhab user doesn’t have the right permissions, or the script itself doesn’t have execute permissions.

Thanks @rlkoshak

I checked the basic things several times.
The script exist, I can run it as pi with sudo.
Unfortunately I cannot run it as openhab to try because infinit know openhab’s password.

What’s weird is, that the whole setup (executecommandline from rule and the same stuff in the script) did run in OH1.

You can run it as openhab using

sudo -u openhab <name of script>

The only clear difference between the OH1 version and this that I can see with the information I have available is that the old one was clearly located in some other folder (presumably /etc/openhab/configurations/scripts).

I can think of no other things that could be wrong.

Try to add sudo to your executecommandline:

var delete_log_tmp = executeCommandLine("sudo /etc/openhab2/scripts/cleanABUSlogs.sh", 10000)

And write the result of delete_log_tmp into the logfile using the loginfo function.

Hi @Boby

I did also try this - thanks for the suggestion though.
Thanks to @rlkoshak - I will try to run it as openhab and see.

Another finding:
I had a script like this:

RESULT=`sudo iptables -t nat -I POSTROUTING -s -j MASQUERADE`
echo $RESULT

Which I had problems with…
I removed the first line and it works fine !?!?!! (using raspbian Jessie).
Does someone have an idea why?

in the beginning I had problems with my text editor (notepad++ on windows), so I always check the syntax on my OH system with vi if I have execution problems of scripts etc…
Sometimes I found ^M at the end of the line. after removing it, the issue has been solved.

Now without the first line (#!/bin/bash) it works like a charm, but I get as “RESULT”:

Now I am lost… :wink:
Any (maybe even obvious) hint is greatly appreciated, because issues with the execution of scripts causes 70% of the time efforts I spend to OH

There is a little tool called dos2unix which will step the ^M from your files. This is more reliable than doing it by hand.

I’m not sure what could be wrong but I recommend writing scripts like theses on the Pi itself to eliminate any possibility of errors like this.

Hi @rlkoshak,
thank you very much. I will try that right away.