Thanks a lot there the issue was…
Hi,
I have just filed
to get this properly documented.
Here is how it will look in the docs:
Actions | v3 Documentation Preview (deploy-preview-1369–openhab-docs-preview.netlify.app)
Feedback is highly appreciated.
cc to @holgerf who commented on my issue on github.
Sorry to interrupt: There is always this syntax used:
-> Duration.fromSeconds(timeout)
But for me it only works (DSL-Rule) with:
Duration.ofSeconds(timeout)
See also here:
next problem:
- How can i use the String output of executedCommandLine. It does not work at OpenHab 3.0:
In my DSL-Rule:
var String test = executeCommandLine(Duration.ofSeconds(5),"/etc/openhab/scripts/ping.sh)
if(test == “ON”){
}else if(test == “OFF”){ }
When i log the value of test, everything is alright but no if condition is working. It is like there was no if condition.
What confusingly works:
var String test = “ON”
if(test == “ON”){
}else if(test == “OFF”){ }
the if conditions are working now but what is different? The waiting time for a correct value is long enough.
Thank you in advance!
Perhaps the command has white space before or after the result.
"ON" != " ON"
Perhaps an invisible character like a CarriageReturn? Can you log the length of the test string?
@NorbertHD
@Confectrician
Good idea but it is not the problem. The if conditions are simply skipped. Its offline, because my start condition is Offline. Both ifs are not active. When i uncomment: //test=“ON” the rules are working again.
Here is my script:
#!/bin/bash
ping -c1 $1 > /dev/null
if [ $? -ne 0 ]; then
echo “OFF”
else
echo “ON”
fi
log:
21:51:25.541 [INFO ] [openhab.event.ItemCommandEvent ] - Item ‘Debug_Button’ received command ON
21:51:25.554 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘Debug_Button’ changed from OFF to ON
21:51:25.837 [ERROR] [org.openhab.core.model.script.test ] - ON
21:51:25.841 [ERROR] [org.openhab.core.model.script.kamera1] - Kamera1_Status (Type=StringItem, State=Offline, Label=Kamera 1, Category=null, Groups=[gStatus])
my rule:
rule "Debug_Button"
when
Item Debug_Button changed
then
var String test = executeCommandLine(Duration.ofSeconds(5),"/etc/openhab/scripts/ping.sh", "192.168.2.11")
logError("test", test)
//test="ON"
if(test == "ON"){
Kamera1_Status.sendCommand("Online")
Kamera1_Status_lastseen.postUpdate( new DateTimeType() )
}else if(test == "OFF"){
Kamera1_Status.sendCommand("Offline")
}
logError("kamera1", Kamera1_Status.toString)
end
No, if conditions don’t just get skipped. Code doesn’t work like that. They are evaluating to false.
Notice how there is a stray newline between your log line of the value of test and the next one. That means the script is returning
"ON
"
not “ON”.
Newline is often represented with \n so maybe that will be more clear.
"ON" != "ON\n"
Change your log statement to
logError("test", "\""+test+"\"")
and you will see that your script is returning more than just “ON”.
thank you very much! You were totally right. But why did it work on OpenHab2?
the log says:
22:03:38.145 [ERROR] [org.openhab.core.model.script.test ] - "ON
"
Why is there a gap?
executeCommandLine
was rewritten in OH 3. Frankly, the current way it behaves is more correct. echo
outputs the message followed by a newline. If OH 2 didn’t include the newline it wasn’t returning the full output of your script which is wrong.
Thank you! I solved it.
For everybody else you need to add:
echo -n “xy”
I have added a comment to my pull request and will fix it.
While you are at it, the paths to the users.properties files in the Console page describing how to change the password are still openhab2. I’ve got a sticky note on my monitor but if you’re already making a PR…
Could you do me a favor and transfer the sticky note to a one liner issue in the repo?
I am on mobile currently.
It was just as easy to create the PR.
Hi all,
I upgradet some days ago from 2.5 to 3.4 and today I was working on updating the rules which use executeCommandLine.
Right now I get no errormessage anymore but it doesn’t add the line of text to the log file. With “>> /var/log/openhab/zigbee.log 2>&1” it worked at 2.5.
var String zigbee_thing = "ZB Online again"
var String zigbee_log_msg = (now.toString("dd-MM-yyyy HH:mm:ss") + zigbee_thing)
executeCommandLine(Duration.ofSeconds(5),"/bin/sh","-c","echo",zigbee_log_msg,">> /var/log/openhab/zigbee.log 2>&1")
Any hints appreciated.
Check if it works as described in: How to use "executeCommandLine" correctly - #2 by NorbertHD
In that solution everything including echo and later needs to be part of one string.
That’s it - many thanks!!!