Expressions

An expression is simply the right-hand side of an assignment to the attribute it is attached to. It can be as simple as an assignment of a source, or as complex as a computation on one or more sources. The expression forms the basis for conditioning data.

The syntax for an expression is basic JavaScript. The Intelligence Hub uses GraalVM to execute the JavaScript in expressions.

Sample Expressions

Expression Description
{{Connection.OPC_Server.Room2BTSTAT_CV}} A simple assignment of the raw value associated with input Room2B_TSTAT_CV on connection OPC_Server.
{{SQL.QueryAssetInfo.AssetID}} A complex data element assignment. Some connectors (e.g., REST, Sparkplug, SQL) return complex data values that contain one or more element values. In this example, a SQL connection named SQL has an input called QueryAssetInfo that returns a result set for which the element AssetID value is assigned.
{{Connection.OPC_Server.LineAPartsProduced}} + {{Connection.OPC_Server.LineBPartsProduced}} Assigns the summation of two inputs named LineAPartsProduced and LineBPartsProduced on connection OPC_Server.
{{Connection.OPC_Server.ProductID}}.split("-") Assuming the raw value of ProductID is a string value that is always formatted as follows: AA-BBBBB-C-DDD. Assigns the substring BBBBB from input ProductID on connection OPC_Server.
Math.sin({{Connection.OPC_Server.DeviceSignal}}) Calculates the sine of the raw value associated with input Signal on connection OPC_Server and assigns the result.
{{Instance.ServerRoom_Thermostat}} A simple assignment of a modeled instance within a parent modeled instance definition.
{{Instance.ServerRoom_Thermostat}}.Current > {{ServerRoom_Thermostat}}.Max A logical expression that tests if the value associated with attribute Current of instance ServerRoom_Thermostat is greater than the value associated with attribute Max of the same named instance.
var data = [1,2,{{Connection.opc.tag}}]; data Build an array from a primitive tag
{{Connection.opc.arraytag}}.slice(0,2) Return a part of an array.
javascript
// Build a complex value (JSON) from primitives
var value = {
	"employees": {
		"name": "steve",
		"value": {{Connection.opc.tag}}
    }
};
return (value)
javascript
// Iterate an array of complex data (CSV in this example) and return the row that meets a given condition. 
var retRow = ({{Connection.csv.data}}).forEach(function (item, index) {                                 
	if(parseInt(item["07A7"]) === 916){                       
		retRow = item;                                                    
	}
};
return (retRow)

Metadata in Expressions

For complex types passed to an expression (ex. Instances, SQL input, OPC UA Branch reads, etc) the following additional metadata is available in the expression.

Metadata Description
#model In the case of an Instance, this is the Model name. For inputs and other sources, it’s empty.
#name In the case of an Instance, this is the Instance name. For inputs and other sources, it’s the source name.

Below is an example of how to get and set these values. These values appear by default in the output payload as _model and _name attributes.

json
// Get the values
var instanceName = {{Instance.myInstance}}["#name"]
var modelName = {{Instance.myInstance}}["#model"]

// Set the values. Changes the values of the default _name and _model output attributes
{{Instance.myInstance}}["#name"] = "changeName"
{{Instance.myInstance}}["#model"] = "changeModel"

// Change the values in a child object
{{Instance.myInstance}}.childInstance["#name"] = "changeName"
{{Instance.myInstance}}.childInstance["#model"] = "changeModel"