Use script in rule

In OH 3 one can create a script. These scripts can be manually triggerd / executed.
In the rules you can execute a scipts as an ‘action’.

Is it possible to execute a script from a rule without copy-pasting the script into the rule?
(So saving it as a script)?

The reason behind this is that I want a few different rules triggering the same script.

This how I do it in JavaScript:

//this is how to run a script file
var FrameworkUtil = Java.type("org.osgi.framework.FrameworkUtil");
var _bundle = FrameworkUtil.getBundle(scriptExtension.class);
var bundle_context = _bundle.getBundleContext()
var classname = "org.openhab.core.automation.RuleManager"
var RuleManager_Ref = bundle_context.getServiceReference(classname);
var RuleManager = bundle_context.getService(RuleManager_Ref);
RuleManager.runNow("bbe68f1179"); //the bbe68f1179 is the id of the script found when you look in the script list

Like this:

There is some overlap in terminology here that needs to be cleared up first.

In OH 2 and earlier, there was a concept of a Script which is a block of Rules DSL code that can be called using callScript(name_of_file.script) Actions | openHAB.

In OH 3, MainUI presents a subsection labeled “Scripts”. These are not the same thing. These are actually fully fledged Rules, though they are Rules that only have a single Script Action and are tagged with “Script”. Because these are fully fledged rules, they are called by executing another rule. Greg posted an example for how to do that in JavaScript. As far as I know this is not possible in Rules DSL, but it is possible through the UI outside of a Script Action.

callScript is in the same class as createTimer and transform (ScriptExecution) so it should be accessible by default in Rules DSL. In the other languages ScriptExecution will have to be imported, just like is required for createTimer.

But note the difference. callScript can still only call a Rules DSL script placed into a file in the $OH_CONF/scripts folder. It cannot execute another Rule and since the things listed under Scripts in MainUI are rules, they cannot be called using callScript.

So, a tl;dr is yes, callScript still works and it works exactly like it did in OH 2.5. That means no UI support is provided to create the Scripts in the first place.

Alternatives to this, and perhaps better alternatives might be to call a rule as Greg demonstrates. It’s even possible to pass stuff to the called rule which is not possible with Scripts. Or, if you are using one of the other languages, put your reusable code into a library and import it. See an example of that in OH 3 Examples: Writing and using JavaScript Libraries in MainUI created Rules.

1 Like

Thanks for the (very elaborated) explanation.

That’s pretty confusing :wink:
I am actually transferring all my DSL into mainUI (mostly using Blockly so far).
In DSL I used sometimes shell scripts and I have tried this with callScript in mainUI (assuming that the engine looks in /etc/openhab/scripts for it.
This does not work though.

Failed to execute rule 'openVPNon': Fail to execute action: 2

image
Is there a way yet to execute shell scripts or bash commands like executeCommandLine in DSL?

Perhaps, but callScript has been there and worked like this since the beginning in OH 1. It’s a legacy action that has always been there.

Use executeCommandLine for that. callScript has never and will never be able to call a shell script.

There should be a block for that in OH 3.2. I’m pretty sure all the core actions have been implemented.

You mean in Blockly? That would be great.
I did not find it an assume it shoulb be here:

Another solution, which I had to resolve to when coming to OH3 from OH2 (because callScript was broken there for a while) was to put the contents of my (DSL) scripts into (DSL) rules and just postUpdate a virtual Switch item to trigger them instead. That will always work.

I may be mistaken and it’s planned to be added. So much new stuff was added recently it’s hard to keep up.

1 Like

No problem - I am still always impressed what you have on top of your head :slight_smile: I will check out @OMR suggestion then.