You can use the rules engine of IoT Platform to forward data from a specified topic to other topics and other Alibaba Cloud services. This topic describes how to create and configure rules for data forwarding. The configuration process includes: create rules, write SQL statements for data processing, set destinations for data, and set destinations for error messages when data forwarding fails.

Procedure

  1. In the left-side navigation pane of the IoT Platform console, click Rules > Data Forwarding.
  2. On the Data Forwarding page, click Create Rule.
  3. Enter Rule Name, and select a Data Type. Click OK.
    Parameter Description
    Rule Name The rule name must be unique. The name can contain letters, digits, underscores (_), and hyphens (-). The name must be 1 to 30 characters in size.
    Data Type Select a data type for the data that this rule processes. Valid values: JSON and Binary.
    Note
    • The rules engine processes data based on topics. Therefore, you must select the format of the data in the topic that you want to process.
    • If you select Binary, this rule cannot process messages from system topics, and cannot forward data to Table Store or ApsaraDB for RDS.
    Rule Description The description of the rule.
  4. After the rule is created, you need to edit the SQL statements for data processing, set the data forwarding destination, and set the destination to which error messages are sent.
    1. Click Write SQL to write an SQL statement for processing message fields.

      For example, the following SQL statement can retrieve the deviceName field from the messages of all the devices under the test1128 product.

      Note You can use the to_base64(*) function to convert binary data into a Base64 string. Built-in functions and conditions are also supported in SQL statements.
      Data forwarding

      For more information about how to write SQL statements, see SQL statements and Functions.

      Parameter Description
      Rule Query Expression The SQL statement that is automatically generated based on the values that you enter for parameters, such as the Field, Topic, and Conditions.
      Field

      The field of the message to be processed. For example, the deviceName field in the message is selected, if you enter deviceName() as deviceName.

      Note The data in system topics is in the Alink JSON format and is parsed before being forwarded to the rule engine. For more information, see Data forwarding procedure. For more information about the parsed data format, see Data format. You must write SQL fields according to the fields in the parsed data.
      Topic

      Select the topics whose messages are to be processed by this rule.

      Options:

      • Custom: The messages are from custom topics. Wildcards+and#are supported when you specify custom topics.
        • All equipment (+): all devices under the specified product.
        • /user/#: all custom topics of the specified device.

        The Operation Authorization of the custom topic must be PublishCustom topics.

      • Device Status Change Notification: The topic to which device status change messages are sent when the device goes online or offline. This option is available when the data type of the rule is JSON.
      • TSL Data Report: The topics to which devices report property and event data. This option is available when the data type of the rule is JSON.

        Valid values:

        • thing/event/property/post: This topic for the device to report property data.
        • thing/event/${tsl.identityId}/post: This topic for the device to report event data.
        • thing/downlink/reply/message: This topic for the device to respond to commands that are issued from the cloud.
      • Historical TSL Data Report: The topic to which devices report historical properties and events. This option is available when the data type of the rule is JSON.
      • Device Changes Throughout Lifecycle: This topic has the messages of device Lifecycle changes such as creating, deleting, disabling, and enabling. This option is available when the data type of the rule is JSON.
      • Sub-Device Data Report Detected by Gateway: The gateway reports the sub-device information to IoT Platform through this topic. This option is available when the data type of the rule is JSON.
      • Device Topological Relation Changes: This topic is used to report messages when you create and delete topological relationships between sub-devices and gateways. This option is available when the data type of the rule is JSON.
      Conditions Set the trigger conditions for the rule.
    2. In the Data Forwarding section, click Add Operation. In the dialog box that appears, select the Alibaba Cloud service to which you want to forward the processed data. Follow the instructions on the page to configure the parameters. For more information about data forwarding configuration, see the data forwarding examples.
      Note You can create up to 10 data forwarding operations for a rule.

      if data forwarding fails due to exceptions in the target Alibaba Cloud services, the rules engine retries three times: after one second, after three seconds, and after ten seconds. If all the retries fail, the message will be discarded. If you do not want to miss the forwarding failed messages, you can proceed to the next step: Add Misoperation. You can then add a destination for error messages.

    3. Click Add Error Operation in the Forward Error Data section. You can configure parameters to forward error messages to the specified destination after all retries fail.
      Note
      • Error messages and device data cannot be forwarded to the same Alibaba Cloud service. For example, you cannot configure Table Store as the destination for both error messages and device data.
      • Rules engine retries three times if data fails to be forwarded to the specified destinations. If all the retries fail, an error message is forwarded according to this configuration.
      • Here, the term "error messages" refers only to messages that relate to errors resulting from exceptions in the target Alibaba Cloud instance.
      • You can add only one destination for error message forwarding.
  5. After you complete the configuration of all settings, go back to the Data Forwarding page. Click Startcorresponding to the rule to start this rule. Data will then be forwarded following this rule.
    You can also perform the following operations:
    • Click View to view or modify the settings of the rule.
    • Click Delete to delete the rule.
      Note You cannot delete a rule while it is enabled.
    • Click Stop to disable the rule.