Hello,
in OpenHab 2.5 I had global variables defined in my rules files.
In Openhab 3.0 I have created my rules via UI, so that when an item is triggered, a Rules DSL script gets executed.
I definded the variables at the beginning of the script, but it seems the variables are not global,e.g.
val String CommandOpen = "OPEN"
val String CommandClose = "CLOSE"
var String LastCommand = ""
logInfo("DaylightRule", "LichtsensorOsten_Daylight is: " + LichtsensorOsten_Daylight.state)
logInfo("DaylightRule", "LichtsensorSuden_Daylight is: " + LichtsensorSuden_Daylight.state)
logInfo("DaylightRule", "LichtsensorWesten_Daylight is: " + LichtsensorWesten_Daylight.state)
logInfo("DaylightRule", "LastCommand was: " + LastCommand)
if(LichtsensorOsten_Daylight.state == ON &&
LichtsensorSuden_Daylight.state == ON &&
LichtsensorWesten_Daylight.state == ON &&
RsAutoDayNight.state == ON &&
LastCommand != CommandOpen)
{
logInfo("DaylightRule", "Open rollershutters, because it is day")
if(RsAutoDayNightExcludeRoof.state == ON)
{
RollerShutterHouseExcludeRoof.sendCommand(UP);
}
else
{
RollerShutterHouse.sendCommand(UP);
}
LastCommand = CommandOpen
}
In this example, the variable ‘LastCommand’ is always empty, when I look in the log file. So I guess it is only a local variable. How do you create a global variable, when using the UI to create rules?
Thinking about it, in this example I could create an item of type string to “remember” the value.
But what about more complex rules using timers?
In OH 2.5 my timers were defined at the beginning of the rules file
var Timer LightSensorEastCloseTimer = null
var Timer LightSensorEastOpenTimer = null
var String LastCommandEast = ""
and the rule then checked if a timer was running
//check if we are above the threshold
if(LightSensorEast.state >= OpenCloseThreshold)
{
//stop OpenTimer, if it is running
if(LightSensorEastOpenTimer !== null)
{
LightSensorEastOpenTimer.cancel
LightSensorEastOpenTimer = null
//logInfo("LightSensorEast", "LightSensorEastOpenTimer stopped")
}
There is a nuance. These really are not “globals” in the same sense as one has with .rules files. In a .rules file, that global variable can be referenced by any rule in that file. That is no achievable in the UI. Instead all you can really do is preserve a locally defined variable from one run to the next. The variable still only exists in that one context (Script Action or Script Condition). Given that, if you have two Script Actions, you can’t even share the variable between them in the same rule.
It should be theoretically possible to do this in all the supported languages, but I only know for sure how to do it in JavaScript.
So the tl;dr is, if it isn’t broken, don’t fix it. UI created rules really are different in how you have to think about them and the over all way to approach solving certain problems. You can’t just copy and paste all your .rules files into the UI and have them work. If you don’t want to have to rework, restructure, or out right rewrite your rules in a whole new language, keep them in your .rules files. They still work there too.
No one I know of right now. I’ve seen some hints of possibilities but someone will have to spend time looking through the code to figure out if and how it’s possible. And there is a perfectly fine work around in just using .rules files so this isn’t anywhere near the top of my personal to do list.