Node-RED Calculator Node
node-red-contrib-calc is a Node-RED node designed to perform a range of basic mathematical operations directly within Node-RED flows. Currently at version 1.0.6, it primarily receives maintenance updates, with recent releases focusing on expanding available operations like `truncate` and enhancing result formatting. Unlike traditional JavaScript libraries, this package integrates as a graphical node in the Node-RED editor. Its core differentiator lies in providing a simplified, visual interface for common arithmetic, aggregation (min, max, average), and rounding tasks, allowing users to easily inject, process, and output numerical data within their IoT and automation workflows without writing custom functions. It is positioned as a simpler alternative to more advanced statistical packages like `node-red-contrib-statistics`.
Common errors
-
Error: Input for operation 'Multiply' must be an array of at least 2 numbers.
cause An operation requiring an array of multiple numbers (e.g., 'Multiply', 'Average') received a single number or an array with too few elements.fixEnsure the input message field (e.g., `msg.payload`) contains an array with the minimum required number of elements for the selected operation, for example, `[2, 3]` for multiplication. -
TypeError: Cannot read properties of undefined (reading 'length')
cause The configured input message field (e.g., `msg.payload`) is either undefined, null, or does not contain a valid number or an array of numbers, preventing the node from processing.fixVerify that the input message field, as configured in the node's properties, correctly points to a numeric value or an array of numbers. Use an upstream debug node to inspect `msg` content. -
Output displays 'NaN' (Not a Number) in debug sidebar.
cause An operation was performed on non-numeric input data, or a mathematical operation resulted in an undefined outcome (e.g., division by zero, or an operation on `Infinity`).fixCheck upstream nodes to ensure only valid numeric inputs are being sent to the `calc` node. Use debug nodes to inspect the input `msg.payload` or the chosen input field before it reaches the calculator node.
Warnings
- gotcha The input data structure (single number, array of numbers, or fixed-length array) required by the node depends on the specific mathematical operation selected. Supplying incorrect data types or structures can lead to errors.
- gotcha This node is designed for basic mathematical calculations and aggregations. For advanced statistical analysis, complex data transformations, or machine learning capabilities, it is generally insufficient.
- gotcha The `round` and `truncate` options for decimal places behave differently. `Round` performs standard mathematical rounding (e.g., 2.5 rounds to 3), while `truncate` simply cuts off decimal places (e.g., 2.9 truncates to 2) without rounding up.
Install
-
npm install node-red-contrib-calc -
yarn add node-red-contrib-calc -
pnpm add node-red-contrib-calc
Quickstart
[
{
"id": "b6bc5399.8385e",
"type": "calculator",
"z": "4142483e.06fca8",
"name": "",
"inputMsgField": "payload",
"outputMsgField": "payload",
"operation": "max",
"constant": "",
"round": false,
"decimals": 0,
"x": 640,
"y": 3060,
"wires": [
["4c297cba.7585a4"]
]
},
{
"id": "e5a3b930.003428",
"type": "inject",
"z": "4142483e.06fca8",
"name": "",
"topic": "",
"payload": "[321,123,333,222,111]",
"payloadType": "json",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"showConfirmation": false,
"confirmationLabel": "",
"x": 420,
"y": 3060,
"wires": [
["b6bc5399.8385e"]
]
},
{
"id": "4c297cba.7585a4",
"type": "debug",
"z": "4142483e.06fca8",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"x": 830,
"y": 3060,
"wires": []
}
]