Global variable vs local variable + virtual device (hub speed)

Is it better to use global variables vs create local+virtual devices in term of hub speed ?

I read here somewhere that Global variables (and also local variables) could decrease the speed of the hub.

I made a rule to measure the avg consumption of my fridge, using global variables

I created minutes ago a new rule with the same result, but using 1 local variable and 1 virtual temperature sensor (decimal).

Am I wasting my time, trying to replace a global variable with a combo local/virtual or does that change could increase the speed of the hub ?

Thank you

Note: I put the 2 rules into 1 to show you my 2 choices.

My understanding is that local variables should be (theoretically) faster than global variables because all is contained in the child app for that rule. For global variables, it has to wake the main/parent Rule Machine app. In real use, I don't think either should cause "slowdowns," as this is likely just milliseconds of difference. Regardless, if you don't have a reason to use the variable outside of that particular rule, there's no reason to use a global variable--besides possible speed differences, you also have to worry less about naming (no name collisions and you can use the most logical name for that rule) and clutter (no need to make it visible everywhere).

That being said, your comparison is different: your first option uses two global variables, whereas your second uses a local variable and a virtual device. If you used a local variable for both, the comparison I mentioned above would apply, but with the virtual device, the rule child app has to call a method on another device. I can only guess here but would say that the performance would likely fall somewhere in between that of two local variables vs. two global variables (it has to call outside the rule--like a global variable--but the device is likely a much smaller driver than the parent Rule Machine app, so may load and run faster...again, all gueses based on what seems likely). Again, in real usage, I can't see this mattering too much but again don't see the need to bring in a virtual device unless you need it somewhere else (e.g., a "regular" app or another rule and don't want to use a global variable).

PS - Small history lesson: variables in in Rule Machine are newer than Rule Machine itself. The only way to do some of these things in the past was to use a virtual device as a sort of pseudo-variable (e.g., you could use a virtual switch as a proxy for a boolean variable--a common technique of the past--or a virtual dimmer as a proxy for certain numeric values). If you're looking at older rules or people who've been here a while and haven't gotten used to variables yet, that may be why you see some of this usage "in the wild" even if not really necessary. As usual with Hubitat, there are lots of ways to do the same thing. :slight_smile:

2 Likes

I like global variable (and connectors) because I can display them in my dashboards and use them in other rules (Like my Day.of.the.month), better than reinvent the wheel everytime. And I use global variables a lot with my plc's (data exchange between sub programs), so it's more than a habit for me.

I need to investigate more on my slowdowns and I disable some apps and see how my system behaves. It's a little bit frustrating because that stops me to play with it :slight_smile:

Anyway, thanks again for the time taken to explain this.