left() and right() string functions
There are left()
and right()
type of methods within expressions. You can find documentation on the available general purpose functions in the documentation:
That being said, you can’t access device state directly in expressions, so you would either need to temporarily store the device state in a variable for use in your expression or you could use IF Conditions (potentially along with the expressions for advanced concatenation).
Array Basics
Sort of. We don’t have an official ‘array’ or ‘object’ variable type for storing values, but they can be used within expressions. And if you want to store an array/object in a variable you can use the parseJson()
and stringify()
methods mentioned in the documentation above to encode it into a string.
You can find some examples that use these methods for encoding array data into a string variable in the following posts:
Conditional Expressions (Ternary Operator)
You can also use ternary operators for conditions. The format is:
condition ? trueExpression : falseExpression
So a simple example might be:
myNumber = 12
myNumber >= 10 ? "It's 10 or bigger" : "It's less than 10"
Example with String Concatenation
Combining some of the above, you might have something like:
- Set variable $truckBlockHeater to Truck Block Heater - switch.
- Set variable $shopThermostat to Shop Thermostat - mode.
- Set variable $reportString to expression:
truckBlockHeater = "on"
shopThermostat = "heat"
tempString = ""
equalText(truckBlockHeater, "on") ? tempString = concat(tempString, "The truck heater is on;") : ""
equalText(shopThermostat, "off") ? tempString = concat(tempString, "The thermostat is off;") : ""
tempString
In this example, I’m using ternary operators to check the string with an equalText()
method on the left side of the ternary. Then only if it’s true, I concat()
the existing tempString along with whatever addition I want. In the else block, I’m just using an empty string (since we’re not actually assigning it, it’s basically just doing nothing).
A key part here is to make sure to return the tempString
as the last line. The way the expression system works is it returns whatever the last line is. So we want to make sure the fully contatenated string with the expression variable is the last thing we return.
You could of course have other logic in here like returning a default string (instead of an empty string) if no strings were concatenated on.
Array Example
You could do this with an array too, but I didn’t see how it would help this particular situation as you would need to build up the array anyway. Here’s some array type features though in case you had something else in mind and you were looking for inspiration.
truckBlockHeater = "on"
shopThermostat = "off"
myArray = []
equalText(truckBlockHeater, "on") ? myArray = concat(myArray, ["The truck heater is on"]) : ""
equalText(shopThermostat, "off") ? myArray = concat(myArray, ["The thermostat is off"]) : ""
join(myArray, "; ")
The key difference in this approach, is we use the concat()
method to merge two arrays together. So you have to make sure both inputs to the method are arrays – basically just wrap the string on the right side in an array. This is the closest approximation to an array ‘push’ type of concept.
Then the last piece that’s near is using the join()
method, but one thing to keep in mind is this will only add the join character/string between items in the array. So if it’s a single item in the array, there’s no ;
at all, if there’s 2 items, there’s just a single ;
between them, and so on. You may have to adapt or use a different approach if you wanted the ;
ending each line, but the join()
is cool for a lot of use cases, so it’s worth mentioning.
Truck is on
Truck is on; Heater is off
Truck is on; Heater is off; Light is on
Also note that I just used the join()
method as the last line here since the result of that method is the output we wanted. Writing it to a local tempString
type of variable would have been superfluous here, so we just returned the joined string directly.