Tutorial: Create a rule and set up notifications in your Azure IoT Central application

You can use Azure IoT Central to remotely monitor your connected devices. Azure IoT Central rules let you monitor your devices in near real time and automatically invoke actions, such as sending an email. This article explains how to create rules to monitor the telemetry your devices send.

Devices use telemetry to send numerical data from the device. A rule triggers when the selected telemetry crosses a specified threshold.

In this tutorial, you create a rule to send an email when the temperature in a simulated sensor device exceeds 70° F.

In this tutorial, you learn how to:

  • Create a rule
  • Add an email action

Prerequisites

To complete the steps in this tutorial, you need:

Add and customize a device template

Add a device template from the device catalog. This tutorial uses the ESP32-Azure IoT Kit device template:

  1. To add a new device template, select + New on the Device templates page.

  2. On the Select type page, scroll down until you find the ESP32-Azure IoT Kit tile in the Use a pre-configured device template section.

  3. Select the ESP32-Azure IoT Kit tile, and then select Next: Review.

  4. On the Review page, select Create.

The name of the template you created is Sensor Controller. The model includes components such as Sensor Controller, SensorTemp, and Device Information interface. Components define the capabilities of an ESP32 device. Capabilities include the telemetry, properties, and commands.

Add two cloud properties to the Sensor Controller device template:

  1. Select Cloud Properties and then + Add cloud property. Use the information in the following table to add two cloud properties to your device template:

    Display Name Semantic Type Schema
    Last Service Date None Date
    Customer Name None String
  2. Select Save to save your changes.

Add a new form to the device template to manage the device:

  1. Select the Views node, and then select the Editing device and cloud data tile to add a new view.

  2. Change the form name to Manage device.

  3. Select the Customer Name and Last Service Date cloud properties, and the Target Temperature property. Then select Add section.

  4. Select Save to save your new form.

Now publish the device template.

Create a rule

To create a telemetry rule, the device template must include at least one telemetry value. This tutorial uses a simulated Sensor Controller device that sends temperature and humidity telemetry. The rule monitors the temperature reported by the device and sends an email when it goes above 70 degrees.

Note

There is a limit of 50 rules per application.

  1. In the left pane, select Rules.

  2. If you haven't created any rules yet, you see the following screen:

    Screenshot that shows the empty list of rules

  3. Select + New to add a new rule.

  4. Enter the name Temperature monitor to identify the rule and press Enter.

  5. Select the Sensor Controller device template. By default, the rule automatically applies to all the devices associated with the device template. To filter for a subset of the devices, select + Filter and use device properties to identify the devices. To disable the rule, toggle the Enabled/Disabled button:

    Screenshot that shows the selection of the device template in the rule definition

Configure the rule conditions

Conditions define the criteria that the rule monitors. In this tutorial, you configure the rule to fire when the temperature exceeds 70° F.

  1. Select Temperature in the Telemetry dropdown.

  2. Next, choose Is greater than as the Operator and enter 70 as the Value.

    Screenshot that shows the temperature condition for the rule

  3. Optionally, you can set a Time aggregation. When you select a time aggregation, you must also select an aggregation type, such as average or sum from the aggregation drop-down.

    • Without aggregation, the rule triggers for each telemetry data point that meets the condition. For example, if you configure the rule to trigger when temperature is above 70 then the rule triggers almost instantly when the device temperature exceeds this value.
    • With aggregation, the rule triggers if the aggregate value of the telemetry data points in the time window meets the condition. For example, if you configure the rule to trigger when temperature is above 70 and with an average time aggregation of 10 minutes, then the rule triggers when the device reports an average temperature greater than 70, calculated over a 10-minute interval.

    Screenshot that shows the aggregate condition filled out

You can add multiple conditions to a rule by selecting + Condition. When multiple conditions are specified, all the conditions must be met for the rule to trigger. Each condition is joined by an implicit AND clause. If you're using time aggregation with multiple conditions, all the telemetry values must be aggregated.

Configure actions

After you define the condition, you set up the actions to take when the rule fires. Actions are invoked when all the conditions specified in the rule evaluate to true.

  1. Select + Email in the Actions section.

  2. Enter Temperature warning as the display name for the action, your email address in the To field, and You should check the device! as a note to appear in the body of the email.

    Note

    Emails are only sent to the users that have been added to the application and have logged in at least once. Learn more about user management in Azure IoT Central.

    Screenshot that shows the email action for the rule

  3. To save the action, choose Done. You can add multiple actions to a rule.

  4. To save the rule, choose Save. The rule goes live within a few minutes and starts monitoring telemetry being sent to your application. When the condition specified in the rule is met, the rule triggers the configured email action.

After a while, you receive an email message when the rule fires:

Screenshot that shows notification email

Delete a rule

If you no longer need a rule, delete it by opening the rule and choosing Delete.

Enable or disable a rule

Choose the rule you want to enable or disable. Toggle the Enabled/Disabled button in the rule to enable or disable the rule for all devices that are scoped in the rule.

Enable or disable a rule for specific devices

Choose the rule you want to customize. Use one or more filters in the Target devices section to narrow the scope of the rule to the devices you want to monitor.

Clean up resources

If you don't plan to complete any further IoT Central quickstarts or tutorials, you can delete your IoT Central application:

  1. In your IoT Central application, navigate to Administration > Your application.
  2. Select Delete and then confirm your action.

Next steps

In this tutorial, you learned how to:

  • Create a telemetry-based rule
  • Add an action

Now that you've defined a threshold-based rule the suggested next step is to learn how to: