Rule Engine Getting Started

What is Rule Engine in the Kaa Platform

The Rule Engine in the Kaa Platform is a powerful tool that allows you to create automated actions based on specific conditions or events generated by IoT devices. It is a core component of the platform that drives real-time decision-making and automation.

Use Cases

Examples use cases that the Rule Engine covers

  • Alerting: Send email, SMS, or webhook notifications when device metrics exceed thresholds.

    Example: If the temperature on a device is higher than 30°C, then send SMS to 555-123-4567

  • Scenario Automation: Automate scenarios across independent devices based on events.

    Example: If a water leak is detected by a leak detector device, send a command to a smart water valve to turn off the water supply.

  • Predictive Maintenance: predict maintenance based on the current device data combined with the historical one

    Example: Schedule maintenance when vibration patterns from the past week suggest that a motor bearing is likely to fail soon.

  • Data Aggregation: take an action based on aggregated data for the period

    Example: If the aggregated water usage for the last 6 hours is greater than 7,000 liters, send an email to admin@acme.com.

  • Data Processing: Enrich or transform data from a device on the fly with access to previously processed data.

    Example: Find the delta between current and the previous fuel level

  • Data Transformation: transform data from a device before storing it

    Example: Convert temperature from a device from a Fahrenheit to Celsius before storing it

Quick Example

Let’s configure trigger, rule, and action for the above scenario with temperature and alert.

Log in into your Kaa Cloud account, go to Rules, and click Add rule. Enter rule name, e.g., Activate alert on high temperature. Click Add trigger, choose Create new. Enter trigger name, e.g., Endpoint reported data. Choose Endpoint data sample updated trigger type, and select your endpoint from the Endpoints dropdown.

Create trigger

This trigger will run rule every time endpoint Sensor 1 reports data.

If we specify either the application or application version name instead of Sensor 1 endpoint, then trigger would react to the reported data from any endpoint inside corresponding application or application version.

Scroll down to the Condition field. Here, you should write the rule expression.

Paste the next JavaScript snippet. In this snippet we take the first reported data sample by endpoint and check its temperature.

return ctx.trigger.endpointDataSamplesReceived.dataSamples[0].temperature > 30;

Create rule

Scroll down to Actions. Click Add action, and Create new. Choose Alert activation action type, enter action name, e.g., Activate high temperature alert, enter HIGH_TEMPERATURE for the Alert type, and arbitrary Message. Leave all other fields to their defaults.

Create action

Click Create in the top right corner.

Let’s simulate that our endpoint sends some data. Go to Device management, choose your endpoint, go to Data publish tab, select HTTP, enter your device token. Set temperature field value to 31 in the Example payload field, and click Send request.

Simulate endpoint data

Right after that, you will see notification about activated alert in the top right corner.

Active alert

Next Steps