Calling JS script from JS rule and passing parameters

I’ve created a Javascript script with Script ID tado-API-script:

var pythoninvenv = '/var/lib/openhab/bin/python/tado-manual-control/venv/bin/python';
var pythonscript = '/var/lib/openhab/bin/python/tado-manual-control/tado-manual-control.py';

function callPython(command) {
  var pythonOutput = actions.Exec.executeCommandLine(time.Duration.ofSeconds(3), command);
  console.log("python output = "+pythonOutput);
}

callPython(command);`

I’d like to call that script from within a JS rule, and pass a parameter as command. But I’m not sure how to do that. The documentation on ScriptExecution only mentions scriptName (which is the Script ID, I assume?) as possible argument…

You are running into the issue where too many things are referred to as a “script”.

The JSscripting ScriptExecution function runs the standard OH action callScript. callScript is only for running DSL files placed in the conf/scripts folder, which cannot be what you want because your “script” is javascript.

In this case, if you created a javascript “script” I assume that means you used the UI to create a managed script with jsscripting. In the UI, managed rules, scripts, scenes, and schedules are all just rules. Scripts, scenes, and schedules are just particular kinds of rules or rules with some particular feature.

In this case a a UI managed “script” is just a rule with no trigger. Because it has no trigger, this rule has to be called from another rule. With all that in mind, the jsscripting function that you want is not ScriptExecution, but instead runRule which does let you pass parameters which can then be referenced directly in the called rule:

2 Likes

Okay, I did that, but something unexpected happened. This is the script I ran:

rules.runRule("tado-API-script")

This is the “script” (i.e. rule) with Script ID tado-API-script:

console.log("hallo");

This is the output:

19:58:58.317 [INFO ] [tion.jsscripting.rule.tado-API-script] - hallo
19:58:58.318 [INFO ] [tion.jsscripting.rule.tado-API-script] - hallo
19:58:58.319 [INFO ] [tion.jsscripting.rule.tado-API-script] - hallo
19:58:58.320 [INFO ] [tion.jsscripting.rule.tado-API-script] - hallo

Why does it run 4 times?

I have never seen this behavior for any of the instances where I use runRule. I also haven’t been looking for it, but I definitely have some rules where it would be noticable.

I can’t try to replicate this at the moment, but if you continue to see this behavior then you should post the complete yaml config for both rules so we can see if there’s something else going on.

I’m creating a new topic for this problem, as it’s no longer relevant for this one. See "Script" run from another "Script" runs 4 times...?.