# Include mathematical functions

**URL:** <https://discourse.nodered.org/t/include-mathematical-functions/27641>\
**Category:** General\
**Created:** [1 June 2020 19:01 UTC](https://discourse.nodered.org/t/include-mathematical-functions/27641 "2020-06-01T19:01:22Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Becker](https://avatars.discourse-cdn.com/v4/letter/b/c77e96/32.png) [@Becker](https://discourse.nodered.org/u/Becker)\
**Post date:** [1 June 2020 19:01 UTC](https://discourse.nodered.org/t/include-mathematical-functions/27641/1 "2020-06-01T19:01:22Z")

</div>

Hi, i have a special node from a project called NibePi, that reads out modbus data:

 ![1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/f/7fc7dcb4e2278e09ee15eb988cf07077daba5fb0.png)  
 ![2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/8/c83186ec97e1a57b9b2ce0780db09bbc6111dac3.jpeg)  
I want to make the difference from two values (spread) and multiply this value by a third, multiply the result by a constant and output the result in the dashboard as the thermal power (W).  
but I don't know how to get the values of the 3 registers as msg.payload for a function.  
Any idea ?

Greetings

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [1 June 2020 19:11 UTC](https://discourse.nodered.org/t/include-mathematical-functions/27641/2 "2020-06-01T19:11:25Z")

</div>

See [this article in the cookbook](https://cookbook.nodered.org/basic/join-streams) for an example of how to join messages into one object.  
Then you can access them in msg.payload.x and msg.payload.y to do your math.

---

<div class="post-metadata">

**Author:** ![Becker](https://avatars.discourse-cdn.com/v4/letter/b/c77e96/32.png) [@Becker](https://discourse.nodered.org/u/Becker)\
**Post date:** [1 June 2020 20:43 UTC](https://discourse.nodered.org/t/include-mathematical-functions/27641/3 "2020-06-01T20:43:44Z")

</div>

thank you, join works.  
how should i name the formula data:

 ![4](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/6/860149884196eb39bc95706164626e6c7ecda9a1.jpeg)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [1 June 2020 20:50 UTC](https://discourse.nodered.org/t/include-mathematical-functions/27641/4 "2020-06-01T20:50:37Z")

</div>

Add a debug node to the output of the join & you can copy the path to the variables.

If you dont understandm then [this](https://nodered.org/docs/user-guide/messages) and [this](https://www.youtube.com/watch?v=TDxL02SInps&list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6&index=8&t=76) should help you access the properies

---

<div class="post-metadata">

**Author:** ![Becker](https://avatars.discourse-cdn.com/v4/letter/b/c77e96/32.png) [@Becker](https://discourse.nodered.org/u/Becker)\
**Post date:** [1 June 2020 20:54 UTC](https://discourse.nodered.org/t/include-mathematical-functions/27641/5 "2020-06-01T20:54:59Z")

</div>

I got my values: {"40012":26.7,"40017":26,"40072":0} in debug  
but how can I link them mathematically and output the result ?  
("40012"-"40017") \* "40072" \* "69,45" = output

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [1 June 2020 21:06 UTC](https://discourse.nodered.org/t/include-mathematical-functions/27641/6 "2020-06-01T21:06:40Z")

</div>

the general format is...

Add a function node after the join

do your math

return the msg.

Example...

```auto
var temp = (msg.payload.a - msg.payload.b) * msg.payload.c * msg.payload.d
msg.originalPayload = msg.payload; //save for later debugging / verifying results
msg.payload = temp; 
return msg; //this causes the msg to be sent out the output & on to the next node

```

As for what `msg.payload.a` and `msg.payload.b` should _really_ be - use the copy path from the debug output

---

<div class="post-metadata">

**Author:** ![Becker](https://avatars.discourse-cdn.com/v4/letter/b/c77e96/32.png) [@Becker](https://discourse.nodered.org/u/Becker)\
**Post date:** [1 June 2020 21:24 UTC](https://discourse.nodered.org/t/include-mathematical-functions/27641/7 "2020-06-01T21:24:50Z")

</div>

I´dont work:

 ![6](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/e/0e1d6772c438e5456ad3a1ef7465e3ee28d5410e.jpeg)

---

<div class="post-metadata">

**Author:** ![Becker](https://avatars.discourse-cdn.com/v4/letter/b/c77e96/32.png) [@Becker](https://discourse.nodered.org/u/Becker)\
**Post date:** [1 June 2020 21:35 UTC](https://discourse.nodered.org/t/include-mathematical-functions/27641/8 "2020-06-01T21:35:41Z")

</div>

So it seems to work:

var temp = (msg.payload[40012] - msg.payload[40017])  
msg.originalPayload = msg.payload; //save for later debugging / verifying results  
msg.payload = temp;  
return msg;

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [1 June 2020 21:45 UTC](https://discourse.nodered.org/t/include-mathematical-functions/27641/9 "2020-06-01T21:45:00Z")

</div>

For accessing properties with strange variable names (like yours) you should use strings

```javascript
var temp = (msg.payload["40012"] - msg.payload["40017"]);
msg.originalPayload = msg.payload; //save for later debugging / verifying results
msg.payload = temp;
return msg;

```

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [2 June 2020 04:43 UTC](https://discourse.nodered.org/t/include-mathematical-functions/27641/10 "2020-06-02T04:43:55Z")

</div>

The general syntax for accessing JavaScript object (like `msg`) properties is that any property can be accessed quoted in brackets with a double or singe quote (`"` or `'`), e.g.  
`msg.payload['123']`, `msg.payload['temperature']`, or `msg.payload["my property"]`.

Alternatively if the property name consists of only alphanumerical characters or underscores, it can be accessed with just a period as a separator, e.g. `msg.payload.temp1` or `msg.payload.my_property`. The property cannot start with a number though.

---

<div class="post-metadata">

**Author:** ![Becker](https://avatars.discourse-cdn.com/v4/letter/b/c77e96/32.png) [@Becker](https://discourse.nodered.org/u/Becker)\
**Post date:** [3 June 2020 20:44 UTC](https://discourse.nodered.org/t/include-mathematical-functions/27641/11 "2020-06-03T20:44:01Z")

</div>

but it works fine without " " or ´´ !?

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [3 June 2020 20:54 UTC](https://discourse.nodered.org/t/include-mathematical-functions/27641/12 "2020-06-03T20:54:21Z")

</div>

In this case yes. The property names (object keys) are always strings (not numbers) internally in JavaScript. In the case of numbers the numeric keys are translated to strings, e.g. `msg.payload[123]` becomes `msg.payload['123']`.

The same does not apply if the key is alphanumerical though. This is why I said "the general syntax". `msg.payload[temperature]` for example would mean that variable named `temperature` would contain the key. I hope this makes sense.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [17 June 2020 20:54 UTC](https://discourse.nodered.org/t/include-mathematical-functions/27641/13 "2020-06-17T20:54:27Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
