Are Local Variables persistent?

Are Local Variables persistent? That is, if a rule sets VarFooBar to "10" at the end of a bunch of commands, will VarFooBar ALWAYS equal "10" the next time the rule is run, at least at the beginning of the command set, even if hours have passed?

Example: a rule for making a light controlled by a wall switch behave like a 3-way light (low, medium, high cycle). When the switch is turned from OFF to ON, VarFooBar is set to 25 as a low dimming level. That part of the code doesn't run again on subsequent upper-paddle presses. So the next upper-paddle press tests for VarFooBar < 33, and (because that's true) sets VarFooBar to 50 (medium). The next paddle-press runs tests that ultimately set VarFooBar to 100 (high). And so on through the cycle.

This rule currently works, but my paddle presses are all within a few minutes (at most) of each other.

So, can I expect the desired behavior to occur when the light is, for example, on Medium, and several hours later I press the upper-paddle again, which should set the light to High? Or might running other rules (for other devices) in the meantime "boot out" my 3-way rule local variables from Hub memory?

They are indeed. Never tested if they survive a hubitat reboot. I think not though. Perhaps that is what the initial value is for. The value put in when the variable is defined.

1 Like

Yes, local variables are persistent. These are part of the rule's application state, and you can see it on the App Status page (gear icon). As such, if a local variable's value is changed during a rule execution, that value will be written to the hub database when the rule exits, and reloaded again when the rule next runs.

Here's what that underlying state looks like, from the App Status page:

That rule has but a single local variable, "days".

3 Likes

Thanks for the quick response, and good to know!

I can tolerate occasional inconsistent results that might stem from a reboot; that shouldn't happen all that much.

I just ran a test. I set local variable VarFooBar = 55 in my rule, rebooted the hub, and VarFooBar still equaled 55 after the reboot. Good to know!

There shouldn't be any inconsistent results caused by a reboot. A reboot entails an orderly shut down of the running hub. Were the hub to lose power, it is possible that a changed value might not make it to the database.

2 Likes