OpenHab 3.2.0.M4 - how to: executeCommandline

I’ve created a rule, and the command.sh script, after adding the shebang and +x permission the command is correctly executed.
Now I’m wondering how to add the results as loginfo.

rule:

var String results = ""

executeCommandLine("/home/openhabian/workspace/command.sh")
    
logInfo("Titolo","testo" + results)

command.sh:

#!/bin/bash
echo "ciao" >> /tmp/ciao

log:

==> /var/log/openhab/openhab.log <==

2021-12-13 10:22:28.701 [INFO ] [org.openhab.core.model.script.Titolo] - testo

2021-12-13 10:22:28.702 [DEBUG] [e.automation.internal.RuleEngineImpl] - The rule 'fde860b80a' is executed.

If I replace this row in rule, it goes to error:

results=executeCommandLine("/home/openhabian/workspace/command.sh")

[ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'fde860b80a' failed:   
var String results = ""
results=executeCommandLine("/home/op ___ enhabian/workspace/command.sh")
    
logInfo("Titolo","testo" + results)
   Type mismatch: cannot convert from void to String; line 4, column 36, length 57
javax.script.ScriptException:   
var String results = ""

First, capture your results -

results = executeCommandLine("/home/...")

but that won’t wok because it doesn’t wait for the external script to run before assigning results. So allow some time -

results = executeCommandLine(Duration.ofSeconds(5), "/home/...")

It works like a charm, thank you!
Have a nice day

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.