The script language

This article explains how to write a script using the script language. If you are looking for information about how to navigate around the script editor page, it is suggested you read Using the script editor.

Upon opening a new script, you will see a basic template of the script language. It consists of three sections, known as blocks. These are referred to in the script language as 'object', 'each', and 'output'. 'Object' is where the machine and sensors to be used in the script are defined. 'Each' defines the calculation. 'Output' defines to which virtual sensors the data is provided.

The curly brackets define the bounds of each block. To edit a script, add lines between the curly brackets.

The 'object' block will automatically be pre-filled with the definition of the machine. In the rows under the machine definition you can then define all the sensors which will be used in the script. The easiest way of doing this is to put the text cursor in the correct place and then click the "Add sensor" button, and select a sensor. Do this for all the sensors you wish to use.

The first part of the 'each' block defines the frequency at which the calculation takes place, with the  statement "(t:second)." This can be edited and should match the lowest frequency of the physical sensors being used, as should the frequency of the related virtual sensors (which can be changed in the sensor definition). The following frequencies are possible:

1 second
10 seconds
30 seconds
1 minute
10 minutes
15 minutes
30 minutes
1 hour
2 hours
4 hours
8 hours
12 hours
1 day

The next part of the 'each' block defines the calculation itself. This consists of references to the sensors, coupled with a reference to the time "[t]" and the equation, defined by the following operators:

Between two values:

For example:  2 + 3,   5 < 7

plus = "+"
minus = "-"
modulo = "%"
multiplication = "*"
division = "/"
booleanAnd = "&&"
booleanOr = "||"
equals = "=="
notEquals = "!="
lessThan = "<"
greaterThan = ">"
lessOrEqual = "<="
greaterOrEqual = ">="

For one value/expression:

For example:  sum(sensor[t-1,t]),   cos(5),    10^(sensor[t])

sum = "sum"
average = "avg"
max = "max"
count = "count"
sin = "sin"
cos = "cos"
tan = "tan"
asin = "asin"
acos = "acos"
atan = "atan"
abs = "abs"
e_exp = "e^"
ln = "ln"
exp10 = "10^"
log10 = "10log"
round = "round"
floor = "floor"
ceil = "ceiling"
latch = "latch"
sqrt = "sqrt"

More unusual options:

"if(predicate){}"                                       Example:     if(S1[t] < 3) {V1[t] = 6}
"if(predicate){}else{}"                              Example:     if(S1[t] < 3) {V1[t] = 6} else {V1[t] = S1[t]}

"sumif(value, predicate)"                           Example:     sumif(S1[t-1,t], S1[t-1,t] >= 80)
“countif(value, predicate)”                         Example:     countif(S2[t-1,t], S1[t-1,t] != 80)

It is also possible to add more 'each' blocks if required.

The 'output' block simply states the virtual sensors which receive the data as an output.

For script examples, refer to the article: Script examples.

Have more questions? Submit a request