Example Formulas
Example | Result | Descriptions | |
Mathematical Expression | 1+1*2 | 3 | |
2/2+1 | 2 | ||
(2+2)/2 | 2 | ||
String operations | "a" + 1 | a1 | |
"a" + "b" | ab | ||
With Functions | base64Encode("a") | YQ== | |
With device attributes | "${firstName}" + " " + "${lastName}" | If firstName is Tyler If lastName is Griffith result = Tyler Griffith | |
${a} + ${b} | if a is 1 if b is 2 result =3 | ||
"${a}" + "${b}" | if a is 1 if b is 2 result =12 | ||
Self referencing formula | ${a} + 1 | If the old value of a is 1, then the new value is 2 | This can be used count the number of times an object got updated |
Conditional Logic | (${a} == 1)? "Success" : "Failure" | ||
(${a} == 1 || ${b} == 1)? "Success" : "Failure" | |||
(${a} == 1 && ${b} == 1)? "Success" : "Failure" | |||
equals("${a}", "true") ? "Success" : "Failure" | |||
Conditional Actions | executeAction(${updateAvailable} == 1 ? "<actionId>" : "") | Here actionId may represent the deviceId of a Global Action | |
Old and new value of an attribute | executeAction($OLD{a} != ${a} ? "<actionId>" : "<anotherActionId>") | executing an action if the value of "a" has changed otherwise executing another action | |
Counting the number of times a filed is updated, reseting counter to zero after a specified count value | $OLD{temperature} != ${temperatures} ? ${a}+1: (${a} > 20 ? 0: ${a}) | ||
Formulas with Timestamps or Longs | ${last_mqtt_timestamp}l / 1000 or timestamp_to_datetime(${last_mqtt_timestamp}l, "MM/dd/yyyy hh:mm:ss a") | Add L after {} | |
Using attributes from a referenced object | ${referenceObjectAttributeJSONKey__r.attributeJsonKey} Ex ${token__r.refresh_token} |
Supported Functions
Function | Description | Arguments | |
1 | toString(Object arg) | Converts to string | argument can be a literal or a field name in a object type |
2 | timestamp_to_datetime(long ts, String format, String timeZone) | returns String representation of date time in the specifid format | 1. 13 digit timestamp 2. Format - it can be any java supported date time format string 3. timeZone - Examples of timeZone strings - "PST", "America/Los_Angeles", "GMT-8:00" |
3 | timestamp_to_datetime(long ts, String format) | returns String representation of date time in the specifid format in GMT timezone | 1. 13 digit timestamp 2. Format - it can be any java supported date time format string |
4 | timestamp_to_datetime(long ts) | returns String representation of date time in the "MM/dd/yyyy HH:mm:ss" format in GMT timezone | 1. 13 digit timestamp |
5 | epoch_to_datetime(long ts, String format, String timeZone) | returns String representation of date time in the specifid format | 1. 10 digit timestamp 2. Format - it can be any java supported date time format string 3. timeZone - Examples of timeZOne strings - "PST", "America/Los_Angeles", "GMT-8:00" |
6 | epoch_to_datetime(long ts, String format) | returns String representation of date time in the specifid format in GMT timezone | 1. 10 digit timestamp 2. Format - it can be any java supported date time format string |
7 | epoch_to_datetime(long ts) | returns String representation of date time in the "MM/dd/yyyy HH:mm:ss" format in GMT timezone | 1. 13 digit timestamp |
8 | epoch() | returns current epoch (miliseconds) | |
9 | now() | returns current time miliseconds | |
10 | today(String format, String timeZone) | returns Today's date in String in specified format | 1. Format - it can be any java supported date time format string 2. timeZone - Examples of timeZone strings - "PST", "America/Los_Angeles", "GMT-8:00" |
11 | today(String format) | returns Today's date in String in specified format. Time zone defaults to GMT | |
12 | base64Encode(String str) | returns base64 encoding of any string | |
13 | base64Decode(String base64Str) | returns decoded string | |
14 | equals(String a, String b) | returns Boolean - true / false | |
15 | executeAction(String actionId) | executes an action an action can be - 1. a mqtt publish to a topic with place holders with a json payload with placeholders 2. HTTP post with a json body with placeholders 3. invoking a lambda function to perform any custom task | actionId - it is the id of the action to be performed |
16 | All java.lang.Math static methods can be used in formula fields | ||
17 | Render Colors | $ {status} == 0 ? "renderColor(\"red\")" : "renderColor(\"green\")" | |
18 | Render a Link | "renderLink(\"${scent_machine__r.name}\", \"/deviceDetails?device=$ {scent_machine} \")" | Here is a sample formula that renders a link to the detail page of the reference field |
19 | startsWith(String str1, String str2) | -returns true if str1 starts with str2 | |
20 | endsWith(String str1, String str2) | -returns true if str1 ends with str2 | |
21 | contains(String str1, String str2) | -returns true if str1 contains str2 | |
22 | equalsIgnoreCase(String str1, String str2) | -returns true if str1 and str2 are identical while ignoring the case | |
23 | toLower(String str) | ||
24 | toUpper(String str) | ||
25 | subString(String str, int beginIndex) | ||
26 | subString(String str, int beginIndex, int endIndex) |
Using References and Account Attributes
Account Field | JSON Key | |
1 | Account Id | ${accountId} |
2 | Name | ${accountId__r.name} |
3 | Parent Id | ${accountId__r.parent_id} |
4 | Address | ${accountId__r.address} |
5 | City | ${accountId__r.city} |
6 | State | ${accountId__r.state} |
7 | Country | ${accountId__r.country} |
8 | Postal Code | ${accountId__r.postal_code} |
9 | Phone Number | ${accountIdr.phone_number} |