Dirk Alders 6b28a8b302 Initial nodered configuration | vor 1 Jahr | |
---|---|---|
.. | ||
examples | vor 1 Jahr | |
icons | vor 1 Jahr | |
images | vor 1 Jahr | |
.npmignore | vor 1 Jahr | |
LICENSE | vor 1 Jahr | |
README.md | vor 1 Jahr | |
math.html | vor 1 Jahr | |
math.js | vor 1 Jahr | |
package.json | vor 1 Jahr |
A Node-Red node to perform basic mathematical calculations
Run the following npm command in your Node-RED user directory (typically ~/.node-red):
npm install node-red-contrib-calc
For more advanced mathematical operations, please have a look at the node-red-contrib-statistics node.
Please buy my wife a coffee to keep her happy, while I am busy developing Node-RED stuff for you …
Four steps are involved to execute a mathematical calculation via this node:
msg.payload
, but another input message field can be selected:How many numbers should be available in the input message, depends on the operation type:
-3
is enough to calculate the absolute value. All operations like this one will also accept an array of numbers as input data. In that case the same operation will be executed on every number in the array! For example the absolute value of array [-7, -3, -9, -12]
will result in [7, 3, 9, 12]
.[2, 3]
is required to multiply, but the result will be a single output number 6
.
msg.payload
, but another output message field can be selected:The following flow shows how to search the maximum number from an array of injected numbers:
[{"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":[]}]
Following operations are available:
Input = [1, 2, 3, 4]
=> Output = 2.5
Input = [1, 2, 3, 4]
=> Output = 4
Input = [1, 2, 3, 4]
=> Output = 1
Input = 4
=> Output = 5
Input = [1, 2, 3, 4]
=> Output = [2, 3, 4, 5]
Input = 4
=> Output = 3
Input = [1, 2, 3, 4]
=> Output = [0, 1, 2, 3]
Input = 4.6
=> Output = 4
Input = [1.3, 2.5, 3.7]
=> Output = [1, 2, 3]
Input = 4.6
=> Output = 5
Input = [1.3, 2.5, 3.7]
=> Output = [2, 3, 4]
Input = 4.6
=> Output = 4
Input = [1.3, 2.5, 3.7]
=> Output = [1, 2, 3]
Input = 4.6
=> Output = 5
Input = [1.3, 2.5, 3.7]
=> Output = [1, 3, 4]
Input = [1.23456, 3]
=> Output = [1.234]
Input = [1, 2, 3, 4]
=> 1 + 2 + 3 + 4 => Output = 10
Input = [3, 2, 1]
=> 3 - 2 - 1 => Output = 0
Input = [3, 2, 1]
=> 3 * 2 * 1 => Output = 6
Input = [3, 2, 1]
=> 3 : 2 : 1 => Output = 1.5
Input = [3, 2]
=> 3 % 2 => Output = 1
Input = -4
=> Output = 4
Input = [-3, -5, -7]
=> Output = [3, 5, 7]
Input = x
=> Output = 0.xxxxx
Input = [x, x, x]
=> Output = [0.xxxxx, 0.xxxxx, 0.xxxxx]
Input = [3, 8]
=> Output = 3
or 4
or 5
or 6
or 7
or 8
Input = [3, 5, 8]
=> Output = 3
or 5
or 8
Input = [7, "text", true, 8]
=> Output = 4
Input = [9, 8, 7]
=> Output = [7, 8, 9]
Input = [7, 8, 9]
=> Output = [9, 8, 7]
Input = [2, 8]
=> Output = [2, 3, 4, 5, 6, 7, 8]
Input = [2.1, 8.6]
=> Output = [2.1, 3.2, 4.2, 5.2, 6.2, 7.2, 8.2]
Input = [2, 9, 1, 8, 3]
=> Output = 8
Input = [2, 3]
=> 23 => Output = 8
When no operation is specified in the config screen, the operation needs to be specifiedin the msg.operation
field of the input message. In the above list of available operations, the operation code is specified between angle brackets.
For example to calculate the ‘Cubic root’ of a number, the msg.operation
field should contain value cbrt
.
Almost all operations allow an optional constant to be used:
When a number is entered in this field, it *will automatically be added at the end of the input array*. When the input is a number, this node will convert it automatically to an array (containing both the number and the constant value).
Some examples with constant value 3
:
Input = 1.23456
=> Internal = [1.23456, 3]
=> Output = [1.234]
Input = 2
=> Internal = [2, 3]
=> Output = 8
Input = [7, 2, 5]
=> Internal = [7, 2, 5, 3]
=> Output = 17