Factry Historian
Calculations
Scripting
tengo scripting language a calculation script makes use of a scripting language, called tengo tengo is a scripting language written in go, with a go like syntax below, you’ll find some resources to help you get started language syntax docid\ bxvtdfxvmschctirsq55h interactive playground full documentation 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 the properties are case sensitive, so e g value will not work 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 tengo 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 tengo 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) tengo 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 tengo metadata = factry get input metadata("\<input alias>") you can then retrieve specific keys tengo 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 tengo factry get input asset metadata("\<input alias>", "\<metadata key>") to access all metadata of the associated asset tengo metadata = factry get input asset metadata("\<input alias>") retrieve a specific key as needed tengo metadata\["\<metadata key>"] standard tengo libraries the following is a list of standard tengo libraries that are always available and do not need to be imported in your script for example, you can use math directly without assigning it using math = import("math") math defines some mathematic constants and functions math docid\ cyyqqun596xk5fw2tgs5q times defines some basic time based constants and functions times docid\ e31dwpd2ifd vovdplb32 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 tengo 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 rand docid\ f3ljcpuv25snhsav7bzb4 text defines string manipulation and formatting functions text docid\ qqnia8o5bnqighh v8bze enum defines some functions to work with enumerables enum docid\ uxoozs 16dd vnrayrznj 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 how to use calculation context docid 9vedqpmtthzwuy1clkavh and can’t be used with these functions an overview of the available functions get local get(key, \[defaultvalue]) retrieves a value from the local context by its key, with an optional default value to be used when the retrieved value is undefined set local set(key, value) sets a value in the local context by its 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 tengo 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, \[defaultvalue]) retrieves a value from the global context by its key, with an optional default value to be used when the retrieved value is undefined 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 examples simple boolean comparison tengo if is equal( a value, true) { return b value } return c value comparing numeric types, in this example a value is of type float tengo return is equal( a value, 12) 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 a value if input is a number, a new value is returned with the given unit if the input is already a value , the input is converted into the given unit conversions can be done between different magnitudes, e g meter to kilometer , and between different systems, e g celsius to fahrenheit, or meters to feet 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 pressure unit names attopascal attopascal,apa,attopascals femtopascal femtopascal,fpa,femtopascals picopascal picopascal,ppa,picopascals nanopascal nanopascal,npa,nanopascals micropascal micropascal,μpa,micropascals millipascal millipascal,mpa,millipascals centipascal centipascal,cpa,centipascals decipascal decipascal,dpa,decipascals pascal pascal,pa,pascals decapascal decapascal,dapa,decapascals hectopascal hectopascal,hpa,hectopascals kilopascal kilopascal,kpa,kilopascals megapascal megapascal,mpa,megapascals gigapascal gigapascal,gpa,gigapascals terapascal terapascal,tpa,terapascals petapascal petapascal,ppa,petapascals exapascal exapascal,epa,exapascals microbar microbar,μbar,microbars millibar millibar,mbar,millibars centibar centibar,cbar,centibars bar bar,bars millimeter of mercury millimeter of mercury,mmmhg,millimeter of mercurys centimeter of mercury centimeter of mercury,cmmhg,centimeter of mercurys meter of mercury meter of mercury,mmhg,meters of mercury,meters of mercury inch of mercury inch of mercury,inhg,inch of mercurys millimeter of water column millimeter of water column,mmmh2o,millimeter of water columns centimeter of water column centimeter of water column,cmmh2o,centimeter of water columns meter of water column meter of water column,mmh2o,meters of water column,meters of water column inch of water column inch of water column,inh2o,inch of water columns newton per square meter newton per square meter,n/m²,newton per square meters pound force per square inch pound force per square inch,psi,pound force per square inchs standard atmosphere standard atmosphere,atm,standard atmospheres,standard atmospheres technical atmosphere technical atmosphere,at,technical atmospheres,technical atmospheres torr torr,torr,torrs barye barye,ba,baryes,baryes time unit names attosecond attosecond,as,attoseconds femtosecond femtosecond,fs,femtoseconds picosecond picosecond,ps,picoseconds nanosecond nanosecond,ns,nanoseconds microsecond microsecond,μs,microseconds millisecond millisecond,ms,milliseconds centisecond centisecond,cs,centiseconds decisecond decisecond,ds,deciseconds second second,s,seconds decasecond decasecond,das,decaseconds hectosecond hectosecond,hs,hectoseconds kilosecond kilosecond,ks,kiloseconds megasecond megasecond,ms,megaseconds gigasecond gigasecond,gs,gigaseconds terasecond terasecond,ts,teraseconds petasecond petasecond,ps,petaseconds exasecond exasecond,es,exaseconds minute minute,min,minutes hour hour,hr,hours day day,d,days fortnight fortnight,fortnights month month,months year year,yr,years decade decade,decades score score,scores century century,centurys millennium millennium,millenniums planck time planck time,𝑡ₚ,planck times bits/bytes unit names bit bit,b,bits kilobit kilobit,kb,kilobits megabit megabit,mb,megabits gigabit gigabit,gb,gigabits terabit terabit,tb,terabits petabit petabit,pb,petabits exabit exabit,eb,exabit unit names nibble nibble,nibbles byte byte,b,bytes kilobyte kilobyte,kb,kilobytes kibibyte kibibyte,kib,kibibytes megabyte megabyte,mb,megabytes mebibyte mebibyte,mib,mebibytes gigabyte gigabyte,gb,gigabytes gibibyte gibibyte,gib,gibibytes terabyte terabyte,tb,terabytes tebibyte tebibyte,tib,tebibytes petabyte petabyte,pb,petabytes pebibyte pebibyte,pib,pebibytes exabyte exabyte,exabytes exbibyte exbibyte,eib,exbibytes zettabyte zettabyte,zettabytes zebibyte zebibyte,zib,zebibytes yottabyte yottabyte,yottabytes yobibyte yobibyte,yib,yobibytes energy unit names attojoule attojoule,aj,attojoules femtojoule femtojoule,fj,femtojoules picojoule picojoule,pj,picojoules nanojoule nanojoule,nj,nanojoules microjoule microjoule,μj,microjoules millijoule millijoule,mj,millijoules joule joule,j,joules kilojoule kilojoule,kj,kilojoules megajoule megajoule,mj,megajoules gigajoule gigajoule,gj,gigajoules terajoule terajoule,tj,terajoules petajoule petajoule,pj,petajoules exajoule exajoule,ej,exajoules zettajoule zettajoule,zj,zettajoules yottajoule yottajoule,yj,yottajoules calorie calorie,cal,calories electronvolt electronvolt,ev,electronvolts watt hour watt hour,wh,watt hours kilowatt hour kilowatt hour,kwh,kilowatt hours megawatt hour megawatt hour,mwh,megawatt hours gigawatt hour gigawatt hour,gwh,gigawatt hours terawatt hour terawatt hour,twh,terawatt hours petawatt hour petawatt hour,pwh,petawatt hours power unit names watt watt,w,watts kilowatt kilowatt,kw,kilowatts megawatt megawatt,mw,megawatts gigawatt gigawatt,gw,gigawatts terawatt terawatt,tw,terawatts petawatt petawatt,pw,petawatts exawatt exawatt,ew,exawatts zettawatt zettawatt,zw,zettawatts yottawatt yottawatt,yw,yottawatts temperature unit names celsius celsius,c fahrenheit fahrenheit,f kelvin kelvin,k volume unit names attoliter attoliter,al,attolitre,attoliters femtoliter femtoliter,fl,femtolitre,femtoliters picoliter picoliter,pl,picolitre,picoliters nanoliter nanoliter,nl,nanolitre,nanoliters microliter microliter,μl,microlitre,microliters milliliter milliliter,ml,millilitre,milliliters centiliter centiliter,cl,centilitre,centiliters deciliter deciliter,dl,decilitre,deciliters liter liter,l,litre,liters decaliter decaliter,dal,decalitre,decaliters hectoliter hectoliter,hl,hectolitre,hectoliters kiloliter kiloliter,kl,kilolitre,kiloliters megaliter megaliter,ml,megalitre,megaliters gigaliter gigaliter,gl,gigalitre,gigaliters teraliter teraliter,tl,teralitre,teraliters petaliter petaliter,pl,petalitre,petaliters exaliter exaliter,el,exalitre,exaliters customary fluid ounce customary fluid ounce,customary fluid ounces fluid ounce fluid ounce,fl oz,floz,fluid ounces fluid pint fluid pint,fl pt,fluid pints fluid quart fluid quart,fl qt,fluid quarts fluid gallon fluid gallon,fluid gallons pint pint,pt,pints quart quart,qt,quarts gallon gallon,gal,gallons length unit names attometer attometer,am,attometre,attometers femtometer femtometer,fm,femtometre,femtometers picometer picometer,pm,picometre,picometers nanometer nanometer,nm,nanometre,nanometers micrometer micrometer,μm,micrometre,micrometers millimeter millimeter,mm,millimetre,millimeters centimeter centimeter,cm,centimetre,centimeters decimeter decimeter,dm,decimetre,decimeters meter meter,m,metre,meters decameter decameter,dam,decametre,decameters hectometer hectometer,hm,hectometre,hectometers kilometer kilometer,km,kilometre,kilometers megameter megameter,mm,megametre,megameters gigameter gigameter,gm,gigametre,gigameters terameter terameter,tm,terametre,terameters petameter petameter,pm,petametre,petameters exameter exameter,em,exametre,exameters angstrom angstrom,å,angstroms,angstroms inch inch,in,inches,inches foot foot,ft,feet,feet yard yard,yd,yards mile mile,mi,miles furlong furlong,fur,furlongs league league,lea,leagues mass unit names attogram attogram,ag,attograms femtogram femtogram,fg,femtograms picogram picogram,pg,picograms nanogram nanogram,ng,nanograms microgram microgram,μg,micrograms milligram milligram,mg,milligrams centigram centigram,cg,centigrams decigram decigram,dg,decigrams gram gram,g,grams decagram decagram,dag,decagrams hectogram hectogram,hg,hectograms kilogram kilogram,kg,kilograms megagram megagram,mg,megagrams gigagram gigagram,gg,gigagrams teragram teragram,tg,teragrams petagram petagram,pg,petagrams exagram exagram,eg,exagrams drachm drachm,dr,drachms grain grain,gr,grains ounce ounce,oz,ounces pound pound,lb,pounds slug slug,slugs stone stone,st,stones ton ton,t,tons examples unit conversion tengo // convert 1000 meters to kilometers // 1 convert the number 1000 to a value with unit "m" meters = units convert(1000, "m") // 2 convert the value to kilometers kilometers = units convert(meters, "km") // 3 return the number stored in the value // note that this conversion is not necessary, as the result is automatically converted to a number kilometersfloat = units float(kilometers) return kilometersfloat unit conversion with find tengo // convert 20 degrees celsius to kelvin, and convert 30 degrees celsius to fahrenheit celsius = units find("c") kelvin = units find("k") fahrenheit = units find("f") twentyc = celsius(20) thirtyc = celsius(30) twentyk = units convert(twentyc, kelvin) thirtyf = units convert(thirtyc, fahrenheit) return \[units float(twentyk), units float(thirtyf)] unit conversion with from and to tengo // convert 1000 meters to kilometers // 1 convert the number 1000 from "m" to "km" kilometers = units convert(1000, "metre", "kilometer") // 2 return the number return kilometers 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 if97 functions massdensity if97 massdensity(temp, pressure) returns the mass density ( kg/m^3 ) as a function of temperature ( k ) and pressure ( pa ) specificenthalpy if97 specificenthalpy(temp, pressure) returns the specific enthalpy ( j/kg ) as a function of temperature ( k ) and pressure ( pa ) specificentropy if97 specificentropy(temp, pressure) returns the specific entropy ( j/kg/k ) as a function of temperature ( k ) and pressure ( pa ) specificinternalenergy if97 specificinternalenergy(temp, pressure) returns the specific internal energy ( j/kg ) as a function of temperature ( k ) and pressure ( pa ) isobaricheatcap if97 isobaricheatcap(temp, pressure) returns the mass constant pressure heat capacity ( j/kg/k ) as a function of temperature ( k ) and pressure ( pa ) isochoricheatcap if97 isochoricheatcap(temp, pressure) returns the mass constant volume heat capacity ( j/kg/k ) as a function of temperature ( k ) and pressure ( pa ) speedsound if97 speedsound(temp, pressure) returns the speed of sound ( m/s ) as a function of temperature ( k ) and pressure ( pa ) derivmassdensitypressure if97 derivmassdensitypressure(temp, pressure) returns (d(rho)/d(p))t ( kg/m³/pa ) as a function of temperature ( k ) and pressure ( pa ) viscosity if97 viscosity(temp, pressure) returns the viscosity ( pa s ) as a function of temperature ( k ) and pressure ( pa ) thermalconductivity if97 thermalconductivity(temp, pressure) returns the thermal conductivity ( w/m k ) as a function of temperature ( k ) and pressure ( pa ) prandtl if97 prandtl(temp, pressure) returns the prandtl number ( dimensionless ) as a function of temperature ( k ) and pressure ( pa ) satliquidmassdensity if97 satliquidmassdensity(pressure) returns the saturated liquid mass density ( kg/m^3 ) as a function of pressure ( pa ) satvapormassdensity if97 satvapormassdensity(pressure) returns the saturated vapor mass density ( kg/m^3 ) as a function of pressure ( pa ) satliquidspecificenthalpy if97 satliquidspecificenthalpy(pressure) returns the saturated liquid specific enthalpy ( j/kg ) as a function of pressure ( pa ) satvaporspecificenthalpy if97 satvaporspecificenthalpy(pressure) returns the saturated vapor specific enthalpy ( j/kg ) as a function of pressure ( pa ) satliquidspecificentropy if97 satliquidspecificentropy(pressure) returns the saturated liquid specific entropy ( j/kg/k ) as a function of pressure ( pa ) satvaporspecificentropy if97 satvaporspecificentropy(pressure) returns the saturated vapor specific entropy ( j/kg/k ) as a function of pressure ( pa ) satliquidmassinternalenergy if97 satliquidmassinternalenergy(pressure) returns the saturated liquid mass internal energy ( j/kg ) as a function of pressure ( pa ) satvapormassinternalenergy if97 satvapormassinternalenergy(pressure) returns the saturated vapor mass internal energy ( j/kg ) as a function of pressure ( pa ) satliquidisobaricheatcap if97 satliquidisobaricheatcap(pressure) returns the saturated liquid isobaric heat capacity ( j/kg/k ) as a function of pressure ( pa ) satvaporisobaricheatcap if97 satvaporisobaricheatcap(pressure) returns the saturated vapor isobaric heat capacity ( j/kg/k ) as a function of pressure ( pa ) satliquidisochoricheatcap if97 satliquidisochoricheatcap(pressure) returns the saturated liquid isochoric heat capacity ( j/kg/k ) as a function of pressure ( pa ) satvaporisochoricheatcap if97 satvaporisochoricheatcap(pressure) returns the saturated vapor isochoric heat capacity ( j/kg/k ) as a function of pressure ( pa ) satliquidspeedsound if97 satliquidspeedsound(pressure) returns the saturated liquid speed of sound ( m/s ) as a function of pressure ( pa ) satvaporspeedsound if97 satvaporspeedsound(pressure) returns the saturated vapor speed of sound ( m/s ) as a function of pressure ( pa ) satliquidviscosity if97 satliquidviscosity(pressure) returns the saturated liquid viscosity ( pa s ) as a function of pressure ( pa ) satvaporviscosity if97 satvaporviscosity(pressure) returns the saturated vapor viscosity ( pa s ) as a function of pressure ( pa ) satliquidthermalcond if97 satliquidthermalcond(pressure) returns the saturated liquid thermal conductivity ( w/m k ) as a function of pressure ( pa ) satvaporthermalcond if97 satvaporthermalcond(pressure) returns the saturated vapor thermal conductivity ( w/m k ) as a function of pressure ( pa ) satliqprandtl if97 satliqprandtl(pressure) returns the saturated liquid prandtl number ( dimensionless ) as a function of pressure ( pa ) satvaporprandtl if97 satvaporprandtl(pressure) returns the saturated vapor prandtl number ( dimensionless ) as a function of pressure ( pa ) sattemp if97 sattemp(pressure) returns the saturation temperature ( k ) as a function of pressure ( pa ) satpressure if97 satpressure(temp) returns the saturation pressure ( pa ) as a function of temperature ( k ) surfacetension if97 surfacetension(temp) returns the surface tension ( n/m ) as a function of temperature ( k ) backwardenthalpytemp if97 backwardenthalpytemp(pressure, specific enthalpy) returns the temperature ( k ) as a function of pressure ( pa ) and specific enthalpy ( j/kg ) backwardenthalpymassdensity if97 backwardenthalpymassdensity(pressure, specific enthalpy) returns the mass density ( kg/m^3 ) as a function of pressure ( pa ) and specific enthalpy ( j/kg ) backwardentropytemp if97 backwardentropytemp(pressure, specific entropy) returns the temperature ( k ) as a function of pressure ( pa ) and specific entropy ( j/kg/k ) backwardentropymassdensity if97 backwardentropymassdensity(pressure, specific entropy) returns the mass density ( kg/m^3 ) as a function of pressure ( pa ) and specific entropy ( j/kg/k ) backwardpressure if97 backwardpressure(specific enthalpy, specific entropy) returns the pressure ( pa ) as a function of specific enthalpy ( j/kg ) and specific entropy ( j/kg/k ) backwardtemp if97 backwardtemp(specific enthalpy, specific entropy) returns the temperature ( k ) as a function of specific enthalpy ( j/kg ) and specific entropy ( j/kg/k ) backwardentropyenthalpy if97 backwardentropyenthalpy(pressure, specific entropy) returns the specific enthalpy ( j/kg ) as a function of pressure ( pa ) and specific entropy ( j/kg/k ) backwardenthalpyentropy if97 backwardenthalpyentropy(pressure, specific enthalpy) returns the specific entropy ( j/kg/k ) as a function of pressure ( pa ) and specific enthalpy ( j/kg ) qualityspecificenthalpy if97 qualityspecificenthalpy(pressure, vapor quality) returns the specific enthalpy ( j/kg ) as a function of pressure ( pa ) and vapor quality ( % ) qualityspecificinternalenergy if97 qualityspecificinternalenergy(pressure, vapor quality) returns the specific internal energy ( j/kg ) as a function of pressure ( pa ) and vapor quality ( % ) qualityspecificentropy if97 qualityspecificentropy(pressure, vapor quality) returns the specific entropy ( j/kg/k ) as a function of pressure ( pa ) and vapor quality ( % ) qualityspecificvolume if97 qualityspecificvolume(pressure, vapor quality) returns the specific volume ( m^3 ) as a function of pressure ( pa ) and vapor quality ( % ) qualitymassdensity if97 qualitymassdensity(pressure, specific enthalpy) returns the mass density ( kg/m^3 ) as a function of pressure ( pa ) and vapor quality ( % ) specificenthalpyquality if97 specificenthalpyquality(pressure, specific enthalpy) returns the vapor quality ( % ) as a function of pressure ( pa ) and specific enthalpy ( j/kg ) specificinternalenergyquality if97 specificinternalenergyquality(pressure, specific internal energy) returns the vapor quality ( % ) as a function of pressure ( pa ) and specific internal energy ( j/kg ) specificentropyquality if97 specificentropyquality(pressure, specific entropy) returns the vapor quality ( % ) as a function of pressure ( pa ) and specific entropy ( j/kg/k ) massdensityquality if97 massdensityquality(pressure, mass density) returns the vapor quality ( % ) as a function of pressure ( pa ) and mass density ( kg/m^3 ) specificvolumequality if97 specificvolumequality(pressure, specific volume) returns the vapor quality ( % ) as a function of pressure ( pa ) and specific volume ( m^3 )