# Problem adding decimal to variable in function

**URL:** <https://discourse.nodered.org/t/problem-adding-decimal-to-variable-in-function/77194>\
**Category:** General\
**Tags:** function-node\
**Created:** [3 April 2023 11:20 UTC](https://discourse.nodered.org/t/problem-adding-decimal-to-variable-in-function/77194 "2023-04-03T11:20:24Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ksga](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ksga/32/78285_2.png) [@ksga](https://discourse.nodered.org/u/ksga)\
**Post date:** [3 April 2023 11:20 UTC](https://discourse.nodered.org/t/problem-adding-decimal-to-variable-in-function/77194/1 "2023-04-03T11:20:25Z")

</div>

Hi  
I have a small problem getting a function to increment a value with decimals and not just whole numbers.  
If I change "BKSerPointStep" to an integer - the function does what I would expect. I tried adding parseFloat to allow it to increase or decrease BKSetPoint in decimal steps, it does not seem to work.

Where am I going wrong?

```auto
var BKSetPoint = parseFloat(global.get("boil_target"));
var BKSetPointStep = parseFloat(0.1);

var loopLimit = 4;
var repeating = context.get("repeating") || 0;

var out = null;
var outLoop = null;

function setBKSetPoint(dir) {
    if (dir > 0) {
        BKSetPoint += BKSetPointStep;
    }
    else {
        BKSetPoint -= BKSetPointStep;
    }
    if (BKSetPoint <= 1) { BKSetPoint = 1; }
    if (BKSetPoint >= 110) { BKSetPoint = 110; }
    out = { "payload": (BKSetPoint) };
}

if (msg.topic == "gpio/5" && msg.payload == "1" || (msg.payload == "BKSetPointIncrease" && repeating)) {
    setBKSetPoint(+1);
    outLoop = { "payload": "BKSetPointIncrease" };
    repeating++;
}
else if (msg.topic == "gpio/6" && msg.payload == "1" || (msg.payload == "BKSetPointDecrease" && repeating)) {
    setBKSetPoint(-1);
    outLoop = { "payload": "BKSetPointDecrease" };
    repeating++;
}
else if (msg.payload == "0" || repeating > loopLimit) {
    repeating = 0;
}

context.set("repeating", repeating);

return [out, outLoop];

```

---

<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:** [3 April 2023 11:28 UTC](https://discourse.nodered.org/t/problem-adding-decimal-to-variable-in-function/77194/2 "2023-04-03T11:28:23Z")

</div>

Do you update the global var `boil_target`?

Do you realise the `var`s in you function are created EVERY time the function node receives a `msg`?  
e.g. `BKSetPoint` will always be set to whatever is inside the global `boil_target` at every run of the function.

Also, I not you are "looping" - this is not ideal in an event driven environment & something I almost always recommend against. Perhaps if you share your flow (this part of it) we can better advise?

---

<div class="post-metadata">

**Author:** ![ksga](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ksga/32/78285_2.png) [@ksga](https://discourse.nodered.org/u/ksga)\
**Post date:** [3 April 2023 11:37 UTC](https://discourse.nodered.org/t/problem-adding-decimal-to-variable-in-function/77194/3 "2023-04-03T11:37:47Z")

</div>

Yes - I update the global var, either from UI or via a button (gpio 5 or 6 respectively).  
The flows are available here ("GPIO Input" and "Boil Controller" are the relevant ones):

> <https://github.com/ksga/brewpanel/blob/master/flows_brewpanel.json>

The var is used as an setpoint input to a PID controlling a brew kettle 🙂

 ![PXL_20230225_132557814](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/b/2bc7da426812cd4ebfdca92a37f7592e6ebb938f.jpeg)

---

<div class="post-metadata">

**Author:** ![edje11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edje11/32/572_2.png) [@edje11](https://discourse.nodered.org/u/edje11)\
**Post date:** [3 April 2023 11:49 UTC](https://discourse.nodered.org/t/problem-adding-decimal-to-variable-in-function/77194/4 "2023-04-03T11:49:03Z")

</div>

Parsefloat is for converting a string to a number, in this case you don't need it.

```auto
var BKSetPointStep = 0.1;

```

Try this instead, not tested by me.

---

<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:** [3 April 2023 11:53 UTC](https://discourse.nodered.org/t/problem-adding-decimal-to-variable-in-function/77194/5 "2023-04-03T11:53:25Z")

</div>

> [@ksga](#):
>
> Yes - I update the global var, either from UI or via a button (gpio 5 or 6 respectively).

What I mean is...

EVERY time a `msg` enters the function, the variable `BKSetPoint` will be assigned whatever is inside the global `boil_target`

So is `boil_target` updated once or immediately AFTER the function runs.

Remeber what I said:

> [@Steve-Mcl](#):
>
> Do you realise the `var`s in you function are created EVERY time the function node receives a `msg`?  
> e.g. `BKSetPoint` will always be set to whatever is inside the global `boil_target` at every run of the function.

For example - if `boil_target` is `123.45` then everytime this function runs, `BKSetPoint` will be set to `123.45` - no matter how many times you trigger this function, `BKSetPoint` will ALAWYS get that value - UNLESS - you update `boil_target` at some point.

HINT: use debug nodes or monitor the `global` variables in the context side bar to see if it is being updated.

---

<div class="post-metadata">

**Author:** ![ksga](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ksga/32/78285_2.png) [@ksga](https://discourse.nodered.org/u/ksga)\
**Post date:** [3 April 2023 12:24 UTC](https://discourse.nodered.org/t/problem-adding-decimal-to-variable-in-function/77194/6 "2023-04-03T12:24:14Z")

</div>

> [@Steve-Mcl](#):
>
> For example - if `boil_target` is `123.45` then everytime this function runs, `BKSetPoint` will be set to `123.45` - no matter how many times you trigger this function, `BKSetPoint` will ALAWYS get that value - UNLESS - you update `boil_target` at some point.

Ahh - got it 🙂  
The var is updated from the "out = { "payload": (BKSetPoint) };" in another flow - so the function works fine as long as I don't use a decimal number as the increment value.

So my question is - why does the first if/else in the function setBKSetPoint not accept the float value 0.1 (or any other for that matter)?  
I'll try and add a debug node to the function and see if anything helpful is shown.

---

<div class="post-metadata">

**Author:** ![ksga](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ksga/32/78285_2.png) [@ksga](https://discourse.nodered.org/u/ksga)\
**Post date:** [3 April 2023 12:32 UTC](https://discourse.nodered.org/t/problem-adding-decimal-to-variable-in-function/77194/7 "2023-04-03T12:32:04Z")

</div>

> [@edje11](#):
>
> Parsefloat is for converting a string to a number, in this case you don't need it.
> 
> ```auto
> var BKSetPointStep = 0.1;
> 
> ```
> 
> Try this instead, not tested by me.

That is where I started out. No luck ☹  
var BKSetPointStep = 1; (or any other integer for that matter) works like a charm

---

<div class="post-metadata">

**Author:** ![ksga](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ksga/32/78285_2.png) [@ksga](https://discourse.nodered.org/u/ksga)\
**Post date:** [6 April 2023 08:41 UTC](https://discourse.nodered.org/t/problem-adding-decimal-to-variable-in-function/77194/8 "2023-04-06T08:41:05Z")

</div>

Stupid mistake.  
The function was working correctly, but it was piped through an input slider with resolution set to 0.5 - so whenever the value changed 0.1 it was rounded back to the original number by the slider.  
parseFloat was indeed not needed.  
Thanks for taking the time though 🙂

---

<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:** [20 April 2023 08:41 UTC](https://discourse.nodered.org/t/problem-adding-decimal-to-variable-in-function/77194/9 "2023-04-20T08:41:40Z")

</div>

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