binderth
(Thomas Binder)
January 13, 2023, 1:05pm
1
OH3.4.1 on openHABian.
I’m using JS Scripting to execute a Bash-Script, which returns a string.
....
var exec = actions.Exec.executeCommandLine("/etc/openhab/scripts/quickchart.sh", param1, param2);
console.log(exec);
....
returns:
2023-01-13 13:55:33.681 [INFO ] [nhab.automation.script.ui.TibberCalc] - null
The return of the script looks like this:
openhabian@openHABMain:/etc/openhab/scripts $ sudo -u openhab ./quickchart.sh 17,16,17,18 19,20,21,22,24
TibberChart_20230113-135533.png
So, I would expect the exec
to have the value “TibberChart_20230113-135533.png”
How can I fill the exec
variable with the output of the Bash-Script?
JustinG
(JustinG)
January 13, 2023, 2:23pm
2
If the command takes any time to run, then you need to give the action a timeout duration so it waits for the return value:
1 Like
rlkoshak
(Rich Koshak)
January 13, 2023, 4:25pm
3
Even if the command doesn’t take any time to run, without the Duration
executeCommandLine will not return the output of the command.
@binderth this leads us to the same place. If you care what the output of the command called is, you must supply a Duration.
1 Like
binderth
(Thomas Binder)
January 13, 2023, 4:35pm
4
Thanks both for the input! Did this with DSL rules, didn’t know that JS Scripting does it, too - but obviously it does!
JustinG
(JustinG)
January 13, 2023, 4:39pm
5
I know that’s how the baseline action works, but that is inconsistent with what is in the library docs:
// Get response from command line.
var response = actions.Exec.executeCommandLine(‘echo’, ‘Hello World!’);
// Get response from command line with timeout.
response = actions.Exec.executeCommandLine(Duration.ofSeconds(20), ‘echo’, ‘Hello World!’);
so (without checking the actual code, I admit) I had assumed that the helper library is passing an additional default duration when none is provided.
rlkoshak
(Rich Koshak)
January 13, 2023, 4:53pm
6
Hmmm. We should be consistent. Looking at the code, it’s actually a direct call on the Java org.openhab.core.model.script.actions.Exec Class so it is the same as Rules DSL and all the rest. The JS Scripting docs are wrong. I’ll file a PR to fix that right now.
openhab:main
← rkoshak:patch-4
opened 05:01PM - 13 Jan 23 UTC
Without a `Duration` argument, `executeCommandLine` always returns `null`. I rem… oved the example that appears to show calling it without a `Duration` and getting the result of the echo. That can't happen as it's currently implemented.
Also, we need to use `time.Duration` right? I'm pretty sure that's the case. If I'm wrong and that's not the case, we should show importing java.time.Duration in the example.
I think I forgot to sign off on this. This is really minor doc update, I hope the exception can apply. If not I'll figure out how to fix it.
2 Likes