# Adding variables

**URL:** https://discourse.nodered.org/t/adding-variables/12272
**Category:** General
**Created:** [15 June 2019 18:14 UTC](https://discourse.nodered.org/t/adding-variables/12272 "2019-06-15T18:14:42Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Duncan](https://avatars.discourse-cdn.com/v4/letter/d/ecc23a/32.png) [@Duncan](https://discourse.nodered.org/u/Duncan)
#### Post date: [15 June 2019 18:14 UTC](https://discourse.nodered.org/t/adding-variables/12272/1 "2019-06-15T18:14:42Z")

</div>

I am trying to convert time to total minutes. The result is always 601 instead of 61. What am I doing wrong?

thanks!

global.set(msg.topic,msg.payload);  
var hour = global.get("timehour");  
var minute = global.get("timeminute");  
var tottime = (hour\*60)+minute;  
if (hour!==undefined && minute!==undefined) {  
var mytime = new Date();  
mytime.setHours(hour, minute, 0);  
mytime.setMilliseconds(0);  
mytime.setFullYear(2000,01,01);  
global.set("start",mytime.getTime());  
}  
msg.payload = tottime;  
return msg;

---

<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: [15 June 2019 19:37 UTC](https://discourse.nodered.org/t/adding-variables/12272/2 "2019-06-15T19:37:48Z")

</div>

What values are in those globals? Look in the context browser on the sidebar (where debug output normally is)

Also, you are setting tottime before checking the variables.

What I often do to debug these situations is instead of creating temp vars, put everything in msg the put a debug node after the function node (debug node set to output "complete object")

E.g.

```auto
global.set(msg.topic,msg.payload);
msg.hour = global.get("timehour");
msg.minute = global.get("timeminute");
var tottime = (msg.hour*60)+msg.minute;
if (msg.hour!==undefined && msg.minute!==undefined) {
var mytime = new Date();
mytime.setHours(hour, minute, 0);
mytime.setMilliseconds(0);
mytime.setFullYear(2000,01,01);
global.set("start",mytime.getTime());
msg.mytime = mytime;
}
msg.payload = tottime;
return msg;

```

Now when the function node outputs a msg, you can inspect the values it used to compute tottime.

---

<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: [15 June 2019 19:57 UTC](https://discourse.nodered.org/t/adding-variables/12272/3 "2019-06-15T19:57:38Z")

</div>

> [@Duncan](#):
>
> ```auto
> var hour = global.get("timehour");
> var minute = global.get("timeminute");
> var tottime = (hour*60)+minute;
> 
> ```

The most likely explanation for that resulting in `601` rather than `61` is that `minute` is a String and not a number. This means it is joining the strings together `60+"1" = "601"` rather than handling both as numbers and adding them as you would expect.

That is where Steve's suggestion comes in - using the context sidebar you can confirm their types (ie, are they displayed with surounding `"quotes"` to indicate they are strings).

The quick fix is to use `parseInt()` (assuming they are integers and not floats) to convert the String to a number.

```auto
msg.hour = parseInt(global.get("timehour"));
msg.minute = parseInt(global.get("timeminute"));

```

But you may want to consider looking at where you are storing those values and whether you want to convert them to numbers before storing them instead.

---

<div class="post-metadata">

### Author: ![Duncan](https://avatars.discourse-cdn.com/v4/letter/d/ecc23a/32.png) [@Duncan](https://discourse.nodered.org/u/Duncan)
#### Post date: [16 June 2019 14:43 UTC](https://discourse.nodered.org/t/adding-variables/12272/4 "2019-06-16T14:43:52Z")

</div>

Thanks for the feedback you were right about the "string". This is what works! Learning everyday.

global.set(msg.topic,msg.payload);  
var hour = global.get("timehour");  
var minute = global.get("timeminute");  
if (hour!==undefined && minute!==undefined) {  
var mytime = new Date();  
mytime.setHours(hour, minute, 0);  
mytime.setMilliseconds(0);  
mytime.setFullYear(2000,01,01);  
global.set("start",mytime.getTime());  
}  
msg.hour = parseInt(global.get("timehour"));  
msg.minute = parseInt(global.get("timeminute"));  
msg.payload = (msg.hour\*60)+msg.minute;  
return msg;
