Hello,
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
rule TEST
when
Item CUL_WZ_Licht_TV received command
then
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)
}
end
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:
Found a workaround myself: I just created a tiny bash script which looks this way:
#!/bin/sh
echo $1 > /dev/ttyACM0
My rule now looks this way:
rule TEST
when
Item CUL_WZ_Licht_TV received command
then
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)
}
end
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.
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)
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.
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.
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.
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”:
^M
Now I am lost…
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