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.
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.
Once the endpoint is created, follow these steps to configure data publishing:
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.
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.
Now that temperature data is being received, let’s configure a rule to convert it from Celsius to Fahrenheit.
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.
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.
Scroll down to create the action.
Note: For more information on Rule Engine actions, refer to the Rule Engine Actions documentation.
Data Sample
.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.
Once all configurations are complete, save the rule by clicking “Create” in the top-right corner.
The rule is now ready for testing.
Navigate back to the “Data Publish” tab and repeat the steps outlined in the Publish Temperature Data section to send a temperature sample.
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.
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.
Refer to the Rule Engine Tracing documentation for in-depth guidance on debugging and troubleshooting your rules.