Creating and managing rules
    • PDF

    Creating and managing rules

    • PDF

    Article Summary

    Available in Classic and VPC

    If you are ready to connect an IoT Device to Cloud IoT Core, you need to create rules defining how to process data received from the IoT device. When creating rules, you need to designate a "rule query" for checking if the rules meet the preset conditions and an "action" for performing a predefined motion when the conditions are met among the message details sent from the IoT device to the Cloud IoT Core. For example, you can create a rule to send push notifications to the user (action) if the temperature in the message is 80 degrees or higher (trigger). When creating rules, you must designate the rule query and the action.

    Rules screen

    cloudiotcore-rules_01_en.png

    ItemDescription
    ① Create rulesA button to create rules
    ② Rule engine statusOperation status of engine for processing the rules
  • Normal: During normal operation of rule engine
  • Operation suspension: The rule engine stops operation. Contact customer service center
  • ③ Function buttons
  • Edit: Edit rules
  • Delete: Delete rules
  • Activate: Activate deactivated rules
  • Deactivate: Suspend the use of rules for some time
  • ④ Rules listList of rules created
    ⑤ SearchYou can search by rule name, rule description, and rule query.

    Create rules

    The following describes how to create rules.

    1. Click Services > Internet of Things > Cloud IoT Core > Rules on the NAVER Cloud Platform console.
    2. Click [Create rules].
    3. When the Create rules screen appears, enter the rule name and the rule description.
    4. Enter the rule query.
      • A rule query refers to a condition for inspecting a message.
      • For more information on how to prepare a rule query, see Writing rule query.
    5. To check the grammar of the rule query, click [Validate query].
      • When the query is completely verified, a Create actions screen appears on the lower end of the screen.
    6. Select an action type and click [Add].
    7. Create an error action.
    8. Click [Create].
    Note
    • English characters, numbers, - (hyphen), and _ (underline) can be used in the rule name and up to 128 characters can be entered.
    • Up to 255 characters can be entered for the rule description.
    • A rule name cannot be registered multiple times.
    • Up to 10,000 characters can be entered for the rule query.
    Note
    • The message inserted into a rule cannot exceed 5 Kbytes (UTF-8 encoding) and must be a JSON object. JSON List isn't supported.
    • The number of incoming messages for rule processing can't exceed 10,000 per second. If more messages are inserted, the processing may be delayed.
    • The order of message rule processing and action execution isn't guaranteed within 1 second.
    • The processing delay time is 1 second on average. An additional delay may occur depending on the number of rules, types of queries, and types of actions.

    Writing rule query

    Write a particular rule query for message content sent from the IoT device. To write a rule query, you must understand the basic SQL phrases. In addition, only the SELECT phrases are supported, so you need to write in the order of SELECT - FROM - WHERE.

    The rule query writing rules are as follows:

    SELECT [<Topic Alias>.<Message JSON Key1>, <Topic Alias>.<Message JSON Key2>, ...] FROM "[Topic]" AS [Topic Alias] WHERE [Condition]
    
    Note

    Create actions

    A motion to be executed when an action name message meets a rule query. You can create an action after completely verifying the rule query, and each rule can set 1-5 actions. Cloud IoT Core supports the functions of "republish as a specified topic" and "send data to Cloud Functions."

    Republish as a specified topic

    The "Republish as a specified topic" action is used to republish an MQTT message published from the IoT device to a topic with another name. For instance, if data matching the rule query pre-defined by the user is detected among messages published by the IoT device, you can set to republish the message to the topic of "alert" and make an alert sound on the device receiving the message for republishing "alert."

    1. Write the rule query by referring to Create rules.

    2. Select the action type as "republish as a specified topic" and click [Add].

    3. When the pop-up window appears, enter the new topic name for publishing message to the "republish topic."

    4. Click [Complete].

    5. After checking if the rule has been added, click [Create].

    Note
    • You can enter English characters, numbers, spaces, and !@$%^&()_-={[}]?></`’ in a topic name.
    • You can enter up to 255 characters for the topic name.
    • An action name cannot be registered multiple times.
    Note
    • To prevent a message from being republished indefinitely, you cannot create a republish topic name as the same as the topic of the received message. The topic of the MQTT republish action can't be set to be included in the topic of the FROM clause of a rule query.

    Send data to Cloud Functions

    To transmit data to Cloud Functions and run an additional action, first complete the request for using Cloud Functions and create a trigger. It's a structure to analyze the message transmitted from the IoT device to Cloud IoT Core and run the action defined in the Cloud Functions when the message meets the predefined rule query.

    1. Write the rule query by referring to Create rules.
    2. Select the action type as "Send data to Cloud Functions" and click [Add].
    3. When the pop-up window appears, select the trigger created in Cloud Functions.
    4. To create a trigger, click [Create Cloud Functions trigger] and create a trigger.
    5. Select the trigger to connect.
    6. Set the batch time to merge the data for a set time (5-600 seconds) as an option and run Cloud Functions trigger.
      • It is possible to reduce the number of times the Cloud Functions trigger runs, and to mass process the messages during the batch time.

      • When the batch is set, the data format transmitted to the Cloud Functions trigger parameter is as below.

        • JSON Object having the "messages" JSON Array field
        • Merge the data collected in Cloud IoT Core in the shape of JSON Array <Example>
        {
          "messages": [
            {
              "deviceId": "1",
              "temperature": 12.6,
              "eventTime": 1606286653000
            },
            {
              "deviceId": "1",
              "temperature": 12.5,
              "eventTime": 1606286658000
            },
            {
              "deviceId": "1",
              "temperature": 12.3,
              "eventTime": 1606286693000
            }
          ]
        }
        
    7. Click [Add].
    Caution

    When set as a batch, the execution of Cloud Functions is once and the fee is imposed for one call, but the fee for running Cloud IoT action is imposed based on the number of actually merged messages. For example, if 100 messages were merged over one minute of batch time, then this will be charged as 100 instances of Cloud IoT Core action execution.

    Note
    • You can only use the triggers registered on Cloud Functions. You need to create Cloud Functions actions for performing the actual logic and connect to the Cloud Functions trigger for running the Cloud Functions actions.
    • You can register only the triggers of the Cloud IoT trigger type among the triggers of Cloud Functions.
    • When an execution error of Cloud Functions occurs, the message processing may be delayed in Cloud IoT Core.
    • The batch time can be set to be between 5 and 600 seconds (10 minutes).
    • The size of parameter transmitted to Cloud Functions action is limited to 1 MB. If it exceeds the limited capacity within the set batch time, then the Cloud Functions action will immediately execute.
    • When updating Cloud IoT Core, the connection may end. If the connection ends, you need to access it again.
    • Due to the nature of the MQTT connection, the network connection isn't stable and may be cut. If the connection ends, you need to access it again.

    Create error actions

    An error action is executed when a user-defined rule query or an action execution fails. Up to 1 error action can be created per rule. For instance, if request to Cloud Functions has failed in a process of requesting for "Send data to Cloud Functions" action, the action failure is recognized and the action set as "error action" is executed. However, an error processing for the execution result of another service, not an error occurring in the process of requesting to another service, is not supported.

    The following describes how to create an error action.

    1. Write the rule query by referring to Create rules.
    2. Set the action information by referring to Create actions.
    3. Select the Unfold Settings of Edit Error Action.
    4. Select Republish as a specified topic or Send data to Cloud Functions, and click [Add].
      • How to create an error action is the same as how to create an action.

    Error action response

    An error action is provided as a JSON type. The errorsList is a list of errors of all actions with an error among several actions since a message runs a large number of actions. In query, when an error occurs, the errorType is indicated by RULE_QUERY, and the actionName is not included.

    <Example>

    {
      "ruleName":"TriggerSensor",
      "payload":{ 
           "temp":50,
           "high":"top"
       },
       "topic" : "myhome/room1",
       "errorsList":[
           { "errorType" : "CLOUD_FUNCTIONS",  
             "actionName" : "aggregate",
             "message":"Function id invalid",
             "returnCode":621
           }, ...
       ]
    }
    
    Note
    • Cloud IoT Core just checks the execution or non-execution of the Cloud Functions, and the result of execution of action in Cloud Functions can be checked only on the Cloud Functions console.
    • When an error occurs in error action, you cannot call another error action.

    Activate and deactivate rules

    You can temporarily stop using rules. If you deactivate rules, the relevant rule queries and actions do not work. The following describes how to deactivate or activate rules.

    1. Click Services > Internet of Things > Cloud IoT Core > Rules on the NAVER Cloud Platform console.
    2. After clicking the rules to be deactivated in the rules list, and click [Deactivate].
      • If you want to activate the deactivated rules again, click [Activate].

    Delete rule

    If a rule that you specified is no longer needed, you can delete the rule. The following describes how to delete a rule.

    1. Click Services > Internet of Things > Cloud IoT Core > Rules on the NAVER Cloud Platform console.
    2. Select the rule to delete, and then click [Delete].

    Was this article helpful?

    Changing your password will log you out immediately. Use the new password to log back in.
    First name must have atleast 2 characters. Numbers and special characters are not allowed.
    Last name must have atleast 1 characters. Numbers and special characters are not allowed.
    Enter a valid email
    Enter a valid password
    Your profile has been successfully updated.