What am I doing wrong

So, this is my current code.

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

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

return [Fan, Pump];

And this is the payload I get.

msg : Object
object
payload: "[Circular ~]"
_msgid: "2237cea5.774e22"

Why are you assigning the variables twice ? you set Pump then set it again to an empty object... ???

(Hmm - that is copied from Steve's code above - - not sure what he was thinking... )

Maybe this instead

var Fan;
var Pump;
if (global.get("FSwitch") === "Off"){
    Fan = { payload : global.get("FWattage") };
}
if (global.get("PSwitch") === "Off");{
    Pump = { payload: global.get("PWattage") };
}
return [Fan, Pump];

Clearly, I did really meant this as a kind suggestion, you definitely need it

Haha. Didn't even spot that. I was fixing up the if statement and showing he didn't need 2 outputs. Doh!

@rburton I don't mean to be mean here but you really really need to do some JS tutorials. There are so many school boy errors in your code that don't make any sense at all.

Good luck though.

Steve.

Thanks for the inputs, ive only been doing it for the last three weeks, and in the process of a masters degree, as for reading tutorials and watching videos not all are usefull if i dont understand the errors im making, that is why im asking the questions useful answers are apprieciated.

1 Like

Yeah, you are lucky, we are not going to decide if you pass your examination, the methods you use here will not work in mathematics & physics

Unless you haven't touched the function node from the initial post (in which case you should state that), you need to provide it again so people can see what you have now cobbled together.

Hello, @zenofmud post 20 was the reconfigure post.
But now gone with @dceejay reply but with that, if the switch goes to on fan stops passing a msg, but pump is still returning a msg even when that global is switched to on.

So

  1. what is the data going into the function? Is a debug node on the node feeding the function.
  2. where are the globals first initialized
  3. copy and paste the function node so it can be examined to make sure you entered it correct (you wouldnā€™t believe how many times Iā€™ve had a typo)

Thanks @zenofmud it was indeed a typo.

I've now changed to suit what I need, Flow.get 1,2,3,4 are as follows and set to numbers.

2019-04-07%2016_01_34-Window

And with the following code I'm getting a NAN (not a number)

var Fan;
var Pump;
var PowerH;
var APump;
var T = Fan+Pump+PowerH+APump;

if (global.get("FSwitch") === "On"){
    Fan = { payload : flow.get("4")};
}else{
    Fan = { payload : 0};
}
if (global.get("PSwittch") === "On"){
    Pump = { payload: flow.get("1") };
}else{
    Pump = { payload : 0};
}
if (global.get("PHSwitch") === "On"){
    PowerH = { payload: flow.get("2") };
}else{
    PowerH = { payload : 0};
}
if (global.get("APSwitch") === "On"){
    APump = { payload: flow.get("3") };
}else{
    APump = { payload : 0};
}


//return [Fan, Pump, PowerH, APump];
msg.payload = (T);
return msg;

what is the reason for getting NaN?

You declare this first;

Then don't do anything with T until the end where you do

//return [Fan, Pump, PowerH, APump];
msg.payload = (T);

But for one thats not what you are returning as you made T a variable.
and I'm not sure what the brackets do...

Take a look at the logging events section of the function docs Redirectingā€¦

If you don't get a result you expect, try adding node.warn lines to your code
egnode.warn("T at the the end of the code is:"+T); to see where it is going wrong...

Once again, this is basic javascript know-how & understanding. You can't just declare the variable and then expect that it will get the value "from heaven"

Do like this and notice the difference and reason for the changed code

var Fan;
var Pump;
var PowerH;
var APump;

if (global.get("FSwitch") === "On"){
    Fan = { payload : flow.get("4")};
}else{
    Fan = { payload : 0};
}
if (global.get("PSwittch") === "On"){
    Pump = { payload: flow.get("1") };
}else{
    Pump = { payload : 0};
}
if (global.get("PHSwitch") === "On"){
    PowerH = { payload: flow.get("2") };
}else{
    PowerH = { payload : 0};
}
if (global.get("APSwitch") === "On"){
    APump = { payload: flow.get("3") };
}else{
    APump = { payload : 0};
}

//return [Fan, Pump, PowerH, APump];
var T = Fan+Pump+PowerH+APump;
msg.payload = T;
return msg;

Once again, @krambriw you input is appreciated, but your approach is not, maybe you own this forum but I doubt that, and you only want to see the correct answer posted all the time.
Let me just enlighten you @krambriw a forum is a place for people to come and learn find information on questions they may otherwise struggle with, I'm sure there are things that you are incapable of understanding outside of the realm of JavaScript, and hope you never need to approach a forum to get shot down by a troll like yourself.

Maybe from now on do not post on my topics!!

Thanks @ukmoose I'm away from my pc ATM but will give that ago when I'm back on, and as for the brackets they wasnt meant to be there I'd adding them trying something I'd read on a forum but was incorrect.

Not entirely certain of the end goal (too much noise, errors and mis-information in this thread) however the based on the suggestion by @krambriw i have made an educated guess. I will provide my solution at the end.

But first and most importantly...
In the interest of helping you to help yourself and help you understand why people keep saying you should perhaps learn JavaScript, PLEASE read and understand the errors in your code below...

Your original code - please read my comments...

Here is some proof of your bugs - please take a look

My version of your code...
So assuming you wish output the total value of the 4 flow variables as the msg.payload based on some globals being === "On", then this code should be more robust and easier to debug (Attach a debug node set for 'complete msg' to the output)

//attach a debug node set for 'complete msg' to the output of 
//this function to see exactly what happened inside here.

//store global vars in msg for later use & easy debugging
msg.FSwitch = global.get("FSwitch");
msg.PSwitch = global.get("PSwitch");
msg.PHSwitch = global.get("PHSwitch");
msg.APSwitch = global.get("APSwitch");

//store flow vars in msg for later use & easy debugging
msg.flowVar1 = flow.get("1") || 0; 
msg.flowVar2 = flow.get("2") || 0;
msg.flowVar3 = flow.get("3") || 0;
msg.flowVar4 = flow.get("4") || 0;

msg.payload = 0; //Reset payload to zero before adding values   

if (msg.FSwitch === "On"){
    msg.payload += msg.flowVar4;
}
if (msg.PSwitch === "On"){
    msg.payload += msg.flowVar1;
}
if (msg.PHSwitch === "On"){
    msg.payload += msg.flowVar2;
}
if (msg.APSwitch === "On"){
    msg.payload += msg.flowVar3;
}

//Only once the msg.payload has been updated do we return it
return msg;

Hope that helps.

1 Like

Thanks @Steve-Mcl for enlightening me with a informative post, that in my eyes is what a forum user should be doing to help another.
Please advise where I should be reading for information on js as everything I have found so far carries alot of unwanted bloat ect, and is not even relative to node red.

@rburton, you are right, my approach was bad, I do apologize

2 Likes

I had a quick look and it seems many tutorials for js are heavily interlocked with html - but not what you're after.

I did spot this though. It makes you do tests and it's all browser based.

I suggest you jump straight to the section "Javascript Algorithms And Data Structures Certification > Basic JavaScript"

It's all relevant. Knowledge is power.