Lambda functions not executing from a rule and null reported in log

I’m trying my hand at lambda functions to centralize some logic. I’m unable to get the lambda function to actually execute and get an odd [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'test sensor 1': null in the openhab log but no issues reported on the rule file.

I’ve created the below test setup to illustrate my issue and I’d appreciate any tips or help in sorting this issue.

I’m using the console and the command smarthome:send test_sensor1 ON to test the rule and hopefully the logic of the lambda function.

test1.items file

Switch test_switch1     "Test Switch #1"       <light>        (gDEBUG)
Switch test_sensor1     "Test Sensor #1"        <wallswitch>    (gDEBUG)

test1.rules file

val logName = "test1"

val lambda1 = [ GenericItem sensorX, GenericItem switchX, Number delayX |
    logInfo( logName, sensorX.name + " state is " + sensorX.state )
    logInfo( logName, switchX.name + " state is " + switchX.state )
    true
]

rule "test sensor 1"
when
    Item test_sensor1 changed
then
    logInfo( logName, "starting: 'test sensor 1' rule...")
    lambda1.apply( test_sensor1, test_switch1, 1 )
    logInfo( logName, "ending: 'test sensor 1' rule!")
end

openhab.log output

2020-02-01 15:37:20.096 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'test1.rules'
2020-02-01 15:37:50.660 [INFO ] [eclipse.smarthome.model.script.test1] - starting: 'test sensor 1' rule...
2020-02-01 15:37:50.660 [INFO ] [eclipse.smarthome.model.script.DEBUG] - test_sensor1 changed from: OFF to: ON
2020-02-01 15:37:50.661 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'test sensor 1': null

Any suggestions on how to fix this?

Edit: probably should have mentioned that I’m running OH 2.5.1 on ubuntu 18.04

Guessing, but I think the 1 literal will be an integer by default, and not fit the Number parameter.

I added a new lambda function and tried again with the same results so I don’t think it’s related to the ‘1’ value in the parameter.

val lambda2 = [ GenericItem sensorX, GenericItem switchX |
    logInfo( logName, sensorX.name + " state is " + sensorX.state )
    logInfo( logName, switchX.name + " state is " + switchX.state )
    true
]

rule "test sensor 1"
when
    Item test_sensor1 changed
then
    logInfo( logName, "starting: 'test sensor 1' rule...")
    //lambda1.apply( test_sensor1, test_switch1, 1 )
    lambda2.apply( test_sensor1, test_switch1 )
    logInfo( logName, "ending: 'test sensor 1' rule!")
end

openhab.log

2020-02-01 16:53:30.626 [INFO ] [eclipse.smarthome.model.script.DEBUG] - test_sensor1 changed from: OFF to: ON
2020-02-01 16:53:30.627 [INFO ] [eclipse.smarthome.model.script.test1] - starting: 'test sensor 1' rule...
2020-02-01 16:53:30.629 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'test sensor 1': null

Oh …

from memory, you need to give the return type(?) too e.g. “three entries” for a two parameter lambda. Look into that.

EDIT it’s more complicated than that. See here for description of “auto procedure or function” action

I’ve looked at that example a few times now and have tried the various examples it provides. The only one that I can get to work is the example provided by @rlkoshak. If I tried to extend his example with additional parameters being passed, I still get the [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'test sensor 1': null in the log.

Can anyone point me to an example lambda function with multiple parameters known to work with OH 2.5?

Works for me, 2.5.0, placed near head of rules file

val loggit= [ GenericItem s , GenericItem t |
    logInfo("lambda", "param 1 " + s.name)
    logInfo("lambda", "param 2 " + t.name)
    s.state.toString + " " + t.state.toString + " states"
]

Called from a rule by either method

val loggedStr = loggit.apply(test_U1RollerW, ct_U1RollerW)
logInfo("test", "return " + loggedStr)

loggit.apply(test_U1RollerW, ct_U1RollerW)
2020-02-02 00:43:30.635 [INFO ] [clipse.smarthome.model.script.lambda] - param 1 test_U1RollerW
2020-02-02 00:43:30.636 [INFO ] [clipse.smarthome.model.script.lambda] - param 2 ct_U1RollerW
2020-02-02 00:43:30.638 [INFO ] [.eclipse.smarthome.model.script.test] - return ON OPEN states
2020-02-02 00:43:30.640 [INFO ] [clipse.smarthome.model.script.lambda] - param 1 test_U1RollerW
2020-02-02 00:43:30.641 [INFO ] [clipse.smarthome.model.script.lambda] - param 2 ct_U1RollerW

EDIT - just realized your problem
logName is undefined in your lambda.
“Peer” globals, like global variables and lambda functions, cannot see each other.
Everything you want to use in a lambda has to be passed in or created.
This is described in Rich’s notes.

Thanks for testing this and finding my error!

I’m finding the DSL Rules to be quite aggravating. I think I’m going to take a look at the new Jython option and see if it’s more my speed.

Again, Thanks!

1 Like