Announce node-red-contrib-math


Hi folks,

I needed this week some very basic mathematic stuff, but I didn't want to create a function node for it. So I tried to find something on, however without success...

I'm pretty sure there must something exist already, but I gave up searching after some time and started creating my own node-red-contrib-math node. So if somebody now offers me a link to an existing node with similar functionality, I will most probably drop dead instantaneously :thinking:

It allows you to do rounding, truncating, summing, subtracting, multiplying, finding maximums, finding minimums, ... It can do that on a single number, but also on all numbers in an array.

As usual, all 'constructive' feedback is more than welcome before I publish it on npm. You can install the current version directly from Github:

npm install bartbutenaers/node-red-contrib-math

Thanks !


What does it output if you ask for 6 times 9?


Hi Simon,

I assume this is some kind of catch-question, but I'm not sure which direction you go ...

This is 6 times 9 using the math-node:


Have asked my 3 year old son, I he agrees that this should be the correct result.
Did you have anything else in mind ? Perhaps "54" for the string-lovers among us?





Uh oh... In the past, I've used node-red-contrib-statistics to do things like sum, min, max, and mean on arrays of numbers. But there are lots of other supported functions that may or may not overlap with what you have -- according to the docs:

  • bernoulliDistribution
  • chunk
  • ckmeans
  • cumulativeStdNormalProbability
  • equalIntervalBreaks
  • errorFunction
  • extent
  • factorial
  • inverseErrorFunction
  • gamma
  • geometricMean
  • harmonicMean
  • interquartileRange
  • medianAbsoluteDeviation
  • max
  • mean
  • median
  • min
  • mode
  • poissonDistribution
  • probit
  • product
  • quantile
  • quantileRank
  • rootMeanSquare
  • sample
  • sampleWithReplacement
  • sampleKurtosis
  • sampleSkewness
  • sampleStandardDeviation
  • shuffle
  • standardDeviation
  • sum
  • sumNthPowerDeviations
  • tTest
  • uniqueCount
  • variance

But I do applaud you for seeing a need and jumping right on it!


I have been guessing who wanted to see me dropping dead instantaneously, by passing me a node name. But Steve, I had never expected it would be you :face_with_raised_eyebrow:

Seems there is a small overlap between our nodes, but yours is for users above 7 years old. Mine is aimed at the remaining user group ... But now at least they can compare both nodes here, when they search the forum for 'math'. Thanks for the link!

Simon, it looks to me that you have been waiting about 3 years (until somebody would release a node-red-contrib-math node) to post that link. But I'm very proud of you :wink:


Well, now I think it's clear that this is not working out according to my "master plan"... :smirk:


Hi Bart,

It is a nice node.

Some feature request suggestions:

  1. the ability to multiply/divide/add/subtract the payload with a value that can be configured in the node.
    1.1. this value can be a constant, but it can also be a flow or global context variable.
  2. To round a number to configurable number of decimals.
  3. Currently you are applying the math to the msg.payload. It would be interesting if you can specify the source for the math like is done in the change node. Especially it would be interesting if you could apply the math to a different field of the msg.

I also think that time arithmetic functions is an interesting area for extension or a separate node.
E.g. adding X minutes or Y hours to a certain timestamp.


This will be useful for "quick and dirty" situations when it's not worth bothering with JavaScript or JSONata. Two observations:

  • It would be good to have the option of getting the result in a message property other than payload, so that the incoming payload could be preserved for use downstream.
  • It would be really nice to be able to choose the operation to be performed with the incoming message (perhaps with msg.operation), so that the same math node could be used to process different messages differently.

Perhaps more effort than you want to invest, but since (based on the rest of this thread) your life seems to be in danger, why not?


Just noticed that npm already has a node-red-contrib-math node registered, don't know if you are aware.



Hi Paul,

Yes indeed I have seen that node a couple of days ago, but there was no readme and I couldn't find the repository anywhere. So I left it behind... But what is more worse: I completely forgot about it :confounded: Damn, I have even forgotten to install it at have a look at the source code. I'm getting old ...

Thanks anyway, I will have to rename mine to e.g. node-red-contrib-basic-math, or node-red-contrib-mathematic....




Maths? The author might be mistaken for British. :blush:


2 other suggestions for the name (maybe exist already ... so to check):

  • node-red-contrib-calc
  • node-red-contrib-calculator


@BartButenaers, I haven't installed this yet (I'm waiting for the name change & publication), but can each of the functions be changed dynamically by a corresponding msg property, instead of, or overriding the setting in the node config.
ie msg.maximum or msg.minimum.


Evening guys,

Based on all the useful feedback, I have changed a series of things:

  1. The node has been renamed on Github to node-red-contrib-calc
  2. When the operation dropdown is set to an empty value, the input message needs to have a msg.operation field (containing e.g. 'sqrt' for Square Root). That was already available in my first version, but I hadn't documented it yet in the readme and the info panel ...
  3. Both the input and output field messages can now be changed on the config screen:
  4. A new operation has been added to round the input number to a specified number of decimal places:
  5. Examples have been added to the readme page
  6. A link to the node-red-contrib-statistics node has been added to the readme page
  7. I have added a field to the config screen, where a fixed constant number can be entered:
    But that is not entirely complete yet ...
    @janvda: How it works now. For example you want to round number 1.23456 to 3 decimals:
    • Normally you need to apply an input message containing an array [ 1.23456 , 3 ]
    • However when you enter 3 in the constant field, the input message will only need to contain the number 1.23456 (and the calc-node will internally create an array [ 1.23456 , 3 ]. Is that sufficient ?

Hey Paul, are you talking about point '2' ?

Hi Jan, haven't searched yet but I would 'expect' that something like that should be possible with other time-related nodes (like node-red-contrib-simpletime, or node-red-contrib-moment, or ...)?


Yes, that will be useful for what I've got in mind.


Don't you mean rounding the output?


Well I'm not native English speaking, so let's try it again: I round the input number to get a an rounded output number. Don't forget that we have to learn the English language by looking at television shows from your country like Monty Python, Mister Bean and Fawlty Towers. I'm not from Barcelona, but I still know nothing :sunglasses:


How about contacting the author of contrib-math to see if they have any interest in maintaining their node and if not, offer to take over the package?