# Function node simple math operation?

**URL:** <https://discourse.nodered.org/t/function-node-simple-math-operation/8779>\
**Category:** General\
**Created:** [9 March 2019 15:11 UTC](https://discourse.nodered.org/t/function-node-simple-math-operation/8779 "2019-03-09T15:11:14Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stefanie](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stefanie/32/6589_2.png) [@Stefanie](https://discourse.nodered.org/u/Stefanie)\
**Post date:** [9 March 2019 15:11 UTC](https://discourse.nodered.org/t/function-node-simple-math-operation/8779/1 "2019-03-09T15:11:14Z")

</div>

Hi,

new here and to node-red.

I've got a sense hat for my Pi 3b+ and want to add a simple minus operation to the temperature sensor on the hat as kind of a correction factor. Basically (temperature - 5) = CorrectedTemp.  
Not sure how to put the code into the function node.

This is how the function node looks like now:

var o = msg.payload  
msg.payload = o.temperature  
return msg;

Cheers,  
Stefanie

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [9 March 2019 15:17 UTC](https://discourse.nodered.org/t/function-node-simple-math-operation/8779/2 "2019-03-09T15:17:43Z")

</div>

Assuming `msg.payload.temperature` contains a number with your temperature, you are nearly there already. 😀

`msg.payload = o.temperature - 5` should do the trick.

---

<div class="post-metadata">

**Author:** ![Stefanie](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stefanie/32/6589_2.png) [@Stefanie](https://discourse.nodered.org/u/Stefanie)\
**Post date:** [9 March 2019 15:20 UTC](https://discourse.nodered.org/t/function-node-simple-math-operation/8779/3 "2019-03-09T15:20:36Z")

</div>

Awww...now that make me blushing. Tried in all variations, just not that one.

You're a gem 🙂

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [9 March 2019 15:22 UTC](https://discourse.nodered.org/t/function-node-simple-math-operation/8779/4 "2019-03-09T15:22:29Z")

</div>

You're welcome!

BTW: If you want to keep the message structure as-is, don't overwrite the payload, just set the payload property to the new value.

```auto
msg.payload.temperature = msg.payload.temperature - 5;
return msg;

```

---

<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:** [9 March 2019 15:24 UTC](https://discourse.nodered.org/t/function-node-simple-math-operation/8779/5 "2019-03-09T15:24:56Z")

</div>

Alternatively you can use a Range node to do the offset, avoiding the use of a function node.

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [9 March 2019 15:28 UTC](https://discourse.nodered.org/t/function-node-simple-math-operation/8779/6 "2019-03-09T15:28:31Z")

</div>

Or a Change node with a JSONata expression. Endless possibilities. 😀

---

<div class="post-metadata">

**Author:** ![Stefanie](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stefanie/32/6589_2.png) [@Stefanie](https://discourse.nodered.org/u/Stefanie)\
**Post date:** [9 March 2019 15:31 UTC](https://discourse.nodered.org/t/function-node-simple-math-operation/8779/7 "2019-03-09T15:31:20Z")

</div>

I see. This seems to be an endless playground. And when one have no idea about coding at all, makes it even more fun 😃

Now I only need to get rid of the decimals and limit that to 1 or 2.

---

<div class="post-metadata">

**Author:** ![Stefanie](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stefanie/32/6589_2.png) [@Stefanie](https://discourse.nodered.org/u/Stefanie)\
**Post date:** [9 March 2019 15:39 UTC](https://discourse.nodered.org/t/function-node-simple-math-operation/8779/8 "2019-03-09T15:39:40Z")

</div>

Thanks, Colin.  
This helped also giving me Integers now. Great.

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [9 March 2019 15:40 UTC](https://discourse.nodered.org/t/function-node-simple-math-operation/8779/9 "2019-03-09T15:40:45Z")

</div>

That is why I like to describe Node-RED as a very mysterious and powerful device and it's mystery is exceeded only by it's power. 🕶

---

<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:** [9 March 2019 15:43 UTC](https://discourse.nodered.org/t/function-node-simple-math-operation/8779/10 "2019-03-09T15:43:02Z")

</div>

> [@Stefanie](#):
>
> Now I only need to get rid of the decimals and limit that to 1 or 2.

Best not to do that till you are displaying it. If you are using the dashboard nodes you can specify the precision in the node itself.

---

<div class="post-metadata">

**Author:** ![Stefanie](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stefanie/32/6589_2.png) [@Stefanie](https://discourse.nodered.org/u/Stefanie)\
**Post date:** [9 March 2019 15:56 UTC](https://discourse.nodered.org/t/function-node-simple-math-operation/8779/11 "2019-03-09T15:56:43Z")

</div>

I guess that's what the Value Format is designed for. Will play with all variants and see the difference.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [9 March 2019 15:57 UTC](https://discourse.nodered.org/t/function-node-simple-math-operation/8779/12 "2019-03-09T15:57:29Z")

</div>

> [@Stefanie](#):
>
> I see. This seems to be an endless playground. And when one have no idea about coding at all, makes it even more fun 😃
> 
> Now I only need to get rid of the decimals and limit that to 1 or 2.

Function nodes are JavaScript running in a Node.JS VM. See the Mozilla Development Network website for more help on JavaScript than you could ever imagine!

Node-RED is a great way to get into JavaScript as you can let various nodes take care of the difficult, complex bits leaving you to think about specific bits of logic.

---

<div class="post-metadata">

**Author:** ![Stefanie](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stefanie/32/6589_2.png) [@Stefanie](https://discourse.nodered.org/u/Stefanie)\
**Post date:** [13 March 2019 11:15 UTC](https://discourse.nodered.org/t/function-node-simple-math-operation/8779/13 "2019-03-13T11:15:35Z")

</div>

> [@Colin](#):
>
> Best not to do that till you are displaying it. If you are using the dashboard nodes you can specify the precision in the node itself.

It works fine with the gauge node as it allows me to set Value format and specify the number of decimals. Unfortunately the chart node does not have the option to specify the precision and when hover over into the chart, it displays all decimals. Not a big deal, but doesn't look nice.

Any other suggestions to get rid of the decimals at the output/display level for a chart node?

---

<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:** [13 March 2019 11:32 UTC](https://discourse.nodered.org/t/function-node-simple-math-operation/8779/14 "2019-03-13T11:32:43Z")

</div>

> [@Stefanie](#):
>
> Any other suggestions to get rid of the decimals at the output/display level for a chart node?

I don't know much about the chart node. It may be that you have to format it in a function node or similar first. Possibly a bit of a deficiency in the chart node.

---

<div class="post-metadata">

**Author:** ![Stefanie](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stefanie/32/6589_2.png) [@Stefanie](https://discourse.nodered.org/u/Stefanie)\
**Post date:** [13 March 2019 11:34 UTC](https://discourse.nodered.org/t/function-node-simple-math-operation/8779/15 "2019-03-13T11:34:56Z")

</div>

> [@Colin](#):
>
> I don't know much about the chart node. It may be that you have to format it in a function node or similar first. Possibly a bit of a deficiency in the chart node.

Yes, seems like. I should make a feature request if it was important.

---

<div class="post-metadata">

**Author:** ![Thewaver](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thewaver/32/11917_2.png) [@Thewaver](https://discourse.nodered.org/u/Thewaver)\
**Post date:** [8 January 2021 09:45 UTC](https://discourse.nodered.org/t/function-node-simple-math-operation/8779/16 "2021-01-08T09:45:56Z")

</div>

I have a similar problem but cant resolve using your answer (msg payload holds the temperature as a decimal ie 16.25:

var x = msg.payload;  
msg.payload = x + 2;  
returm.msg

my answer returns 16.252

Any ideas what I'm doing wrong please??

---

<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:** [8 January 2021 10:01 UTC](https://discourse.nodered.org/t/function-node-simple-math-operation/8779/17 "2021-01-08T10:01:56Z")

</div>

Your payload is not a decimal number 16.25 but is the string "16.25". When you add something to a string it uses string concatenation so it adds a 2 to the end. Try replacing the first line with  
`var x = Number(msg.payload)`  
Actually in a situation like this you should be using `const x =` rather than `var` which tells the interpretor that x will not be modified after initial setup which may allow the interpretor to generate more efficient code.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [8 January 2021 10:02 UTC](https://discourse.nodered.org/t/function-node-simple-math-operation/8779/18 "2021-01-08T10:02:07Z")

</div>

Your original payload is text not a number. Convert to a number.

---

<div class="post-metadata">

**Author:** ![Thewaver](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thewaver/32/11917_2.png) [@Thewaver](https://discourse.nodered.org/u/Thewaver)\
**Post date:** [8 January 2021 10:05 UTC](https://discourse.nodered.org/t/function-node-simple-math-operation/8779/19 "2021-01-08T10:05:43Z")

</div>

Spot on, many thanks for your answer which worked.
