Variables (local/hub/global): Could someone please explain?

Hey there,

even if I'm not new to HE, I've still a lot to learn. One thing I never really got my head around was variables. The biggest reason right now is, that I have no real idea, what they are and how to use them. Could someone please explain them to me. I've used the search function and tried to find something in the documentation, but I just got more confused. Please make it for dumdums. Examples for better understanding are appreciated. :smiley:

Here is my understanding:

Local Variable = variable that only the one rule can use (the one where it was created)
Global Variable = variable that all Rule Machine Legacy rules can use (now retired I think)
Connector Variable = variable that could be created in Rule Machine Legacy that created a device. The device had the same state as the variable. (Ex.: yes; present; open; etc. Vs no; absent; closed, etc.) (Also retired for Rule Machine 5.0 and up)
Hub Variable = variable that (I think) all apps on the hub can use

1 Like

Ok, thank you, but this doesn't solve my underlying issue. I have no idea, what all the values stand for, and what they are telling me.

I just hit some random stuff to get a matrix for all the values. Could you explain it on those values. As I said, I'm really confused.

Unbenannt

Are you asking what these specific variables are for? Or are you asking about the difference between variable types? Or are you asking more generally what might one use a variable for?

All of it. I would love not to bother one of you guys, but as I've written, nothing right now to find at the documentation, giving me any clue and going through all the threads made me more confused...

Iā€™m not sure if this is what you are asking, but a variable is basically a place holder that holds whatever value your assign to it.

There are different variable types based on the kind of data the variable holds, such as string, number, boolean (true or false), decimal, date/time. When you first create the variable, you decide what type based on what type of data it will be holding.

Besides type, there is also the concept of scope, which determines where the variable will be available. Examples of scope with Hubitat is local and global.

To elaborate on what @didymus wrote, variables are a concept that that come to us from algebra by way of traditional computer programming. Here's a quick video on the basic concept:

In your example a string is a type of variable that generally holds alphanumeric or text information - letters, numbers, and symbols. "Today is Sunday" or "123XYZ" or "Jane Doe" are all strings. A number is an integer value, and a decimal is a value with a decimal point. A boolean variable can be only two values, true or false. And last but not least a datetime variable contains a specific date and time.

You can use variables in rules and other applications to replace specific values. For instance, I use the following variables;

Screen Shot 2022-02-12 at 1.17.03 PM

I have a rule which repeatedly pings my phone but I want to be able to easily change the number of times it retries the ping and the delay between pings. So I have created variables called "ping_retries" and "ping_retry_delay" to represent these two numbers. In my rule I use the variable instead of the actual number:

If you look at the repeat statement itself, you'll see where I use the variable instead of an actual value;

And then I have the variables in my dashboard so I can change them without actually editing the rule:

Screen Shot 2022-02-12 at 1.21.08 PM

Hope this helps a bit

That helped! Thank you. I'll still have to learn something, but at least I have a basic idea now. :smiley:

1 Like