Global variables in rules defined in UI with RulesDSL?

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?

Regards
Bernd

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")
            }

How can I do this via UI?

You canā€™t.

Or strictly, you can but not with DSL.

You can continue to use file-based DSL rules, with complex ā€˜globalsā€™ like Timers or Maps.

1 Like

Ok, then please tell me, what is the OH3 way to create rules with timers?

You can continue to use file-based DSL rules. This works in OH3.

You can use UI based javascript rules, and hijack ā€œthisā€ object for globals equivalence.

Iā€™m not sure about how it may be done in jython etc.

What are you hoping to gain here?

I just want to get all my rules transferred to OH3 and I try to do all via UI.

Thatā€™s easy.

Thatā€™s your choice. Youā€™d have to make changes in the way you work.

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.

1 Like

That would be fine for me!

Who could know how to do this in Rules DSL?

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.

OK, no problem.

Then I will try javascript rules :slight_smile: