slefevre
(Scott LeFevre)
February 1, 2020, 8:55pm
1
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
rossko57
(Rossko57)
February 1, 2020, 9:40pm
2
Guessing, but I think the 1 literal will be an integer by default, and not fit the Number parameter.
slefevre
(Scott LeFevre)
February 1, 2020, 9:56pm
3
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
rossko57
(Rossko57)
February 1, 2020, 10:01pm
4
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
Major Edit: Rewrote with the cleaner OH 2.3 syntax.
The example lambda on the OH 1 wiki is overly complex and as written will not work in OH 2. This quick example will show a super simple lambda example with copious notes.
Here is the code without comments. It takes in an Item, logs it’s state and returns a slightly modified version of the String logged.
val log= [ GenericItem s |
logInfo("lambda", s.state.toString)
s.state.toString + " logged"
]
rule "Call Lambda"
when
// some t…
slefevre
(Scott LeFevre)
February 2, 2020, 12:05am
5
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?
rossko57
(Rossko57)
February 2, 2020, 12:47am
6
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.
slefevre
(Scott LeFevre)
February 3, 2020, 2:09pm
7
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