JRuby Scripting with JRuby 9.4

This bundle provides the same JRuby scripting automation add-on as in the official add-on. The differences are:

  • Compiled with JRuby 9.4.x., which offers Ruby 3.1 compatibility.
  • The default gems setting is openhab-jrubyscripting=>5.0.0.rc1;<6
  • The default require setting is openhab/dsl

The following changes are also part of the official openhab-addons:

  • The default script directory is now CONF_DIR/automation/ruby/
  • The default rubylib directory is now CONF_DIR/automation/ruby/lib/
  • The default GEM_HOME directory is now CONF_DIR/automation/ruby/.gem/9.4.0.0/
  • This add-on now implements dependency tracking feature. When the required scripts are updated, all dependant scripts will be reloaded automatically.

This makes the addon ready to use with the openhab-jrubyscripting helper library pre-configured. Just install the addon and start writing rules!

Examples

Quick examples of scripting with JRuby, with the help of the pre-configured helper library:

rule 'Turn on light when sensor changed to open' do
  changed Door_Sensor, to: OPEN 
  run { Cupboard_Light.on for: 3.minutes } # Automatically turn it off after 3 minutes
end

Trigger on changes to member of group:

# Assumption: Motion sensor items are named using the pattern RoomName_Motion
# and Light switch items are named with the pattern RoomName_Light
rule 'Generic motion rule' do
  changed Motion_Sensors.members, to: ON
  run do |event|
    light = items[event.item.name.sub('_Motion', '_Light')] # Lookup item name from a string
    light&.on 
  end
end

Simple timed rule:

rule 'Turn off watering system after 5 minutes' do
  changed Watering_System, to: ON, for: 5.minutes
  run do
    Watering_System.off
  end
end

Simple timer:

# Turn on My_Light item 3 minutes from now
after 3.minutes do
  My_Light.on
end

Working with date/time:

max = Solar_Power.maximum_since(24.hours.ago)
if max.timestamp < LocalTime::NOON
  logger.info 'Max solar power #{max} happened before noon'
end

For more examples, see openhab-jrubyscripting

Changelog

Version 0.0.3 - 2022-12-05 (3.4.0.202212051035)

Version 0.0.2 - 2022-11-29 (3.4.0.202211290009)

Version 0.0.1

  • initial release

Resources

The latest jar:
https://github.com/jimtng/openhab-addons/releases/download/jrubyscripting-jruby-9.4.0.0-0.0.3/org.openhab.automation.jrubyscripting-3.4.0-SNAPSHOT.jar

Source:

2 Likes

reserved

v0.0.3 includes the dependency tracking feature.