Scripts Expressions(Reference) provide lightweight, event-driven automation for simple calculations and method calls, ideal for scenarios with many triggers but straightforward logic. A Script Expression is single-line statements that:
- Execute on events or schedules without creating full tasks
- Run in multi-threaded environment for optimal performance
- Automatically detect tag changes in the expression
- Support both arithmetic operations and Script Class calls
Use Expressions when you need many simple automations that would otherwise require numerous one-line tasks. The parse of the expression is VB.NET syntax, but it automatically converts and accepts C# syntax also.
In
Overview
Expressions are plain VB.NET statements, such as arithmetic expressions or calls to script.class
methods from the project, executed according to the Trigger or execution settings. IntelliSense only shows tags and application objects, but the standard VB.NET compiler is the one that compiles expressions. Whatever you write in the code editor should also be accepted in expressions.
this page:
Table of Contents maxLevel
Tip | ||
---|---|---|
| ||
2 minLevel 2 indent 10px exclude Steps style none
Creating Script Expressions
- Navigate to Scripts → Expressions
- Select insert row (first blank row)
- Configure:
- Object: Target tag to receive result
- Expression: VB.NET statement or calculation
- Execution: Trigger method
- Domain: Server (default) or Client
Configuration Properties
Property | Description |
---|---|
Object | Tag or object that receives the expression result |
Expression | VB.NET statement (arithmetic, conditional, or method call) |
Domain | Server (global) or Client (local) execution |
Execution | When expression runs (see Execution Modes) |
Trigger | Tag/object that triggers execution (optional) |
DisableCondition | Tag/object that disables execution when true |
Time | Specific time for time-based execution |
Label | User-defined label for organization |
BuildStatus | ? Green = Success, ? Red = Errors |
BuildErrors | Compilation error details |
BuildMessages | Status messages after verification |
Execution Modes
Mode | Description | Use Case |
---|---|---|
OnChange | Runs when any tag in expression changes | Real-time calculations |
TriggerOrTimeInterval | Runs on trigger change OR time interval | Periodic updates with override |
ChangeOrStartup | Runs on tag change OR at startup | Initialize and maintain values |
TriggerOrTimeOfDay | Runs on trigger OR specific time | Scheduled operations with manual trigger |
Expression Syntax
Key Syntax Rules
<ac:structured-macro ac:name="info"> ac:rich-text-body Automatic Conversions: The platform automatically converts C#-style operators to VB.NET:
==
becomes=
- No semicolon needed at the end
- No
@
prefix needed for tags (unlike Script Tasks) </ac:rich-text-body> </ac:structured-macro>
Direct Tag Access
vbnet
' No @ symbol needed in expressions
Tag.Temperature * 1.8 + 32 ' Convert C to F
Tag.Output = Tag.Input1 + Tag.Input2
Calling Script Classes
vbnet
' Call methods from Script Classes
Script.Class.Calculations.GetAverage(Tag.Value1, Tag.Value2)
Script.Class.DataProcessor.Validate(Tag.RawData)
Arithmetic Operations
vbnet
' Standard .NET operators
(Tag.Flow * Tag.Density) / 1000
Math.Round(Tag.Value, 2)
Math.Max(Tag.Limit, Tag.Current)
Conditional Evaluation Methods
IIF Method (Immediate If)
Evaluates all parameters regardless of condition:
vbnet
IIF(Tag.Status = 1, "Running", "Stopped")
' Both "Running" and "Stopped" are evaluated
IIF(Tag.Level > 80, Script.Class.Alerts.High(), Script.Class.Alerts.Normal())
' WARNING: Both High() and Normal() methods execute!
TIF Method (True If) - Recommended
Evaluates only the selected branch:
vbnet
TIF(Tag.Status = 1, "Running", "Stopped")
' Only evaluates the matching result
TIF(Tag.Level > 80, Script.Class.Alerts.High(), Script.Class.Alerts.Normal())
' Only executes High() OR Normal(), not both
<ac:structured-macro ac:name="warning"> ac:rich-text-body Important: Use TIF instead of IIF when calling methods to avoid unnecessary execution. </ac:rich-text-body> </ac:structured-macro>
Threading and Performance
Multi-Threading
- Each expression executes in thread pool
- Hundreds/thousands of expressions don't block each other
- Automatic thread management
- No manual synchronization needed
Compilation
- Automatic compilation before runtime
- Manual verification available via button
- All expressions compile together
- No need to compile after each change
Domain Execution
<ac:structured-macro ac:name="info"> ac:rich-text-body Default Setting: Client-side expressions are disabled by default for web compatibility. Enable in Solution → Settings if needed. </ac:rich-text-body> </ac:structured-macro>
Server Domain (Default)
- Executes on server (TServer.exe)
- Global scope
- Typical use: Calculations, business logic
Client Domain
- Executes on each client
- Local scope
- Only for WPF clients (not web)
- Typical use: UI calculations, local formatting
Examples
Simple Calculations
vbnet
' Temperature conversion
Tag.TempF = Tag.TempC * 1.8 + 32
' Percentage calculation
Tag.Efficiency = (Tag.Actual / Tag.Target) * 100
' Conditional assignment
Tag.Status = TIF(Tag.Value > 100, 2, 1)
Time-Based Operations
vbnet
' Reset counter at midnight
Tag.DailyCount = 0 ' Execution: TriggerOrTimeOfDay, Time: 00:00
' Calculate hourly average
Tag.HourlyAvg = Script.Class.Statistics.GetHourlyAverage()
Complex Logic via Classes
vbnet
' Call complex processing in Script Class
Script.Class.Production.CalculateOEE()
' Conditional processing
TIF(Tag.AlarmActive, Script.Class.Alerts.Send(), 0)
Best Practices
- Use for simple logic - Complex code belongs in Script Tasks or Classes
- Prefer TIF over IIF - Avoid unnecessary method execution
- Group related expressions - Use Label field for organization
- Monitor performance - Many OnChange expressions can impact performance
- Test with Verify - Use button to check syntax before runtime
- Consider execution mode - Choose appropriate trigger method
- Document purpose - Use Label field to explain expression
Troubleshooting
Expression not executing:
- Check BuildStatus for compilation errors
- Verify DisableCondition is not active
- Confirm Domain matches deployment
- Check trigger tag is changing
Performance issues:
- Review OnChange expressions for high-frequency tags
- Consider time-based instead of change-based
- Move complex logic to Script Classes
- Check for IIF with method calls
Compilation errors:
- Click Verify button to see errors
- Check VB.NET syntax (not C#)
- Verify referenced tags exist
- Ensure Script Classes are compiled
Unexpected results:
- Remember IIF evaluates all branches
- Check automatic operator conversion
- Verify tag data types match
- Test with simpler expression first
Claude can make mistakes.
Please double-check responses.
Research
Opus 4.1
Configurating Script Expressions
Besides the table in Script / Expressions, you can use expressions in several other places using the syntax described here to determine a value or configure a condition. The following is a list of recommendations when using expressions:
- Our platform implements automated replacements, such as "==" to "=". The syntax of an expression is close to a C# statement but without a need to add the semicolon “;” at the end. Therefore, VB.NET and C# programmers can use the expression fields seamlessly.
- You do not need to put an "@" before tag names in expressions. Instead, insert the "@" in the code editor to differentiate your Tags from .NET variables. However, expressions do not have local .NET variables, so you use the solution object directly.
- Use the standard operands described in the .NET documentation for arithmetic operands.
Evaluation methods
The platform has two methods for single-line evaluation, they are as follows:
The IIF method, which currently is used only with VB.Net, has three parameters: IIF (A, B, C)
the first being a condition. This method will return B if condition A is true and C if the condition is false. For example:
Code Block |
---|
IIF (tag.A = 1, "True", "False") |
The code above will return the string True
if the value of the tag.A
is 1, or False
if tag.A
has a different value.
In this .NET method, all three parameters are evaluated regardless of the condition. For instance, given the following code:
Code Block |
---|
IIF (tag.A = 1, script.class.client.Func1(), script.class.client.Func2()) |
Func1
and Func2
will always be executed. Only the return value will be different based on the value oftag.A
The TIF method should be used when you want to execute only the function according to the value. For example, given the following code:
Code Block |
---|
TIF (tag.A = 1, script.class.client.Func1(), script.class.client.Func2()) |
Func1()
or Func2()
will be executed, according the value of Tag.A.
The TIF method is defined in the class library that is automatically exposed to expressions that are in the toolkit function library.For more complex calculations, you can call a class that you create on the Classes tab. See Configuring Classes earlier in this chapter.
Adding Script Expressions
Go to Scripts → Expressions.
Select an expression, or select the insert row (first blank row) to create a new expression.
Enter or select information, as needed.
Script Expressions Configuration Properties
Field
Description
Object
Select an existing tag or object.
Expression
Enter the expression. The expression can be a basic mathematical expression, a class, or a conditional expression.
Domain
Select where the expression executes:
- Client: expression executes on each client system. These are expressions that apply locally (on the user's computer). For example, report generation.
Server: expression executes on the server system. These are expressions that apply across the application, that is, globally.
Info |
---|
The creation of Expressions on Domain Client is restrict by default, in order to ensure compatibility with Web (HTML5) Clients, which don't execute the expressions configured here. You can modify that setup on Solution → Settings. |
Execution
Select when the expression executes:
OnChange: the expression executes when the value of any tag in the expression changes.
TriggerOrTimeInterval: the expression executes when the trigger event occurs or when the interval set in the period elapses.
ChangeOrStartup: the expression executes when the value of any tag in the expression changes or at startup.
TriggerOrTimeOfDay: The expression executes when a trigger event occurs or on a specific time of day
Trigger
Enter or select the tag or object that triggers the expression execution. The expression executes when the value of the object changes.
DisableCondition
Enter or select the tag or object that disables the expression execution.
Time
Specify the time when the expression runs.
Label
Set a label to the specified class.
BuildMessages
Return the message status after the expression runs
BuildStatus
Read-only. Set after you click Verify.
Green check mark: expression runs without errors.
Red X: expression has errors.
BuildErrors
Read-only. Displays any errors encountered during the last build.
Managing External References
If you need references to your own assemblies, you can use Scripts References (Reference).
As a fully compliant .NET application, you can find free source code to use, including .NET components, products, and libraries. You can also use your own libraries.In this section...
Page Tree | ||||
---|---|---|---|---|
|