Calculations
A calculation computes a point from the live values of other points. Use it when the target system needs a value the devices do not have: a total, an average, a state derived from several signals, or a value that only changes beyond a deadband.
Calculated points belong to a Calculation endpoint and are mapped onto server endpoints like any device point.
Example: total power of two feeders
Section titled “Example: total power of two feeders”-
Open Calculations. The first time, click Create a calculation endpoint.
-
Click Add calculation and fill in:
Field Value Point TotalPowerType FloatRuns When an input changes Inputs a= RTU Substation A4001,b= RTU Substation A4002Script a + b -
Click Test with current values to see the result with the inputs’ live values.
-
Click Save.
-
On Mappings, map the calculation endpoint’s point
TotalPoweronto a server endpoint, like any other point.
Scripts
Section titled “Scripts”Scripts are written in Rhai, a small language that looks like JavaScript or
Rust. The value of the last expression is the result; returning () publishes nothing.
Every input is a variable with the name you gave it. Names use letters, digits and _ and do
not start with a digit. Besides the inputs, a script can use:
| Name | Meaning |
|---|---|
state |
a map that is kept between runs |
now |
the current time, Unix milliseconds |
dt |
seconds since the previous run |
Average of three temperatures
Section titled “Average of three temperatures”(t1 + t2 + t3) / 3.0Deadband: publish only changes larger than 0.5
Section titled “Deadband: publish only changes larger than 0.5”if state.last == () || (x - state.last).abs() > 0.5 { state.last = x; x} else { ()}Alarm from two signals
Section titled “Alarm from two signals”pressure > 8.0 && pump_runningUse type Boolean for this result.
Energy from power (periodic)
Section titled “Energy from power (periodic)”Set Runs to Periodically with a period of, for example, 1000 ms:
let kwh = if state.kwh == () { 0.0 } else { state.kwh };kwh += power_kw * dt / 3600.0;state.kwh = kwh;kwhIn the configuration file
Section titled “In the configuration file”"calculations": [ { "id": "c1", "endpointId": "calc", "address": "TotalPower", "type": "Float", "inputs": { "a": { "endpointId": "rtu-a", "address": "4001" }, "b": { "endpointId": "rtu-a", "address": "4002" } }, "expression": "a + b", "trigger": "OnChange" }]trigger is OnChange (default) or Periodic with periodMs (default 1000, at least 100).
The endpoint calc is an endpoint with "protocol": "calculation".