How do I call an OH3 UI script from a rule

I think Rich also asked you to add the duration parameter and capture the return value, so that you can see what if any error message is being returned.

I haven’t run the below code, but it would look something like the following.

var ExecuteOutput = Exec.executeCommandLine(Duration.ofSeconds(120),"/etc/openhab/scripts/openhablogs.sh");
log.debug("--> Execute Output: " + ExecuteOutput + "\n\n");

You would then have a look at the logs to see if there is any info in the return that tells you what is going on.

Thanks

Trev

I’m new to openHAB, and started playing with openHAB 3.1. Version number implies a mature experience, although I spent mockerflopping days trying to get some sense of logical hierarchy in script flow.

As I was about to give up, I stumble upon this gem of a post! I can finally execute ui scripts from rule scripts! Thanks a lot to the people figuring this out, repeating the solution, and clicking the google result enough for it to rank high enough for me to notice. :smiling_face_with_three_hearts:

I probably made a bad choice that first moment in openHAB where I decided to use ECMAscript, because it seems to be a bit of a second class citizen in reality, and a 9000th class citizen in the openHAB documentation.

It would save me some time because I already know ECMAscript, or so I thought. Little did I know…

PS in case writing 7 lines of code messes with your zen too, just put the longer lines at the top.

var BundleContext = Java.type('org.osgi.framework.FrameworkUtil').getBundle(scriptExtension.class).getBundleContext()
var RuleManager = BundleContext.getService(BundleContext.getServiceReference('org.openhab.core.automation.RuleManager'))

// At some point
RuleManager.runNow('0bdeadbeef')

Just for the record, with the latest release it is even possible to call a script. from a UI (blockly) rule which makes it pretty easy.

Is it correct that for ECMAScript 262 Edition 11 those libraries are not available and therefor the call to the framework doesn’t work?

I found it :slight_smil:
Just do this:

RuleManager = osgi.getService(“org.openhab.core.automation.RuleManager”);

Then I can use :

RuleManager.runNow(#uid_of_the_rule)

1 Like

Calling another rule is now built into openhab-js. But you might need to install the latest until the next release of OH where it should be included with the add-on.

Using npm i openhab from the $OH_CONF/automation/js folder.

Then

rules.runRule(<uid of rule>, <optional map of data to pass>, <optional boolean, when true the called rule's conditions will apply>)