JRuby OpenHAB Rules System

try this in karaf console

log:set INFO jsr223.jruby
2 Likes

That works! Thanks

4.25.0 is now released

Biggest changes since 4.13.01:

  1. The JRuby Automation Add-On (required by this library) is now part of OpenHAB!
  2. Generic Trigger Support
  3. Access ‘thing’ and ‘things’ from items
  4. Support for ‘attach’ for cron trigger types
  5. hsb/location types support commands from a hash
  6. Ands lots of fixes and other features

As always, for more details on the changes go here and latest docs are always here

A very special thanks to all of the contributors this past year! We just rounded the 1 year anniversary of this library.

4.30.0 is now released

Biggest changes since 4.25.0:

  1. The logging path is now ‘org.openhab.automation.jruby’ to better align with other automation scripting languages
  2. Group items are no longer excluded from ‘items’ enumeration
  3. to/from now support both ranges and generic procs/lambdas
  4. Lots and lots of fixes from @ccutrer and @JimT

As always, for more details on the changes go here and latest docs are always here

1 Like

4.37.1 is no released

There has been a lot of work since the last post - apologies for not updating more frequently here!

@JimT has been pushing regular updates to the library and driving many improvements.

Some changes since 4.30.0:

1.Triggers support ranges with string quantity. e.g. from: '0 W'..'10 W
2. received_command now supports ranges and procs
3. Cron style trigges now support month-day e.g. every '02-14', at: '2pm'
4. Lots of fixes and other enhancements

The biggest update is the work @JimT did on creating a very comprehensive “how do I?” document.

As always, for more details on the changes go here and latest docs are always here

4.39.1 is now released. This version introduced support for openHAB’s semantic model with complementary methods to augment the semantic model. Thanks to @ccutrer for this great new feature.

This is very useful in finding associated items in rules instead of relying on item naming pattern.

Links:

Example:

This script closes the curtains in the room when the TV is turned on

// to create triggers
Group          gTVs

Group          LivingRoom                                           ["Livingroom"]

Group          LivingRoom_TV                 (LivingRoom)           ["Television"]
Switch         LivingRoom_TV_Power           (LivingRoom_TV, gTVs)  ["Control", "Power"]

Group          LivingRoom_Curtain1           (LivingRoom)           ["Blinds"]
Rollershutter  LivingRoom_Curtain1_Position  (LivingRoom_Curtain1)  ["Control", "Opening"]

Group          LivingRoom_Curtain2           (LivingRoom)           ["Blinds"]
Rollershutter  LivingRoom_Curtain2_Position  (LivingRoom_Curtain1)  ["Control", "Opening"]
rule 'Automatically close all the curtains in the room when the TV turns on' do
  changed gTVs.members, to: ON
  triggered do |item|
    # All the following can be done in a single chained method calls
    curtains = item.location # This returns the semantic Location for the TV
                   .equipments(Semantics::Blinds)  # This returns the matching Equipments within the location
                   .members                        # Return the members of the matching equipments 
                   .points(Semantics::Opening)     # Select all the Points that are of Opening class

    # `ensure` here will only send the command when necessary
    curtains.ensure.command DOWN
  end
end

For more info, see: Semantics documentation

I am trying to get JRuby and this script library going on OpenHAB, but I am pulling a blank here.

I’ve installed the JRuby Scripting addon, and configured openhab-scripting=~>4.0 as Ruby Gem to install in the configuration. However when I try to write a script, and require 'openhab' it errors out with:

[ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab/automation/jsr223/ruby/personal/WasherStateMachine.rb': Error during evaluation of Ruby in org/jruby/RubyKernel.java at line 1017: (LoadError) no such file to load -- openhab

I suspect the ruby gem hasn’t properly been installed, as a search through the filesystem doesn’t reveal any either. But I can’t find the right starting point to troubleshoot.

The system is running openhab-3.2.0-1 installed via apt, on a Armbian armv7l machine.

Marcus
try this to figure out if jruby is installed

go here if you don’t know how to run he openHAB console

The error message indicates that jruby addon is installed. It is just not installing the openhab-scripting gem for some reason.

@mvandamn could you please paste your configuration here?

Also try restarting openhab if you haven’t already done that.

Hi @JimT and @Andrew_Rowe,

JRuby is indeed installed, just to confirm:

openhab> bundle:list -s | grep jruby
305 │ Active │  80 │ 3.2.0                 │ org.openhab.automation.jrubyscripting

OpenHAB (and the machine) have been restarted.

I have configured it via the web-interface, leaving everything to its defaults, except the gems.

I do see that the parameter is ending up in a slightly different pid then it it would if configured by files.

----------------------------------------------------------------
Pid:            org.openhab.automation.jrubyscripting.internal.JRubyScriptEngineFactory
BundleLocation: null
Properties:
   gems = openhab-scripting=~>4.0                                                                                                                                                                                                                                                                                                 service.pid = org.openhab.automation.jrubyscripting.internal.JRubyScriptEngineFactory                                                                                                                                                                                                                                       ----------------------------------------------------------------                                      

Which made me suspicious, and created the config file by hand.

/etc/openhab # cat services/jruby.cfg
org.openhab.automation.jrubyscripting:gems=openhab-scripting=~>4.0

Which shows up at the correct pid:

----------------------------------------------------------------                                                                                                                                                                                                                                                               Pid:            org.openhab.automation.jrubyscripting                                                                                                                                                                                                                                                                          BundleLocation: null                                                                                                                                                                                                                                                                                                           Properties:                                                                                                                                                                                                                                                                                                                       gems = openhab-scripting=~>4.0                                                                                                                                                                                                                                                                                                 service.pid = org.openhab.automation.jrubyscripting                                                                                                                                                                                                                                                                         ----------------------------------------------------------------                    

This seems to have solved the issue, I now see gems show up in the OpenHAB lib folder, and the script loads successfully.

Is it safe to say I ran into a bug with the JRuby Scripting bundle?

@mvandamn What command did you use to see this pid?

In the OpenHAB console you can get all config settings using config:list, and look for the JRuby ones.

openhab> config:list | grep -B 5 -A 1 automation.jruby
Properties:
   defaultSink = enhancedjavasound
   felix.fileinstall.filename = file:/var/lib/openhab/etc/org.openhab.audio.cfg
   service.pid = org.openhab.audio
----------------------------------------------------------------
Pid:            org.openhab.automation.jrubyscripting
BundleLocation: null
Properties:
   gems = openhab-scripting=~>4.0
   service.pid = org.openhab.automation.jrubyscripting
----------------------------------------------------------------
Pid:            org.openhab.automation.jrubyscripting.internal.JRubyScriptEngineFactory
BundleLocation: null
Properties:
   gems = openhab-scripting=~>4.0
   service.pid = org.openhab.automation.jrubyscripting.internal.JRubyScriptEngineFactory
----------------------------------------------------------------

Thanks!

EDIT: Sorry I was mistaken. I encountered the same issue as you reported. I’ll look into this more, thank you for the report.

PR created [jrubyscripting] Fix UI configuration property id by jimtng · Pull Request #12650 · openhab/openhab-addons · GitHub

@mvandamn Would you mind testing the fixed addon from here:

The jar should work on your openhab 3.2.0 installation, I think. You’d need to uninstall the built in one first, and drop this jar in your /addons directory.

An openhab restart will be necessary.

Thank you!

Hi @JimT,

I’ve done the following to test:

  • removed the JRuby Scripting addon
  • stopped OpenHAB
  • removed the /etc/openhab/scripts/ruby folder
  • removed my jruby.cfg file
  • restarted OpenHAB

This didn’t clear out the configuration so I deleted those per console

openhab> config:delete org.openhab.automation.jrubyscripting
openhab> config:delete org.openhab.automation.jrubyscripting.internal.JRubyScriptEngineFactory
  • downloaded the new version to the addons folder (/usr/share/openhab/addons)
  • restarted OpenHAB
  • confirmed that we now have a working engine, but without library (LoadError) no such file to load -- openhab
  • added openhab-scripting=~>4.0 to the gems parameter via the web-interface

This seems to end up at the right pid.

Pid:            org.openhab.automation.jrubyscripting
BundleLocation: null
Properties:
   gems = openhab-scripting=~>4.0
   service.pid = org.openhab.automation.jrubyscripting

After a couple of seconds the gems have reappeared in the scripts folder, and the automation script loads without errors.

1 Like

@mvandamn Thank you again for the excellent test and feedback. It sounds like it had solved the problem.

The PR has been merged to be included in the upcoming 3.3.0.M4

You are welcome to continue using the new jar until you’ve upgraded to 3.3 (although you’d need to remember to swap it back with the release build then).

There have been several fixes since 3.2.

1 Like

Thanks @JimT and you’re welcome :slight_smile:

JRuby combined with the library is a real joy to write automatons in!

1 Like

I don’t know if I’m being blind or stupid, but where am I supposed to put the actual rules files written in ruby?

Simply changing existing rules in conf/rules/some_rule.rules to the jruby syntax complains about do. If putting them into conf/automation/lib/ruby/personal (the path referred by “org.openhab.automation.jrubyscripting:rubylib” in jruby.cfg also isn’t doing anything.

jruby scripting is definitely working as evident by dropping hello world into conf/automation/jsr223/ruby/personal/test.rb' which is triggering.

Your scripts should be in conf/automation/jsr223/ruby/personal/

1 Like

So just to make this completely clear:

conf/automation/jsr223/ruby/personal

This is for:

  • openHAB scripts (triggerable from any rule written in any language)
  • JRuby rules

conf/automation/lib/ruby

This is for:

  • functionality shared by multiple scripts/JRuby rules.

conf/rules

This is only for rules written in the openHAB rule DSL

Is this correct?