How to call a simple script in a rule?

Hi,

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

// Alexa
AlexaMusikgruppeUberall_Player.sendCommand("PAUSE")
EchoFlex_Player.sendCommand("PAUSE")
// Logitech Media Server
SqueezeplayerSqueezeboxen_Control.sendCommand("PAUSE")
SqueezeplayerTechniSatDigitRadio450_Control.sendCommand("PAUSE")
SqueezeplayerPiZero1_Control.sendCommand("PAUSE")
SqueezeplayerPiZero2_Control.sendCommand("PAUSE")


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)

Hi,
You can do it several ways.

Via the command

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

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)

1 Like

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);
RuleManager.runNow("tocall");
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.

2 Likes

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.

There is no list to select scripts in OH 3.4.4. How do I select my script written in Main UI to be run by a rule?

Yes there is.

image

image

image

1 Like

To me this UI flow is counterintuitive. It is a long logical leap from saving a script in the Scripts function in the UI to understanding it can be found as a rule.

The intuitive location for the script would be in the “Run scripts” box.

You are welcome to file an issue with a better idea.

1 Like

Done!