JRuby OpenHAB Rules System

Glad to hear you’re interested. To get it up and running, follow this link and you’ll find a jar compiled for OH3, even though the repo is a clone of openhab2-addons. After you drop it in the addons-folder, the rest should take care of itself.

You are very welcome to come with suggestions and/or PRs to improve the helper libraries!

Okay, I’ve got the openhab bundle installed, and restarted OpenHAB. It appears to be loading my script file, but the require "openhab" is broken. I created the services/jruby.cfg file, but I don’t see that the openhab-scripting gem was installed to my gem_home. Also, where does it find JRuby? Is it included in the jar? I.e. how would I install additional gems accessible to my rules? I obviously don’t want to use my system ruby (MRI 2.7), even if I set my GEM_HOME.

Jruby is included as a dependency, so it’s included in the bundle. Can you post the contents of the services/jruby.cfg file? This is how mine look e.g:

org.openhab.automation.jrubyscripting:gem_home=/etc/openhab/automation/lib/ruby/gem_home
org.openhab.automation.jrubyscripting:gems=openhab-scripting=~>3.0
org.openhab.automation.jrubyscripting:ruby_lib=/etc/openhab/automation/lib/ruby/personal

If it is read correctly by openhab it should also be reflected in /var/lib/openhab/config/org/openhab/automation/jrubyscripting.config

org.openhab.automation.jrubyscripting:gem_home=/etc/openhab/automation/lib/ruby/gem_home
org.openhab.automation.jrubyscripting:gems=openhab-scripting=~>3.0

and no, /var/lib/openhab/config/org/openhab/automation/jrubyscripting.config is not showing anything. but I’m clearly running a ruby file in /etc/openhab/automation/jsr223/ruby/personal/test.rb that’s doing things like raise RUBY_VERSION and I can see the output in openhab.log. Note that I’m using OpenHAB 3.2.0-SNAPSHOT from 2 days ago.

Have you restarted OH after you saved the jruby.cfg-file? Don’t think it should be necessary, but not sure why it’s not being read otherwise. If that doesn’t work, try setting the configuration manually via the karaf console:

openhab> config:edit org.openhab.automation.jrubyscripting
openhab> config:property-set gems "openhab-scripting=~>3.0"
openhab> config:property-set gem_home "/etc/openhab/automation/lib/ruby/gem_home"
openhab> config:update

ah, I found the problem. the automation directory was owned by my user, not openhab, so it couldn’t write to it. I might have more questions, but I’m over that hurdle

Was there an error message anywhere?

Not until I manually set the gem_home in karaf. Then there was a big stack trace in openhab.log.

question: I see the ruby_lib config variable, which looks like at https://github.com/boc-tothefuture/openhab2-addons/blob/46307a90e08158440349d8da7bd7ce75d0195913/bundles/org.openhab.automation.jrubyscripting/src/main/java/org/openhab/binding/jrubyscripting/internal/JRubyScriptEngineConfiguration.java#L54 should set RUBY_LIB env var. which I’m able to confirm is working by some logging in my ruby file. Except shouldn’t that by RUBYLIB, in order to get it on the $LOAD_PATH?:

cody $ RUBY_LIB=/etc irb
irb(main):001:0> $LOAD_PATH
=> ["/Users/cody/.rubies/jruby-9.2.16.0/lib/ruby/gems/shared/gems/did_you_mean-1.2.1/lib", "/Users/cody/.rubies/jruby-9.2.16.0/lib/ruby/2.5/site_ruby", "/Users/cody/.rubies/jruby-9.2.16.0/lib/ruby/stdlib"]
irb(main):002:0> exit
cody $ RUBYLIB=/etc irb
irb(main):001:0> $LOAD_PATH
=> ["/etc", "/Users/cody/.rubies/jruby-9.2.16.0/lib/ruby/gems/shared/gems/did_you_mean-1.2.1/lib", "/Users/cody/.rubies/jruby-9.2.16.0/lib/ruby/2.5/site_ruby", "/Users/cody/.rubies/jruby-9.2.16.0/lib/ruby/stdlib"]

This might be the reason why it wasn’t working for me.

New release here: https://github.com/boc-tothefuture/openhab2-addons/releases/tag/3.2

Let me know if that fixes the issue.

1 Like

:frowning: nope.

logger.info(ENV.inspect)
logger.info($LOAD_PATH)

produces

2021-09-14 09:43:44.486 [INFO ] [jsr223.jruby.Object                 ] - {..., "RUBYLIB"=>"/etc/openhab/automation/lib/ruby/personal", "GEM_HOME"=>"/etc/openhab/automation/lib/ruby/gem_home"}
2021-09-14 09:43:44.486 [INFO ] [jsr223.jruby.Object                 ] - ["uri:classloader:/META-INF/jruby.home/lib/ruby/gems/shared/gems/did_you_mean-1.2.1/lib", "/etc/openhab/automation/lib/ruby/gem_home/gems/bundler-2.2.27/lib", "/etc/openhab/automation/lib/ruby/gem_home/gems/marcel-1.0.1/lib", "/etc/openhab/automation/lib/ruby/gem_home/gems/openhab-scripting-3.7.1/lib", "uri:classloader:/META-INF/jruby.home/lib/ruby/2.5/site_ruby", "uri:classloader:/META-INF/jruby.home/lib/ruby/stdlib"]

so the env var is getting set correctly, but it’s not affecting $LOAD_PATH. I wonder if however JRuby is starting up inside the script host is not using the environment variable (as opposed to booting as its own process). https://github.com/jruby/jruby/blob/master/spec/ruby/command_line/rubylib_spec.rb sure implies that they only expect it to work with command line. maybe we should do $LOAD_PATH.unshift(ENV['RUBYLIB']) if ENV['RUBYLIB'] inside the openhab-jruby gem (with proper handling of splitting into multiple entries)?

(https://github.com/jruby/jruby/blob/a309a88614916621de4cc5dc3693f279dae58d0c/core/src/main/java/org/jruby/runtime/load/LoadService.java#L224 for reference of where it actually happens in JRuby, but I’m not familiar enough to know where LoadService fits in)

I have been negligent in announcing new releases here…

Version 3.9.0 was just released and I would welcome @ccutrer who added the new feature and has been contributing across the board on bug fixes and optimizations.

As always - full changelog is here.

Features added since 3.4:

  • Fix for Rubylib, creating the ability to share libraries across rules - docs here
  • Duration support on floats - can now do ‘after 2.5 seconds’
  • implement positive?, negative?, and zero? for quantity and dimensioned numberitem

I’m loving the recent improvements on this library! Thanks so much @broconne, @pacive, and @ccutrer for your great work!

Another changelog update post is overdue, @broconne! :wink:

1 Like

Hi,

Trying to move my rules from jython and dsl, any example of using the actions? ex. pushover or ipcamera binding.

In case you haven’t seen it, here’s the docs on actions: Actions | OpenHAB JRuby Script Library

If you could you paste your existing code, we might be able to help further.

Hi
Thanks for the quick reply.

I did have a look but could not understand it completely.

Am trying to get this working. This is jython
“actions.get(“pushover”, “pushover:pushover-account:Pushover”).sendMessage(result, “Openhab 3 - Front door Lock”)”

How would that be written in Jruby?

The document also mentions about 2 ways to access actions. one being the action way and the other thing based.

things['pushover:pushover-account:Pushover'].sendMessage(result, 'Openhab 3 - Front door Lock')

Yes, there are two ways to access actions. The above code is what we’d recommend. The other way is to make the transition easier.

1 Like

That’s easy!!! Thanks