Global variables in UI Rules

Hello

I’ve been porting DSL rule files to the new UI based configuration but I have a problem: I don’t know how to share timers between rules.
In my actual setup I declare the timer with global scope and put all rules in the same file.
I don’t know how you can achieve same functionality using the UI. It’s a pretty common use case I think?

Regards
Pablo

AFAIK you can‘t. Every UI rule is running independent, like defining every rule in a single file.

@hmerk is correct, there is no way to share variable between rules. In fact there isn’t even a way to share variables between Script Actions and Script Conditions within the same rule.

There is an issue open and a discussion about how to handle this but it’s not see much action in awhile so I don’t know how long we need to wait. Theoretically it should be possible to do it now but I’ve not yet figured out how to do it.

While it is a common used case in Rules DSL, it’s not always necessary. Often the rules can be combined or Expire can be used instead. However, one gotcha is that Rules DSL does not support an operator to test whether a variable exists or not. Because of that we don’t have a way to preserve the state of a variable from one run of a rule to the next. Consequently, often to work with Timers in UI rules one needs to use JavaScript or one of the other rules languages.

In JavaScript

this.myTimer = (this.myTimer === undefined) ? null : this.myTimer;

That tests to see if this.myTimer exists and if it doesn’t initialize it to null. If it does exist keep its current value.

https://github.com/openhab/openhab-core/issues/2084 is the issue.

Yes you are right, but I prefer my rules to be clean than combine them with unsightly if-then-else or case control statements. In fact I do like rules to be very short if possible.
Openhab has a very powerful rule engine and perhaps this is too much to ask. I will migrate those rules that can be configured w/o using scripts, with are the majority.

1 Like

I followed your hint of using expire feature with a virtual item and worked better than the timer object. Thanks

I have used an item to remember something I needed to use later in a rule.

That’s a good choice a lot of the time. If using a language other than Rules DSL one can access Item metadata as well which can be convenient. For example, I have some lights that are controlled by time of day. However, if the light is manually controlled during the day, the automation is turned off. I determine whether or not to turn on or off the automation using a flag stored in the Item metadata.

However, you can’t really store or manage a timer in an Item except through the Expire function which is not universally applicable to all use cases.

1 Like

Could you share that solution? Sounds like it would solve one of my problems

Which solution, setting an Item metadata flag, detecting when a light is manually triggered, turning off the automation based on a flag?

The whole thing is really quite complex all put together. The individual parts though are not.

Which language? Which version of OH? Using the Helper Libraries or not? UI rules or text based rules?