What are variables used for?

I see that you can create "Hub Variables", but what are they used for? What gives them values and what takes their value? The documentation regarding hub variables was pretty lousy. Maybe I'm missing some other document page. Help?

Hub variables are global in the sense you can store values that can be used across multiple rules and i believe apps.

There are also local variables that can be used only in an individual rule.

You can set the value in rules or connect them to dashboard inputs for example.

Their value can be set in a rule action. They can be used to trigger a rule or in a required expression. They can be evaluated in conditional rule actions.

An example, I have one "Dark in Living Area" that evaluates/combines several Illuminance sensors.

I'm assuming you were looking at this page:

https://docs.hubitat.com/index.php?title=Hub_Variables

That documents how to use the Settings > Hub Variables page itself.

With regards to variables themselves, there are pretty much arbitrarily many possible uses for them, so that would be difficult to document. As noted above, they are just ways to store a value (a value of a specific time, like string/text, number/integer, decimal, boolean/true-or-false, etc.). This is a concept borrowed from most programming languages. You do not need to use hub variables to use Hubitat, but they can be helpful for sharing information among apps or rules.

Some people used to use virtual devices to store "global" information like this to share among multiple apps; this is a lighter-weight, built-in, alternative. At the moment, you also have the ability to create "connectors" to turn variables into devices, though the ability to create these will probably go away as more and more apps become able to use variables directly (this is a relatively new feature). Previously, these were a Rule-Machine-specific feature (called global variables), so a connector was necessary to get them out of that app and into the hub in general in the first place, which is no longer the case. Rules are, perhaps, still the most common area where you might use variables given that RM lets you do pretty much anything, but, as noted above, an increasing variety of other apps are also able to use them now.

More of a history lesson, I guess. :smiley: But how you use them, or if you do at all, is totally up to you.

2 Likes

To all of this I'll add. If you create a connector device for a hub variable, the value of the variable can be set/manipulated by any app that uses that device, and not just rule machine.