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:

4 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.

is there a documentation to learn how to use the Variables?
iam new to Hubitat and saw Variable boolean,Variable string,Variable number in Rule Machine ,and also saw this Variable in dashboard in a Tile,but have no idea how to use them,any advice will be appreciated.
Thanks in advanced

1 Like

I'm not aware of any specific documentation, but in summary, variables are just a way to store a value. You have to provide a "type" for that variable, like number (integer), decimal, or Boolean (true/false). You can make a local variable in a Rule (local to that specific Rule), or you can make a hub variable that is usable in any Rule and an increasing variety of apps that can use them. But you certainly don't need to use them, and their uses are so varied that it's hard to give guidance without knowing something specific that you want to do and are wondering if a variable could help.

I would probably not recommend using connectors at this point. They are being phased out in favor of direct access to variables now that hub variables exist (previously, the closest thing was global variables in Rule Machine, which have also been phased out in favor of these--though existing instances of either will continue to work). However, many apps do not yet support variables, so in the meantime you may find a connector helpful. But the app still has to support that (some connector types are very general, like the Switch connector you can create for a Boolean variable--but your experience per app may vary).

3 Likes

thanks you for your replay and time spent.

i have a sensor on my water tank which read in Volts, every 1 volt = 60 Liters of water.
I used" sensor" from Pick A Device- "attribute"from Pick A template-"voltage" from Pick an Attribute on dashboard to add a tile and it works.
some people recommend me to use the Variables,which I have no idea about it.
iam willing to make this calculation above appear automaticly on my dashboard .
do you think Variables could do it?

Thanks in Advanced

I suppose you could store the value from this device attribute in a variable, then display that variable on a dashboard (which, for the time being, you'll also need a connector to do), but if the device has the data you need in an attribute already, I don't think I see any advantage to that.

the device only gives me voltage readings ,the measurment (1 volt=60 Liters) is my measurment in the tank , as shown in photo on the dashbored i wrote a text above the water tank tile to make it easior (for my family) to make calculations,
but it would be perfect if this calculations done automaticly and appear even on another tile.

Oh yeah! You could use a variable for that. If you're on firmware 2.2.9, I'd use a hub variable, and dor the moment you'll need to create a conector to get it on Dashboard. You could then use a rule triggered on voltage change for that device, then set the variable to whatever using the necessary math (the Rule UI provided means for you to do this). That's the general idea--I can go into more specifics if you aren't sure how to start.

1 Like

much appreciated.
frankly iam new to Hubitat ,almost 3 weeks now. so specific help and step by step will be much appreciated. whenever you have the time to write me the steps ,please do it.
thanks a lot :slightly_smiling_face:

Sure! So, here's what I'd do:

  1. Go to Settings > Hub Variables, then create a new variable by clicking New Variable.
    i) For Name the hub variable, type something that makes sense to you; I might suggest something like "waterTankLiters" (without the quotes).
    ii) for Variable type..., choose Decimal (looks like you need this since you have fractional volt reportings--not sure if that is standard or if you'll have problems with this later, but that's a different issue). You can put anything you want for the initial value (it will be set by your Rule when it's first run), but 0 might make the most sesnse.
  2. Verify that you have Rule Machine installed (under Apps on the left-side menu, where you should then see "Rule Machine" in the list on the right; if you see "Rule Machine Legacy" installed but not "Rule Machine"--or if you have neither--then you don't). If not, click Add Built-In App and choose Rule Machine.
  3. If you are not already, there go to Apps and open Rule Machine.
  4. Click Create New Rule, and name it whatever you want (I prefix all my app names to make logs clearer, so I'd do something like "Rule 5.1 - Water Tank Volts to Liters," but anything you want is fine).
  5. Click Select Trigger Events.
  6. On the screen that appears, under Select capability for new Trigger Event, chose Custom attribute (necessary since "Voltage" isn't an option), then under Select attribute, choose voltage. For Comparison, choose **changed**.
  7. Click Done with Trigger Events.
  8. Click Select Actions to Run.
  9. On the screen that appears, under Select Action Type to add, choose Set Variable, Mode or File, Run Custom Action, then choose Set Variable from the second menu that will appear.
  10. For Select Variable to Set, choose waterTankLiters (or whatever you named your variable).
  11. For Select operation, choose device attribute, then choose your device again and the voltage attribute again.
  12. Click "Done with this action."
  13. Now, it sounds like you need to multiply this value by 60 to make it report the number of liters. To that, again use the choose Set Variable, Mode or File, Run Custom Action > Set Variable action. Then:
    i) Again, for Select Variable to Set, choose waterTankLiters (or whatever you named your variable)
    ii) For Select operation, choose variable math
    iii) For first variable, again choose waterTankLiters (or whatever you named your variable)
    iv) For operator, choose * (the asterisk, for multiplication).
    v) For Second variable, choose (constant), then type 60 for the Second constant.
  14. Click Done with this action, then click Done With Actions, and you should be returned to the "main" screen for this rule. Verify that your triggers and actions look like you expect them to look.
  15. Hit Done to install or update the rule, upon which you will be returned to the apps list. (Alternatively, you can hit "Install Rule" or, after installation, "Update Rule," to do the same while remaining in the Rule--both the same as "Done" otherwise.)

