What am I doing wrong

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.

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];

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?

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

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

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

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

Have you read 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)

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

You code is doing:

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.

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

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.

1 Like

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.

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];

You can simplify that a fair bit

// 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];
1 Like

Thanks, definatly helped me understand.

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

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];

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

Be careful here:

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.

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

Some food for thought...

Do you need 2 outputs on your function?

2 outputs...

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

//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;

Consider a visit here
https://www.w3schools.com/js/

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

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

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