Tips and Tricks
Here is a list of a few tips we gathered to improve your configuration skills with variables.
Use NOW and timestamps to handle time
NOW is a variable prebuild in the application, and allows you to handle time. It returns the number of milliseconds between the 1st of January 1970 and today, which is called a timestamp.
Timestamp when you arrived on this page: 1665750220161 (refresh to see it change)
Here is the time you spent on this page in seconds as a variable:
- Variable expression: ($NOW - $time_of_arrival) / 1000
- Result: 0
Boolean variables
Booleans, in computer science, is a data type with two possible values: true or false.
Booleans are usually two expressions (left / right) evaluated by a logic comparator:
- 1 > 2 means "Is 1 greater than 2?" ==> FALSE
- 1 == 2 means "Is 1 equals 2?" ==> FALSE
- 1 < 2 means "Is 1 lower than 2?" ==> TRUE
Essential in programmation, they are also essential to advanced configuration, in the Machine State per exemple. Indeed, the variable that defines the Machine State is a boolean: TRUE when the machine is working and FALSE when the machine is stopped.
We often use booleans with time to create delays in the application to implement cases like: "The machine did not create a part for 5 minutes".
We will translate this example with a simple one and create a variable that is TRUE when it has been less than 10 seconds that your arrived on the page, FALSE otherwise. Refresh to see it change.
Here is the variable expression and the result:
- Variable expression: ($NOW - $time_of_arrival) < 10000
- Time on the page (in seconds): 0
- Result: TRUE
The modulo operator and how to use it
Definition
The modulo operation, represented by the % in a variable expression, is the remainder of a division, after one number is divided by the other.
For example, the expression "5 % 2" evaluates to 1 because 5 divided by 2 has a quotient of 2 and a remainder of 1.
"9 % 3" evaluates to 0, because the division of 9 by 3 has a quotient of 3 and a remainder of 0.
This operation can be useful if you want to "loop" a number in time. Here is an example where our variable never goes above 5000 (ms).
- Variable expression: $NOW % 5000
- Result: 2
Example
The most concrete example is when you want to make a tile blink with a Color rule. When we want to make a tile blink every second per example, we use the modulo to create the variable, and a boolean comparator to make the tile blink.
Variable expression: ($NOW % 2000)/1000
Variable evaluation: 0
Tile color rules
Result: BLINK
Ternary expressions
A ternary is the logical expression of the IF / THEN / ELSE. It can allow you to give a different value to your variable according to the evaluation of a boolean. This is the syntaxe of a ternary:
IF_CONDITION ? THEN : ELSE
The simplest example we could give is to transform a boolean into a number. Let's take the example we presented with the modulo operator.
IF the modulo is greater than 1 THEN result = 1 ELSE result = 0
- Variable expression: ($NOW % 2000)/1000 > 1 ? 1 : 0
- Time: 0
- Result of the evaluation of the variable: 0