Problem adding decimal to variable in function

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?

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)) {
    outLoop = { "payload": "BKSetPointIncrease" };
else if (msg.topic == "gpio/6" && msg.payload == "1" || (msg.payload == "BKSetPointDecrease" && repeating)) {
    outLoop = { "payload": "BKSetPointDecrease" };
else if (msg.payload == "0" || repeating > loopLimit) {
    repeating = 0;

context.set("repeating", repeating);

return [out, outLoop];

Do you update the global var boil_target?

Do you realise the vars 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?

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):

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

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

var BKSetPointStep = 0.1;

Try this instead, not tested by me.

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:

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.

Ahh - got it :slight_smile:
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.

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

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 :slight_smile:

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