Calculations
You can define Calculation Tags to perform automatic processing of math formulas with any number of local or remote Tags as a data source. You can even use values directly from OPC Servers with the DirectOPC interface, and these OPC Items can also come from the local service or a remote service.View the following video presentation on Calculations:
If you enable Security on a remote Service to disable all or selected Tags you will need to define the OAS Service User Name and Password under Configure Options, but also recommend to then enable Security to limit access of Read Tags under Configure-Security.
The following is a quick guide as an example to create a Calculation Tag:
Step 1
Using Configure-Tags create a new Tag with the name Total.
Select the local Service or the remote Service with the Ramp, Sine, and Random Tags.
Step 2
Select the new Total Tag and set the Data Source as Calculation.
Step 3
Use the Edit Calculation button at the right to show the Calculation editor.
Select the Insert Tag button and select Ramp.Value.
Add a + symbol after [Ramp.Value] and insert the Tag Sine.Value.
Add a + symbol after [Sine.Value] and insert the Tag Random.Value.
The equation should now represent a total of all 3 Tags as the following:
[Ramp.Value]+[Sine.Value]+[Random.Value]
You can select OK and Apply Changes to then see the Total Tag value update to the total of all 3 Tags.
Refer to the Configure-Tags-Calculations section in the Open Automation Software Help file for descriptions of each Function for the Calculation engine.
Note: If any tag data source in a calculation is bad the resultant data quality for the calculation tag will also be bad unless you use the property Source When Bad.
Function Reference
Following is a list of functions and their expected parameters.
ABS
Summary:
Returns absolute value.
Parameters:
Value to convert to absolute value
Internal code:
return Math.Abs(p[0].GetValueAsDouble());
ASCTOINT
Summary:
Converts ascii character to integer value.
Parameters:
Ascii character value to convert
Internal code:
Int32 testInteger;
string stringValue = p[0].GetValueAsString();
if (stringValue == null)
return 0;
if (stringValue.Length < 1)
return 0;
Char[] testChars;
testChars = stringValue.ToCharArray();
byte[] testBytes;
testBytes = Encoding.ASCII.GetBytes(testChars);
testInteger = testBytes[0];
return testInteger;
ASIN
Summary:
Returns the angle whose sine is the specified number.
Parameters:
A number representing a sine, where d must be greater than or equal to -1, but less than or equal to 1.
Internal code:
return Math.Asin(p[0].GetValueAsDouble());
ATAN
Summary:
Returns the angle whose tangent is the specified number.
Parameters:
A number representing a tangent.
Internal code:
return Math.Atan(p[0].GetValueAsDouble());
AVG
Summary:
Returns the moving average over a specified time period.
Parameters:
Tag value to evaluate in the moving average.
Time period to evaluate in seconds.
Internal code:
Complex code using arrays of values and timestamps.
BCDINT
Summary:
Converts binary coded decimal to integer value.
Parameters:
BCD value to convert
Internal code:
double doublebcdvalue = p[0].GetValueAsDouble();
ulong bcdvalue = System.Convert.ToUInt64(doublebcdvalue);
string convertedString = bcdvalue.ToString(“X4”);
return System.Int32.Parse(convertedString);
BITC
Summary:
Bit compare of a specific bit postion of an integer value.
Parameters:
Integer value to evaluate
Bit position
Internal code:
double x = p[0].GetValueAsDouble();
double y = p[1].GetValueAsDouble();
ulong intVal = Convert.ToUInt64(Math.Floor(x));
int bitIndex = Convert.ToInt32(Math.Floor(y));
ulong shiftValue = 1;
ulong valueToCompare = shiftValue << bitIndex;
return (intVal & valueToCompare) > 0 ? 1 : 0;
BITSHIFTLEFT
Summary:
Bit shift left a specified number of bits of an integer value.
Parameters:
Integer value to evaluate
Number of bits to shift left
Internal code:
double x = p[0].GetValueAsDouble();
double y = p[1].GetValueAsDouble();
ulong intVal = Convert.ToUInt64(Math.Floor(x));
int bitIndex = Convert.ToInt32(Math.Floor(y));
return (intVal << bitIndex);
BITSHIFTRIGHT
Summary:
Bit shift right a specified number of bits of an integer value.
Parameters:
Integer value to evaluate
Number of bits to shift right
Internal code:
double x = p[0].GetValueAsDouble();
double y = p[1].GetValueAsDouble();
ulong intVal = Convert.ToUInt64(Math.Floor(x));
int bitIndex = Convert.ToInt32(Math.Floor(y));
return (intVal >> bitIndex);
BITWISEAND
Summary:
Bitwise AND of 2 integers.
Parameters:
Integer value to evaluate
Integer value to evaluate
Internal code:
double x = p[0].GetValueAsDouble();
double y = p[1].GetValueAsDouble();
ulong intVal = Convert.ToUInt64(Math.Floor(x));
ulong bitIndex = Convert.ToUInt64(Math.Floor(y));
return (intVal & bitIndex);
CEIL
Summary:
Ceiling of value. Returns the smallest integral value that is greater than or equal to the specified double-precision floating-point number.
Parameters:
Value to evaluate
Internal code:
return Math.Ceiling(p[0].GetValueAsDouble());
CHR
Summary:
Returns a specific character within a string.
Parameters:
String value
Position of character within the string to return.
Internal code:
IConvertible o = p[0].GetValue();
IConvertible i = p[1].GetValue();
int index = Convert.ToInt32(i);
if(o is String)
{
return ((String)o)[index].ToString();
}
return Convert.ToString(o)[index].ToString();
CONCAT
Summary:
Concatenate strings together.
Parameters:
Strings to concatenate.
Internal code:
StringBuilder sb = new StringBuilder();
for(int i=0; i<p.Length; i++)
{
sb.Append(p[i].GetValueAsString());
}
return sb.ToString();
CONTAINS
Summary:
Returns true if string contains the second string parameter.
Parameters:
String to evaluate.
String to check for.
Internal code:
String temp = p[0].GetValueAsString();
String temp2 = p[1].GetValueAsString();
if (temp.Contains(temp2) )
return 1;
else
return 0;
COS
Summary:
Returns the cosine of the specified angle.
Parameters:
Value to convert.
Internal code:
return Math.Cos(p[0].GetValueAsDouble());
COSH
Summary:
Returns the hyperbolic cosine of the specified angle.
Parameters:
Value to convert.
Internal code:
double x__ = p[0].GetValueAsDouble();
return (Math.Exp(x__)+Math.Exp(-x__))*0.5;
COTAN
Summary:
Returns the cotangent of the specified angle.
Parameters:
An angle, measured in radians.
Internal code:
return 1/Math.Tan(p[0].GetValueAsDouble());
ENDSWITH
Summary:
Returns true if string ends with the second string parameter.
Parameters:
String to evaluate.
String to check for.
Internal code:
String temp = p[0].GetValueAsString();
String temp2 = p[1].GetValueAsString();
if (temp.EndsWith(temp2))
return 1;
else
return 0;
EXP
Summary:
Returns e raised to the specified power.
Parameters:
Value to convert.
Internal code:
return Math.Exp(p[0].GetValueAsDouble());
FLOOR
Summary:
Floor of value. Returns the largest integer less than or equal to the specified double-precision floating-point number.
Parameters:
Value to convert
Internal code:
return Math.Floor(p[0].GetValueAsDouble());
HIGHBYTE
Summary:
Returns the high byte of a word.
Parameters:
Value to convert
Internal code:
double rawAsDouble = p[0].GetValueAsDouble();
UInt16 intVal = Convert.ToUInt16(rawAsDouble);
byte[] bytes = BitConverter.GetBytes(intVal);
if (BitConverter.IsLittleEndian)
return bytes[1];
else
return bytes[0];
HIGHWORD
Summary:
Returns the high word of an integer.
Parameters:
Value to convert
Internal code:
double rawAsDouble = p[0].GetValueAsDouble();
UInt32 intVal = Convert.ToUInt32(rawAsDouble);
byte[] bytes = BitConverter.GetBytes(intVal);
UInt16 tempWord;
if(BitConverter.IsLittleEndian)
tempWord = BitConverter.ToUInt16(bytes, 2);
else
tempWord = BitConverter.ToUInt16(bytes, 0);
return tempWord;
IF
Summary:
If, Then, Else. Returns second parameter if first parameter is true, returns third parameter if first parameter is false.
Parameters:
Condition to evaluate
Value to return if condition is true
Value to return if condition is false
Internal code:
return (p[0].GetValueAsDouble()!=0.0) ? p[1].GetValue() : p[2].GetValue();
INDEXOF
Summary:
Returns position of first occurance of string to check for within the string to evaluate.
Parameters:
String to find index in
String to check for
Internal code:
String temp = p[0].GetValueAsString();
String temp2 = p[1].GetValueAsString();
Int32 firstIndex;
firstIndex = temp.IndexOf(temp2);
return firstIndex;
INTBCD
Summary:
Converts integer to binary coded decimal.
Parameters:
Integer value to convert
Internal code:
double doublebcdvalue = p[0].GetValueAsDouble();
ulong intvalue = System.Convert.ToUInt64(doublebcdvalue);
string convertedString = intvalue.ToString(“0”);
ulong returnvalue = System.Convert.ToUInt64(convertedString, 16);
return returnvalue;
INTPOW
Summary:
Returns a specified number raised to the specified power.
Parameters:
Value to be raised to a power.
The power.
Internal code:
double x = p[0].GetValueAsDouble();
double y = p[1].GetValueAsDouble();
return Math.Pow(x, Math.Floor(y));
INTTOASC
Summary:
Converts integer to ascii character value.
Parameters:
Integer value to convert
Internal code:
double doubleintvalue = p[0].GetValueAsDouble();
Int32 intvalue = System.Convert.ToInt32(doubleintvalue);
string testString;
Char testChar;
testChar = System.Convert.ToChar(intvalue);
testString = testChar.ToString();
return testString;
LN
Summary:
Returns the natural (base e) logarithm of a specified number.
Parameters:
Value to convert
Internal code:
return Math.Log(p[0].GetValueAsDouble());
LOG
Summary:
Returns the base 10 logarithm of a specified number.
Parameters:
Value to convert
Internal code:
return Math.Log(p[0].GetValueAsDouble())/Math.Log(10);
LOGN
Summary:
Returns the base logn of a specified number.
Parameters:
Value to convert
Internal code:
return Math.Log(p[0].GetValueAsDouble())/Math.Log(p[1].GetValueAsDouble());
LOWBYTE
Summary:
Returns the low byte of a word.
Parameters:
Value to convert
Internal code:
double rawAsDouble = p[0].GetValueAsDouble();
UInt16 intVal = Convert.ToUInt16(rawAsDouble);
byte[] bytes = BitConverter.GetBytes(intVal);
if (BitConverter.IsLittleEndian)
return bytes[0];
else
return bytes[1];
LOWWORD
Summary:
Returns the low word of an integer.
Parameters:
Value to convert
Internal code:
double rawAsDouble = p[0].GetValueAsDouble();
UInt32 intVal = Convert.ToUInt32(rawAsDouble);
byte[] bytes = BitConverter.GetBytes(intVal);
UInt16 tempWord;
if (BitConverter.IsLittleEndian)
tempWord = BitConverter.ToUInt16(bytes, 0);
else
tempWord = BitConverter.ToUInt16(bytes, 2);
return tempWord;
LTRIM
Summary:
Removes all leading occurrences spaces from a string.
Parameters:
String to convert.
Internal code:
IConvertible o = p[0].GetValue();
if(o is String)
{
return ((String)o).TrimStart();
}
return Convert.ToString(o).TrimStart();
MAX
Summary:
Returns highest value of 2 values.
Parameters:
First value to evaluate.
Second value to evaluate.
Internal code:
double x__ = p[0].GetValueAsDouble();
double y__ = p[1].GetValueAsDouble();
if (x__ > y__)
return x__;
else
return y__;
MEDIAN
Summary:
Returns median value from multiple values. If an even number of values then it returns the average of the middle 2 numbers.
Parameters:
Each value to evaluate in the median calculation.
Internal code:
Complex
MIN
Summary:
Returns lowest value of 2 values.
Parameters:
First value to evaluate.
Second value to evaluate.
Internal code:
double x__ = p[0].GetValueAsDouble();
double y__ = p[1].GetValueAsDouble();
if (x__ < y__)
return x__;
else
return y__;
MOD
Summary:
Returns the modulus of parameter 1 with parameter 2 value applied.
Parameters:
Number to evaluate.
Operator.
Internal code:
double x = p[0].GetValueAsDouble();
double y = p[1].GetValueAsDouble();
int p1 = (int)Math.Floor(x);
int p2 = (int)Math.Floor(y);
return p1 % p2;
MOVMAX
Summary:
Returns the highest value over a specified time period.
Parameters:
Tag value to evaluate in the moving maximum.
Time period to evaluate in seconds.
Internal code:
Complex code using arrays of values and timestamps.
MOVMIN
Summary:
Returns the lowest value over a specified time period.
Parameters:
Tag value to evaluate in the moving minimum.
Time period to evaluate in seconds.
Internal code:
Complex code using arrays of values and timestamps.
MOVSUM
Summary:
Returns the total of all samples over a specified time period.
Parameters:
Tag value to evaluate in the moving summation.
Time period to evaluate in seconds.
Internal code:
Complex code using arrays of values and timestamps.
POW
Summary:
Returns a specified number raised to the specified power.
Parameters:
Number to be raised to a power.
The power.
Internal code:
double x = p[0].GetValueAsDouble();
double y = p[1].GetValueAsDouble();
return Math.Pow(x, y);
REPLACE
Summary:
Returns a string with all occurrences matching the string value of parameter 2 replaced with the string value of parameter 3.
Parameters:
String to evaluate.
String to search for.
String to replace the occurrences found.
Internal code:
string stringValue = p[0].GetValueAsString();
string stringToCheck = p[1].GetValueAsString();
string stringToReplace = p[2].GetValueAsString();
return stringValue.Replace(stringToCheck, stringToReplace);
RND
Summary:
Returns a random number.
Parameters:
None.
Internal code:
if(random==null)
{
random = new Random();
}
return random.NextDouble();
RTRIM
Summary:
Removes all ending occurrences spaces from a string.
Parameters:
String to convert.
Internal code:
IConvertible o = p[0].GetValue();
if(o is String)
{
return ((String)o).TrimEnd();
}
return Convert.ToString(o).TrimEnd();
SIGN
Summary:
Returns 1 if value is greater than 0. Returns -1 if less than 0. Returns 0 if equal to 0.
Parameters:
Value to evaluate.
Internal code:
double x__= p[0].GetValueAsDouble();
if (x__ < 0)
return -1;
else
if (x__ > 0)
return 1.0;
else
return 0.0;
SIN
Summary:
Returns the sine of the specified angle.
Parameters:
An angle, measured in radians.
Internal code:
return Math.Sin(p[0].GetValueAsDouble());
SINH
Summary:
Returns the hyperbolic sine of the specified angle.
Parameters:
An angle, measured in radians.
Internal code:
double d = p[0].GetValueAsDouble();
return (Math.Exp(d)-Math.Exp(-d))*0.5;
SQR
Summary:
Returns the square of a specified number.
Parameters:
Value to convert.
Internal code:
double d = p[0].GetValueAsDouble();
return (d*d);
SQRT
Summary:
Returns the square root of a specified number.
Parameters:
Value to convert.
Internal code:
return Math.Sqrt(p[0].GetValueAsDouble());
STARTSWITH
Summary:
Returns true if string starts with the second string parameter.
Parameters:
String to evaluate.
String to check for.
Internal code:
String temp = p[0].GetValueAsString();
String temp2 = p[1].GetValueAsString();
if (temp.StartsWith(temp2))
return 1;
else
return 0;
STR
Summary:
Converts a number to a string.
Parameters:
Number to convert.
Internal code:
IConvertible o = p[0].GetValue();
if (o is String)
{
return o;
}
return Convert.ToString(o);
STRFORMT
Summary:
Converts a number to a string with the format of the second parameter. For valid format codes search for .NET ToString Format.
Parameters:
Number to convert.
String containing format codes.
Internal code:
IConvertible o = p[0].GetValue();
string formatstring = p[1].GetValueAsString();
if (o is String)
{
return o;
}
else
{
double tempDouble = System.Convert.ToDouble(o);
return tempDouble.ToString(formatstring);
}
STRLEN
Summary:
Returns the length of a string.
Parameters:
String to evaluate.
Internal code:
String temp = p[0].GetValueAsString();
return Convert.ToDouble(temp.Length);
SUBSTR
Summary:
Returns a substring of a string.
Parameters:
String to evaluate.
Starting position within the string.
Length of string to return.
Internal code:
String s = p[0].GetValueAsString();
int start = (int)(p[1].GetValueAsDouble());
int length = (int)(p[2].GetValueAsDouble());
length = Math.Min(length, s.Length-start);
if (s == “0”)
return “0”;
return s.Substring(start,length);
SUM
Summary:
Summation of all values.
Parameters:
Values to sum. As many parameters as you desire.
Internal code:
double total = 0;
for(int i=0; i<p.Length; i++)
{
total += p[i].GetValueAsDouble();
}
return total;
TAN
Summary:
Returns the tangent of the specified angle.
Parameters:
An angle, measured in radians.
Internal code:
return Math.Tan(p[0].GetValueAsDouble());
TRIM
Summary:
Removes all occurrences of spaces from a string.
Parameters:
String to convert.
Internal code:
IConvertible o = p[0].GetValue();
if(o is String)
{
return ((String)o).Trim();
}
return Convert.ToString(o).Trim();
TRUNC
Summary:
Truncates a value to the nearest integer.
Parameters:
Value to convert.
Internal code:
double x__ = p[0].GetValueAsDouble();
if (x__>=0)
{
return Math.Floor(x__);
}
else
{
return Math.Ceiling(x__);
}
UNIXTODATETIME
Summary:
Returns a string of the date and time with passing in the number of seconds since 1970.
Parameters:
Number of seconds since 1970.
String containing format codes.
Internal code:
double seconds = p[0].GetValueAsDouble();
string formatstring = p[1].GetValueAsString();
DateTime dt1970 = new DateTime(1970, 1, 1).AddSeconds(seconds);
return dt1970.ToString(formatstring);
VAL
Summary:
Converts a string to a number.
Parameters:
String to convert.
Internal code:
IConvertible o = p[0].GetValue();
if(o is String)
{
return Convert.ToDouble((String)o);
}
return Convert.ToDouble(o);