How to call a simple script in a rule?


i’ve written a simple script in the oh3 ui to turn all my audio off:

// Alexa
// Logitech Media Server

If I run it in the UI (CTRL+R) it is working fine.

How do I call this in some rules? I’ve found callScript(“audio_aus”) but this fails:

(“audio_aus” is the unique id)

You can do it several ways.

Via the command

executeCommandLine(Duration.ofSeconds(1), “/etc/openhab/scripts/”)

or via the rule Gui „Then run script“ action and paste your code,

or add your commands as „Then“ in the Gui rule and don‘t use a script…

Best regards

Thanks, but the script is written in the oh3 ui, so it is not saved in the normal scripts folder.
Also I want to call this script within several rules at the beginning, not as a single rule itself.
(just don’t want to copy and paste this in several different rules)

That looks like Rules DSL.

Here’s the problem. The term “Script” is overloaded in OH and it’s confusing.

  • One can write a .script file and place it into the $OH_CONF/scripts folder. Then one can use the callScript() in one of your rules. I think the code above will work as written as a .script file. But there are limitations as you cannot pass data to a .script nor can you receive a value back from a .script.

  • One can write a shell script and call that from openHAB using executeCommandLine or the Exec binding.

  • In a rule, one can create Script Actions and Script Conditions which have the code to execute when the rule triggers.

  • In MainUI there is a Scripts section. This has a special type of rule which consists only of a single Script Action. But it’s still a rule like any other.

You’ve created that last one. So to call it from another rule you have a couple of options.

  1. In MainUI, in your rule you can choose “Run Script” as the action and select your Script from the list.

  2. There is no way for a rule to call another rule in Rules DSL though. So you’d have to use a different language if you want to do that. In Nashorn JavaScript (ECMAScript 5.1) it’d look something like:

// Run another rule
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);
var map = new java.util.HashMap();
map.put("test_data", "Passed data to called function!")
RuleManager.runNow("tocall", true, map); // second argument is whether to consider the conditions, third is a Map<String, Object> (way to pass data)

To access the map in the called rule: context.getAttribute("test_data");

Note that the rule called does not need to be written in the same language, so you can call a Rules DSL script from a JavaScript rule.

Also be aware that in the other languages you can put the code into a library and import that into your rule as another alternative to calling another script.

1 Like

Ah I understand. Furthermore to the solutions from Rich you can create a single rule with the actions you got in your script and then run this particular rule from your other rules. As „Then“ run rule and select it.
Should be the same like calling the script in others rules.