A calculation script makes use of a scripting language, called Tengo.

Tengo scripting language

Tengo is a scripting language written in Go and with a Go-like syntax. An overview of the basic Tengo syntax can be found here. You can play around with Tengo here .

Standard Tengo libraries

A list of standard Tengo libraries that are supported are listed below.

math

Defines some mathematic constants and functions.

More info

times

Defines some basic time based constants and functions.

More info

rand

Defines functions to generate random values.

More info

text

Defines string manipulation and formatting functions.

More info

enum

Defines some functions to work with enumerables.

More info

Custom Tengo libraries

We’ve added some custom libraries on top of the existing libraries to allow for more versatile scripts.

local

Local allows to save variables in a local context specific to a particular calculation. Only this calculation script can store/retrieve the value across all script executions. The key Context is reserved for the calculation context and can’t be used with these functions.

An overview of the available functions:

get: local.get(key) retrieves a value from the local context by it’s key.

set: local.set(key, value) sets a value in the local context by it’s key.

Example

This example retrieves a previously set value from the local context. If the value does not exist, a default value 0.0 is used. After adding 1.0 to the result, the value is set again in the local context and returned as output by the script.

previous := float(local.get("previous"),0.0)
result := previous + 1.0
local.set("previous", result)
return result

global

Global allows to save variables in a global context accessible to all calculation scripts. Any calculation script can store/retrieve the value across all scripts executions. An overview of the available functions:

get: global.get(key) retrieves a value from the global context by it’s key.

set: global.set(key, value) sets a value in the global context by it’s key.

util

Util provides custom functions for basic operations which are not available or incomplete in the standard Tengo library.

is_equal: checks whether 2 objects are equal, providers better handling of undefined values

Examples

Simple boolean comparison

if is_equal(_A.Value, true) {
  return _B.Value
}

return _C.Value

Comparing numeric types, in this example _A.Value is of type float

return is_equal(_A.Value, 12)

Calculation context helper functions

set_tag: set_tag(tag, value) sets a tag in the result

set_status: set_status(status) sets the status of the result

Input measurement(s) via alias

To access the data of an input measurement, one can use the according alias for the input measurement given in the calculation settings. The alias contains the following properties:

  • Value: the value of the measurement
  • Timestamp: the timestamp of the measurement
  • Tags: the tags of the measurement

Example

This example uses the Value property of an alias _A to access the value of an input measurement and multiplies that value by 2.

return _A.Value * 2