How do I call an OH3 UI script from a rule

Ah, but a Script is a Rule. It’s just a special type of rule that only as a single Script Action and no triggers or conditions. And really the only difference is a Script has the Script tag.

It’s as good a reason as any. Keep in mind though that you will have to pass any “arguments” to the Script by adding them to a Map and passing that Map to runNow as demonstrated in the last three lines of the example code you have above.

Ideally you will want to adopt the Helper Libraries or put all the Framework stuff into a library though so you can just import it and just call “runRule(“uid”, true, map);” without littering all your scripts and Rules with the same seven lines of code over and over. You can’t do that through a Script though.

That’s good to know. It would be helpful if this was referenced on the Scripts page in the UI, and would also be helpful if you could copy and paste the ID somehow.

1 Like

You can copy and paste the id and for that matter items etc.
Here is how I do it.

This is in the scripts

The id is 8e1e6893d6 so to copy and paste it I just click on the script to open it.

If you look in the url bar of your browser you will see the url link. I just click twice on the id and it highlights just that part of the url and then I just ctrl-c and ctrl-v to paste it where ever I want.

You can do the same to copy and paste item names.


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

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.

There is a typo the, should be "

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


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/", 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

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.


Thnx a lot i will review tomorrow
Cheers M.


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/")
what in my case would be “executeCommandLine(”/etc/openhab/scripts/")"
It works when connected through SSH but it wont fire the rule with a trigger.

rule "executecommandLine Test"
       Item Dummy received update




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.


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/");
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.



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

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.