- Use case
- Prerequisites
- Step 1: Adding temperature transformation node
- Step 2: Validation script debugging
- TL;DR
- Next steps
Use case
Let’s assume your device is using custom sensor to collect and push temperature readings to ThingsBoard. This sensor collects temperature readings in °F and you would like to convert them to °C before storage into the database and visualization.
In this tutorial we will configure ThingsBoard Rule Engine to modify temperature readings according to a formula:
[°C] = ([°F] - 32) × 5/9.
Prerequisites
We assume you have completed the following guides and reviewed the articles listed below:
- Getting Started guide.
- Rule Engine Overview.
Step 1: Adding temperature transformation node
We will modify default rule chain and will add transformation rule node with temperature transformation script. We will place this rule node between default “message type switch” and “save timeseries” rule nodes. Please note that we have removed irrelevant rule nodes from the root rule chain as well.
Let’s assume the data that arrive to a system may or may not have the “temperature” field. We will treat all data that does not have “temperature” field as valid. In order to do this we will use the following function
1
2
3
4
5
6
7
8
9
10
function precisionRound(number, precision) {
var factor = Math.pow(10, precision);
return Math.round(number * factor) / factor;
}
if (typeof msg.temperature !== 'undefined'){
msg.temperature = precisionRound((msg.temperature -32) * 5 / 9, 2);
}
return {msg: msg, metadata: metadata, msgType: msgType};
Step 2: Validation script debugging
Let’s check that our script is correct by using built-in “Test transformer function” button
You can check few more cases, for example when temperature is not set.
TL;DR
Download and import attached json file with a rule chain from this tutorial. Don’t forget to mark new rule chain as “root”.
Next steps
-
入门指南 - 快速学习ThingsBoard相关功能。
-
安装指南 - 学习如何在各种操作系统上安装ThingsBoard。
-
连接设备 - 学习如何根据你的连接方式或解决方案连接设备。
-
可 视 化 - 学习如何配置复杂的ThingsBoard仪表板说明。
-
数据分析 - 学习如何使用规则引擎执行基本的分析任务。
-
硬件样品 - 学习如何将各种硬件平台连接到ThingsBoard。
-
高级功能 - 学习高级ThingsBoard功能。
-
开发指南 - 学习ThingsBoard中的贡献和开发。