As an example of what your actions look like, here is what mine does (you may see device values in parentheses after; I turned that feature off for this screenshot; I also didn't have any "voltage" devices and used "energy" instead for demonstration, so don't do exactly what I did here):

Screenshot: Rule 5.1 configured as described above

If it doesn't work as you expect, turning on trigger and action logging for your rules may help you figure out out, or someone here can probably help if you share more about the device and Rule.

PS - if you want an added adventure, you could store the voltage in a local variable first, then set the hub variable to that variable times 60 (instead of temporarily storing the "wrong" value in the global variable--which should happen so fast that you're unlikely to notice, but if you do want to avoid that, here's one option).

PPS - To get this on Dashboard, you'll need a connector (at least until a future hub platform version where they add variables to Dashboard directly). You can do that from Settings > Hub Variables. I'd probably use the Variable type of connector (the most versatile; not sure any others can hold the range of values you might get for this, at least in a sensible way). To put this on your Dashboard, use the connector device that this created, using either the "Variable" template (sounds right, but also lets you edit the value on the Dashboard, which may not be what you want) or the "Attribute" template and the "variable "attribute chosen as the one you want (presents a read-only view).

6 Likes

wow, what a great profashional lead, thank you for your time spent and effort done to write me this clear instructions.much appreciated. :grin:
i will follow your instruction step by step and let you know the results as soon as i back home .
:+1: :+1: :+1: :+1:

1 Like

As you gain more experience, pay it forward and help other users with their issues if the are facing a problem you know how to solve. Robert Morris (@bertabcd1234) just happens to be one of the best at this, always providing excellent help with patience and clarity.

1 Like

wow,wow,wow, what a great ,clear lead. you are a true ambassador :grinning_face_with_smiling_eyes: :+1:
i followed your steps and done ,bingo.
thanks a lot ,you saved me a lot of searching and time by writing this leading.
pictures attached





2 Likes

yes he is ,Robert is a super helper , i do appreciat his time and efforts to write all this leading. :+1: :+1: :+1:

1 Like

Btw, what kind of tank sensor do you have ? Always interested to measure other things in my own house (and I could measure the level of water inside a tank too, with floaters or ultrasonic).

Thank you in advance.

1 Like

please see the link.
its a presure sensor,from Ali express.
https://ar.aliexpress.com/item/4000415423859.html?aff_fcid=c26069fe2b3e416e971477adecbf75d8-1634709686406-05694-_9hPH23&aff_fsk=_9hPH23&aff_platform=shareComponent-detail&sk=_9hPH23&aff_trace_key=c26069fe2b3e416e971477adecbf75d8-1634709686406-05694-_9hPH23&terminal_id=ed3c74db04c84ffc9c0e72eb6e0cb4f7

please watch this video which explain everything about this sensor and did a great demonstration .
the video made by "WE SMART" channel on YOUTUBE.

2 Likes

I purchased the same water level sensor (0-10V) and hooked to a modbus PLC I have.
Received the unit today: it behaves as expected and the voltage, when my drain flushes, is accurate.
I just need to integrate with Hubitat the usual way for me (plc => node-red => HE)
Will replace my floaters soon (and keep one above everything as a security measure)

1 Like