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

  • time_weekday: times.time_weekday(t time) returns an integer that represents the day of the week of a given timestamp. The numeric representation of weekdays start from 0 with Sunday and end at 6 with Saturday.
Example

Simple shift calculation.

tz := "Europe/Brussels"
ts := times.to_location(_A.Timestamp, tz)
diw := times.time_weekday(ts) // numeric representation of the day of week
hid := times.time_hour(ts) // hour of the day

if diw >= 0.0 && diw <= 5.0 && hid >= 5.0 && hid < 13.0 {
  return 1.0
}

if diw >= 0.0 && diw <= 5.0 && hid >= 13.0 && hid < 21.0 {
  return 2.0
}

if diw >= 0.0 && diw <= 5.0 && hid >= 21.0 && hid < 5.0 {
   return 3.0
}

if diw >= 6.0 && diw <= 7.0 && hid >= 5.0 && hid < 17.0 {
  return 4.0
}

if diw >= 6.0 && diw <= 7.0 && hid >= 17.0 && hid < 5.0 {
  return 4.0
}

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

Factry 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 data reference(s) via alias

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

  • Value: the value of the data reference
  • Timestamp: the timestamp of the data reference
  • Tags: the tags of the data reference

Example

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

return _A.Value * 2