JRuby: accessing item from a sub routine in the lib directory

Hi,

I wonder how I can access items inside a submodule from within the lib directory.

In my main file I have access:

require 'java'
require_relative './lib/yield_extensions.rb'
require_relative './lib/consumption_extensions.rb'
    
java_import org.openhab.core.library.types.QuantityType
    
TOTAL_CAPA_BYD = QuantityType.new("12.8 kWh")

DEFAULT_CHARGE_RATE = QuantityType.new("1.0 kW")
MIN_CHARGE_RATE     = QuantityType.new("2560 kW")
   
MIN_SOC  = QuantityType.new("20 %")
WAIT_SOC = QuantityType.new("80 %")
MAX_SOC  = QuantityType.new("80 %")
    
IS_ENABLED = true
    

rule 'pv_storage_optimisation' do
    #cron '0 0 0 * * ?' 
    cron '0 0 * * * ?' 
    #cron '0 0 23 * * ?' 
  
    run{
 
        logger.warn("CCConsumption::GetDailyForecast - IBBS_TotalPower=#{IBBS_TotalPower.state}")
        ...

I get

2025-09-22 15:21:41.508 [WARN ] [tion.rule.pv_storage_optimisation:20] - CCConsumption::GetDailyForecast - IBBS_TotalPower=2573 W

but inside ./ruby/lib/consumption_extension.rb it is not possible:

java_import java.time.LocalDateTime
java_import org.openhab.core.library.types.QuantityType

module CCConsumption

    def self.GetDailyForecast()
        logger.warn("CCConsumption::GetDailyForecast - IBBS_TotalPower=#{IBBS_TotalPower.state}")
    ...

I get

2025-09-22 15:21:41.911 [ERROR] [tion.rule.pv_storage_optimisation:20] - uninitialized constant CCConsumption::IBBS_TotalPower (NameError)

Is this not possible in general or is there a easy solution for this ?
My goal is to call persistence functions like “average_since” on these item.

No need for require ‘java’

No need to import QuantityType

And I think importing Localdatetime is also unnecessary

Add include OpenHAB::DSL in your module.

Hm, I tried this but it’s not working.

For testing I created a new module:

#include OpenHAB::DSL 

module MTest

    def self.test()

        return 0

    end
end

I call it in the main rule:

    run{

        ltest = MTest.test

After editing and saving the rule “pv_storage_optimisation” is shown again in the rules section of the openHAB menu after a few moments :

But when I uncomment the line

include OpenHAB::DSL 

and I reload the files (the test file and after that the main rule file) then the rule “pv_storage_optimisation” is not shown anymore ?!
And in the log there’s no error or warning message - only the “(Re-)Loading” hint.

2025-09-22 17:06:29.271 [INFO ] [ort.loader.AbstractScriptFileWatcher] - (Re-)Loading script '/etc/openhab/automation/ruby/pv_storage_optimisation.rb'

Edit
Oh, I’m sorry for this - I forgot to tell that I still running on openHAB 4.3.7 (Openhabian 64Bit)

I was answering from phone, so it was super terse.

This should work - change module to class

class Test
  def self.daily_forecast
    logger.warn("CCConsumption::GetDailyForecast - IBBS_TotalPower=#{IBBS_TotalPower.state}")
  end
end

Test.daily_forecast

The latest JRuby library applies to all supported versions (OH 4.1+)

OH 3.4.x up to OH 4.0.x will use jruby scripting library version 5.35.1.

Ha, now it’s working.
It’s so easy once you know how.

Thank you :slightly_smiling_face:

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