Variable Declarations in new setup of openHAB 3

Hi,

I setting up a new openHAB 3 server from scratch.

As described in Rules | openHAB I would like to declare variables for rules, like

var TempNotif = new Boolean(false)

How can this be done with the rule UI of openHAB 3?

Thanks

Like openHAB itself the UI is flexible and supports many different rule / scripting languages. The choice is a personal one after weighing the advantages & disadvantages.

Meaning it can’t be done with rules UI? At least the new doc in the link above is misleading, because it comes immediately after the rules UI.

I mean there are many choices in the UI.
Actually for automation there are now scripts & rules which can server similar functionality.

With language are you closing for the script action?

If this a global variable or local to the rule?

There isn’t enough information to say one way or the other.

1 Like

I am using Rules DSL.

I am using the variable as follows: Not clear to me whether this is a global variable. All I know is that it is not in the “then” body of the rule:

var TempNotif = false

rule "Sprudelstein" when
    Channel 'astro:sun:local:rise#event' triggered START
then
    if ( (Localweatherandforecast_OutdoorTemperature.state as Number).floatValue >= 1 && TempNotif == false) {
        shellyplugs977729192168248_Power.sendCommand(ON)
        logInfo("Sprudelstein", "Set to on at sun rise as Temp = " + Localweatherandforecast_OutdoorTemperature.state.toString + "°C" )
        TempNotif = true
    } else if ( (Localweatherandforecast_OutdoorTemperature.state as Number).floatValue <= 0.5 && TempNotif == true) {
        TempNotif = false
        shellyplugs977729192168248_Power.sendCommand(OFF)
        logInfo("Sprudelstein", "Set to off at sun rise as Temp =" + Localweatherandforecast_OutdoorTemperature.state. toString + "°C" )
    }
end

Any variable defined outside of a rule is a global variable. And I know of no way to do that in Rules DSL in MainUI Rules. And that’s fine, you can keep the .rules file for that.

Or you can use ECMAScript in which case you can set and keep the variable from one run to another using:

this.TempNotif = (this.TempNotif === undefined) ? false : this.TempNotif;

I’m sure Python and Groovy have a similar capability.

Thank you @rlkoshak