JRuby Scripting with JRuby 9.4 for openHAB 3.4.x [3.4.0;3.5.0)

This bundle provides the same JRuby scripting automation add-on for OpenHAB 3.4.x 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-scripting=~>5.0
  • The default require setting is openhab/dsl

This makes the addon ready to use with the latest official 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-scripting

Changelog

Version 0.0.4 - 2023-03-09

  • Upgraded to jruby 9.4.2.0
  • Use the official openhab-scripting gem version 5.0

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-0.0.4-jruby-9.4.2.0-oh3/org.openhab.automation.jrubyscripting-3.4.3-SNAPSHOT.jar

2 Likes

reserved

v0.0.3 includes the dependency tracking feature.

Updated to v0.0.4