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.
It receives arguments configured in the calculated field setup, along with an additional `ctx` object that stores `msgTs` and provides access to all arguments.
### Function Signature
@ -44,7 +44,7 @@ Let's modify the function that converts Fahrenheit to Celsius to also return the
| `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> |
| `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> |
| `other` or `others` | Another rolling argument or array of rolling arguments to merge with. |
@ -187,29 +188,51 @@ function calculate(ctx, temperature, defrost) {
The result is a list of issues that may be used to configure alarm rules:
```json
[{
[
{
"ts": 1741613833843,
"values": {
"issue": {
"temperature": -3.12,
"defrostState": false
}
"issue": {
"temperature": -3.12,
"defrostState": false
}
}
}, {
},
{
"ts": 1741613923848,
"values": {
"issue": {
"temperature": -4.16,
"defrostState": false
}
"issue": {
"temperature": -4.16,
"defrostState": false
}
}
}]
}
]
```
### Function return format
The return format depends on the output type configured in the calculated field settings (default: **Time Series**).
### Message timestamp
The `ctx` object also includes property `msgTs`, which represents the timestamp of the incoming telemetry message that triggered the calculated field execution in milliseconds.
You can use `ctx.msgTs` to set the timestamp of the resulting output explicitly when returning a time series object.
```javascript
var temperatureC = (temperatureF - 32) / 1.8;
return {
ts: ctx.msgTs,
values: {
"temperatureC": toFixed(temperatureC, 2)
}
}
```
This ensures that the calculated data point aligns with the timestamp of the triggering telemetry.
##### Time Series Output
The function must return a JSON object or array with or without a timestamp.
@ -246,7 +269,7 @@ With timestamp:
"someArray": [1,2,3],
"someNestedObject": {"key": "value"}
}
}
}
}
```
@ -265,7 +288,7 @@ Array containing multiple timestamps and different values of the `airDensity` :