JRuby in OH4

Continuing the discussion from the openhab 4.0 snapshot discussion thread


I have uninstalled the built in jrubyscripting addon from OH4 and dropped this jar file in my add-ons folder.

I have used the console to ensure the addon was properly loaded.

openhab> bundle:list  
309 │ Active  │  80 │     │ openHAB Add-ons :: Bundles :: Automation :: JRuby Scripting

the errors in the logs have changed. It does appear the helper library is now loading.
I’m seeing this on start up

==> /var/log/openhab/openhab.log <==
2023-03-12 09:33:00.398 [WARN ] [ort.loader.AbstractScriptFileWatcher] - Failed to unload script '/etc/openhab/automation/ruby/.gem/': script reference not found.

the file referred to in the error message does NOT appear to exist in that location

And the typical error when a jruby automation runs is now this

==> /var/log/openhab/openhab.log <==
2023-03-12 09:38:00.745 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'AmbientLightDimmer' failed: Error during evaluation of Ruby in uri:classloader:/META-INF/jruby.home/lib/ruby/stdlib/delegate.rb at line 91: (NoMethodError) undefined method `to_i' for #<OpenHAB::Core::Items::Proxy:0x2a71c5c0>

This is a harmless warning. It will be silenced in the future build.

You might now be using version 5 of the helper library? If that’s the case, please read through the changelog for 5.0.0 carefully.

Specifically, items are not treated as state anymore. So if you meant to refer to its state / value, you need to write ItemName.state instead of ItemName.

You can still use the predicate methods on the item such as ItemName.on?

Mostly affected things like dimmer where you want to check the actual value, so change MyDimmer > 50 to MyDimmer.state > 50

There are other changes too so please read through. Timers also changed a bit especially timers with id.

1 Like

error message is gone, rule runs
so… snapshot seems to have cured your race condition!

:smiling_face: :upside_down_face:
ok… where is the face palm emoji?
I actually do remember reading this in the jruby thread and just got reckless with the upgrade for the purpose of testing, big thank you Jim for helping

Great job to all the developers and folks who have done an amazing job on OH4
The speed which Java 17 has been integrated and all the refinements since the early version I had been running are amazing

Not quite. The “fix” I had was just a hack. I’m still working on figuring out/testing a permanent fix.

ok, cool, let me know if I can help with testing or documenting

The PR to fix the NPE in jruby addon in OH4 just got merged into core, so keep an eye out for the next snapshot, possibly #3378.

1 Like

4.0.0-SNAPSHOT - Build #3379 included the fix so jrubyscripting addon can run without errors.

1 Like

4.0.0-SNAPSHOT - Build #3380 included Fix variable binding in ScriptTransformationService by jimtng · Pull Request #3464 · openhab/openhab-core · GitHub which means we no longer need to add the extra conditional variable assignments at the top of ruby transformations.

JRuby now has the simplest (yet probably the most powerful and succinct) syntax for script transformation.

You could do this inline transformation for a temperature item (which can of course be implemented as a file-based transformation for better code clarity). Just to show off what Ruby can do in “one line” of code.

Number:Temperature Outside_Temperature "Outside Temperature [SCRIPT(rb:|  input.to_f.|('°C').then { |t| %(#{t.format('%d °C')} / #{t.to_unit('°F').format('%d °F')}) }   ):%s]"

When the item contains 0 °C, this will produce a formatted state of 0 °C / 32 °F.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.