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. Similarly, you can use a generated cURL command to send a sample of data from outside Kaa.

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.Note: If you’re unfamiliar with triggers, you can learn more here.
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.
Note: If you’re unfamiliar with actions, you can learn more 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.

This rule flattens the received data sample payload and outputs the result to Kaa.
It is useful when you’re unable to modify the payload on the device side.
Repeat the initial setup. For Data Publish use this JSON payload instead:
{
"DEVICE_DATA": [
{
"temperature_c": 45,
"combustion_power_mw": 0.61,
"lambda": 1.05,
"fuel_inputs": {
"fuel_power_mw": 30.26,
"solid_1_2_kg_s": 28.75,
"liquid_gas_kg_s": 1.51,
"solids_ratio": 0.6,
"g_l_ratio": 0.083
}
}
]
}
If you send a payload like this, no data will be written to the endpoint. When such a payload is sent, it will not appear in your endpoint graphs because, by default, Kaa extracts data only from the first level of the object’s hierarchy.
To successfully save data to an endpoint, Kaa expects the object to be flattened:
[
{
"temperature_c": 45,
"combustion_power_mw": 0.61,
"lambda": 1.05,
"fuel_power_mw": 30.26,
"solid_1_2_kg_s": 28.75,
"liquid_gas_kg_s": 1.51,
"solids_ratio": 0.6,
"g_l_ratio": 0.083
}
]
So our goal is to write a rule that flattens the payload and outputs this data to Kaa.
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 dataSample =
ctx.trigger.endpointDataSamplesReceived.dataSamples[0];
const deviceDataArray = dataSample.DEVICE_DATA;
const result = deviceDataArray.map((data) => ({
...data,
...data.fuel_inputs,
}));
return result;
This code extracts nested data and flattens the object.
Of course, for different objects, you would need to apply a different algorithm.

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.

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.