Context Variables (Rule Engine)
Context Variables are variables that only exist during the execution of a rule and can provide additional context about the rule or event which triggered the rule. For example, if a rule was triggered by a device, context variables are set which include details about the device name, attribute, value, and other helpful details of the event that triggered the rule.
Variables are a Premium feature. If you are using the free tier, please let us know if you’d like us to reset your trial, so you can try things out.
In this article:
You may also be interested in the Use Cases and Examples screenshots posted in the community which helps provide some examples in the context of real rules.
Context Variable Structure
There are two primary categories of context variables:
- Event - these are variables that provide context about the event that triggered the rule to start running.
- Metadata - this provides information about the rule itself.
- Rule Name ($context.event.ruleName) - the name of the rule defined by the user.
- Rule ID ($context.event.ruleId) - the rule ID created by the system and can be found in the final part of the URL when editing a rule.
These context variables are set based on the event that triggers the rule. As of this writing, there are three subsections or types of events that can trigger a rule and thus set the associated context variables:
And at this level, there’s also the Event Type context variable ($context.event.type). The Event Type tells you how a rule was triggered and effectively tells which one of the three subsections / types of variables are going to be set.
- location - rule was triggered by a Location event
- device - rule was triggered by a Device event
- variable - rule was triggered by a SharpTools Variable event
- dashboard - rule was run directly from a SharpTools dashboard via Rule Tile
- timer - rule was triggered by a timer (eg. recurring time, sunrise, sunset)
How to Use Context Variables
Context variables can be used in actions and conditions in the rule flow. For example, when using multiple devices as triggers, you can have the rule take different actions if the rule was triggered by a specific triggering device or from a specific location.
Please note that the available dropdown options are filtered based on the rule triggers defined in the Triggers section.
You can also use context variables as command arguments or to set a Custom Variable’s value based on the context variable’s value.
If available, you can tap the </> button to the right of an input field to toggle the input between Manual Input or Variable selection.
If the Manual Input field supports variable insertion, you can also tap the +VARIABLE button to insert variable(s) to the text input field.
Context variables can also be used in Notifications. See the Variables in Notifications community post for more details.