How to do a Global Variable shared between Rule and Script

402 views
Skip to first unread message

Gerry Duprey

unread,
Mar 6, 2015, 9:22:37 AM3/6/15
to ope...@googlegroups.com
Howdy,

I'm trying to extract common logic from some of my rules into scripts so there is a "one stop" location for such logic.  However, as I've mentioned before, scripts has a very "step-child" feel compared to scripts (no import, no overall closure, etc).  I'm working around most of these one way or another.

One that has come up a few times is the need to have a variable that is shared between a rule and a script.  In the particular case I have, I'm creating a timer and need to be able to cancel and/or restart the timer.  The timer needs to be "shared" between a rule that is run sometimes and a script that is run from several other rules.

I cannot isolate the timer to a script because scripts don't seem to run in a closure (or some sort of context) and script variables are not available for subsequent executions of the script (although, if someone knows a way around that, I'd love to solve it that way instead).

I'd be curious if any more advanced rule/script folks have a solution?

Thanks!!

Gerry

Martin Klimke

unread,
Mar 6, 2015, 1:45:04 PM3/6/15
to ope...@googlegroups.com
the workaround I am using is by using an item for this.
regards Martin

Gerry Duprey

unread,
Mar 6, 2015, 2:00:16 PM3/6/15
to ope...@googlegroups.com
I can assign the result of a createTimer call to an Item?  I'm more than a little surprised

What sort of Item type would I use for such a thing?

Thanks,

Gerry

Martin Klimke

unread,
Mar 6, 2015, 2:20:31 PM3/6/15
to ope...@googlegroups.com
I guess you are right, my workaround is not working with a timer.

Bernd Pfrommer

unread,
Mar 7, 2015, 6:50:19 AM3/7/15
to ope...@googlegroups.com
Gerry,

I had a similar issue: a large number of items that needed timers, and I didn't want to have a separate script for each timer:

https://github.com/openhab/openhab/wiki/Taking-Rules-to-New-Heights

For this purpose I added the createTimerWithArgument() method, e.g:

var cookie = "whatever"
var Timer myTimer =  createTimerWithArgument(expireTime, "cookie", [ k | logInfo("subject", "this is my cookie: " + k)])

(Code untested, just as illustration). When you start the timer you can pass an argument (let's say a string) such that when the timer expires it knows what started it, and can use the argument to look up state in a hash table.

Gerry Duprey

unread,
Mar 9, 2015, 2:48:37 PM3/9/15
to ope...@googlegroups.com
That's a good tool to know about!

In this case, it won't accomplish what I'm looking for.

In particular, this script can be called from a few different places.  When the script is called, it starts a timer.  If the script is called again from somewhere else, I want to cancel the timer and restart it.  To do that requires something like a global variable.

If I was able to do it all in a rules file (as it was before), there can be a global variable that holds the timer instance and resets it/re-creates it.  But scripts do not seem to have this sort of context/closure/etc to allow for a global variable.

I'm losing hope this is possible, meaning I have to have duplicated code in all the rules that need this ability ;-(

Gerry
Reply all
Reply to author
Forward
0 new messages