Script syntax
Time-series data storage rules in IoT Platform let you process and store device data from custom topics. The rules support retrieving message content, converting data formats, processing strings, assembling JSON data, and handling binary data. This topic describes the script syntax for these rules.
Background information
To configure custom topic data storage, add time-series data storage rules that process device-reported data and store it in the IoT Platform time-series storage. For more information, see Configure Custom Topic Data Storage.
The time-series data storage rule script resembles JavaScript. You can use JavaScript syntax to edit scripts, but the parser supports only a subset of JavaScript. For details, refer to the sections below.
Write a script
Follow these steps to write a script:
-
Use the payload() function to retrieve device-submitted data and convert it to JSON format.
var data = payload("json");ImportantThe data source must be converted to JSON format, either as an array or nested JSON.
-
Define fields to filter and assign device-reported data.
-
See the "Identifiers" and "Data Types" sections for guidance on defining a field with an identifier and setting its data type.
-
You can use JSONPath and the
getOrNull()function to retrieve field values from the data. For more information, see LanguageManual UDF and getOrNull().
ImportantWhen retrieving a specific field value from device data:
-
If the identifier of a field begins with a number, JSONPath cannot be used directly. Use the getOrNull() method instead. As shown in the "Sample script" section, 2C0 cannot be accessed with
data.items.2Co.value. UsegetOrNull(data, "items", "2Co", "value")to retrieve the value10. -
If the desired field is absent:
-
The function method returns
null, and the script continues execution. -
JSONPath returns a null pointer, which halts the script.
-
You can perform further data processing or calculations based on your business requirements. The script supports various operators and functions. For more information, see Operators and Function List.
-
-
Use the transformation function convert2HotStorageData(fields, time) to convert the field data from the preceding step into the standard format.
ImportantThe fields parameter can include a maximum of 500
keys.The standard format after conversion is:
{ "key1": { "value": value1, "time": timestamp() }, "key2": { "value": value2, "time": timestamp() } }You can also merge the retrieved field data into the standard format and customize the time value for each field. For example:
{ "key1": { "value": value1, "time": 1626948134319 }, "key2": { "value": value2, "time": 1626948134000 } } -
Assign the converted data to params and call the writeHotStorage(params) function to store it.
ImportantWithin the same time-series data storage rule, the data type of the
keymust remain consistent for the correspondingvalue. Changing the data type (for example, from int32 to text) prevents new data from being written and historical data from being retrieved.You can use control statements to apply additional filter conditions for data storage. For supported control statements, see the "Control Statements" section.
Sample script
Example of device-reported data:
{
"deviceType": "CustomCategory",
"iotId": "JCp9***",
"requestId": "1626948228247",
"productKey": "a1o***",
"gmtCreate": 1626948134445,
"deviceName": "Device1",
"items": {
"Temperature": {
"value": 38,
"time": 1626948134319
},
"Humidity": {
"value": 25,
"time": 1626948134319
},
"2Co": {
"value": 10,
"time": 1626948134319
}
}
}
Corresponding script example:
-
Storing data with the current system time:
// Retrieve device-submitted data and convert to JSON format using the payload() function. var data = payload("json"); // Extract temperature and humidity values submitted by the device. var h = getOrNull(data, "items", "Humidity", "value"); var t = data.items.Temperature.value; var c = getOrNull(data, "items", "2Co", "value"); var fields = { "h": h, "t": t, "c": c } var params = convert2HotStorageData(fields, timestamp()); writeHotStorage(params); -
Storing data with a custom time:
// Retrieve device-submitted data and convert to JSON format using the payload() function. var data = payload("json"); // Extract temperature and humidity values submitted by the device. var h = getOrNull(data, "items", "Humidity", "value"); var t = data.items.Temperature.value; var c = getOrNull(data, "items", "2Co", "value"); var params = { "h": { "value": h, "time": 1626948134319 }, "t": { "value": t, "time": 1627048134319 }, "c": { "value": c, "time": 1628048134319 } } writeHotStorage(params);
Identifiers
The following keywords and reserved words cannot be used as identifiers:
- Keywords:
for,break,continue,if,else,true,false,var,new,null, andreturn. - Reserved words:
breakdo,instanceof,typeof,case,catch,finally,void,switch,while,debugger,function,this,with,default,throw,delete,in,try,as,from,classenum,extends,super,const,export,import,await,implementslet,let,private,public,interface,package,protected,static, andyield.
Data types
Constants can be null. Supported numeric constant types include decimal integer, hexadecimal integer, and floating point.
Control statements
IoT Platform supports for and if...else statements. for statements support the break and continue keywords.
When using the for statement to iterate within a flow function, the loop count must not exceed 100.
Operators
- Logical operators:
&&and||.In logical conditions, a null value evaluates to false and all other values evaluate to true. For example,
null && "x"returns false, andnull || "x"returns true. - Mathematical operators:
*,/,%,+, and-.Only numeric data types are supported. Using other types causes an error.
- Comparison operators:
>,=>,<,<=,==, and!=. The == comparison operator supports only numeric values.
Comments
The script supports multi-line comments (/* ${comments}*/) and single-line comments (// ${comments}).