This guide explains how to use the Kaa Rule Engine to transform and manipulate received data. It provides step-by-step examples to help you get started. By the end of this guide, you will be able to write your own data transformation rule.
Before proceeding with the practical examples below, some setup is required.
This section explains how to:
After creating the endpoint, follow these steps to publish data sample to Kaa simulating data from a device:
Paste a JSON payload into the Example Payload field. It will be our device data.
{
"tempCelsius": 25
}
Click Send Request to send the JSON payload.
Also, below on the Kaa UI, you have a cURL
command generated for you to send data from your laptop or any device with an internet connection.
After sending the data sample, navigate back to the device “Overview” page to verify that the data was received.
The received data will be displayed on the “Telemetry” widget.
In this first example, we’ll create a rule that automatically converts the temperature from Celsius to Fahrenheit and outputs the result in a separate time-series auto~tempFahrenheit
.
Complete the initial setup. This is the data we’ll send through “Data Publish” and then transform using the Rule Engine:
{
"tempCelsius": 25
}
name
for the rule.Endpoint data sample updated
.For more details about triggers and their types, refer to the Rule Engine Triggers documentation.
This trigger will execute the rule whenever we publish new data.
Scroll down to the Condition section and add the expression:
return true;
You can add conditions to the rule, but for now, simply skip them by using the above return statement in order to execute the below action unconditionally. More info on what rule can return can be found here.
Scroll down to create the action.
Data Sample
.function celsiusToFahrenheit(celsius) {
return (celsius * 9) / 5 + 32;
}
const { tempCelsius } = ctx.trigger.endpointDataSamplesReceived.dataSamples[0];
console.log(`tempCelsius: ${tempCelsius}`);
return {
tempFahrenheit: celsiusToFahrenheit(tempCelsius)
};
This code takes the tempCelsius
directly from JSON you sent earlier, converts it to Fahrenheit, and generates a new data sample, which will be transformed into auto~tempFahrenheit
time-series.
Now when rule and corresponding action are configured, save the rule by clicking Create in the top-right corner.
Note: For more information on Rule Engine actions, refer to the Rule Engine Actions documentation.
To test the rule we need to send data through “Data Publish” tool. Navigate back to the “Data Publish” page and send a new data sample like it was described before.
After sending the data, return to the “Device” page and check the chart.
You should now see the new param auto~tempFahrenheit
appeard.
Refer to the Troubleshooting section below if the data transformation doesn’t work as expected.
This example is very similar to the first one except this time we have data sample with two fields.
We will create a rule to transform data from a battery sensor.
This rule calculates power
by using the voltage
and current
values.
Repeat the initial setup. For “Data Publish” use this JSON payload instead:
{
"voltage": 3.5,
"current": 0.1
}
Most of the steps for creating this rule are the same as in the first example.
Follow the instructions in Example 1 to create the trigger
and condition
.
The only difference is the action where we handle different data sample.
Scroll down to create the action.
This step is similar to action from Example 1, but with a different code:
const { current, voltage } = ctx.trigger.endpointDataSamplesReceived.dataSamples[0];
console.log(`Current: ${current}`);
console.log(`Voltage: ${voltage}`);
return {
power: current * voltage
}
This code extracts voltage
and current
directly from JSON you sent earlier, calculates the power
, and creates a new data sample, which will be transformed in the auto~power
time series with the help of Auto-extract feature.
Let’s run the rule.
Navigate back to the “Data Publish” page and send a new data sample.
After sending the data, return to the “Device” page and check the chart.
You should now see the new param auto~power
appeared.
If the data transformation doesn’t work as expected, follow these steps for verification and debugging:
Go to “Rules” -> “Tracing”.
Here, you can view debug information on rule and their action execution, which is essential for debugging errors.
Click on the trace to get more info.
Refer to the Rule Engine Tracing documentation for in-depth guidance on debugging and troubleshooting your rules.