Rules using Lua scripts
Expand all | Collapse all
The function in a rule's Lua script described in the Lua language is called whenever the value of any tag used in the function is changed. The function is first called when all values of tags used in the function are received.
You can change functions in Lua scripts when performing the following actions:
- Creating a Process Control rule with a Lua script
To create a Lua rule script:
- Select the Process control tab.
- If the list of Process Control rules contains groups, select the group to which you need to move the new rule. You can select the group itself or one of the existing rules from the group.
- Click the Add Lua script button.
The Lua script editor appears in the lower part of the tab.
- In the Lua script editor above the script entry field, select the Rule script option.
- Perform the following actions:
- Enter the rule name and description.
- Enter the code of the script in the Lua language.
The script entry field displays the template for a Lua language function with brief comments. Click the help button above the script entry field to open the window containing detailed comments for creating a script.
- Select or configure the type of registered event.
- Click OK.
The new Lua script appears in the list.
- To apply the changes, apply the security policy.
- Creating or editing a global Lua script
To edit a global Lua script:
- Select the Process control tab.
- Open the Lua script editor. To do so, you can use one of the following methods:
- If the list of Process Control rules does not contain rules containing Lua scripts, create a new rule with a Lua script. To do so:
- If the list of Process Control rules contains groups, select the group in which the rule containing a Lua script will be created. You can select the group itself or one of the existing rules from the group.
- Click the Add Lua script button.
- If the list of Process Control rules contains at least one rule that has a Lua script:
- Select any rule containing the Lua script.
- Click the Edit button.
- In the Lua script editor above the script entry field, select the Global script option.
- In the script entry field, enter the code of the script in the Lua language.
To open the window containing comments on creating a global script, click the help button above the script entry field.
- Click OK.
- To apply the changes, apply the security policy.
The defined global variables and functions of a global Lua script can be used when creating or editing rules containing Lua scripts.
- Editing a Lua script in a Process Control rule
To edit a Lua rule script:
- Select the Process control tab.
- In the list of Process Control rules, select the rule with the Lua script that you want to edit.
- Click the Edit button.
The Lua script editor appears in the lower part of the tab.
- In the Lua script editor above the script entry field, select the Rule script option.
- Perform the following actions:
- Enter the rule name and description.
- Enter the code of the script in the Lua language.
Click the help button above the script entry field to open the window containing detailed comments for creating a script.
- Select or configure the type of registered event.
- Click OK.
- To apply the changes, apply the security policy.
The application Server will begin to register events based on the changes made to the rule.
Tags are described in the function code by an expression in the following format:
X = tag'tag_name'[.R/.W/.RW]
,
where the following modifier values are used: .R
– tag is intercepted when reading the tag from the device, .W
– tag is intercepted when writing the tag to the device, .RW
– any most recent value of the tag. You are not required to specify a modifier. If a modifier is not specified, any most recent tag value is used.
When creating a rule using a Lua script, you can use additional variables with a user-defined name and value.
The following function is used to add a variable:
_AddEventParam('parameter_name', parameter_value)
You can use the added variable in the settings of custom event types. The added variable may be used in the format $extra.<parameter_name>.
You can use functions for adding a record to the process log in which the Lua script is executed (this is normally a process whose name starts with the word Filter
). A record defined by an argument of the function (variable or constant) is added to the log:
_WriteCriticalLog(function_argument)
creates a log record with the Critical level._WriteErrorLog(function_argument)
creates a log record with the Error level._WriteWarningLog(function_argument)
creates a log record with the Warning level._WriteInfoLog(function_argument)
creates a log record with the Informational level._WriteDebugLog(function_argument)
creates a log record with the Debug level.print(function_argument1, function_argument2,…)
creates a log record with the Debug level that may contain multiple arguments of the function. Variables or constants defined by function arguments are separated by a tab character in a log record.
Records are not created in the log if the level of the record is lower than the log level set for the process in the Settings of Server and sensors window.
Page top