Function list

Updated at:

You can use the following functions in time series data storage rule scripts to process device data. The functions include data type conversion, time type conversion, and basic data type operations.

Background information

You can use the supported script syntax to call functions that store custom topic data from devices into time series data storage.

Time series data storage related functions

Expression

Description

convert2HotStorageData(fields, time)

Converts fields data into a standardized format 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 field identifiers that represent the names of the data to store.

    • value1 and value2 are the corresponding field values from the device data.

    Important

    The fields parameter may include a maximum of 500 key.

  • time: The timestamp for the data, in milliseconds.

    You can call timestamp() to get the current system timestamp, or specify a custom time.

    All fields in the fields data share this time value.

The data is converted into the following format:

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

writeHotStorage(params)

Writes standardized data params to time series data storage.

params must include key, value, and time elements. Example:

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

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

Data type conversion functions

Expression

Description

toBoolean(Object)

Converts an Object value to a Boolean value. The parameter supports the following value types:

  • Boolean: Returns the corresponding Boolean value.

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

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

  • If the parameter value is null, the function returns false.

toNumber(Object)

Converts an Object value to a Number value. The parameter supports the following value types:

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

  • Number: Returns the corresponding numeric value.

  • String: Parses the string as a numeric type.

  • If the parameter value is null, the function returns 0.

toString(Object)

Converts an Object value to a String value.

The parameter can be of any type. The function returns the corresponding String value. If the parameter value is null, the function returns an empty string.

If the parameter is a binary type, the function returns the corresponding value based on UTF-8 encoding.

toMap(Object)

Converts an Object value to a Map value. The parameter supports the following value types:

  • Map: Returns the corresponding value.

  • String: Parses the string as a Map in JSON format.

  • If the parameter value is null, the function returns an empty Map.

toArray(Object)

Converts an Object value to an Array value. The parameter supports the following value types:

  • Array: Returns the corresponding value.

  • String: Parses the string as an Array in JSON format.

  • If the parameter value is null, the function returns an empty Array.

toBinary(Object)

Converts an Object value to a binary value. The parameter supports the following value types:

  • Binary: Returns the corresponding value.

  • String: Returns the value based on UTF-8 encoding.

  • If the parameter value is null, the function returns an empty Binary value.

Time type conversion functions

Function expression

Description

format_date(timestamp, pattern, timeZone)

Converts a millisecond timestamp to a time string in a specified format. Returns a String value.

  • timestamp: The millisecond timestamp.

  • pattern: The time format to which you want to convert. For example, yyyy-MM-dd HH:mm:ss.

  • timeZone: The time zone. For example, GMT, UTC, or CST. Set the time zone. If you do not set this parameter, the default time zone UTC+8 is used.

to_timestamp(dateString, pattern, timeZone)

Converts a time string in a specified format to a millisecond timestamp. Returns a numeric timestamp.

  • dateString: The time string.

  • pattern: The specified time format. For example, yyyy-MM-dd HH:mm:ss.

  • timeZone: The time zone. For example, GMT, UTC, or CST. Set the time zone. If you do not set this parameter, the default time zone UTC+8 is used.

Functions supported by basic data types

  • Map type

    Expression

    Description

    [Object]

    Gets the Value for a specified Key.

    size()

    Gets the number of key-value pairs in the Map data.

    containsKey(String)

    Checks whether the Map data contains a specified key.

    keySet()

    Gets the set of keys in the Map data. Returns an Array.

    remove(Object)

    Removes the key-value pair for a specified key from the Map data.

    put(Object, Object)

    Adds a key-value pair to the Map data.

    putAll(map)

    Adds a set of Map data to the Map data in a batch.

  • Array type

    Function

    Description

    [int]

    Gets the value at a specified index. The index of the first element is 0.

    contains(Object)

    Checks whether the array contains a specified element.

    remove(Object)

    Removes a specified element from the array.

    removeAt(int)

    Removes the element at a specified index from the array.

    add(Object)

    Adds an element to the end of the array.

    add(index, Object)

    Adds an element at a specified index.

    addAll(array)

    Adds another array to the array.

    size()

    Gets the number of elements in the array.

  • String type

    Function

    Description

    substring(start, end)

    Extracts a substring from the start position to the end-1 position. If end is not specified, the substring is extracted to the end of the string.

    length()

    Gets the length of the string.

    split(String)

    Splits a string using a separator character.

    startsWith(String)

    Checks whether a string starts with a specified string.

    endsWith(String)

    Checks whether a string ends with a specified substring.

    indexOf(String, index)

    Returns the position of the first occurrence of a specified string, starting from a specified index.

    If index is not specified, the default value is 0.

Other functions

Expression

Description

endWith(input, suffix)

Checks whether the string input ends with the string suffix.

Returns a Boolean value.

productKey()

Returns the ProductKey of the current product.

getDeviceTag(key)

Returns the device tag that corresponds to the specified key. If the device does not have a tag for the key, the function returns an empty value.

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

Returns the value of a specified field from the JSON data data.

You can specify any number of fields, but you must specify them in hierarchical order. The function returns the value of the last field. If the last field does not exist or its value is empty, the function returns null.

For example, the data parameter contains the following:

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

Function 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"): The Temperature field does not exist. The function returns null.

payload(textEncoding)

Returns the escaped data of the payload that a device publishes. textEncoding specifies the character encoding for escaping the payload. Valid values:

  • If you do not specify this parameter, the payload is converted to a string based on UTF-8 encoding by default. payload() is equivalent to payload('utf-8').

  • 'json': Converts the payload data to a Map variable. If the payload is not in the JSON format, an error is returned.

  • 'binary': Converts the payload data to a binary variable for pass-through transmission.

parseNumber(numberString, radix)

Converts the string numberString to a number in the specified radix.

radix is an optional parameter. Valid values:

  • 10 (default): Converts the string to a decimal number.

  • 2: Converts the string to a binary number.

  • 16: Converts the string to a hexadecimal number.

For example:

// Data reported by the device.
var content = '0xFF';
// Get the string FF to be converted.
var x = content.substring(2);
// Convert FF to a hexadecimal number.
var value = parseNumber(x, 16);