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: