It's not adding up - Down works ok - FIXED

So This works as expected:
var level=global.get("cb1temp") - 2.0;

But this does not:
var level=global.get("cb1temp") + 2.0;

The only difference is that the first one correctly subtracts 2.0 from the global variable cb1temp whereas the second one adds 0.2 (regardless of the value, 2.0 in this case....)
Am I missing something fundamental in the syntax?
I really wanted to use a global variable number in a switch node where each output was plus or minus a single variable, but think that needs j: query string that is beyond my capability at the min....
Thanks

Here's the debug output:
image
Notice the set temp is 11.0, the low number is correct at 9, whilst the high number is 11.02.

And here's the Function node for the high number.
image

Here's my flow if it helps.

[{"id":"46a21413.f5194c","type":"server-state-changed","z":"ad006b4a.4c1a08","g":"59f5633d.67713c","name":"CoolBox 1 Temp","server":"32f2c474.d2eeec","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_number.cb1_temp","entityidfiltertype":"exact","outputinitially":true,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":false,"x":1040,"y":1184,"wires":[["c0092ed4.e226f"]]},{"id":"c0092ed4.e226f","type":"function","z":"ad006b4a.4c1a08","g":"59f5633d.67713c","name":"Set GlobalVar CB1 - cb1temp","func":"var level=msg.payload;\nglobal.set(\"cb1temp\", level);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1336,"y":1184,"wires":[["70d86437.e413fc","b1cb9902.6678b8"]]},{"id":"70d86437.e413fc","type":"function","z":"ad006b4a.4c1a08","g":"59f5633d.67713c","name":"Set low level","func":"var level=global.get(\"cb1temp\") - 2.0;\nglobal.set(\"cb1templo\", level);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1558,"y":1184,"wires":[[]]},{"id":"b1cb9902.6678b8","type":"function","z":"ad006b4a.4c1a08","g":"59f5633d.67713c","name":"Set high level","func":"var level=global.get(\"cb1temp\") + 2.0;\nglobal.set(\"cb1temphi\", level);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1568,"y":1216,"wires":[[]]},{"id":"32f2c474.d2eeec","type":"server","name":"Home Assistant","legacy":false,"addon":false,"rejectUnauthorizedCerts":false,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

To post a flow here please use the </> button at the top of the forum edit window.

Please show us the evidence that it does not do what you expect. If you read the node-red docs page on Writing Functions it will show you how you can use node.warn() to debug your function. So for example you could put
node.warn("before: " + global.get("cb1temp"))
before the line in the function that is not working, and
node.warn("After: " + level))
after it. Then you will see those in the debug pane.

Hi,
Edited post as requested.
Interestingly the function does work, just not as expected. so no error.

FWIW, I'm sure it's just a syntax error, var level=global.get("cb1temp") + 2.0; but I'm getting out of my comfort zone wading into this; I got into NR because it eskews coding for a graphical approach.

just on mobile so can't check... maybe try changing + 2.0; to be += 2.0;

The problem is that you've stored cb1temp as a String type - not a number.

When you do "11.0" - 2.0, JavaScript knows you are doing a subtraction and converts "11.0" to the number 11.0 and then does the sum - giving you 9 as the result.

When you do "11.0" + 2.0, JavaScript thinks you want to concatenate Strings, so you get "11.0" + "2" which is "11.02"

The solution is to either convert cb1temp to a number before you store it in context, or after you retrieve it.

For example:

var level = parseFloat(global.get("cb1temp")) - 2.0;
1 Like

Ahh that makes sense, when I created the variable I didn't specify a type.
So var level = parseFloat(global.get("cb1temp")) - 2.0; works in the second function, but how do I make the original creation of the variable a number to start with?

Side note: The "" around the original variable should have been my clue... :wink:

Thanks Nick

Edit: I just checked the docs under function node but can't see reference to the variable type?

Hi,
I did just try that before I saw Nick's reply. I got an error when I did.
ReferenceError: Invalid left-hand side in assignment (line 1, col 18)
:slight_smile:

Where/how are you setting it originally?

var aNumber = 123;
var aString = "123";

Hi
A HA Event state node pulls in a temp value and sets that value using a function node
var level=msg.payload; global.set("cb1temp", level); return msg;

Just not sure how to specify the var type in the node?

Flow for reference:

[{"id":"46a21413.f5194c","type":"server-state-changed","z":"ad006b4a.4c1a08","g":"59f5633d.67713c","name":"CoolBox 1 Temp","server":"32f2c474.d2eeec","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_number.cb1_temp","entityidfiltertype":"exact","outputinitially":true,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":false,"x":1040,"y":1200,"wires":[["c0092ed4.e226f"]]},{"id":"c0092ed4.e226f","type":"function","z":"ad006b4a.4c1a08","g":"59f5633d.67713c","name":"Set GlobalVar CB1 - cb1temp","func":"var level=msg.payload;\nglobal.set(\"cb1temp\", level);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1336,"y":1200,"wires":[["70d86437.e413fc","b1cb9902.6678b8"]]},{"id":"32f2c474.d2eeec","type":"server","name":"Home Assistant","legacy":false,"addon":false,"rejectUnauthorizedCerts":false,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

Sorry, don't know anything about the HA nodes.

You could always do the conversion on that function...

global.set("cb1temp", parseFloat(level))

Perfect!
Setting the variable type at the start seems cleaner to me rather than doing it afterwards.
image

Thanks Nick.

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