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

**URL:** <https://discourse.nodered.org/t/its-not-adding-up-down-works-ok-fixed/34752>\
**Category:** General\
**Created:** [24 October 2020 15:53 UTC](https://discourse.nodered.org/t/its-not-adding-up-down-works-ok-fixed/34752 "2020-10-24T15:53:41Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![dougle03](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dougle03/32/32979_2.png) [@dougle03](https://discourse.nodered.org/u/dougle03)\
**Post date:** [24 October 2020 15:53 UTC](https://discourse.nodered.org/t/its-not-adding-up-down-works-ok-fixed/34752/1 "2020-10-24T15:53:41Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/f/8f6d0279e32d509c6bced744971be5ec580f8fbb.png)  
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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/3/93dbce2886661215706868e236c6417f2dd6074e.png)

Here's my flow if it helps.

```auto
[{"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}]

```

---

<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:** [24 October 2020 16:10 UTC](https://discourse.nodered.org/t/its-not-adding-up-down-works-ok-fixed/34752/2 "2020-10-24T16:10:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dougle03](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dougle03/32/32979_2.png) [@dougle03](https://discourse.nodered.org/u/dougle03)\
**Post date:** [24 October 2020 16:24 UTC](https://discourse.nodered.org/t/its-not-adding-up-down-works-ok-fixed/34752/3 "2020-10-24T16:24:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dougle03](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dougle03/32/32979_2.png) [@dougle03](https://discourse.nodered.org/u/dougle03)\
**Post date:** [24 October 2020 16:28 UTC](https://discourse.nodered.org/t/its-not-adding-up-down-works-ok-fixed/34752/4 "2020-10-24T16:28:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Kazzy](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Kazzy](https://discourse.nodered.org/u/Kazzy)\
**Post date:** [24 October 2020 16:39 UTC](https://discourse.nodered.org/t/its-not-adding-up-down-works-ok-fixed/34752/5 "2020-10-24T16:39:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [24 October 2020 16:42 UTC](https://discourse.nodered.org/t/its-not-adding-up-down-works-ok-fixed/34752/6 "2020-10-24T16:42:45Z")

</div>

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:

```auto
var level = parseFloat(global.get("cb1temp")) - 2.0;

```

---

<div class="post-metadata">

**Author:** ![dougle03](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dougle03/32/32979_2.png) [@dougle03](https://discourse.nodered.org/u/dougle03)\
**Post date:** [24 October 2020 16:54 UTC](https://discourse.nodered.org/t/its-not-adding-up-down-works-ok-fixed/34752/7 "2020-10-24T16:54:39Z")

</div>

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... 😉

Thanks Nick

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

---

<div class="post-metadata">

**Author:** ![dougle03](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dougle03/32/32979_2.png) [@dougle03](https://discourse.nodered.org/u/dougle03)\
**Post date:** [24 October 2020 16:56 UTC](https://discourse.nodered.org/t/its-not-adding-up-down-works-ok-fixed/34752/8 "2020-10-24T16:56:42Z")

</div>

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)`  
🙂

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [24 October 2020 17:16 UTC](https://discourse.nodered.org/t/its-not-adding-up-down-works-ok-fixed/34752/9 "2020-10-24T17:16:48Z")

</div>

Where/how are you setting it originally?

```auto
var aNumber = 123;
var aString = "123";

```

---

<div class="post-metadata">

**Author:** ![dougle03](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dougle03/32/32979_2.png) [@dougle03](https://discourse.nodered.org/u/dougle03)\
**Post date:** [24 October 2020 17:24 UTC](https://discourse.nodered.org/t/its-not-adding-up-down-works-ok-fixed/34752/10 "2020-10-24T17:24:25Z")

</div>

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:

```auto
[{"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}]

```

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [24 October 2020 17:30 UTC](https://discourse.nodered.org/t/its-not-adding-up-down-works-ok-fixed/34752/11 "2020-10-24T17:30:54Z")

</div>

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

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

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

```

---

<div class="post-metadata">

**Author:** ![dougle03](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dougle03/32/32979_2.png) [@dougle03](https://discourse.nodered.org/u/dougle03)\
**Post date:** [24 October 2020 17:34 UTC](https://discourse.nodered.org/t/its-not-adding-up-down-works-ok-fixed/34752/12 "2020-10-24T17:34:50Z")

</div>

Perfect!  
Setting the variable type at the start seems cleaner to me rather than doing it afterwards.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/a/eab84351c149db9c67800c0e9cd40392d2257101.png)

Thanks Nick.

---

<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:** [7 November 2020 17:34 UTC](https://discourse.nodered.org/t/its-not-adding-up-down-works-ok-fixed/34752/13 "2020-11-07T17:34:51Z")

</div>

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