Creates a rule for a specified topic.

Debugging

OpenAPI Explorer automatically calculates the signature value. For your convenience, we recommend that you call this operation in OpenAPI Explorer. OpenAPI Explorer dynamically generates the sample code of the operation for different SDKs.

Request parameters

Parameter Type Required Example Description
Action String Yes CreateRule

The operation that you want to perform. Set the value to CreateRule.

Name String Yes iot_test1

The name of the rule. The name must be 1 to 30 characters in length, and can contain Chinese characters, English letters, digits, underscores (_), and hyphens (-). Each Chinese character is counted as two encoding characters.

IotInstanceId String No iot_instc_pu****_c*-v64********

The ID of the instance. This parameter is not required for public instances. However, the parameter is required for the instances that you have purchased.

Select String No deviceName() as deviceName, items.Humidity.value as Humidity, items.Temperature.value as Temperature

The SQL SELECT statement that you want to execute. For more information, see SQL expressions.

Note Specify the fields that follow the Select keyword for this parameter. For example, if the Select statement is Select a,b,c, specify a,b,c for this parameter.
ShortTopic String No +/thing/event/property/post

The topic to which this rule is applied. Syntax: ${deviceName}/topicShortName. ${deviceName} specifies the name of the device, and topicShortName specifies the topic category customized for the specified device.

You can call the QueryDevice operation to view all devices under the product. The devices are displayed in the DeviceName parameter in the response.

You can call the QueryProductTopic operation to view all topic categories under the product. The topic categories are displayed in the TopicShortName parameter in the response.

Examples

  • Example of a system-defined topic category: ${deviceName}/thing/event/property/post. You can replace ${deviceName} with the + wildcard. The wildcard indicates that the topic category applies to all devices under the product.
  • Example of a user-defined topic category:${deviceName}/user/get.

    When you specify a topic category, you can use the + and # wildcards.

    • You can replace ${deviceName} with the + wildcard. The wildcard indicates that the topic category applies to all devices under the product.
    • You can replace the fields that follow ${deviceName} with /user/#. The # wildcard indicates that the topic category applies whatever values are specified for the fields that follow /user.

      For more information about how to use wildcards, see Wildcards in topic categories.

  • Example of a topic category that is used to submit device status data: ${deviceName}.
Where String No Temperature>35

The condition that is used to trigger the rule. For more information, see SQL expressions.

Note Specify the fields that follow the Selectkeyword for this parameter. For example, if the Where statement is Where a>10, specify a>10 for this parameter.
ProductKey String No a1T27vz****

The key of the product to which the rule applies.

RuleDesc String No rule test

The descriptions of the rule. The description can be up to 100 characters in length. Each Chinese character is counted as one encoding character.

DataType String No JSON

The format of the data to be processed by the rule. You must specify the format of device data to be processed for this parameter. Valid values:

  • JSON: JSON data
  • BINARY: binary data
Note If you specify BINARY, you cannot set the TopicType parameter to 0 and forward data to Table Store, Time Series Database (TSDB), or ApsaradDB for RDS.

Default value: JSON.

TopicType Integer No 1
  • 0: system-defined topic. System-defined topics include the following topics:
    • /thing/event/property/post: submits the property data of a device.
    • /thing/event/${tsl.event.identifier}/post: submits the event data of a device. ${} specifies the identifier of an event in the TSL.
    • /thing/lifecycle: submits the device data about lifecycle change.
    • /thing/downlink/reply/message: sends a response to a command request from IoT Platform.
    • /thing/list/found: submits the data when the gateway discovers a sub-device.
    • /thing/topo/lifecycle: submits the data when the topology of a device changes.
    • /edge/driver/${driver_id}/point_post: submits pass-through data from Link IoT Edge. ${} specifies the ID of the driver that a device uses to access Link IoT Edge.
  • 1: user-defined topic.
  • 2: the topic that is used to submit device status data. Syntax: /as/mqtt/status/${productKey}/${deviceName}.
ResourceGroupId String No rg-acfmxazb4ph****

The ID of the resource group to which the rule is assigned. You can the resource group information in the Resource Management console.

If you do not specify this parameter, the rule is assigned to the default resource group.

In addition to the preceding exclusive request parameters, you must specify common request parameters when calling this API operation. For more information about common request parameters, see Common parameters.

Note To enable a rule, you must specify the ProductKey, ShortTopic, and Select parameters.

Response parameters

Parameter Type Example Description
Code String iot.system.SystemException

The error code returned if the call failed. For more information about error codes, see Error codes.

ErrorMessage String A system exception occurred.

The error message returned if the call failed.

RequestId String E4C0FF92-2A86-41DB-92D3-73B60310D25E

The ID of the request.

RuleId Long 100000

The rule ID that is generated by the rules engine if the call was successful.

Note Secure the rule ID, which must be provided when you call the rule-related operations.
Success Boolean true

Indicates whether the call was successful. true indicates that the call was successful. false indicates that the call failed.

Examples

Sample requests

https://iot.cn-shanghai.aliyuncs.com/?Action=CreateRule
&Name=iot_test1
&ProductKey=a1T27vz****
&ShortTopic=+/thing/event/property/post
&Select=deviceName() as deviceName, items.Humidity.value as Humidity, items.Temperature.value as Temperature
&RuleDesc=rule test
&DataType=JSON
&Where=Temperature>35
&TopicType=1
&ResourceGroupId=rg-acfmxazb4ph****
&<Common request parameters>

Sample success responses

XML format

<CreateRuleResponse>
      <RequestId>E4C0FF92-2A86-41DB-92D3-73B60310D25E</RequestId>
      <RuleId>100000</RuleId>
      <Success>true</Success>
</CreateRuleResponse>

JSON format

{
  "RequestId": "E4C0FF92-2A86-41DB-92D3-73B60310D25E", 
  "RuleId": 100000, 
  "Success": true
}

Error code

For a list of error codes, visit the API Error Center.