The **calculate()** function is a user-defined script that enables custom calculations using [TBEL](\${siteBaseUrl}/docs\${docPlatformPrefix}/user-guide/tbel/) on telemetry and attribute data.
The **calculate()** function is a user-defined script that enables custom calculations using [TBEL](${siteBaseUrl}/docs${docPlatformPrefix}/user-guide/tbel/) on telemetry and attribute data.
It receives arguments configured in the calculated field setup, along with an additional `ctx` object that provides access to all arguments.
### Function Signature
```javascript
@ -18,8 +17,6 @@ There are three types of arguments supported in the calculated field configurati
These arguments are single values and may be of type: boolean, int64 (long), double, string, or JSON.
Attribute and Latest telemetry are single value arguments that may be one of: boolean, int64 (long), double, string and JSON.
**Example: Convert Temperature from Fahrenheit to Celsius**
```javascript
@ -95,25 +92,24 @@ for(var i = 0; i < temperature.values.size; i++) {
sum += temperature.values[i].value;
}
// use built-in function to calculate the sum
sum = t.sum();
sum = temperature.sum();
```
##### Built-in Methods for Rolling Arguments
Time series rolling arguments support built-in functions for calculations. These functions accept an optional `ignoreNaN` boolean parameter.
| `merge(other, settings)` | Merges with another rolling argument. Aligns timestamps and filling missing values with the previous available value. | <ul><li>`other` (another rolling argument)</li><li>`settings` (optional) - configuration object, supports:<ul><br/><li>`ignoreNaN` (boolean, default true) - controls whether NaN values should be ignored.</li><li>`timeWindow` (object, default {}) - defines a custom time window for filtering merged values.</li></ul></li></ul>| Merged object with timeWindow and aligned values. |
| `mergeAll(others, settings)` | Merges with multiple rolling arguments. Aligns timestamps and filling missing values with the previous available value. | <ul><li>`others` (array of rolling arguments)</li><li>`settings` (optional) - configuration object, supports:<ul><br/><li>`ignoreNaN` (boolean, default true) - controls whether NaN values should be ignored.</li><li>`timeWindow` (object, default {}) - defines a custom time window for filtering merged values.</li></ul></li></ul>| Merged object with timeWindow and aligned values.|
Assuming the following arguments and their values:
```json
{
"humidity": {
"timeWindow": {
"startTs": 1741356332086,
"endTs": 1741357232086
},
"values": [{
"ts": 1741356882759,
"value": 43
}, {
"ts": 1741356918779,
"value": 46
}]
},
"pressure": {
"timeWindow": {
"startTs": 1741356332086,
"endTs": 1741357232086
},
"values": [{
"ts": 1741357047945,
"value": 1023
}, {
"ts": 1741357056144,
"value": 1026
}, {
"ts": 1741357147391,
"value": 1025
}]
},
"temperature": {
"timeWindow": {
"startTs": 1741356332086,
"endTs": 1741357232086
},
"values": [{
"ts": 1741356874943,
"value": 76
}, {
"ts": 1741357063689,
"value": 77
}]
}
}
```
**Usage:**
```javascript
var mergedData = temperature.merge(humidity, { ignoreNaN: false });
| `merge(other, settings)` | Merges with another rolling argument. Aligns timestamps and filling missing values with the previous available value. | Merged object with `timeWindow` and aligned values. | <spantb-help-popup="calculated-field/examples/merge-functions/merge_input"tb-help-popup-placement="top"trigger-text="Input"></span><br><spantb-help-popup="calculated-field/examples/merge-functions/merge_usage"tb-help-popup-placement="top"trigger-text="Usage"></span><br><spantb-help-popup="calculated-field/examples/merge-functions/merge_output"tb-help-popup-placement="top"trigger-text="Output"></span> |
| `mergeAll(others, settings)` | Merges multiple rolling arguments. Aligns timestamps and filling missing values with the previous available value. | Merged object with `timeWindow` and aligned values. | <spantb-help-popup="calculated-field/examples/merge-functions/merge_input"tb-help-popup-placement="top"trigger-text="Input"></span><br><spantb-help-popup="calculated-field/examples/merge-functions/merge_all_usage"tb-help-popup-placement="top"trigger-text="Usage"></span><br><spantb-help-popup="calculated-field/examples/merge-functions/merge_all_output"tb-help-popup-placement="top"trigger-text="Output"></span> |
```javascript
var mergedData = temperature.mergeAll([humidity, pressure], { ignoreNaN: true });
| `other` or `others` | Another rolling argument or array of rolling arguments to merge with. |
| `settings`(optional) | Configuration object that supports: <ul><li>`ignoreNaN` - controls whether NaN values should be ignored.</li><li>`timeWindow` - defines a custom time window.</li></ul> |