Kaa Documentation

Data Transformation

Overview

This guide explains how to use the Kaa Rule Engine to automatically convert temperature data from Celsius to Fahrenheit and publish it as a separate time series. At the end of the guide, you will be able to write your own data transformation functions using JavaScript.

Prerequisites

  1. A valid Kaa Cloud account.
  2. Basic knowledge of creating applications and endpoints in Kaa.
  3. Familiarity with the Rule Engine and its architecture.

Setting Up Your Kaa Application and Endpoint

This section covers how to set up an application and an endpoint, as well as simulate device telemetry by publishing mock data using the “Data Publish” tab on the “Device” page.

Create an Application and Endpoint

  1. Log in to your Kaa account.
  2. Create a new application and endpoint.
    If you’re unfamiliar with this process, refer to the Getting Started Guide.

Configure Data Publishing

Once the endpoint is created, follow these steps to configure data publishing:

  1. Navigate to the “Device” page and open the “Data Publish” tab.
  2. In the “Data Publish” UI:
    • Enable the “HTTP” communication type.
    • Paste your endpoint token into the provided field on the right.
    • Ensure that “Send data samples” is selected.

Data Publish Configuration

Publish Temperature Data

To mock a device data sample, paste this JSON into “Example Payload”:

{
  "tempCelsius": 25
}

You have two options for sending the temperature data:

  • From the “Data Publish” Page:
    Click “Send Request” to send the JSON data. This method is ideal for quick testing on the Kaa platform.

  • Using cURL:
    Copy the generated cURL command, paste it into your terminal, and execute it to send the data from your laptop or any device with an internet connection.

Data Publish Send

After sending the data sample, navigate back to the “Device” page to verify that the data was received.
The received data will be displayed on the “Telemetry” widget.

Device Page Celsius

Transforming Celsius to Fahrenheit

Now that temperature data is being received, let’s configure a rule to convert it from Celsius to Fahrenheit.

Create a New Rule

  1. In the sidebar, navigate to “Rules”.
  2. Click “Add Rule” and provide a name for the rule.

Add a Trigger

  1. Click “Add Trigger”.
  2. Set the “Trigger Type” to “Endpoint time series updated”.
  3. Fill in the required details:
    • Trigger Name
    • Application
    • Application Version (leave empty to apply the trigger to all endpoints within the specified above application)
    • Endpoint(s) (leave empty to apply the trigger to all endpoints within the specified above application version)
    • Time Series: Use auto~tempCelsius to specify the time series to monitor.

For more details about triggers and their types, refer to the Rule Engine Triggers documentation.
To understand why tempCelsius is prefixed with “auto~,” see the explanation here.

This trigger will execute the rule whenever the tempCelsius value is updated.

Rule Trigger

Set Condition

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.

Add Action

Scroll down to create the action.

Note: For more information on Rule Engine actions, refer to the Rule Engine Actions documentation.

  1. Set the “Action Type” to Data Sample.
  2. Provide a Name for the action.
  3. Paste the following code into the action body:
function celsiusToFahrenheit(celsius) {
  return (celsius * 9) / 5 + 32;
}

const tempCelsius = ctx.endpoint.getTimeSeries("auto~tempCelsius").last()
    .values.value;
console.log(`tempCelsius: ${tempCelsius}`);

return {
  tempFahrenheit: celsiusToFahrenheit(tempCelsius),
};

This code takes the temperature in Celsius, converts it to Fahrenheit, and generates a new data sample, which will be transformed into auto~tempFahrenheit time series.

Rule Action

Save the Rule

Once all configurations are complete, save the rule by clicking “Create” in the top-right corner.
The rule is now ready for testing.

Monitoring Data

Navigate back to the “Data Publish” tab and repeat the steps outlined in the Publish Temperature Data section to send a temperature sample.

Verify on the Device Page

After sending the data, return to the “Device” page and check the chart.
You should now see the temperature data displayed in both Celsius and Fahrenheit.

Device Page Celsius and Fahrenheit

Troubleshooting

If the data transformation doesn’t work as expected, follow these steps for verification and debugging:

Go to “Rules” -> “Tracing”.
Here, you can view the sequence in which rules were executed.
You’ll also find detailed information about the rule execution process, which is essential for debugging errors.

Rule Tracing

Refer to the Rule Engine Tracing documentation for in-depth guidance on debugging and troubleshooting your rules.