String operations with Calculated Properties

Calculated properties aren't just about numbers

Joining strings

Calculated Properties support the full range of DevicePilot expressions so they can work on (and return) strings as well as numbers. For example if your devices have a building and a floor, then you can create a CP to join these together so you can GROUP BY the unique combination. 


building + ", " + floor

What happens if a particular device doesn’t have building or floor defined? In that case the CP will never be evaluated (understand why here) so building_and_floor will never be defined either, which is probably what you want.

Creating complex strings by inspecting properties

You can do quite complex conditional property extraction and string concatenation using the ternary "?" if-then-else operator:


"{metadata.type}" == "gateway" ? "gateway" :
device_type == "weather" ? "weather" :
"{metadata.app_id}" + " (" +
(L1 != null ? "sound," : "") +
(PM1 != null ? "dust," : "") +
(humidity != null ? "humidity," : "") +
(temperature != null ? "temp," : "") + ")"

The above produces a capabilities property for each device with values such as "dust sensor (dust, humidity, temp,)" which list the capabilities of each device based on the properties that it is emitting in its telemetry.

Note the use of "{escaping}" to use properties whose names contain special characters (dots in this case). 

Extracting parts of strings

We can also extract and test parts of strings:


startsWith(owner, "Walmart")


contains(battery_log, "100%"

In the last case, perhaps you’re receiving log messages with variable length like “Last Monday battery was 95% full” so you want to match for the specific string containing "100%". You can do even more complex extraction of strings with regular expression matching using the regex() function.

Learn more about Calculated Properties