How do I call an OH3 UI script from a rule

Just had the same Problem: I have created a Blockly Script in the Script Section an then wanted to execute it in a Rule.
Expected behavior was, that in the Rule I click „Run script“ then select my Script in a List of all my Scripts, click OK and be ready. Should be definitly made more easy.

Took me a while to understand how to use the example from the First Post in this thread.

So just for anyone you whant to do the same.
Click „Run Script“ → than „ECMA Script“ than past the following:

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(„XOXOXOXOXO");

replace XOXOXOXOXO with the ID of your Script, which you see under the Script Name in the Script List (as shown in @rlkoshak ´s Post of Jan 07.)

1 Like

It is that easy. You just looked in the wrong place.

  • Add Action
  • Other Rules
  • run these rules(s)
  • Select your Blockly Script from the list.

Oh thats great, didn’t saw that, because i just looked for script. Maybe add a „…/script(s)“ in the GUI at this place.

1 Like

There is a typo the, should be "
RuleManager.runNow(“XOXOXOXOXO”);

Yeah, thats the annoying auto-correct in Safari. Don’t have that in my Rule.

Hi,

i am trying to get a script running that worked finr in 2.5
The script is executable over SSH login and starting it in the directory.
I cant get it to start in a rule. This is the old line:

executeCommandLine("/etc/openhab/scripts/openhablogs.sh", 10000)

I read a few posts about executing scripts but none got me to get it working. As i am a visual learner, i mostly get the curve when i see code that works.
The owner of the Script is openhabian:openhab
rights on the script are -rwxrwxrwx
Members of the group openhab are openhab, openhabian so it shouldnt be any right issue as far as i can see as a windows user.

I would like to try the Blockly part for that but have no idea where to start.
Pointing me in the right direction with either a codeline or any other ideas is highly appreciated. Helping me building a blockly rule for this would be brilliant :slight_smile:

Thnx a lot
M.

During the upgrade you didn’t review or missed the list of breaking changes. The way to use executeCommandLine has changed between OH 2 and OH 3. See Actions | openHAB

Blockly does not yet support Actions.

This topic is about running rules/scripts from within other rules, so won’t help you.

Having said that, this link should point you in the right direction ExecuteCommandLine and the following code is working in my implementation to run a command line executable -

'use strict';

var log = (context.logger === undefined) ? Java.type("org.slf4j.LoggerFactory").getLogger("org.openhab.core.model.script.actions") : context.logger
var Exec = (context.Exec === undefined) ? Java.type('org.openhab.core.model.script.actions.Exec') : context.Exec
var Transformation = (context.Transformation === undefined) ? Java.type('org.openhab.core.transform.actions.Transformation') : context.Transformation
var Duration = (context.Duration === undefined) ? Java.type("java.time.Duration") : context.Duration;

var SpeedtestOutput = Exec.executeCommandLine(Duration.ofSeconds(120),"/usr/bin/speedtest","-f","json");

Most of this was gained by getting help from @rlkoshak Thanks again Rich.

Trev

Thnx a lot i will review tomorrow
Cheers M.

Hi,

just tried what i read in the Link you posted under OH3 Actions.
It looks nearly the same as the old line so i am a bit confused…
Reading the example the line should look liek this: executeCommandLine("path/to/my/script.sh")
what in my case would be “executeCommandLine(”/etc/openhab/scripts/openhablogs.sh")"
It works when connected through SSH but it wont fire the rule with a trigger.

rule "executecommandLine Test"
when
       Item Dummy received update
then

                  executeCommandLine("/etc/openhab/scripts/openhablogs.sh")

     

end

I just dont see the trick yet…
Cheers M.

What is the script returning? Add the Duration argument and log what is returned by executeCommandLine.

Its really hard to help when you don’t give us any details. What does this script do? How do you know it’s not working? what is the output when you run it through ssh? What user are you when it runs through ssh?

In general, the Exec binding is one of the most challenging bindings to get to work right because you must be proficient in both openHAB and the operating system that OH is running on.

Hi,

sorry i didnt give the purpose of the script. its clean simple:
“tac /mnt/ext-storage/oh3/mirhome.log | head -n 10 > /etc/openhab/html/mirhome10.log”
It just takes the logfile mirhome.log, takes info out of it and writes it to a new mirhome10.log
It just creates a new file.

I just want to show the last 10 log entries on the BasicUI. If you got a better idea for doing this i am more than willing to do it different.

Thnx M.

And the answer to the rest of the questions?

How do you know it’s not working? what is the output when you run it through ssh? What user are you when it runs through ssh?

The mirhome10.log is not written or created. Im on the openhabian user on SSH. the funny thing when i try to change user to openhab it pops back to openhabian user.
Changing to other users works. The rights on the Script are free for all. rwxrwxrwx and owner is openhab:openhab. The group mebers of openhab are openhabian and openhab.

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>)