Rule Machine Global Variables


#1

Rule Machine Global Variables [Beta]

In response to community requests, we have added Global Variables to Rule Machine. This new feature is a beta release. There is preliminary functionality within a framework. We're looking for feedback as to additional functionality that is needed.

Creating Global Variables

Before you can use a Global Variable you must first create it in the Rule Machine app. There are four types: Number, Decimal, String and Boolean. Numbers are integer values, while Decimal has decimal point and decimal fraction. Strings are text. Boolean are true or false.

Setting the value of a Global Variable

You can set the value of a Global Variable in Rule actions.

For Numbers and Decimals, there are four options: set the value, add to the value (can be positive or negative, so subtraction is possible), set the value from a sensor current value, and set the value from another variable. An offset can be added to a sensor value or the value of another variable. For Strings the value can be set. When setting a String value, %device%, %value%, %time%, and %date% are available for most recent event for that rule, and a Global Variable value can be included with the name of the variable inside braces, e.g. {variableName}. For Boolean, you can set it to true or false.

Using Global Variables as Rule Conditions or Trigger Events

After you have defined a Global Variable in Rule Machine you can use it as a Condition or Trigger Event in a Rule, Trigger, or Triggered Rule. For Numbers and Decimals, six comparison operators are available, and for Strings and Boolean equal and not equal comparisons are available. When a Global Variable changes value, the Rule with the changed variable in a Condition is evaluated, or the Trigger Event is fired. With conditions for sensors, the sensor value can be compared to a variable value, with optional offset. With conditions for variables, the variable value can be compared to another variable value, with optional offset.

Using Global Variables in Notifications

You can put the value of a Global Variable in the message sent for a notification. Simply put the name of the Global Variable in curly braces, as in {My-Variable}, in the string for the message.

WARNING about Race Conditions

Since multiple rules can reference and/or set Global Variables, it is quite possible for there to be race conditions caused by multiple simultaneous interactions with a Global Variable. For example, if two different rules, or even two different instances of the same rule, set a Global Variable at the same time, the resulting value may be one or the other value set, with no assurance as to which value it is. Similarly, a rule that references a Global Variable will get the most recent value, which may change an instant later. Think through such interactions to avoid problems and surprises.

Screen Shots

Below are a few screen shots to show how this all hangs together. The first one is from Rule Machine after creating two Global Variables:

Now, we have a simple trigger that will put values in those variables:

Notice that Beta is set to the current temperature of a sensor. Also, notice in the string to set Charlie, how it references Beta with { x }, to put that value in the string. Now, we can see the Global Variable values back in Rule Machine after that trigger fires. You can just refresh the page to see the updated variables:


Hub Update 2.0.5
#2

Love this! Can we add Global Variables to the Button App? The button App is the closest thing to a CASE STATEMENT on this platform. I'd like to make a mood variable to run along side mode. I want to try a global string for mood that I can set via a button controller like the MI Cube.


#3

These global variables work only within Rule Machine. They aren't global in the sense of being system-wide.

We have looked at system-wide variables, such as your Mood, and most of the platform elements exist to support them. These will be forthcoming in a future release. But, getting apps to make use of them would be a whole other rather large undertaking.


#4

This is going to be fun :grin:


#5

There is a bug in 2.0.5.112 that prevents the setting of a global variable from a sensor device. This will be fixed in a hot fix today.


#6

This is awesome functionality, I immediately found a use and have implemented it. While my clothes dryer is running, I want to know if the temperature decreases (ran out of propane). Now I have a trigger to set a Prior Temperature variable 2 minutes after the Dryer sensor reports. In a rule I compare the two. Not sure if this would have been possible previously


#7

@bravenel hey Bruce. Just wondering the best way to capture continual changes in illuminance to a global variable but only when a specific switch is off? The following didn’t work for me (my thoughts where to compare device illuminance to itself or to the stored variable name and when not equal to save that to the variable)


(“Lux. Master Bedroom” is the global variable name as well as the rule name)


#8

Sorted. In case anyone is interested, this is what I did:

Create a new global variable with type set to decimal.

Create a new triggered rule with the following:


This rule is called “Lux. Lounge” and so is the rule.
This now saves the illuminance value of the motion sensor into a global variable but only when the lights in the room are off. Just like how I used to have it on WebCoRE in the ST days :blush: I’m now referencing that variable to determine if Lights should come on or not in other rules


