Functions
We develop some functions to handle the most common operations in LCL. Here is a list of the available functions:
Be careful!
None, if this happens, the function returns None. Extracting data
First of all, to any operation, you need to extract the data, or extract pre-calculated data from the asset, for this, you can use the following functions:
GET_PARAM
First of all, you need to extract the raw data from the message sent from the device, so, you can use the GET_PARAM function to extract the data from the message. The GET_PARAM function receives two parameters:
GET_PARAM(search, fallback)searchis the name of the parameter you want to extract from the message.fallbackis the value that will be returned if the parameter is not found in the message.
GET_PARAM("my.param", "fallback value")Due to the Layrz capability to associate multiple devices to an asset, uses the nomenclature of {device_ident}.{parameter} to identify the parameters sent by the device. For example, if your device has the ident 123456, and you want to extract the parameter temperature, you should use 123456.temperature as the search parameter.
GET_PARAM("123456.temperature", "fallback value")Also, you can add complexity of the formula to allow replication along other assets trough clonation or global sensors, for example:
Be careful!
GET_PARAM(
CONCAT(
PRIMARY_DEVICE(),
".temperature",
),
)GET_SENSOR
In the real world, you usually need more than a raw value, or you need to combine other values to get the final value. For this, you can use the GET_SENSOR function to extract the value from other sensor. The GET_SENSOR function receives two parameters:
GET_SENSOR(sensor, fallback)sensoris the name of the sensor you want to extract the value.fallbackis the value that will be returned if the sensor is not found.
GET_SENSOR("my.sensor", "fallback value")Be careful!
For this example, we assume that you have a sensor called tank.temperature in degree celcius, and you want to convert it to Fahrenheit. You can use the GET_SENSOR function to extract the value from the sensor, and then use the MULTIPLY and SUM functions to convert the value to Fahrenheit.
SUM(
MULTIPLY(
GET_SENSOR("tank.temperature", 0),
1.8
),
32
)GET_CUSTOM_FIELD
In some scenarios, you have some custom fields in your asset, a custom field is a pre-defined field used to store additional information about the asset. For this, you can use the GET_CUSTOM_FIELD function to extract the value from the custom field. The GET_CUSTOM_FIELD function receives two parameters:
GET_CUSTOM_FIELD(field, fallback)fieldis the name of the custom field you want to extract the value.fallbackis the value that will be returned if the custom field is not found.
Be careful!
My custom field than my custom fieldGET_CUSTOM_FIELD("My custom field", "fallback value")GET_DISTANCE_TRAVELED
Sometimes, you need to validate if your asset moves a certain distance. For this, you can use the GET_DISTANCE_TRAVELED function to extract the distance traveled by the asset. The GET_DISTANCE_TRAVELED does not receive any parameter.
Be careful!
GET_DISTANCE_TRAVELED()GET_PREVIOUS_SENSOR
In some scenarios, you need to compare the current value of a sensor with the previous value of the sensor. For this, you can use the GET_PREVIOUS_SENSOR function to extract the previous value from the sensor. The GET_PREVIOUS_SENSOR function receives two parameters:
GET_PREVIOUS_SENSOR(sensor, fallback)sensoris the name of the sensor you want to extract the previous value.fallbackis the value that will be returned if the sensor is not found.
Be careful!
GET_PREVIOUS_SENSOR function only works with sensors that have a history of values, so, you need to ensure that the sensor has a history of values before using the GET_PREVIOUS_SENSOR function. GET_PREVIOUS_SENSOR("my.sensor", "fallback value")IS_PARAMETER_PRESENT
What happens when you need to validate if a parameter is present in the message? For this, you can use the IS_PARAMETER_PRESENT function to validate if a parameter is present in the message. The IS_PARAMETER_PRESENT function receives one parameter:
IS_PARAMETER_PRESENT(search)searchis the name of the parameter you want to validate if it is present in the message.
Be careful!
IS_PARAMETER_PRESENT function returns a boolean value, so, you can use the result in a conditional statement. IS_PARAMETER_PRESENT("my.param") # returns True or FalseIS_SENSOR_PRESENT
Similar to the IS_PARAMETER_PRESENT function, the IS_SENSOR_PRESENT function is used to validate if a sensor is present in the asset. The IS_SENSOR_PRESENT function receives one parameter:
IS_SENSOR_PRESENT(sensor)sensoris the name of the sensor you want to validate if it is present in the asset.
Be careful!
IS_SENSOR_PRESENT function returns a boolean value, so, you can use the result in a conditional statement. IS_SENSOR_PRESENT("my.sensor") # returns True or FalseIS_NONE
Not all of sensors works correctly, or, in Layrz you can have a sensor that may return a None value. For this, you can use the IS_NONE function to validate if a sensor returns a None value. The IS_NONE function receives one parameter:
IS_NONE(sensor)sensoris the name of the sensor you want to validate if it returns aNonevalue.
Be careful!
IS_NONE function returns a boolean value, so, you can use the result in a conditional statement. IS_NONE("my.sensor") # returns True or FalseLogical operations
Comparisons are the most common operations in programming, so, we develop some functions to handle the most common comparisons in LCL. Here is a list of the available functions:
Be careful!
Data type mismatch
COMPARE
What happens when we need to compare two values? For this, you can use the COMPARE function to compare two values. The COMPARE function receives two parameters:
COMPARE(value1, value2)value1is the first value you want to compare.value2is the second value you want to compare.
Note
COMPARE function is equivalent to the == operator in other programming languages. COMPARE(10, 20) # returns False
COMPARE(10, 10) # returns True
COMPARE("Hello", "World") # returns False
COMPARE("Hello", "Hello") # returns True
COMPARE("10", 10) # returns False due a data type mismatchOR_OPERATOR
One of the most common operations in programming is the logical OR operation. For this, you can use the OR_OPERATOR function to perform the logical OR operation. The OR_OPERATOR function receives many parameters as you want:
OR_OPERATOR(value1, value2, ..., valueN)value1is the first value you want to compare.value2is the second value you want to compare.valueNis the N value you want to compare....refers to the rest of the values you want to compare.
Note
OR_OPERATOR function is equivalent to the || or or operator in other programming languages. Data type mismatch
OR_OPERATOR function should only receive boolean values, so, you need to ensure that the data types are the same before using the OR_OPERATOR function. OR_OPERATOR(True, False) # returns True
OR_OPERATOR(False, False) # returns False
OR_OPERATOR(False, False, True) # returns True
OR_OPERATOR(False, False, False) # returns FalseAND_OPERATOR
Similar to the OR_OPERATOR function, the AND_OPERATOR function is used to perform the logical AND operation. The AND_OPERATOR function receives many parameters as you want:
AND_OPERATOR(value1, value2, ..., valueN)value1is the first value you want to compare.value2is the second value you want to compare.valueNis the N value you want to compare....refers to the rest of the values you want to compare.
Note
AND_OPERATOR function is equivalent to the && or and operator in other programming languages. Data type mismatch
AND_OPERATOR function should only receive boolean values, so, you need to ensure that the data types are the same before using the AND_OPERATOR function. AND_OPERATOR(True, False) # returns False
AND_OPERATOR(True, True) # returns True
AND_OPERATOR(True, True, False) # returns False
AND_OPERATOR(True, True, True) # returns TrueNOT
What happens when you need to negate a value? For this, you can use the NOT function to negate a value. The NOT function receives one parameter:
NOT(value)valueis the value you want to negate.
Note
NOT function is equivalent to the ! or not operator in other programming languages. Data type mismatch
NOT function should only receive boolean values, so, you need to ensure that the data types are the same before using the NOT function. NOT(True) # returns False
NOT(False) # returns TrueIF
What happens when you need to perform a conditional statement? For this, you can use the IF function to perform a conditional statement. The IF function receives three parameters:
IF(condition, valueIfTrue, valueIfFalse)conditionis the boolean value that you want to evaluate.valueIfTrueis the value that will be returned if the condition is true.valueIfFalseis the value that will be returned if the condition is false.
Note
if statement, in case that you are not a programmer, here is a brief explanation of the if statement: The if statement is used to perform a conditional statement, if the condition is true, the valueIfTrue is returned, otherwise, the valueIfFalse is returned.
Let's see an example:
For this example, we know that our temperature is 10 degrees, and we want to know if the temperature is greater than 20 degrees, if the temperature is greater than 20 degrees, we want to return "It's hot", otherwise, we want to return "It's cold".
Pseudo-code is a way to represent a programming language in a human-readable way, here is the statement in pseudo-code:
if temperature is greater than 20 degrees then
It's hot
else/otherwise
It's coldif statements is as simple as that, it's a question that has two possible answers, if the answer is true, the first answer is returned, otherwise, the second answer is returned.
Now, how it looks in languages like Python:
temperature = 10
if temperature > 20:
return "It's hot"
else:
return "It's cold"Or in JavaScript:
let temperature = 10
if (temperature > 20) {
return "It's hot"
} else {
return "It's cold"
}Or in dart:
int temperature = 10;
if (temperature > 20) {
return "It's hot";
} else {
return "It's cold";
}IF(
GREATER_THAN(10, 20), # More about this function later
CONSTANT("It's hot"),
CONSTANT("It's cold")
)GREATER_THAN_OR_EQUALS_TO
What happens when you need to validate if a value is greater than or equals to another value? For this, you can use the GREATER_THAN_OR_EQUALS_TO function to validate if a value is greater than or equals to another value. The GREATER_THAN_OR_EQUALS_TO function receives two parameters:
GREATER_THAN_OR_EQUALS_TO(value, compare)valueis the first value you want to compare.compareis the second value you want to compare.
Note
GREATER_THAN_OR_EQUALS_TO function is equivalent to the >= operator in other programming languages. Data type mismatch
GREATER_THAN_OR_EQUALS_TO function should only receive numeric values, so, you need to ensure that the data types are the same before using the GREATER_THAN_OR_EQUALS_TO function. GREATER_THAN_OR_EQUALS_TO(10, 20) # returns False
GREATER_THAN_OR_EQUALS_TO(10, 10) # returns TrueGREATER_THAN
Similar to the GREATER_THAN_OR_EQUALS_TO function, the GREATER_THAN function is used to validate if a value is greater than another value. The GREATER_THAN function receives two parameters:
GREATER_THAN(value, compare)valueis the first value you want to compare.compareis the second value you want to compare.
Note
GREATER_THAN function is equivalent to the > operator in other programming languages. Data type mismatch
GREATER_THAN function should only receive numeric values, so, you need to ensure that the data types are the same before using the GREATER_THAN function. GREATER_THAN(10, 20) # returns False
GREATER_THAN(10, 10) # returns False
GREATER_THAN(20, 10) # returns TrueLESS_THAN_OR_EQUALS_TO
Similar to the GREATER_THAN_OR_EQUALS_TO function, the LESS_THAN_OR_EQUALS_TO function is used to validate if a value is less than or equals to another value. The LESS_THAN_OR_EQUALS_TO function receives two parameters:
LESS_THAN_OR_EQUALS_TO(value, compare)valueis the first value you want to compare.compareis the second value you want to compare.
Note
LESS_THAN_OR_EQUALS_TO function is equivalent to the <= operator in other programming languages. Data type mismatch
LESS_THAN_OR_EQUALS_TO function should only receive numeric values, so, you need to ensure that the data types are the same before using the LESS_THAN_OR_EQUALS_TO function. LESS_THAN_OR_EQUALS_TO(10, 20) # returns True
LESS_THAN_OR_EQUALS_TO(10, 10) # returns True
LESS_THAN_OR_EQUALS_TO(20, 10) # returns FalseLESS_THAN
Similar to the GREATER_THAN function, the LESS_THAN function is used to validate if a value is less than another value. The LESS_THAN function receives two parameters:
LESS_THAN(value, compare)valueis the first value you want to compare.compareis the second value you want to compare.
Note
LESS_THAN function is equivalent to the < operator in other programming languages. Data type mismatch
LESS_THAN function should only receive numeric values, so, you need to ensure that the data types are the same before using the LESS_THAN function. LESS_THAN(10, 20) # returns True
LESS_THAN(10, 10) # returns False
LESS_THAN(20, 10) # returns FalseDIFFERENT
Similar to the COMPARE function, the DIFFERENT function is used to validate if two values are different. The DIFFERENT function receives two parameters:
DIFFERENT(value1, value2)value1is the first value you want to compare.value2is the second value you want to compare.
Note
DIFFERENT function is equivalent to the != operator in other programming languages. DIFFERENT(10, 20) # returns True
DIFFERENT(10, 10) # returns False
DIFFERENT("Hello", "World") # returns True
DIFFERENT("Hello", "Hello") # returns False
DIFFERENT("10", 10) # returns TrueINSIDE_RANGE
Sometimes, we need to validate if a value is inside a range. For this, you can use the INSIDE_RANGE function to validate if a value is inside a range. The INSIDE_RANGE function receives three parameters:
INSIDE_RANGE(value, min, max)valueis the value you want to validate if it is inside a range.minis the minimum value of the range.maxis the maximum value of the range.
Note
INSIDE_RANGE function is equivalent to the value >= min && value <= max operator in other programming languages. In Python, the equivalent is min <= value <= maxData type mismatch
INSIDE_RANGE function should only receive numeric values, so, you need to ensure that the data types are the same before using the INSIDE_RANGE function. INSIDE_RANGE(10, 5, 20) # returns True
INSIDE_RANGE(10, 10, 20) # returns True
INSIDE_RANGE(2, 5, 10) # returns FalseOUTSIDE_RANGE
Similar to the INSIDE_RANGE function, the OUTSIDE_RANGE function is used to validate if a value is outside a range. The OUTSIDE_RANGE function receives three parameters:
OUTSIDE_RANGE(value, min, max)valueis the value you want to validate if it is outside a range.minis the minimum value of the range.maxis the maximum value of the range.
Note
OUTSIDE_RANGE function is equivalent to the value < min || value > max operator in other programming languages. In Python, the equivalent is value < min or value > maxData type mismatch
OUTSIDE_RANGE function should only receive numeric values, so, you need to ensure that the data types are the same before using the OUTSIDE_RANGE function. Alternative
OUTSIDE_RANGE function is equivalent to the combination of NOT and INSIDE_RANGE functions, so, you can use the NOT and INSIDE_RANGE functions to achieve the same result. COMPARE(
NOT(INSIDE_RANGE(5, 0, 5)),
OUTSIDE_RANGE(5, 0, 5)
) # returns TrueOUTSIDE_RANGE(10, 5, 20) # returns False
OUTSIDE_RANGE(10, 10, 20) # returns False
OUTSIDE_RANGE(2, 5, 10) # returns TrueArithmetic operations
SUM
Not all the operations are comparisons, sometimes you need to perform a mathematical operation. For this, you can use the SUM function to perform the sum operation. The SUM function receives many parameters as you want:
SUM(value1, value2, ..., valueN)value1is the first value you want to sum.value2is the second value you want to sum.valueNis the N value you want to sum....refers to the rest of the values you want to sum.
Note
SUM function is equivalent to the + arithmetic operator in other programming languages. Data type mismatch
SUM function should only receive numeric values, so, you need to ensure that the data types are the same before using the SUM function. SUM(10, 20) # returns 30
SUM(10, 10, 10) # returns 30
SUM(10, 10, 10, 10) # returns 40SUBSTRACT
Similar to the SUM function, the SUBSTRACT function is used to perform the substraction operation. The SUBSTRACT function receives many parameters as you want:
SUBSTRACT(value1, value2, ..., valueN)value1is the first value you want to substract.value2is the second value you want to substract.valueNis the N value you want to substract....refers to the rest of the values you want to substract.
Note
SUBSTRACT function is equivalent to the - arithmetic operator in other programming languages. Data type mismatch
SUBSTRACT function should only receive numeric values, so, you need to ensure that the data types are the same before using the SUBSTRACT function. SUBSTRACT(10, 20) # returns -10
SUBSTRACT(10, 10, 10) # returns -10
SUBSTRACT(10, 10, 10, 10) # returns -20MULTIPLY
Similar to the SUM function, the MULTIPLY function is used to perform the multiplication operation. The MULTIPLY function receives many parameters as you want:
MULTIPLY(value1, value2, ..., valueN)value1is the first value you want to multiply.value2is the second value you want to multiply.valueNis the N value you want to multiply....refers to the rest of the values you want to multiply.
Note
MULTIPLY function is equivalent to the * arithmetic operator in other programming languages. Data type mismatch
MULTIPLY function should only receive numeric values, so, you need to ensure that the data types are the same before using the MULTIPLY function. MULTIPLY(10, 20) # returns 200
MULTIPLY(10, 10, 10) # returns 1000
MULTIPLY(10, 10, 10, 10) # returns 10000DIVIDE
Similar to the SUM function, the DIVIDE function is used to perform the division operation. The DIVIDE function receives many parameters as you want:
DIVIDE(value1, value2, ..., valueN)value1is the first value you want to divide.value2is the second value you want to divide.valueNis the N value you want to divide....refers to the rest of the values you want to divide.
Note
DIVIDE function is equivalent to the / arithmetic operator in other programming languages. Data type mismatch
DIVIDE function should only receive numeric values, so, you need to ensure that the data types are the same before using the DIVIDE function. DIVIDE(10, 20) # returns 0.5
DIVIDE(10, 10, 10) # returns 0.1
DIVIDE(10, 10, 10, 10) # returns 0.01CEIL
Converts the float value to the next integer value. The CEIL function receives one parameter:
CEIL(value)valueis the value you want to convert to the next integer value.
Data type mismatch
CEIL function should only receive numeric values, so, you need to ensure that the data types are the same before using the CEIL function. CEIL(10.1) # returns 11
CEIL(10.9) # returns 11
CEIL(10) # returns 10FLOOR
Similar to the CEIL function, the FLOOR function is used to convert the float value to the previous integer value. The FLOOR function receives one parameter:
FLOOR(value)valueis the value you want to convert to the previous integer value.
Data type mismatch
FLOOR function should only receive numeric values, so, you need to ensure that the data types are the same before using the FLOOR function. FLOOR(10.1) # returns 10
FLOOR(10.9) # returns 10
FLOOR(10) # returns 10ROUND
Similar to the CEIL and FLOOR functions, the ROUND function is used to convert the float value to the nearest integer value. The ROUND function receives one parameter:
ROUND(value)valueis the value you want to convert to the nearest integer value.
Data type mismatch
ROUND function should only receive numeric values, so, you need to ensure that the data types are the same before using the ROUND function. ROUND(10.1) # returns 10
ROUND(10.9) # returns 11
ROUND(10) # returns 10SQRT
Another common operation is the square root, for this, you can use the SQRT function to calculate the square root of a value. The SQRT function receives one parameter:
SQRT(value)valueis the value you want to calculate the square root.
Note
SQRT function is equivalent to the sqrt function in other programming languages. Data type mismatch
SQRT function should only receive numeric values, so, you need to ensure that the data types are the same before using the SQRT function. SQRT(9) # returns 3
SQRT(16) # returns 4
SQRT(25) # returns 5Data transformers
UNIX_TO_STR
UNIX_TO_STR allows conversion from UNIX timestamp (Defined in seconds) to a string value. This function receives at least two parameters, and a maximum os 3 parameters:
UNIX_TO_STR(unix, format, timezone?)unixis theUNIXtimestamp you want to convert to astringvalue.formatis the format of thestringvalue you want to return. You can get the format from the Python documentation.timezoneis the timezone you want to use to convert theUNIXtimestamp to astringvalue. Thetimezoneparameter is optional, by default is defined asUTC, but you can change it to any timezone you want, check the list of available timezones in the This link.
UNIX_TO_STR(1634025600, "%Y-%m-%d %H:%M:%S") # returns "2021-10-12 00:00:00"
UNIX_TO_STR(
NOW(),
"%H:%M:%S",
"America/Panama",
) # returns the current time in PanamaTO_BOOL
TO_BOOL allows conversion from string or int to a boolean value. This function receives one parameter:
TO_BOOL(value)valueis the value you want to convert to abooleanvalue.
TO_BOOL("True") # returns True
TO_BOOL("False") # returns False
TO_BOOL(1) # returns True
TO_BOOL(0) # returns FalseTO_STR
TO_STR allows conversion from int, float, or boolean to a string value. This function receives one parameter:
TO_STR(value)valueis the value you want to convert to astringvalue.
TO_STR(10) # returns "10"
TO_STR(3.1416) # returns "3.1416"
TO_STR(True) # returns "True"
TO_STR(None) # returns "None"TO_INT
TO_INT allows conversion from string, float, or boolean to an int value. This function receives one parameter:
TO_INT(value)valueis the value you want to convert to anintvalue.
TO_INT("10") # returns 10
TO_INT(3.1416) # returns 3
TO_INT(True) # returns 1
TO_INT(False) # returns 0TO_FLOAT
TO_FLOAT allows conversion from string, int, or boolean to a float value. This function receives one parameter:
TO_FLOAT(value)valueis the value you want to convert to afloatvalue.
TO_FLOAT("10") # returns 10.0
TO_FLOAT(10) # returns 10.0
TO_FLOAT(True) # returns 1.0
TO_FLOAT(False) # returns 0.0HEX_TO_STR
HEX_TO_STR allows conversion from hex to a string value. This function receives one parameter:
HEX_TO_STR(value)valueis the value you want to convert to astringvalue. This hex value should not have the0xprefix.
HEX_TO_STR("48656c6c6f") # returns "Hello"STR_TO_HEX
STR_TO_HEX allows conversion from string to a hex value. This function receives one parameter:
STR_TO_HEX(value)valueis the value you want to convert to ahexvalue.
Note
0x prefix. STR_TO_HEX("Hello") # returns "48656c6c6f"HEX_TO_INT
HEX_TO_INT allows conversion from hex to an int value. This function receives one parameter:
HEX_TO_INT(value)valueis the value you want to convert to anintvalue. This hex value may or may not have the0xprefix.
HEX_TO_INT("10") # returns 16
HEX_TO_INT("0x10") # returns 16INT_TO_HEX
INT_TO_HEX allows conversion from int to a hex value. This function receives one parameter:
INT_TO_HEX(value)valueis the value you want to convert to ahexvalue.
Note
0x prefix. INT_TO_HEX(16) # returns "10"String operations
CONCAT
What happens when you need to join two or more strings? For this, you can use the CONCAT function to join two or more strings. The CONCAT function receives many parameters as you want:
CONCAT(value1, value2, ..., valueN)value1is the first value you want to join.value2is the second value you want to join.valueNis the N value you want to join....refers to the rest of the values you want to join.
Note
CONCAT function is equivalent to the + or & operator in other programming languages. Data type mismatch
CONCAT function should only receive string values, so, you need to ensure that the data types are the same before using the CONCAT function. CONCAT("Hello", " ", "World") # returns "Hello World"
CONCAT("Hello", " ", "World", "!") # returns "Hello World!"SUBSTRING
In some scenarios, you need to extract a substring from a string. For this, you can use the SUBSTRING function to extract a substring from a string. The SUBSTRING function receives at least two parameters and a maximum of three parameters:
SUBSTRING(value, start, end?)valueis the string you want to extract the substring.startis the start index of the substring.endis the end index of the substring. Theendparameter is optional, by default is defined as the length of the string, but you can change it to any index you want.
Note
SUBSTRING function is equivalent to the substring function in other programming languages. Data type mismatch
SUBSTRING function should only receive string values, so, you need to ensure that the data types are the same before using the SUBSTRING function. SUBSTRING("Hello, World!", 0, 5) # returns "Hello"
SUBSTRING("Hello, World!", 7, 12) # returns "World"CONTAINS
You have SUBSTRING to extract an specific part of a string, but what happens when you need to validate if a string contains another string? For this, you can use the CONTAINS function to validate if a string contains another string. The CONTAINS function receives two parameters:
CONTAINS(value, search)valueis the string you want to validate if it contains another string.searchis the string you want to validate if it is present in the value.
Note
CONTAINS function is equivalent to the in or contains operator in other programming languages. Be careful!
CONTAINS function is case sensitive, so, Hello is different from hello. Data type mismatch
CONTAINS function should only receive string values, so, you need to ensure that the data types are the same before using the CONTAINS function. CONTAINS("Hello, World!", "Hello") # returns True
CONTAINS("Hello, World!", "World") # returns True
CONTAINS("Hello, World!", "Layrz") # returns FalseSTARTS_WITH
Similar to the CONTAINS function, the STARTS_WITH function is used to validate if a string starts with another string. The STARTS_WITH function receives two parameters:
STARTS_WITH(value, search)valueis the string you want to validate if it starts with another string.searchis the string you want to validate if it is present at the start of the value.
Note
STARTS_WITH function is equivalent to the startswith function in other programming languages. Be careful!
STARTS_WITH function is case sensitive, so, Hello is different from hello. Data type mismatch
STARTS_WITH function should only receive string values, so, you need to ensure that the data types are the same before using the STARTS_WITH function. STARTS_WITH("Hello, World!", "Hello") # returns True
STARTS_WITH("Hello, World!", "World") # returns FalseENDS_WITH
Similar to the CONTAINS function, the ENDS_WITH function is used to validate if a string ends with another string. The ENDS_WITH function receives two parameters:
ENDS_WITH(value, search)valueis the string you want to validate if it ends with another string.searchis the string you want to validate if it is present at the end of the value.
Note
ENDS_WITH function is equivalent to the endswith function in other programming languages. Be careful!
ENDS_WITH function is case sensitive, so, Hello is different from hello. Data type mismatch
ENDS_WITH function should only receive string values, so, you need to ensure that the data types are the same before using the ENDS_WITH function. ENDS_WITH("Hello, World!", "Hello") # returns False
ENDS_WITH("Hello, World!", "World") # returns TrueREGEX
String operations are relatively simple, but sometimes you need to perform a complex operation, for example, you want to validate if the string contains a number, for this, you can use the REGEX function to perform a regular expression operation. The REGEX function receives two parameters:
REGEX(value, pattern)valueis the string you want to validate if it matches the pattern.patternis the regular expression pattern you want to use to validate the value. To more information about regular expressions, you can check the Python documentation.
Note
REGEX function is equivalent to the re module in Python or RegExp in Dart. Data type mismatch
REGEX function should only receive string values, so, you need to ensure that the data types are the same before using the REGEX function. REGEX("Hello!", "\d") # returns False due to the string does not contain a number
REGEX("Hello 123!", "\d") # returns True due to the string contains a numberRandomizers
RANDOM
What happens if you need to demo a sensor, for the Grid system of Layrz, or chart demo purposes? For this, you can use the RANDOM function to generate a random value. The RANDOM function receives two parameters:
RANDOM(min, max)minis the minimum value of the random value.maxis the maximum value of the random value.
Note
RANDOM function is equivalent to the random function in other programming languages. Data type mismatch
RANDOM function should only receive numeric values, so, you need to ensure that the data types are the same before using the RANDOM function. Also, the return value of the RANDOM function is a float value.
RANDOM(0, 100) # returns a random value between 0 and 100
RANDOM(0, 1) # returns a random value between 0 and 1RANDOM_INT
Similar to the RANDOM function, the RANDOM_INT function is used to generate a random integer value. The RANDOM_INT function receives two parameters:
RANDOM_INT(min, max)minis the minimum value of the random integer value.maxis the maximum value of the random integer value.
Note
RANDOM_INT function is equivalent to the randint function in other programming languages. Data type mismatch
RANDOM_INT function should only receive numeric values, so, you need to ensure that the data types are the same before using the RANDOM_INT function. Also, the return value of the RANDOM_INT function is an integer value.
RANDOM_INT(0, 100) # returns a random integer value between 0 and 100
RANDOM_INT(0, 1) # returns a random integer value between 0 and 1Utilities
CONSTANT
Not all the values are dynamic, sometimes you need to use a constant value in your formula. For this, you can use the CONSTANT function to use a constant value in your formula. The CONSTANT function receives one parameter:
CONSTANT(value)valueis the constant value you want to use in your formula.
CONSTANT(10) # this value is an integer
CONSTANT(3.1416) # this value is a float
CONSTANT("Hello, World!") # this value is a string
CONSTANT(True) # this value is a booleanNOW
In some scenarios, you need the current date and time. For this, you can use the NOW function to get the current date and time. The NOW function does not receive any parameter.
NOW() # returns the current date and time in `UNIX` timestampGET_TIME_DIFFERENCE
In the real world, the data stream from the devices is not constant, or you need to calculate some predictions or average based on time, to do that, GET_TIME_DIFFERENCE allows you to calculate the time difference between two UNIX timestamps. The GET_TIME_DIFFERENCE does not receive any parameter.
GET_TIME_DIFFERENCE() # returns the difference in seconds between the previous and the current messagePRIMARY_DEVICE
Due to the Layrz multi-device mode of the assets, you may need to know the primary device ident to use it in your formulas. For this, you can use the PRIMARY_DEVICE function to get the primary device ident. The PRIMARY_DEVICE function does not receive any parameter.
PRIMARY_DEVICE() # returns the primary device identVERSION
This function is only an utility function, ideally you shouldn't use it in your formulas, but if you need to know the version of the Layrz formula engine, you can use the VERSION function. The VERSION function does not receive any parameter.
VERSION() # returns the version of the Layrz formula engineLook at this example
VERSION() # Returns LCL 1.0.0 - Layrz SDK Runtime