# Math formula in Function node for Darkroom Timer Program

**URL:** <https://discourse.nodered.org/t/math-formula-in-function-node-for-darkroom-timer-program/38452>\
**Category:** General\
**Created:** [3 January 2021 11:16 UTC](https://discourse.nodered.org/t/math-formula-in-function-node-for-darkroom-timer-program/38452 "2021-01-03T11:16:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnrt](https://avatars.discourse-cdn.com/v4/letter/j/e36b37/32.png) [@johnrt](https://discourse.nodered.org/u/johnrt)\
**Post date:** [3 January 2021 11:16 UTC](https://discourse.nodered.org/t/math-formula-in-function-node-for-darkroom-timer-program/38452/1 "2021-01-03T11:16:38Z")

</div>

Hello,

I am a newbie to the forums and Node Red too.

I am using Node Red to control my darkroom equipment using 2 remote control sockets. The user inputs a time that triggers a counter, turns the light on and then when the counter ends switches the light off.

I would like to add to the basic functionality by displaying f/stop calculations on the dashboard for information. I need to apply the formula T x 2^+f to the message payload. The value T is the message payload input and f is the stop increase or decrease which will be specified in each function node. An increase of half a stop of light would be T x 2^+0.5

Can anyone assist in helping me for apply the formula in a way that the function node can understand correctly?

Many thanks!

John.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [3 January 2021 11:38 UTC](https://discourse.nodered.org/t/math-formula-in-function-node-for-darkroom-timer-program/38452/2 "2021-01-03T11:38:29Z")

</div>

Hi and welcome to the forum.

You may need to clarify the formula as given.

I can't parse that as valid sorry. `2^+f`? Is that `2^2+f`? (Two squared + f)

Ok, something else. (Not a problem though)

I have written an example flow that shows you how to do this, but you need to understand that the `T` and `f` must be sent at the same time - or: in the same message.

So ok: in my example `T` is defined in the `inject` node. But at some stage you are going to have to set things up.

If you are wanting `T` to be the `msg.payload` it isn't that hard to re-write the code in the `function` node.

```auto
[{"id":"c9949860.264198","type":"function","z":"703c61ac.837e1","name":"","func":"// T x 2^+f\n\nvar T = parseInt(msg.t);\nvar f = parseInt(msg.f);\n\nvar answer = T * 2^2 + f;\n\nmsg.payload = answer;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1910,"y":540,"wires":[["9626001.13ad18"]]},{"id":"2f36bac6.3f5cf6","type":"inject","z":"703c61ac.837e1","name":"Set T and f","props":[{"p":"T","v":"3","vt":"num"},{"p":"f","v":"5","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":1740,"y":540,"wires":[["c9949860.264198"]]},{"id":"9626001.13ad18","type":"debug","z":"703c61ac.837e1","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2080,"y":540,"wires":[]}]

```

Does that help?

---

<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:** [3 January 2021 12:13 UTC](https://discourse.nodered.org/t/math-formula-in-function-node-for-darkroom-timer-program/38452/3 "2021-01-03T12:13:51Z")

</div>

> [@Trying\_to\_learn](#):
>
> `var answer = T * 2^2 + f`

`^` is the bitwise XOR operator. The operator to raise to the power of is `**`

---

<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:** [3 January 2021 12:16 UTC](https://discourse.nodered.org/t/math-formula-in-function-node-for-darkroom-timer-program/38452/4 "2021-01-03T12:16:26Z")

</div>

> [@johnrt](#):
>
> apply the formula T x 2^+f to the message payload

That will be something like  
`msg.payload = msg.payload * (2**f)`  
I am never sure of the precedence of \*\* so I tend to add extra parentheses just in case.

---

<div class="post-metadata">

**Author:** ![johnrt](https://avatars.discourse-cdn.com/v4/letter/j/e36b37/32.png) [@johnrt](https://discourse.nodered.org/u/johnrt)\
**Post date:** [3 January 2021 13:22 UTC](https://discourse.nodered.org/t/math-formula-in-function-node-for-darkroom-timer-program/38452/5 "2021-01-03T13:22:03Z")

</div>

Excellent, thank you for the help. I've got it working as I wanted!

---

<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 January 2021 13:22 UTC](https://discourse.nodered.org/t/math-formula-in-function-node-for-darkroom-timer-program/38452/6 "2021-01-17T13:22:09Z")

</div>

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