All Products
Search
Document Center

IoT Platform:Function list

Last Updated:Nov 01, 2024

This topic outlines the functions available for scripting time series data storage rules on the IoT Platform, enabling diverse data processing. It covers data type conversion functions, time type conversion functions, and basic data type functions, along with their usage instructions.

Background information

Utilize the supported script syntax to invoke functions for storing custom topic data from devices into the IoT Platform's time series data storage.

Time series data storage related functions

Expression

Description

convert2HotStorageData(fields, time)

Transforms the fields data into a standardized format suitable for storage.

  • fields: The data record in Map or JSON format to be stored, structured as follows:

    {
        "key1": value1,
        "key2": value2
    }
    • key1 and key2 are identifiers for the fields to be stored, representing the names of the data.

    • value1 and value2 are the values assigned to the respective fields from the device data.

    Important

    The fields parameter may include a maximum of 500 key.

  • time: The timestamp in milliseconds corresponding to the data, expressed as a numeric value.

    The timestamp() function can be used to obtain the current system timestamp, or you can specify a custom time.

    This timestamp is assigned to the fields data for all fields as time.

The data is converted into the following standardized format:

{
    "key1": {
        "value": value1,
        "time": timestamp()
    },
    "key2": {
        "value": value2,
        "time": timestamp()
    }
}

writeHotStorage(params)

Stores data in the standardized format params into the IoT Platform's time series data storage.

The params must include key, value, and time elements. For instance:

{
    "key1": {
        "value": value1,
        "time": 1626948134319
    },
    "key2": {
        "value": value2,
        "time": 1626948134000
    }
}
Important

The params parameter can include a maximum of 500 key entries.

Convert data types

Function

Description

toBoolean(Object)

Converts the Object parameter into a Boolean value. The parameter supports the following data types:

  • Boolean: The function returns the same value as the parameter.

  • Number: If the parameter is set to 0, the function returns false. Otherwise, the function returns true.

  • String: If the parameter is set to "true", the function returns true. Otherwise, the function returns false.

  • If the parameter is set to NULL, the function returns false.

toNumber(Object)

Converts the value of the Object parameter into a number. The parameter supports the following data types:

  • Boolean: If the parameter is set to true, the function returns 1. If the parameter is set to false, the function returns 0.

  • Number: The function returns the same value as the parameter.

  • String: The function parses the string to a number.

  • If the parameter is set to NULL, the function returns 0.

toString(Object)

Converts the value of the Object parameter into a string.

If the parameter is set to a value other than NULL, the function returns the value as a string. If the parameter is set to NULL, the function returns an empty string.

If the parameter is of the binary type, the function returns a UTF-8 decoded value.

toMap(Object)

Converts the value of the Object parameter into a map. The parameter supports the following data types:

  • Map: The function returns the same value as the parameter.

  • String: The function parses the string to a map in the JSON format.

  • If the parameter is set to NULL, the function returns an empty map.

toArray(Object)

Converts the value of the Object parameter into an array. The parameter supports the following data types:

  • Array: The function returns the same value as the parameter.

  • String: The function parses the string to an array in the JSON format.

  • If the parameter is set to NULL, the function returns an empty array.

toBinary(Object)

Converts the value of the Object parameter into a binary value. The parameter supports the following data types:

  • Binary: The function returns the same value as the parameter.

  • String: The function returns a UTF-8 encoded value.

  • If the parameter is set to NULL, the function returns an empty binary value.

Convert time formats

Function

Description

format_date(timestamp, patten, timeZone)

Converts a timestamp in milliseconds into a time value in a specified format. A string is returned.

  • timestamp: the timestamp in milliseconds.

  • patten: the required time format. Example: yyyy-MM-dd HH:mm:ss.

  • timeZone: the time zone. Examples: GMT, UTC, and CST. We recommend that you specify a time zone. If you do not configure the timeZone parameter, the function returns a time value in GMT+8.

