I am new to openhab, but for 30+ years programming in different languages (Fortran, Pascal, Assembler, C/C++, some Java). I have an issue getting ANY rule DSL running, even the examples quit with errors (which is quite strange for me.). I am running OH3 (latest stable) in Portainer on Asustor, everything else works fine, rules clicked together with “when” … “then” from the UI are working fine.
What is my basic problem here? I checked the documentation, I tried the sample snippets, nothing is working.
Example:
var Timer T = NULL
T = createTimer(now.plusSeconds(5), [ |
logInfo(“rules”, “Timer activated”)
//Do something…
])
logInfo(“rules”, “Timer created”)
T = NULL
throws errors:
2021-11-04 13:28:49.512 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘1f41eb55cb’ failed: var Timer T = ___ NULL
T = createTimer(now.plusSeconds(5), [ |
logInfo(“rules”, “Timer activated”)
//Do something…
])
logInfo(“rules”, “Timer created”)
T = NULL
Type mismatch: cannot convert from UnDefType to Timer; line 1, column 14, length 4
Type mismatch: cannot convert from UnDefType to Timer; line 8, column 211, length 4
NULL is a state-type object, something you can set an Item state to.
You can’t set a Timer type object to a state type value.
What you intended was null, the void placeholder in DSL.
But if you look at your code, you don’t need this T at all. You set it null, create a timer, set it null again.
Just create an anonymous timer. createTimer(now.plusSeconds(5), ...
Thanks for your quick and helpful answer, just tested, and it works. I assumed it should work this way because I used exactly the code which is provided with OpenHAB. I did not see that NULL != null.
somewhere else here i found a explanation that i noted for me:
// NULL (==) is a state that indicates that an Item is uninitialized.
// null (===) is something that is only meaningful in Rules and means a variable doesn’t have a value.
Thanks, that difference is clear. But what is still confusing to me is that the example code snippets throw errors. E.g:
var Timer myTimer = null
rule "timer example"
when
Item YourItem changed
results in:
Script execution of rule with UID ‘1f41eb55cb’ failed: var Timer myTimer = null
I need to reference the timer (the anonymous version works, but it’s not the one I need), can pls anyone explain why the given examples seem to be “wrong”?
You cannot copy-paste DSL rules intended for xxx.rules file usage into GUI entered rule action script section. The GUI manages all the when-then-end stuff, you only need the body.
You can’t in GUI based DSL. (There is nowhere “outside the rule” to define ‘globals’)