**To get started with an overview of V2 Adapter Scripts, visit this Knowledge Base Article. **

Expressions are a type of value supported in several different parts of an IO adapter script. Expressions allow a value to be calculated as a kind of mathematical or logical formula using other **identifiers**. Identifiers are **variables** or **data sources** defined in the adapter script.

- Variables come from the variables section of an adapter script. A variable is a named list of operations applied to other variables or data sources. For more information, see:
**IO Configuration - Variables**. - Data sources are defined in other sections specific to a particular integration type. Some examples of data sources would be:
**Pins**for Labjack U3/T4/T7 integrations**Registers**and**coils**for Modbus-TCP integrations**Tags**for Ethernet/IP and OPC-UA integrations**Declared keys**for MTConnect and MTConnect Adapter integrations- Some expressions used within variable definitions also support the special “
**this**” identifier. The**this**identifier represents the result of the previous step in the variable’s operation list.

Here are a few example expressions, with the variables or data sources highlighted:

- Convert a temperature from C to F
`(`

**temp-in*** 9 / 5) + 32 - Check if either is running
**spindle-1**or**spindle-2** - Check if a pin is in a specific range
**AIN0**> 1.2 and**AIN0**< 3.4 - Check if an execution key is set to STOPPED
**exec**== ‘STOPPED’ - Cause a counter to reset after counting up to 10
**this**>= 10

## Update Triggers

Variables and data sources are treated the same way in an expression. When any variable or data source referenced within an expression updates, the entire expression is immediately re-computed. This will cause cascading updates to occur where the expression is used.

For example, if an expression is used in the middle of a variable definition, an expression update will cause the entire variable’s operation list to be re-executed with the last data it received. If this causes the variable’s value to change, then any other expression that references the variable will be re-computed as well. Avoid creating circular update loops via expressions.

Expressions can also appear in **data item** and **condition** definitions, which are responsible for outputting data to the MachineMetrics cloud. If an expression update changes the value of the data item or condition, the update will immediately be sent out.

## Operators and Functions

Expressions support the following operators:

- Arithmetic:
**+**,**-**,*****,**/**,**%**(modulus),**^**(power) - Boolean:
**and**,**or**,**not** - Bitwise:
**&**,**|**,**~**,**^|**,**<<**,**>>**,**>>>** - Relational:
**>**,**<**,**>=**,**<=**,**==**,**!=** - Conditional expression:
`(statement)`

**?**(yes)**:**(no)

Expressions support grouping statements with parentheses. Parentheses can be nested arbitrarily deep and are useful for more complex logic or forcing order of operations.

Examples:

`(`

**temp-in*** 9 / 5) + 32**AIN0**> 1.2 and**AIN0**< 3.4**hv-relay-on**?**hv-analog**: (**lv-analog*** 10)

### Arithmetic Functions

Expressions support many standard arithmetic functions:

: Compute absolute value of a number**abs(x)**

: Round value up to next integer**ceil(x)**

: Round a value down to next integer**floor(x)**

: Calculate the logarithm of a value**log(x, base)**

: Round to the nearest integer or n decimal places**round(x, n)**

: Calculate the square root of a value**sqrt(x)**

Examples:

`floor(`

**AIN0**)`round(abs(`

**spindle-speed**))

### Statistics Functions

Expressions support several statistical functions. These functions can consume an arbitrary number of arguments, including variables, data sources, or constants.

: Compute the maximum value of the passed values`max(a, b, ...)`

: Compute the mean value of the passed values`mean(a, b, ...)`

: Compute the median value of the passed values`median(a, b, ...)`

: Compute the minimum value of the passed values`min(a, b, ...)`

: Compute the mode of the passed values`mode(a, b, ...)`

: Compute the product of the passed values`prod(a, b, ...)`

: Compute the standard deviation of the passed values`std(a, b, ...)`

: Compute the sum of the passed values`sum(a, b, ...)`

: Compute the variance of the passed values`variance(a, b, ...)`

Examples:

`max(`

**spindle-1**,**spindle-2**,**spindle-3**) > 6000

### String Functions

Expressions support some functions intended to work with string values.

: Checks if the string x starts with the passed term. Matches are case-sensitive.`startsWith(x, term)`

: Checks if the string x ends with the passed term. Matches are case-sensitive.`endsWith(x, term)`

: Checks if the string x contains the passed term anywhere. Matches are case-sensitive.`contains(x, term)`

Examples:

`startsWith(`

**msg**, ‘EX’)`endsWith(`

**msg**, ‘EX’)`contains(`

**msg**, ‘error’)

### Trig Functions

Expressions support several trigonometry functions.

: Calculate the inverse cosine of a value`acos(x)`

: Calculate the inverse cotangent of a value`acot(x)`

: Calculate the inverse cosecant of a value`acsc(x)`

: Calculate the inverse secant of a value`asec(x)`

: Calculate the inverse sine of a value`asin(x)`

: Calculate the inverse tangent of a value`atan(x)`

: Calculate the cosine of a value`cos(x)`

: Calculate the cotangent of a value`cot(x)`

: Calculate the cosecant of a value`csc(x)`

: Calculate the secant of a value`sec(x)`

: Calculate the sine of a value`sin(x)`

: Calculate the tangent of a value`tan(x)`

### Utility Functions

These miscellaneous functions support other useful functionality in expressions.

: Read one bit as true or false from a bit flag value. The index is 0-based.**flag(x, index)**- # The following lines are equivalent

flag(**register70**, 3)

(**register70**& (1 <<**index**)) != 0

### Have Questions?

Reach out to Support@machinemetrics.com.

## Comments

0 comments

Please sign in to leave a comment.