to_timestamp(dateString, patten, timeZone)

Converts a time value in a specified format into a timestamp in milliseconds. A numeric value is returned.

  • dateString: the time string.

  • patten: the time format. Example: yyyy-MM-dd HH:mm:ss.

  • timeZone: the time zone. Examples: GMT, UTC, and CST. We recommend that you specify a time zone. If you do not configure the timeZone parameter, the function returns a timestamp in GMT+8.

Functions that are supported by each basic data type

  • Map

    Function

    Description

    [Object]

    Obtains the value of a specified key.

    size()

    Obtains the number of key-value pairs in a map.

    containsKey(String)

    Checks whether a map contains a specified key.

    keySet()

    Obtains keys in a map. An array is returned.

    remove(Object)

    Removes a key-value pair from a map based on a specified key.

    put(Object, Object)

    Adds key-value pairs to a map.

    putAll(map)

    Adds another map to an existing map.

  • Array

    Function

    Description

    [int]

    Obtains the value at a specified index. The index of the first element in an array is 0.

    contains(Object)

    Checks whether an array contains a specified element.

    remove(Object)

    Removes a specified element from an array.

    removeAt(int)

    Removes an element at a specified index in an array.

    add(Object)

    Adds an element to the end of an array.

    add(index, Object)

    Adds an element to a specified index.

    addAll(array)

    Adds another array to an array.

    size()

    Obtains the number of elements in an array.

  • String

    Function

    Description

    substring(start, end)

    Extracts a string from the start position to the end-1 position. If you do not configure the parameter, the extraction process continues until the end of the string.

    length()

    Queries the length of a string.

    split(String)

    Splits a string by using a specified separator.

    startsWith(String)

    Checks whether a string starts with a specified substring.

    endsWith(String)

    Checks whether a string ends with a specified substring.

    indexOf(String, index)

    Queries the position in which a substring first appears from a specified index of a string.

    The default value of the index parameter is 0.

Other functions

Function

Description

endWith(input, suffix)

Checks whether the input string ends with a specified suffix.

A Boolean value is returned.

productKey()

Returns the ProductKey of the current product.

getDeviceTag(key)

Returns the device tag value of a tag key. If the tag that contains the specified key is not attached to a device, no tag value is returned.

getOrNull(data, "items", ......)

Returns the value of a specified field in the JSON-formatted data parameter.

You can configure one or more fields in the function. Multiple fields must be configured based on levels. The function returns the value of the last field. If the last field does not exist or the field is empty, the function returns null.

Example of data:

"items": {
    "Humidity": {
        "value": 25,
        "time": 1626948134319
    }
}

Examples:

  • getOrNull(data, "items"): returns the value of the items field "Humidity": {"value": 25,"time": 1626948134319}.

  • getOrNull(data, "items", "Humidity", "value"): returns the value of the value field 25.

  • getOrNull(data, "items", "Temperature"): returns the value null if the Temperature field does not exist.

payload(textEncoding)

Encodes the message payload that is sent by a device. The textEncoding parameter specifies an encoding scheme.

  • If you do not configure the parameter, the message payload is converted into a UTF-8 encoded string. payload() is equivalent to payload('utf-8').

  • If you set the parameter to 'json', the message payload is converted into a map. If the payload is not in the JSON format, an error occurs.

  • If you set the parameter to 'binary', the message payload is converted into pass-through binary data.

parseNumber(numberString, radix)

Converts a string specified by the numberString parameter into a number based on a number system specified by the radix parameter.

The radix parameter is optional. Valid values:

  • 10 (default): converts a string into a decimal number.

  • 2: converts a string into a binary number.

  • 16: converts a string into a hexadecimal number.

Examples:

// A device submits data. 
var content = '0xFF';
// The function obtains the string FF to be converted.
var x = content.substring(2);
// The function converts the string FF into a hexadecimal number. 
var value = parseNumber(x, 16);