Variables, not following how to use either local or global

Okay, so, here I go. I have spent the last week plus looking through the forum, Hubitat documents, web search, etc. I like to think that I am a fairly intelligent person but this has me puzzled as to the use and how to use. I was hoping to get @bcopeland, @bobbyD, @bravenel, or anyone in the community to either explain and kind of mentor me with the use and difference between local and global. I am sorry but I am very lost with this.

1 Like

Global variables can be used in any rule -- they are defined in the parent app, Rule Machine. Local variables can only be used in the rule within which they are defined.

Otherwise both types are pretty much the same. Both can have connectors defined. Both can be set to different values by a rule, or tested for their value, etc.

That helps with the difference but as to how to use them / use case. Are there any examples of variables that you can point me to? Just so lost, I think I am getting to the point that I need to understand these to truly get what I want done . Example is I have a rule for turning lights on in the eve which is spread out over time so we really don't notice the lights abruptly turning on. I am looking to have some or all of the devices that are already on to stay on and not drop in dim level and start working up to the final level and if turned off it will go to the level for where the sequence is at in the defined rule. I am assuming this is with a variable but I may be wrong.

Crawl, walk, run...

You just have to start out with something simple, and work your way up....

Here's an example.

First I created a global variable named vMailbox_Time of the type "time". You create global variables on the page where you create a rule. You must create the variable separately and before you create your rule.
Then in the rule I assign a time to the variable using set variable command.
If you want to display a variable in a message you enclose the variable with % characters (see below).

So, if I am following, I would use set boolean to false if I wanted a device to look as though it is off when it is really on for a certain action to run in a rule and have it disregarded or as in @didymus reply with the time variable it will read the current time. Am I on the correct path at least?

Isn’t there also a speed advantage in using local variables? I had read some statements here about them being “atomic”. In the programming I did over 15 years ago, I had never heard that term, but I think it prevents a local variable from being read before it is finished being written to? This would be good info to have in the documentation.

I do something similar in my kitchen without needing variables. This is part of the rule that triggers when mode changes to Evening, which happens just before sunset. "Kitchen" is a group of Hue bulbs that are grouped on the Hue bridge. Kitchen can be dimmed to various levels depending on the light level.

This rule treats Kitchen as both a switch (on/off) and a dimmer. If Kitchen is "off" then it's turned on by dimming to level zero. Then it fades Kitchen up to 100%, starting from either 0 or whatever level it was at when the rule triggered. The Delay lets the fade up finish. Then the color temperature is changed from daylight white to warm white.

And here's a rule that uses a local variable. It's triggered by motion by the front door and turns on a light at a level based on the light level in the house. "Weather - Lux Percent" is a virtual dimmer that is set in another rule based on lux. In the evening, the light is always dimmed to 100%. At night, it's always 35%. During the day, it's set to the level determined by the variable.

This is what the "Color temperature per mode" looks like when it's entered in Rule Machine so you can see how setLuxPercent is used.

1 Like

Definitely at the crawling stage. Any recommendations for reading/learning outside of the HE forums that will help me along?

I use variables if i need to do any sort of math within my rules.
For instance i have a transfer fan between my master bedroom and living room where my pellet stove and heat pump is, if the temperature of my bedroom is higher or lower than my living room, then i increase the transfer fan speed.

Looks like this

BedrmTempVar = bedroom temp
LivingRmTempVar = living room temp

TempDiffVar = LivingRmTempVar - BedrmTempVar

I then take the absolute value of my result and use that in my calculations for my fan speeds.

If TempDiffVar is = 1 then set fan speed to 40%
If TempDiffVar is = 2 then set fan speed to 60%
If TempDiffVar is >= 3 then set fan speed to 80%

If you only need to use the variable in one place, i.e one rule, then use a local variable. If you need to see the variable on a graphic or to use among multiple rules, then use a global variable

1 Like