Tengo scripting language
A calculation script makes use of a scripting language, called Tengo.
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 .
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 referenceTimestamp
: the timestamp of the data referenceTags
: 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
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
Accessing Metadata
You can access metadata in different contexts, including metadata for the calculation itself or metadata for inputs. These inputs can be either asset properties or measurements. Additionally, if the input is an asset property, you can also access the metadata of the associated asset.
Accessing Metadata of the Calculation Itself
To access the metadata of the calculation itself, use:
factry.get_metadata("<metadata-key>")
Replace <metadata-key>
with the key you need.
Accessing Metadata of Inputs
Inputs can be either measurements or asset properties, and the metadata can be accessed as follows:
-
Accessing Metadata of Measurements or Asset Properties
To access a specific metadata key of an input (either a measurement or asset property):
factry.get_input_metadata("<input-alias>", "<metadata-key>")
Replace
<input-alias>
with the alias of the input data and<metadata-key>
with the key you need.To access all metadata of an input:
metadata := factry.get_input_metadata("<input-alias>")
You can then retrieve specific keys:
metadata["<metadata-key>"]
-
Accessing Metadata of the Asset (When Input is an Asset Property)
If the input is an asset property, you can also access the metadata of the associated asset:
-
To access a specific metadata key of the associated asset:
factry.get_input_asset_metadata("<input-alias>", "<metadata-key>")
-
To access all metadata of the associated asset:
metadata := factry.get_input_asset_metadata("<input-alias>")
Retrieve a specific key as needed:
metadata["<metadata-key>"]
-
Standard Tengo libraries
A list of standard Tengo libraries that are supported are listed below.
math
Defines some mathematic constants and functions.
times
Defines some basic time based constants and functions.
- 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.
rand
Defines functions to generate random values.
text
Defines string manipulation and formatting functions.
enum
Defines some functions to work with enumerables.
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 its key.
set: local.set(key, value)
sets a value in the local context by its key.
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 its key.
set: global.set(key, value)
sets a value in the global context by its 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
units
Units provides simple unit conversion. Its main datatype is the Value
, which represents a number with a given unit.
When a Value
is returned from a script, it is automatically converted to the bare number.
All functions that accept the name of a unit accept the the shorthand of the unit, the singular or plural form of the unit name, and varying spellings of the unit name, eg. m
, meter
, meters
, metre
, metres
.
convert:
units.convert(input, unit)
converts the input into aValue
. Ifinput
is
a number, a newValue
is returned with the given unit. If the input is
already aValue
, the input is converted into the given unit.
Conversions can be done between different magnitudes, e.g.meter
tokilometer
, and between different systems, e.g.Celsius
toFahrenheit
, ormeters
tofeet
.units.convert(input, from, to)
converts the given number from one unit into another, returning a number.
float: units.float(value)
returns the number stored in the Value
.
find: units.find(unit)
returns the unit object for the given unit. The unit object can be used to add units to numbers, or to convert between units.
Supported units
Examples
if97
IF97 provides functions for calculating thermodynamic properties of steam and water according to the IAPWS IF97 specification. This module uses names for thermodynamic quantities as defined on page 3 of the specification PDF .
All IF97 functions take either a regular number or a units.Value
unit-aware value as input. Units will be converted where needed. Return values are also unit.Value
objects.