logInfo from a lambda-created lambda

Hi,
I am trying to “DRY”-up my rules by consolidating the creation of timers to a lambda-function.

I have read else-where, that within the context of the “global” lambda, I can’t access global variables.

I got most of the stuff working, but the logInfo (and any other log*) function within the inner lambda is not showing up in any OpenHAB logs.

Example:

val startShortTimer = [String roomName,GenericItem kommandoItem, long duration, String logRuleName, Map <String, Timer > timerMap,int localCommand | 
  logInfo(logRuleName,"Timer for "+roomName+" started with " + duration + " seconds")
  if (timerMap.containsKey(roomName) && timerMap.get(roomName)!==null){
    timerMap.get(roomName).cancel()
    timerMap.get(roomName).purge()
  }
  val Timer timer = createTimer (now.plusSeconds(duration),[|
                    logDebug("DUMMY","ResetTimer absence")
                    sendCommand(kommandoItem,localCommand)
                    ])

  logInfo(logRuleName,"Placing timer in data-structure")
  if (timer == null) {
    logInfo(logRuleName,"Reporting: timer is NULL")
  }
  if (roomName == null) {
    logInfo(logRuleName,"Reporting: roomName is NULL")
  }
  if (timerMap === null){
    logInfo(logRuleName,"Reporting: kurzerNachlaufTimer is NULL")
  }
  timerMap.put(roomName,timer)
]

How can I enable the inner logging?
Is there some log-object I can pass to the lambda?

I’m not deep into functions (the outer lambda) but afaik logRuleName will not be valid as it’s not a global variable.

However, you don’t need the “per timer and per room individual logger”, so simply set the logger name to a meaningful string, e.g. “roomTimer”. all log lines then will be logged through the logger org.openhab.core.model.script.roomTimer

Hi,
thanks for the quick reply.

  1. logRuleName is passed in as an argument
  2. and the main issue: the logDebug(“DUMMY”,…) from the inner lambda is showing up nowhere, even though the send-command seems to dispatch properly

Sorry, did not read the code properly.

Did you setup org.openhab.core.model.script.DUMMY to DEBUG level? per default, all loggers org.openhab.core.model.script.* are at INFO level, so no debug messages are logged.

If you are going to go through all that effort, you might consider looking at the other rules options where true libraries are possible as opposed to Rules DSL’s minimal support for functions.

use logInfo or change the logging level of “org.openhab.core.model.script.DUMMY” to DEBUG. By default loggers log at the INFO and above level, skipping DEBUG and TRACE as @Udo_Hartmann indicates.

Hi, if I move the log outside the inner lambda it shows up.
It must be s.th. with the nesting.

What other stable languages do you recommend? I don’t wanna go the visual languages though.

Any of them are better than Rules DSL. JS is probably most popular but jRuby has a good following and Python 3 support is coming to OH 5.

With JS, there’s openHAB Rules Tools Announcements which includes a TimerMgr which looks like it dose much of what you are trying to do here for you. That can be installed using npm.

Thanks for the update.
I stuck with the “old” text-rules in order for my installation not to require furthe dependencies. I will investigate js a bit.

Regardless, I tested moving the inner logInfo(“Dummy”) to the outer lambda, and it works.
So I would conclude, that some info gets lost when constructing the inner lambda.

Cheers

No doubt. There is a reason why I assert Rules DSL is no longer fit for purpose. It has so amny quirks and inconsistencies that far outweigh any benefits it once had now that the other rules languages have caught up.

Note that most of the rules languages are official add-ons now and they come with their helper library preinstalled. The only dependency is to install the add-on.