# What am I doing wrong

**URL:** https://discourse.nodered.org/t/what-am-i-doing-wrong/9513
**Category:** General
**Created:** [27 March 2019 22:17 UTC](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513 "2019-03-27T22:17:40Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![rburton](https://avatars.discourse-cdn.com/v4/letter/r/ec9cab/32.png) [@rburton](https://discourse.nodered.org/u/rburton)
#### Post date: [27 March 2019 22:17 UTC](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513/1 "2019-03-27T22:17:40Z")

</div>

I've started playing around with function nodes and get errors and solve them with google but this one is just giving me grief, I'm sure it's just a simple mistake but can't work it out I need advice thank you.

```auto
if (msg.payload === 1); {
}
var add = (flow.get("WaterPump")+1);
var multi = (flow.get("WPWattage")+0.1666666666666667);
var T = 10;

flow.set("WaterPump", add);
flow.set("WPWattage", multi);

var add={};
var multi={};

add.payload= (flow.get("WaterPump"));
multi.payload= (flow.get("WPWattage"));
T.payload = T;

return [add, multi, T];

```

---

<div class="post-metadata">

### Author: ![michaelblight](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/michaelblight/32/445_2.png) [@michaelblight](https://discourse.nodered.org/u/michaelblight)
#### Post date: [27 March 2019 22:58 UTC](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513/2 "2019-03-27T22:58:21Z")

</div>

What is going wrong? That first semicolon after the `if` looks misplaced, but since the following block has no content it shouldn't matter anyway. Also, T starts off as an integer, and then you try to set its `payload` property, so this doesn't look right. And what if the flow variables are undefined upon entry?

---

<div class="post-metadata">

### Author: ![rburton](https://avatars.discourse-cdn.com/v4/letter/r/ec9cab/32.png) [@rburton](https://discourse.nodered.org/u/rburton)
#### Post date: [27 March 2019 23:04 UTC](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513/3 "2019-03-27T23:04:05Z")

</div>

Hi, the flow.get is working its just the var T that doesnt im getting undifined.

```auto
[{"id":"d656a3be.1bc718","type":"inject","z":"1e76e5b.37a231a","name":"","topic":"Water Pump","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":120,"wires":[["cb3082a0.d496c8"]]},{"id":"cb3082a0.d496c8","type":"function","z":"1e76e5b.37a231a","name":"Water Pump Counter","func":"if (msg.payload === 1); {\n}\nvar add = (flow.get(\"WaterPump\")+1);\nvar multi = (flow.get(\"WPWattage\")+0.1666666666666667);\nvar T = 10;\n\nflow.set(\"WaterPump\", add);\nflow.set(\"WPWattage\", multi);\n\n\nvar add={};\nvar multi={};\nvar T={};\n\nadd.payload= (flow.get(\"WaterPump\"));\nmulti.payload= (flow.get(\"WPWattage\"));\n//T.payload = (T);\n\nreturn [add, multi, T];","outputs":3,"noerr":0,"x":440,"y":120,"wires":[["16d0dc7c.63e314"],["8683e6f9.01a52"],["fb284b50.aa6268"]]},{"id":"be5ffc55.5dde2","type":"inject","z":"1e76e5b.37a231a","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":40,"wires":[["9d0580cc.ca9cd8","8ec8bb47.14df4"]]},{"id":"9d0580cc.ca9cd8","type":"change","z":"1e76e5b.37a231a","name":"","rules":[{"t":"set","p":"WaterPump","pt":"flow","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":40,"wires":[[]]},{"id":"8ec8bb47.14df4","type":"change","z":"1e76e5b.37a231a","name":"Water Pump Wattage","rules":[{"t":"set","p":"WPWattage","pt":"flow","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":80,"wires":[[]]},{"id":"8683e6f9.01a52","type":"debug","z":"1e76e5b.37a231a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":700,"y":120,"wires":[]},{"id":"16d0dc7c.63e314","type":"debug","z":"1e76e5b.37a231a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":720,"y":80,"wires":[]},{"id":"fb284b50.aa6268","type":"debug","z":"1e76e5b.37a231a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":710,"y":160,"wires":[]}]

```

---

<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: [27 March 2019 23:19 UTC](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513/4 "2019-03-27T23:19:10Z")

</div>

As @michaelblight has pointed out, `T` is declared as a number. It cannot have properties and cannot be returned as a message object.

---

<div class="post-metadata">

### Author: ![rburton](https://avatars.discourse-cdn.com/v4/letter/r/ec9cab/32.png) [@rburton](https://discourse.nodered.org/u/rburton)
#### Post date: [27 March 2019 23:30 UTC](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513/5 "2019-03-27T23:30:52Z")

</div>

Ok, so how can I get the function to return a 10?

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [28 March 2019 00:11 UTC](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513/6 "2019-03-28T00:11:19Z")

</div>

Have you read [https://nodered.org/docs/writing-functions](https://nodered.org/docs/writing-functions)? Maybe the second paragraph should say

The message is passed in as an object called `msg` . By convention it will have a `msg.payload` property containing the body of the message. **The function node must always return a msg object or null.**

I added the bold part (@knolleary - does that make sense? It is implied that it should return a msg object but never clearly stated i the opening. If you agree, I'll happily do a PR)

---

<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: [28 March 2019 00:12 UTC](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513/7 "2019-03-28T00:12:32Z")

</div>

> [@rburton](#):
>
> how can I get the function to return a 10?

You function needs to return message objects. The objects will have properties with the values you want.

You code is doing:

```auto
var T = 10;
T.payload = T;

```

It is trying to set the `payload` property of a number (which can't have properties) to itself. That is the bit that isn't working.

If you initialise T as an object, you can then set properties on it.

```auto
var T = {};
T.payload = 10;

```

---

<div class="post-metadata">

### Author: ![bohtho](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bohtho/32/6153_2.png) [@bohtho](https://discourse.nodered.org/u/bohtho)
#### Post date: [28 March 2019 07:46 UTC](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513/8 "2019-03-28T07:46:10Z")

</div>

I would also friendly suggest you google an introduction to JavaScript to get the basics down. It’s going to be a lot less trial and error, and you will see the patterns of it quickly.

---

<div class="post-metadata">

### Author: ![rburton](https://avatars.discourse-cdn.com/v4/letter/r/ec9cab/32.png) [@rburton](https://discourse.nodered.org/u/rburton)
#### Post date: [28 March 2019 22:24 UTC](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513/9 "2019-03-28T22:24:31Z")

</div>

Thanks for the info, I have it up and running now with modification.  
I got a book from the libary that covers basic terminolgy and syntax so will have a look at that too.

```auto
if (msg.payload === 1) {
}
var gob = (global.get ("PWattage"));
var math = 60;
var sum = gob / math;
var add = (flow.get("WaterPump")+1);
var multi = (flow.get("WPWattage"))+sum;

flow.set("WaterPump", add);
flow.set("WPWattage", multi);

var add={};
var multi={};
var gob={};

add.payload= (flow.get("WaterPump"));
multi.payload= (flow.get("WPWattage"));
gob.payload= Number(global.get ("PWattage"))

return [add, multi, gob];

```

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [28 March 2019 22:58 UTC](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513/10 "2019-03-28T22:58:12Z")

</div>

You can simplify that a fair bit

```auto
// redundant
// if (msg.payload === 1) { }

// NOTE: Better to name variables with meaning - 
//var gob = (global.get ("PWattage"));
var pwattage = (global.get ("PWattage"));

// var math = 60; // redundant
// NOTE: Better to name variables with meaning - the following isn't a `sum` for example
var sum = pwattage / 60;
var add = (flow.get("WaterPump")+1);
var multi = (flow.get("WPWattage"))+sum;

flow.set("WaterPump", add);
flow.set("WPWattage", multi);

var add={};
var multi={};
var gob={}; // confusing since you've repurposed a variable name containing a simple value to an object. Best to avoid this as it is hard to debug.

// Best not to `get` things again if you already have the variables in memory
add.payload= (add );
multi.payload= (multi);
gob.payload= Number(pwattage )

return [add, multi, gob];

```

---

<div class="post-metadata">

### Author: ![rburton](https://avatars.discourse-cdn.com/v4/letter/r/ec9cab/32.png) [@rburton](https://discourse.nodered.org/u/rburton)
#### Post date: [30 March 2019 22:20 UTC](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513/11 "2019-03-30T22:20:22Z")

</div>

Thanks, definatly helped me understand.

---

<div class="post-metadata">

### Author: ![rburton](https://avatars.discourse-cdn.com/v4/letter/r/ec9cab/32.png) [@rburton](https://discourse.nodered.org/u/rburton)
#### Post date: [30 March 2019 22:23 UTC](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513/12 "2019-03-30T22:23:27Z")

</div>

Is it possible to call a global in a if statement?

```auto
if (global.get("FSwitch") === "Off");
if (global.get("PSwitch") === "Off");{
}
var Fan = (global.get("FWattage"));
var Pump= (global.get("PWattage"));

var Fan={};
var Pump={};

Fan.payload= Fan;
Pump.payload= Pump;

return [Fan, Pump];

```

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [30 March 2019 23:11 UTC](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513/13 "2019-03-30T23:11:33Z")

</div>

Yes. (As long as its a properly formed if statement)

---

<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: [30 March 2019 23:18 UTC](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513/14 "2019-03-30T23:18:22Z")

</div>

Be careful here:

```auto
if (global.get("PSwitch") === "Off");{
}

```

You have a semicolon after the closing bracket - that ends the `if` block.

If you added code inbetween the braces that follow thinking it would only run if the `if` condition was true, it won't because of that semicolon. If you remove the semicolon then the braces will be the block associated with the if statement.

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [31 March 2019 00:31 UTC](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513/15 "2019-03-31T00:31:46Z")

</div>

> [@rburton](#):
>
> if (global.get("FSwitch") === "Off");

What do you expect this to do? You have an if statement ending in a semi-colin, no action statements here eithor.

---

<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: [31 March 2019 07:01 UTC](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513/16 "2019-03-31T07:01:25Z")

</div>

Some food for thought...

Do you **need** 2 outputs on your function?

2 outputs...

```auto
//NOTE, NO SEMICOLON AFTER IF()
if (global.get("PSwitch") === "Off"){
    //Do stuff if PSwitch is a string EXACTLY equal to "Off"
}
var Fan = global.get("FWattage");
var Pump= global.get("PWattage");

var Fan={};
var Pump={};

Fan.payload= Fan;
Pump.payload= Pump;

return [Fan, Pump];

```

1 output...

```auto
//NOTE, NO SEMICOLON AFTER IF()
if (global.get("PSwitch") === "Off"){
    //Do stuff if PSwitch is a string EXACTLY equal to "Off"
}
msg.fan = global.get("FWattage");
msg.pump = global.get("PWattage");
return msg;

```

---

<div class="post-metadata">

### Author: ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)
#### Post date: [31 March 2019 08:17 UTC](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513/17 "2019-03-31T08:17:57Z")

</div>

Consider a visit here  
[https://www.w3schools.com/js/](https://www.w3schools.com/js/)

---

<div class="post-metadata">

### Author: ![rburton](https://avatars.discourse-cdn.com/v4/letter/r/ec9cab/32.png) [@rburton](https://discourse.nodered.org/u/rburton)
#### Post date: [31 March 2019 20:53 UTC](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513/18 "2019-03-31T20:53:01Z")

</div>

Thanks for your inputs,

What it is that I am trying to achieve as if something is on ie a fan then print the global watts.  
So currently I have 4 devices being monitored and want the wattage added up as a total.

I've been to w3schools and its basic syntax covered, and to be honest the wiki pages are, to be honest for a beginner confusing (Node-Red)

And what does payload: circular ~ mean as I can't find any information anywhere

---

<div class="post-metadata">

### Author: ![rburton](https://avatars.discourse-cdn.com/v4/letter/r/ec9cab/32.png) [@rburton](https://discourse.nodered.org/u/rburton)
#### Post date: [31 March 2019 21:15 UTC](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513/19 "2019-03-31T21:15:00Z")

</div>

If its wrote like that it just returns msg.payload true, doesnt return the global.get which is a number

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [31 March 2019 22:33 UTC](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513/20 "2019-03-31T22:33:21Z")

</div>

Did you look at the complete msg object or just the msg.payload?

[Next page](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513.md?page=2)
