Functions
We develop some functions to handle the most common operations in LCL. Here is a list of the available functions:
Be careful!
All of the functions validate if some of the arguments are 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)
search
is the name of the parameter you want to extract from the message.fallback
is 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!
This formula only works when the parameter comes from the primary device. For asset cluster mode (Aka, an asset with other assets associated) or static asset, this formula does not work
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)
sensor
is the name of the sensor you want to extract the value.fallback
is the value that will be returned if the sensor is not found.
GET_SENSOR("my.sensor", "fallback value")
Be careful!
Layrz has the possibility to change the execution order using the iteration cycle, so, if you want to use a second level of sensors (Aka, a sensor that uses another sensor), you may use a 2nd iteration cycle to get the value of the sensor. This iteration cycle is defined in the sensor configuration, through your asset or global sensor.
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)
field
is the name of the custom field you want to extract the value.fallback
is the value that will be returned if the custom field is not found.
Be careful!
The name of the custom field is case sentive, so, you need to use the exact name of the custom field. It's not the same My custom field
than my custom field
GET_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!
The return value of the GET_DISTANCE_TRAVELED
function is in meters and is a float value.
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)
sensor
is the name of the sensor you want to extract the previous value.fallback
is the value that will be returned if the sensor is not found.
Be careful!
The 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)
search
is the name of the parameter you want to validate if it is present in the message.
Be careful!
The 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 False
IS_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)
sensor
is the name of the sensor you want to validate if it is present in the asset.
Be careful!
The 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 False
IS_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)
sensor
is the name of the sensor you want to validate if it returns aNone
value.
Be careful!
The IS_NONE
function returns a boolean value, so, you can use the result in a conditional statement.
IS_NONE("my.sensor") # returns True or False
Logical 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!
All of the functions in this section return a boolean value, so, you can use the result in a conditional statement.
Data type mismatch
All of the functions in this section should only receive the same data types, so, you need to ensure that the data types are the same before using the functions in this section.
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)
value1
is the first value you want to compare.value2
is the second value you want to compare.
Note
The 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 mismatch
OR_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)
value1
is the first value you want to compare.value2
is the second value you want to compare.valueN
is the N value you want to compare....
refers to the rest of the values you want to compare.
Note
The OR_OPERATOR
function is equivalent to the ||
or or
operator in other programming languages.
Data type mismatch
The 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 False
AND_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)
value1
is the first value you want to compare.value2
is the second value you want to compare.valueN
is the N value you want to compare....
refers to the rest of the values you want to compare.
Note
The AND_OPERATOR
function is equivalent to the &&
or and
operator in other programming languages.
Data type mismatch
The 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 True
NOT
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)
value
is the value you want to negate.
Note
The NOT
function is equivalent to the !
or not
operator in other programming languages.
Data type mismatch
The 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 True
IF
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)
condition
is the boolean value that you want to evaluate.valueIfTrue
is the value that will be returned if the condition is true.valueIfFalse
is the value that will be returned if the condition is false.
Note
If you are a programmer, you are familiar with the 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 cold
if
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)
value
is the first value you want to compare.compare
is the second value you want to compare.
Note
The GREATER_THAN_OR_EQUALS_TO
function is equivalent to the >=
operator in other programming languages.
Data type mismatch
The 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 True
GREATER_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)
value
is the first value you want to compare.compare
is the second value you want to compare.
Note
The GREATER_THAN
function is equivalent to the >
operator in other programming languages.
Data type mismatch
The 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 True
LESS_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)
value
is the first value you want to compare.compare
is the second value you want to compare.
Note
The LESS_THAN_OR_EQUALS_TO
function is equivalent to the <=
operator in other programming languages.
Data type mismatch
The 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 False
LESS_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)
value
is the first value you want to compare.compare
is the second value you want to compare.
Note
The LESS_THAN
function is equivalent to the <
operator in other programming languages.
Data type mismatch
The 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 False
DIFFERENT
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)
value1
is the first value you want to compare.value2
is the second value you want to compare.
Note
The 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 True
INSIDE_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)
value
is the value you want to validate if it is inside a range.min
is the minimum value of the range.max
is the maximum value of the range.
Note
The INSIDE_RANGE
function is equivalent to the value >= min && value <= max
operator in other programming languages. In Python, the equivalent is min <= value <= max
Data type mismatch
The 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 False
OUTSIDE_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)
value
is the value you want to validate if it is outside a range.min
is the minimum value of the range.max
is the maximum value of the range.
Note
The 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 > max
Data type mismatch
The 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
The 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 True
OUTSIDE_RANGE(10, 5, 20) # returns False
OUTSIDE_RANGE(10, 10, 20) # returns False
OUTSIDE_RANGE(2, 5, 10) # returns True
Arithmetic 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)
value1
is the first value you want to sum.value2
is the second value you want to sum.valueN
is the N value you want to sum....
refers to the rest of the values you want to sum.
Note
The SUM
function is equivalent to the +
arithmetic operator in other programming languages.
Data type mismatch
The 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 40
SUBSTRACT
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)
value1
is the first value you want to substract.value2
is the second value you want to substract.valueN
is the N value you want to substract....
refers to the rest of the values you want to substract.
Note
The SUBSTRACT
function is equivalent to the -
arithmetic operator in other programming languages.
Data type mismatch
The 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 -20
MULTIPLY
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)
value1
is the first value you want to multiply.value2
is the second value you want to multiply.valueN
is the N value you want to multiply....
refers to the rest of the values you want to multiply.
Note
The MULTIPLY
function is equivalent to the *
arithmetic operator in other programming languages.
Data type mismatch
The 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 10000
DIVIDE
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)
value1
is the first value you want to divide.value2
is the second value you want to divide.valueN
is the N value you want to divide....
refers to the rest of the values you want to divide.
Note
The DIVIDE
function is equivalent to the /
arithmetic operator in other programming languages.
Data type mismatch
The 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.01
CEIL
Converts the float value to the next integer value. The CEIL
function receives one parameter:
CEIL(value)
value
is the value you want to convert to the next integer value.
Data type mismatch
The 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 10
FLOOR
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)
value
is the value you want to convert to the previous integer value.
Data type mismatch
The 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 10
ROUND
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)
value
is the value you want to convert to the nearest integer value.
Data type mismatch
The 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 10
SQRT
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)
value
is the value you want to calculate the square root.
Note
The SQRT
function is equivalent to the sqrt
function in other programming languages.
Data type mismatch
The 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 5
Data 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?)
unix
is theUNIX
timestamp you want to convert to astring
value.format
is the format of thestring
value you want to return. You can get the format from the Python documentation.timezone
is the timezone you want to use to convert theUNIX
timestamp to astring
value. Thetimezone
parameter 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 Panama
TO_BOOL
TO_BOOL
allows conversion from string
or int
to a boolean
value. This function receives one parameter:
TO_BOOL(value)
value
is the value you want to convert to aboolean
value.
TO_BOOL("True") # returns True
TO_BOOL("False") # returns False
TO_BOOL(1) # returns True
TO_BOOL(0) # returns False
TO_STR
TO_STR
allows conversion from int
, float
, or boolean
to a string
value. This function receives one parameter:
TO_STR(value)
value
is the value you want to convert to astring
value.
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)
value
is the value you want to convert to anint
value.
TO_INT("10") # returns 10
TO_INT(3.1416) # returns 3
TO_INT(True) # returns 1
TO_INT(False) # returns 0
TO_FLOAT
TO_FLOAT
allows conversion from string
, int
, or boolean
to a float
value. This function receives one parameter:
TO_FLOAT(value)
value
is the value you want to convert to afloat
value.
TO_FLOAT("10") # returns 10.0
TO_FLOAT(10) # returns 10.0
TO_FLOAT(True) # returns 1.0
TO_FLOAT(False) # returns 0.0
HEX_TO_STR
HEX_TO_STR
allows conversion from hex
to a string
value. This function receives one parameter:
HEX_TO_STR(value)
value
is the value you want to convert to astring
value. This hex value should not have the0x
prefix.
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)
value
is the value you want to convert to ahex
value.
Note
The return value does not have the 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)
value
is the value you want to convert to anint
value. This hex value may or may not have the0x
prefix.
HEX_TO_INT("10") # returns 16
HEX_TO_INT("0x10") # returns 16
INT_TO_HEX
INT_TO_HEX
allows conversion from int
to a hex
value. This function receives one parameter:
INT_TO_HEX(value)
value
is the value you want to convert to ahex
value.
Note
The return value does not have the 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)
value1
is the first value you want to join.value2
is the second value you want to join.valueN
is the N value you want to join....
refers to the rest of the values you want to join.
Note
The CONCAT
function is equivalent to the +
or &
operator in other programming languages.
Data type mismatch
The 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?)
value
is the string you want to extract the substring.start
is the start index of the substring.end
is the end index of the substring. Theend
parameter is optional, by default is defined as the length of the string, but you can change it to any index you want.
Note
The SUBSTRING
function is equivalent to the substring
function in other programming languages.
Data type mismatch
The 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)
value
is the string you want to validate if it contains another string.search
is the string you want to validate if it is present in the value.
Note
The CONTAINS
function is equivalent to the in
or contains
operator in other programming languages.
Be careful!
The CONTAINS
function is case sensitive, so, Hello
is different from hello
.
Data type mismatch
The 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 False
STARTS_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)
value
is the string you want to validate if it starts with another string.search
is the string you want to validate if it is present at the start of the value.
Note
The STARTS_WITH
function is equivalent to the startswith
function in other programming languages.
Be careful!
The STARTS_WITH
function is case sensitive, so, Hello
is different from hello
.
Data type mismatch
The 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 False
ENDS_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)
value
is the string you want to validate if it ends with another string.search
is the string you want to validate if it is present at the end of the value.
Note
The ENDS_WITH
function is equivalent to the endswith
function in other programming languages.
Be careful!
The ENDS_WITH
function is case sensitive, so, Hello
is different from hello
.
Data type mismatch
The 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 True
REGEX
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)
value
is the string you want to validate if it matches the pattern.pattern
is 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
The REGEX
function is equivalent to the re
module in Python.
Data type mismatch
The 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 number
Randomizers
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)
min
is the minimum value of the random value.max
is the maximum value of the random value.
Note
The RANDOM
function is equivalent to the random
function in other programming languages.
Data type mismatch
The 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 1
RANDOM_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)
min
is the minimum value of the random integer value.max
is the maximum value of the random integer value.
Note
The RANDOM_INT
function is equivalent to the randint
function in other programming languages.
Data type mismatch
The 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 1
Utilities
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)
value
is 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 boolean
NOW
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` timestamp
GET_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 message
PRIMARY_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 ident
VERSION
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 engine
Look at this example
VERSION() # Returns LCL 1.0.0 - Layrz SDK Runtime