Comparisons: Operators || Functions
Boolean. Noun, always capitalized. A binary variable having two possible values, either true or false. Named for George Boole.
You will often want to choose between actions based on the current situation. For example, if the room is too cold, increase the heat, otherwise reduce it. If the system is down for maintenance, disable the alarms, otherwise leave them enabled.
Typically, test expressions use conditional operators such as:
> | Greater than |
>= | Greater than or equal to |
< | Less than |
<= | Less than or equal to |
== | Equal to |
!= | Not equal to |
Multiple Comparisons | |
&& | And |
|| | Or |
The function to choose between two options based on a comparison is IfElse and there are two ways to write it:
IfElse(Comparison-Expression, Expression if TRUE, Expression if FALSE)
Comparison-Expression ? Expression if TRUE : Expression if FALSE
Functionally, these are identical. It's your choice as to which form you prefer to use, but in general the IfElse form is recommended when there is a choice between running one expression and another:
IfElse([Pressure] > \Setpoint), {Do this... }, {Otherwise do this})
or returning the appropriate message:
Concat("The pressure is ", [Pressure] > \Setpoint ? "high" : "low")
Any expression that will return either a 0 (FALSE) or 1 (TRUE), can be considered to be a test expression. (Any numeric other than zero is considered TRUE, but 1 is typically used by convention).
If your goal is a form of trigger, there's no need to write IfElse( Comparison-Expression, TRUE, FALSE). Simply write Comparison-Expression. The result of the comparison is returned as TRUE or FALSE, therefore you don't need to write extra code to return those values.
Situational Message or Instructions
You can use the MultiText widget to display a message that varies according to a changing value. But, what if it also makes a difference whether equipment is running or an alarm is active? Further, what if you want the text of the message to include data calculated from two or more other tags? For this, you're going to need an expression.
- Open the Idea Studio.
- Working on the Overview or Station Status page, draw text (plain text from the ribbon, not a text widget) on the page.
- Open the Edit Text dialog and change the data source to Expression.
- Create an expression so that when the Level value is more than 50%, the message is "Level exceeds safe amount". When the Level is less than 50% the message should be "Safe level".
- [Optional] Adjust the message to tell your operators the percentage by which the level exceeds the safe amount.
If you knew the math behind the system, you could create a message that tells operators how to adjust the pump or valve to balance the flow rate, or predict when the tank will empty or fill.
Keep the Else's under control !
Writing long "If Else - Else - Else ... " expressions is not efficient and can be avoided. To prevent excessively nested expressions, the property MaxNestedExpressionDepth limits you to 200 function calls (including IfElse) within a single expression. There is no need to test this limit, regardless of the complexity of your application.
Alternatives to nested IfElse structures:
A common task is to build a string based on the current value of a tag (or several tags). For this example, suppose that you need to build a message or address that varies with a tag's current state. A long and inefficient method might be to write:
[SomeTagName] == 0 ? "Message (or) Address 0" : [SomeTagName] == 1 ? "Message (or) Address 1" : [SomeTagName] == 2 ? "Message (or) Address 2" : etc. for 100 or more messages...
There are alternatives:
Option 1
A much easier method is to use a CASE statement in steady state. All statements will execute, but only the result of the statement matching the requested index will be returned.
X = Case([SomeTagName],
{ 0 } "Message (or) Address 0", { 1 } "Message (or) Address 1", { 2 } "Message (or) Address 2", { etc });
Option 2
Another option is somewhat more complicated to create, but has the benefit that the messages are stored in application properties instead of code and therefore can be added to or edited by anyone with the Configuration privilege. It is not necessary to write more code when messages change or new messages are created.
1. Create a set of application properties for each situation:
Situation0 = Message or Address 0 Situation1 = Message or Address 1 Situation2 = Message or Address 2 (etc.)
2. Write an expression that uses the Variable function to concatenate the tag value onto the prefix "Situation" to return the appropriate property value.
Variable(Concat("Situation",[<SomeTagName1>]))
No comparisons or If-Else statements are required. This single line of code can return any message that you have created. For more complicated situations, use Concat to build the appropriate property name or combine several values into one message or address.