OH 3 - Quick way to set a lot of global variables to a value

I have a lot of global variables that I want to mass update them.

Is there anyway to do this via a group, at the rule level, vs. making these global variables an item to use a group?

Best, Jay

Do you mean items or variables?

Global Variables, not items.

Best, Jay

Which scripting language?


Best, Jay

Is this at startup or what event would you use to set the variables?

It’s at startup and in certain periods of the day based on X scenario.

I have 90+ timers I want to cancel and null them based on them being !== null based on X scenario.

I have a very large OH system (2,400 items and 38k lines of rules).

Best, Jay

What about putting all your variables into a map or array, so that you can loop through it?

Of cause this would require updating existing rules …

Or a selection of Maps, so that you can look up property “fred” in myMapDaytime, or myMapEvening, etc. as required.

Here’s a scenario, you have 30+ timers like this:

var timer one
var timer two

I want to check if “any” timer !== null then cancel them with 1 line of code.

I also want to set all these timers to null with 1 line of code.

Not sure how to group these global variables at the top of the rule file, not even sure it’s possible?

Hence, reducing lots of code in my rule file.

Best, Jay

This is a bit different to ‘managing variables’. The var Timer xxx is indeed a global variable, but it is not “the timer”, it is just a pointer or reference to an independent Timer thread. Wishing hard for something to cancel thirty Timers in one line won’t make it happen, but it needn’t be too hard.

Put all the references into a single Map object (an array-like set of key-value pairs) instead of individual variables. This is a common technique where you might have a bunch of timers associated with individual Items, storing with key as Item name string, value as the createTimer() return.

If you just want to destroy all the references, set the Map to null. The actual Timers will continue.

If you want to cancel the associated Timers, you’ll have to iterate through the Map (a for-each construction) and check/cancel individually. It sounds more complicated than it is.

1 Like

Just to be clear because it hasn’t been explicitly stated here. The concept of Groups only exists for Items.

The closest thing to a Group for variables is, as recommended here, using a Map or a List to store the variables. Then you can iterate over the entries in the Map or List and do your operations on them as desired.

In JS Scripting I’ve a library that actually does this sort of thing with Timers for you. I’m working on making it installable as an npm and will then see if there is interest in adding it as a part of openhab-js. But with TimerMgr there is a cancelAll() method with will cancel all the currently existing timers handled by the manager. Null timers would not exist.

I posted the code for TimerMgr at Some JS Scripting UI Rules Examples which might give you some ideas for how to handle manage the book keeping involved when using a Map for this in Rules DSL. See Design Pattern: Working with Groups in Rules for how to filter and otherwise manipulate Lists in Rules DSL (a Group’s members is just a List.).

1 Like