# Announce node-red-contrib-math

**URL:** <https://discourse.nodered.org/t/announce-node-red-contrib-math/5100>\
**Category:** Share Your Nodes\
**Created:** [21 November 2018 20:09 UTC](https://discourse.nodered.org/t/announce-node-red-contrib-math/5100 "2018-11-21T20:09:06Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [25 November 2018 10:08 UTC](https://discourse.nodered.org/t/announce-node-red-contrib-math/5100/21 "2018-11-25T10:08:24Z")

</div>

Hey Nick, that is indeed the way of working I always prefer. But there is no readme page and I couldn't find the package on Github. And then I gave up, since I have already too many questions, issues, pull requests, ... that have never been answered. Have toom much work and too few time to keep on waiting for the rest of the world ...

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [25 November 2018 16:27 UTC](https://discourse.nodered.org/t/announce-node-red-contrib-math/5100/22 "2018-11-25T16:27:02Z")

</div>

Had a weak moment, so I have considered (during two minutes) to contact the author of the original node-red-contrib-math node and create a pull request to merge my stuff into his node.

Since that node has no documentation, I have installed it to discover its functionalities.  
Seems it contains currently only **two operations** :

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/3/30c15bb540a27c04d273fd6ad1f476d0dac5e7bf.png)

Therefore I'm not going to spend some evenings converting all my developments/documentation into a pull request.

I'm pretty sure all of you will understand 🤥

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [2 December 2018 21:05 UTC](https://discourse.nodered.org/t/announce-node-red-contrib-math/5100/23 "2018-12-02T21:05:41Z")

</div>

Hey guys,

I have published version **1.0.0** on NPM which contains some extra operations:

- Random value between a minimum and maximum value
- Random from an array of values
- Length of the input array
- Sort array numbers ascending
- Sort array numbers descending

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [5 December 2018 19:44 UTC](https://discourse.nodered.org/t/announce-node-red-contrib-math/5100/24 "2018-12-05T19:44:39Z")

</div>

I've release version **1.0.1** on npm with two new operations, to create a **range** array and calculate a **distance** inside an array:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/48c4df4474e590ae225fcce5d4f2ca1b3f822cde.png)

Now I would also like to implement a **moving average** operation.  
For example the input is an array `[1, 2, 3, 4, 5, 6, 7, 8, 9]` and the window size is `4`.  
Then the output would be following array:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/b/b55db90f6ba6751493d5514f41db9c7fd205ad94.png)

I don't think this exist yet in other contributions? It seems to me that e.g. the [node-red-node-smooth](https://www.npmjs.com/package/node-red-node-smooth) and the [node-red-contrib-average](https://www.npmjs.com/package/node-red-contrib-average) nodes calculate the average across _multiple_ messages?

But I'm not sure **where** the user should specify the window length. Indeed that would be the first operation that has as input both an array AND a separate number (see [readme](https://github.com/bartbutenaers/node-red-contrib-calc#operations) for examples of other operations). Any tips are welcome!

Bart

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [5 December 2018 20:27 UTC](https://discourse.nodered.org/t/announce-node-red-contrib-math/5100/25 "2018-12-05T20:27:43Z")

</div>

So how would the window work across multiple messages ? Or are they independent ? Do you have an actual use case for this ?

The batch node can create arrays with or without overlapping members - and then the join node can apply a reduce function to those - aka a sliding window... so I would say it can be done already.

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [5 December 2018 20:45 UTC](https://discourse.nodered.org/t/announce-node-red-contrib-math/5100/26 "2018-12-05T20:45:44Z")

</div>

Hi Dave,  
I would like to calculate it only on a **single** message (which contains an array), since my calc-node only operates on a single message. The other nodes (like average-node) seem to operate on data from multiple messages:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/7/765b5b8c2c153e14dec77ab1f3f18827df673ee2.png)

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [5 December 2018 20:54 UTC](https://discourse.nodered.org/t/announce-node-red-contrib-math/5100/27 "2018-12-05T20:54:56Z")

</div>

So a split then a join in reduce mode with a suitable expression could do same - albeit in two nodes.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [5 December 2018 21:44 UTC](https://discourse.nodered.org/t/announce-node-red-contrib-math/5100/28 "2018-12-05T21:44:27Z")

</div>

> [@BartButenaers](#):
>
> I've release version **1.0.1** on npm with two new operations

If you are adding new features should it not go to 1.1.0?

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [5 December 2018 21:56 UTC](https://discourse.nodered.org/t/announce-node-red-contrib-math/5100/29 "2018-12-05T21:56:18Z")

</div>

> [@dceejay](#):
>
> So a split then a join in reduce mode with a suitable expression could do same - albeit in two nodes

Yes that is true. But I was in the mood to spoil the NR users. Remember that in Belgium tonight Saint Nicholas will throw presents through the chimney, since it is tomorrow [Saint Nicholas Day](https://en.wikipedia.org/wiki/Saint_Nicholas_Day) ...

> [@Colin](#):
>
> If you are adding new features should it not go to 1.1.0?

Yep, you are right. I shall update it, as soon as I add a new operation ... So if you have good ideas for extra operations on my node, I can set this right much faster 😉

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [5 December 2018 22:16 UTC](https://discourse.nodered.org/t/announce-node-red-contrib-math/5100/30 "2018-12-05T22:16:17Z")

</div>

Spoil or confuse ? Too much choice (or chocolate) can be a bad thing 😉

---

<div class="post-metadata">

**Author:** ![DeanC](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/deanc/32/298_2.png) [@DeanC](https://discourse.nodered.org/u/DeanC)\
**Post date:** [5 December 2018 22:17 UTC](https://discourse.nodered.org/t/announce-node-red-contrib-math/5100/31 "2018-12-05T22:17:26Z")

</div>

> Now I would also like to implement a **moving average** operation. I don't think this exist yet in other contributions?

node-red-contrib-statistics does that. You can set a window size for all of the statistical measures and pass in arrays of data.

Or are you wanting to output an array of moving averages, like a data reduction/smoothing?

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [5 December 2018 22:24 UTC](https://discourse.nodered.org/t/announce-node-red-contrib-math/5100/32 "2018-12-05T22:24:55Z")

</div>

> [@DeanC](#):
>
> Or are you wanting to output an array of moving averages

Hey Dean, long time no seen ...  
Yes I wanted to create an **array** of moving averages, based on an input array and a window size (like in my example above). But didn't know where to specify the window size, since all _other operations_ expect (as input) an array of numbers **OR** a single number. But not both ...

---

<div class="post-metadata">

**Author:** ![DeanC](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/deanc/32/298_2.png) [@DeanC](https://discourse.nodered.org/u/DeanC)\
**Post date:** [5 December 2018 22:29 UTC](https://discourse.nodered.org/t/announce-node-red-contrib-math/5100/33 "2018-12-05T22:29:24Z")

</div>

Use the property/property type approach so that it can be specified wherever the user chooses - msg property, flow/global property, static value, calculated (jsonata).

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [5 December 2018 22:44 UTC](https://discourse.nodered.org/t/announce-node-red-contrib-math/5100/34 "2018-12-05T22:44:57Z")

</div>

> [@DeanC](#):
>
> Use the property/property type approach

That is indeed a possible solution.  
But currently - when an operation needs **two parameters** - I expect them to arrive as an array of length two. Some examples:

- Round 1.23456 at a 3 decimal places:

- 2 to the **power** of 3:

But I cannot use this approach when the input is both an array **AND** a number (since the extra number at the end of the array will just be considered as a longer array). So when I now require an extra field to be filled in, it might be confusing.

Perhaps it is better if I drop this new operation.  
Moreover, I don't want to give Dave an overdose of chocolate ...

---

<div class="post-metadata">

**Author:** ![DeanC](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/deanc/32/298_2.png) [@DeanC](https://discourse.nodered.org/u/DeanC)\
**Post date:** [6 December 2018 02:50 UTC](https://discourse.nodered.org/t/announce-node-red-contrib-math/5100/35 "2018-12-06T02:50:34Z")

</div>

The property/property type approach will handle this as you can do deep object specification ie `msg.payload[2]` or `msg.payload[1]` or `msg.payload.length` or even `msg.payload[1][0]` (that's an array of arrays).

In all honesty, hard coding stuff like this just makes life difficult for everyone and really reduces the usefulness of utility nodes like this.

Let me give you an example. I have a climate monitoring flow that passes around an object containing current temp, humidity, and air pressure readings. All these readings need to stay together as they are related and represent a complex measurement. Along the way I do some calculations for heat factor which come out with an excessive number of decimal places. It would be nice to be able to pass my object through your node and tell it to round the heat factor property to a sensible 1 decimal place. In your current approach, I need to save the original climate object somewhere, extract the heat factor property, turn it into an array, add the required decimal places, pass it through your node, extract the result from the array, and insert the result back into the original climate object before sending it on its merry way. I would much prefer to just be able to pass the entire object through your node and tell it to operate on a specific property of that object.

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [6 December 2018 06:57 UTC](https://discourse.nodered.org/t/announce-node-red-contrib-math/5100/36 "2018-12-06T06:57:45Z")

</div>

Morning Dean, that is indeed correct. What a pitty that you were not available at the start of this discussion. Now I have published it with the array behaviour already ...

---

<div class="post-metadata">

**Author:** ![DeanC](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/deanc/32/298_2.png) [@DeanC](https://discourse.nodered.org/u/DeanC)\
**Post date:** [6 December 2018 09:03 UTC](https://discourse.nodered.org/t/announce-node-red-contrib-math/5100/37 "2018-12-06T09:03:38Z")

</div>

You can still do it and maintain backwards compatibility - the property/property type approach is really flexible.

Give me a shout if you would like some assistance

---

<div class="post-metadata">

**Author:** ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)\
**Post date:** [6 December 2018 09:35 UTC](https://discourse.nodered.org/t/announce-node-red-contrib-math/5100/38 "2018-12-06T09:35:46Z")

</div>

@BartButenaers

Just a modest question:

Did you consider the effort/effectiveness' relationship for this contrib node?

It seems to me, that it's a wrap of the Math() function. I generally use Math() in function nodes or in a contrib node, I can't see the benefit of using your node in a flow.

Could you show some practical examples?

E.g. who would use `Hyperbolic cosine (cosh)` in a node-red flow?

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [6 December 2018 10:09 UTC](https://discourse.nodered.org/t/announce-node-red-contrib-math/5100/39 "2018-12-06T10:09:05Z")

</div>

> [@cflurin](#):
>
> E.g. who would use `Hyperbolic cosine (cosh)` in a node-red flow?

@cflurin: You haven given a very bad example 🤨 Normally I try to add documentation for all functionalities, but on my readme page you will see that I haven't even added examples for those kind of operations. Have just added them _for completeness_, but they are at the **end** of the list to make sure they don't disturb users. I have experienced in the last two years that people do complete crazy things with my nodes, so it is just there in case some weird mind needs it...

And yes, performance will be much better when you just call the Math functions in a Function node (without type checks, and so on ....). However, this node is aimed at another user group, who prefer to search hours on Google to find an out-of-the-box solutions instead of writing a single line of Javascript... When I'm in a social mood, I develop nodes that I'm never going to use myself ... And you are also not part of my target audience 😉

> [@DeanC](#):
>
> You can still do it and maintain backwards compatibility

Have put it on my TODO list...

---

<div class="post-metadata">

**Author:** ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)\
**Post date:** [6 December 2018 10:48 UTC](https://discourse.nodered.org/t/announce-node-red-contrib-math/5100/40 "2018-12-06T10:48:27Z")

</div>

Hi Bart,

Well, it's your node and it's open source 😀. I hope some users will post interesting flow examples.

Generally I try to use the core nodes and only add specific contrib-nodes.  
This way I can reduce the software maintenance and increase the stability.

[Previous page](https://discourse.nodered.org/t/announce-node-red-contrib-math/5100.md?page=1)

[Next page](https://discourse.nodered.org/t/announce-node-red-contrib-math/5100.md?page=3)