#9

Hello, I had Hubitat now for just over a week so very new to it yet and have a lot to learn. These forums have been a great help and seems to have a great core of people interested in helping each other! With that I’ve been working with rules machine and reading quite a bit on best practices and setup for efficiency. I wavered back and forth with having a simple set of Modes (Night, Morning, Afternoon, Evening) to incorporating presence into the string (Night, Night-Away, Morning, Morning-Away, Afternoon, Afternoon-Away, Evening, Evening-Away). This seemed to be a benefit for use in Rules Machine and its restriction functionality. As I understand it the restriction functionality will cut down overhead when used properly as it terminates rule processing / evaluation immediately upon a restriction being met; prior to getting into the actual workload of pushing device and any other environment information into the database and evaluating. Due the impact restrictions have on rules machine flow it seemed that adding the -Away modes may have been beneficial. There are Pro’s and Con’s with everything and the use of modes seems to be a highly discussed variable for both Smartthings and Hubitat. Global Variable are a great addition and will save on virtual device creation to get a global variable like avenue to use for rules, reference, etc… In reading the Global Variable documentation compiled by bravenel we will not be able to pick up dynamic changes to the variable value on the fly; the process using the variable will pick up the new value on it’s next execution or attribute load into a process. My question is are there plans to add global variable evaluation into Rule Machines Restrictions? Similar to the mode restriction where you can select a mode\modes for a rule to progress, possibly have the ability to evaluate a Global Variable in an expression for validity like {My-bHome) = True; where bHome is boolean and is true for home false for away. Maybe this option would only be for very simple evaluation of global variables. Hopefully this makes sense and I didn’t miss discussion on it elsewhere. Again, still very new to this and learning…hope I’m making sense!


#10

This "savings" is not material. The largest expense of any app is the loading of the app itself, not its execution. So the difference between evaluating a restriction and doing that plus evaluating a rule is small. Therefore, using restrictions to be more efficient is not what I'd call a valid strategy.

Not sure what you're thinking of with this. If a rule uses a global variable as a condition, then any change to the value of that global variable (caused by some other rule) will trigger the evaluation of the rule. Likewise, a global variable as a trigger event will cause that trigger to be fired when the global variable value changes to the desired value. This is all happening dynamically. Every event that a rule is connected to causes the rule to execute, sometimes only to discover that it was paused or restricted, or that the value wasn't the right value, or that the rule's truth didn't change -- in each case no action is taken.

No, there is no plan to add global variables to restrictions. Restrictions are not intended to be some second rule within a rule. They serve a very limited purpose, and I see no virtue in expanding their scope.


#11

Thank you for the quick response bravenel! I must have misunderstood what I had read in the best practices topic for rules machine and restrictions. Seems I need to do further experimenting with a rules and triggers to better understand just what causes them to load, evaluate, and execute. I appreciate the feedback and will get after some testing and logging.


#12

@bravenel what are your thoughts on adding the ability to use a GV (number) as an input for a "delay"?

Example:

True Action: Delay by {variable} minutes (or seconds).

Or for that matter be able to use a GV as a number input throughout RM?

This may be a little too major of a code change, but I can think of a couple ways I could use it in my automations.


#13

It's not an unreasonable request. That's one of the reasons this is Beta, to get this sort of feedback.

I will look into it. One of the issues it presents is the difference between a number and a string. It is possible to input a number as a string, but it does have impact as the string then has to be converted to a number in certain contexts. Presumably, the way we'd do this is to just allow {variable} as the input for a number. It's not a small change.


#14

A minor but of feedback, I cannot set a GV to a negative number in the RM. Would be a nice option, but real easy to RM around.


#15

What context was this?


#16

APPS/RM/Create a rule/define a rule/rule name/select a condition/between two dates/hurricane start and end/done/actions for true/set GV/pick GV/number/etc/"-10"/ must be greater than 0/, Just feedback, and curiosity if the numeric GV's are strictly >0?


#17

If you use a "decimal" variable type instead of "number" it will allow negative values


#18

There is the workaround, and I was just about to mention the same issue in the context of the set GV "add number" has no "subtract number" equivalent!


#19

The key benefit to the "number" variable is that if you're using it in a "notification" it doesn't read out "dot-0". Otherwise "decimal" has definite advantages, I use decimal variables to countdown (using add number -xx) but I use number variables for notifications .


#20

This will be fixed.