Rules with java imports not working after migration from 2.5 to 3.0

Hi,

After migrating my rules to openHAB 3 (from rules file to web-rule) all my rules with Java imports are not working anymore.

E.g. “import import java.util.concurrent.locks.ReentrantLock” throws an error and the rule cannot be executed. It works fine in a rule file though.

How do I have to state the import in the openHAB rule engine to make it work?

Thank you!

Without example code is impossible to tell.

But in OH 3 there is absolutely no need to use a Reentrant lock. Any individual rule can only have one instance running at a given time. If the rule is triggered while it’s already running, that the subsequent triggers are queued up and processed in sequence.

Therefore, even if you have a reentrant lock, it won’t do anything because it will never be the case where two separate threads need to view for the lock.

1 Like

Hi @rlkoshak,

Thank you, I was trying to run the following script to get the status of my washing machine. This ran well in 2.5 for quite some time:

// import
import java.util.concurrent.locks.ReentrantLock

// variables and values
val Number MODE_OFF = 0
val Number MODE_STANDBY = 1
val Number MODE_ACTIVE = 2
val Number MODE_FINISHED = 3
val ReentrantLock Waschmaschine_lock = new ReentrantLock()
val telegramAction = getActions("telegram","telegram:telegramBot:xxx")

// rule
if (Wash_power.state < 0.5) Washingmachine_OpState.postUpdate(MODE_OFF)
else if (Wash_power.state > 100) Washingmachine_OpState.postUpdate(MODE_ACTIVE)
else if (Wash_power.state < 6.5) {
  if (Washingmachine_OpState.state == MODE_OFF) Washingmachine_OpState.postUpdate(MODE_STANDBY)
  else if (Washingmachine_OpState.state == MODE_ACTIVE) {
    Waschmaschine_lock.lock()
      try {
        Thread::sleep(100000) // Debounce for 100 seconds
        if (Wash_power.state < 6.5) { 
          Washingmachine_OpState.postUpdate(MODE_FINISHED)
          telegramAction.sendTelegram("Washing Machine Finished")
        }
      } finally {
        Waschmaschine_lock.unlock()
      }
  }
}

So in OH 3 i can scratch that lock-function completely?

What about other java imports like hash maps?
This I am using for a DSL scripts that gives me the gas station prices neatly listed on telegram request:

import java.util.HashMap

    if (telegram_last_message_text.state.toString == "Diesel")
{
        var reply = "Dieselpreise:"

        val HashMap<String, DecimalType> DieselMap = newHashMap
        if (TankstelleShellRissenerLandstr1a_Diesel.state != NULL && TankstelleShellRissenerLandstr1a_Diesel.state != UNDEF && TankstelleShellRissenerLandstr1a_Openingstate.state == OPEN) {
            DieselMap.put("SHELL Rissener Landstrasse 1a", (TankstelleShellRissenerLandstr1a_Diesel.state as DecimalType))
        }
        if (Tankstelle_Suelldorfer_Landstrasse_Diesel.state != NULL && Tankstelle_Suelldorfer_Landstrasse_Diesel.state != UNDEF && Tankstelle_Suelldorfer_Landstrasse.state == OPEN) {
            DieselMap.put("ARAL Suelldorfer Landstrasse 91", (Tankstelle_Suelldorfer_Landstrasse_Diesel.state as DecimalType))
        }
        if (TankstelleJETOsdorferLandstr170_Diesel.state != NULL && TankstelleJETOsdorferLandstr170_Diesel.state != UNDEF && TankstelleJETOsdorferLandstr170_Openingstate.state == OPEN) {
            DieselMap.put("JET Osdorfer Landstrasse 170", (TankstelleJETOsdorferLandstr170_Diesel.state as DecimalType))
        }
        for (Price : DieselMap.entrySet.sortBy[value]) {
            var String key = Price.getKey().toString
            var String value = Price.getValue().toString
            reply = reply + String::format("\n%s: %s €", key, value)
        }

        logInfo("Dieselpreise", reply)
        val telegramAction = getActions("telegram","telegram:telegramBot:xxx")
		telegramAction.sendTelegramAnswer(telegram_reply_id.state.toString, reply)
}
    else
{
        var reply = "Preise Super:\n"

        val HashMap<String, DecimalType> E5Map = newHashMap
        if (TankstelleShellRissenerLandstr1a_E5.state != NULL && TankstelleShellRissenerLandstr1a_E5.state != UNDEF && TankstelleShellRissenerLandstr1a_Openingstate.state == OPEN) {
            E5Map.put("SHELL Rissener Landstrasse 1a", (TankstelleShellRissenerLandstr1a_E5.state as DecimalType))
        }
        if (Tankstelle_Suelldorfer_Landstrasse_E5.state != NULL && Tankstelle_Suelldorfer_Landstrasse_E5.state != UNDEF && Tankstelle_Suelldorfer_Landstrasse.state == OPEN) {
            E5Map.put("ARAL Suelldorfer Landstrasse 91", (Tankstelle_Suelldorfer_Landstrasse_E5.state as DecimalType))
        }
        if (TankstelleJETOsdorferLandstr170_E5.state != NULL && TankstelleJETOsdorferLandstr170_E5.state != UNDEF && TankstelleJETOsdorferLandstr170_Openingstate.state == OPEN) {
            E5Map.put("JET Osdorfer Landstrasse 170", (TankstelleJETOsdorferLandstr170_E5.state as DecimalType))
        }
        for (Price : E5Map.entrySet.sortBy[value]) {
            var String key = Price.getKey().toString
            var String value = Price.getValue().toString
            reply = reply + String::format("\n%s: %s €", key, value)
        }

        logInfo("Preise Super", reply)
        val telegramAction = getActions("telegram","telegram:telegramBot:xxx")
		telegramAction.sendTelegramAnswer(telegram_reply_id.state.toString, reply)

    }

I am not a big code person therefore I did not even dare to think about moving to other rule languages than DSL…

Yes

You should be able to import those. If it’s not working, without seeing the specfic error all I can suggest is to file an issue.

1 Like