Failed: null when passing SwitchItem to lambda

I am trying to create a lambda that will accept a SwitchItem as a paremeter but I am getting a:

Script execution of rule with UID 'automation_shutter-1' failed: null in automation_shutter

when trying to execute rule.
I have isolated issue to this example:

val LOGGER_NAME = "automation_blinds"
val foo = [ SwitchItem test |
  logInfo(LOGGER_NAME, "foo")
  //test.state
]

rule "automation_blinds"
when
  Time cron "*/5 * * ? * * *"
then
then
  logInfo(LOGGER_NAME, "calling foo with: " + aByAngleRoletaKuchniaOknoEnabled)
  foo.apply(aByAngleRoletaKuchniaOknoEnabled)
end

The result is:

2021-08-04 00:17:15.438 [INFO ] [.core.model.script.automation_blinds] - calling foo with: aByAngleRoletaKuchniaOknoEnabled (Type=SwitchItem, State=ON, Label=Auto (kat) - Roleta Kuchnia okno, Category=null, Groups=[gAParterRolety])
2021-08-04 00:17:15.442 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'automation_shutter-1' failed: null in automation_shutter

I have also tried to use GenericItem as parameter type but with no luck.

What’s LOGGER_NAME?

It is a constant that I forgot to copy paste to the post. Logger works reliably which is visible in logs

calling foo with: aByAngleRoletaKuchniaOknoEnabled (Type=SwitchItem, State=ON, Label=Auto (kat) - Roleta Kuchnia okno, Category=null, Groups=[gAParterRolety])

It was more of a hint, really.
LOGGER_NAME is a ‘global’ to this rule file.
You lambda is also a ‘global’.
These globals can’t “see” each other.

val X = 2  // so-called global 
val Y = X + 2 // fails, can't "see" X in global space

Golden rule of DSL lambdas defined in global space, everything you want to use must be passed in as parameters.

In this case of course, you could just use a constant instead
logInfo("myLambda", "works with hardwired constant")

2 Likes

Thank you very much it was really unexpected for me.