Need help identifying what i did wrong in my function node

ok so learning javascript on the fly.
I'm trying to write a function.
to gather ambient temp and target temp. I need to set up an if ambient is greater then target then msg.payload is 1 , else 0
here is what i wrote please let me know what i did wrong or what i forgot .
i just get syntax error for debug.
also not i did a change node moving the msg.payload for ambient to msg.ambient
and msg.payload for target to msg.target

var glycolA = msg.ambient;
var glycolS = msg.target;
 
 If (glycolA>glycolS)
 {
    msg.payload=msg.payload ="1";
 } 
 Else 
 {
    msg.payload=msg.payload ="0 ";
 }
return msg;

As a test I tried this and it works fine (for me with lower case letters for if/else).

var glycolA = 15;
var glycolS = 20;
 
 if (glycolA > glycolS)
 {
    msg.payload = "1";
 } 
 else 
 {
    msg.payload = "0";
 }
return msg;
var glycolA = 20;
var glycolS = 15;
 
 if (glycolA > glycolS)
 {
    msg.payload = "1";
 } 
 else 
 {
    msg.payload = "0";
 }
return msg;

If @Colin saw this he would have a fit.

You have used capitals in you IF/Else. While not a single capital in you sentences.
(Just joking as i to have been guilty of such solecisms)

4 Likes

Indeed, I am only just recovering.

5 Likes

I Knew It Had To Be Something simple,
I get zero now but should be getting 1 when when the ambient temp is above the target temp.
I guess a guy who is poor in gramma shouldn't try to code.
But honestly, i have been learning a lot if stuff in a short period of time here, so i have a little bit of brain fatigue, maybe even beer fatigue!
im assuming its the way i refferenced the msg.ambient and msg.target
i dont think its actually pulling up the number in the message

Can you show the msg going into the function node, I suspect they maybe strings not numbers. Add a debug before the function and set it to complete message object.

[{"id":"ac59f7e267fc3d4b","type":"tab","label":"testfunciton","disabled":false,"info":"","env":[]},{"id":"41758f13d98873e3","type":"link in","z":"ac59f7e267fc3d4b","name":"","links":["4c70d126cbf9865b"],"x":144.56469068001942,"y":129.21984140639202,"wires":[["a922fcdd8a17911b"]]},{"id":"a922fcdd8a17911b","type":"change","z":"ac59f7e267fc3d4b","name":"set payload to Temperature value","rules":[{"t":"move","p":"payload.Temperature","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":319.5498962402344,"y":108.76004219055176,"wires":[["3f40293643e84bc1","22014610d4f238ee","abc25e24e1b4cb8d"]]},{"id":"3f40293643e84bc1","type":"ui_gauge","z":"ac59f7e267fc3d4b","name":"Ambient","group":"f5e2c3bf3d5a99b2","order":9,"width":0,"height":0,"gtype":"donut","title":"Ambient","label":"F","format":"{{msg.payload}}","min":0,"max":"200","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":597.9502750101586,"y":125.72012931960525,"wires":[]},{"id":"5fb069e430ec1040","type":"ui_slider","z":"ac59f7e267fc3d4b","name":"Target","label":"Target Temp","tooltip":"","group":"f5e2c3bf3d5a99b2","order":10,"width":0,"height":0,"passthru":true,"outs":"end","topic":"target","min":0,"max":"200","step":1,"x":369.0841790644309,"y":196.25263093958537,"wires":[["65cb62c27f8a575e","4e2e6f3263e9d0dd"]]},{"id":"65cb62c27f8a575e","type":"debug","z":"ac59f7e267fc3d4b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":594.4112667723941,"y":211.86670258056373,"wires":[]},{"id":"1bc3fd3f7de03fa3","type":"function","z":"ac59f7e267fc3d4b","name":"","func":"var glycolA = msg.ambient;\nvar glycolS = msg.target;\n \n if (glycolA>glycolS)\n {\n   msg.payload =\"1\";\n } \n else \n {\n    msg.payload =\"0\";\n }\nreturn msg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":579.8740353073642,"y":306.6278986621994,"wires":[["bc45c97c3fe28e8e"]]},{"id":"22014610d4f238ee","type":"debug","z":"ac59f7e267fc3d4b","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":572.6054195748493,"y":54.38001605270947,"wires":[]},{"id":"bc45c97c3fe28e8e","type":"debug","z":"ac59f7e267fc3d4b","name":"functionresults","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":816.0087661743164,"y":261.9393939971924,"wires":[]},{"id":"4e2e6f3263e9d0dd","type":"change","z":"ac59f7e267fc3d4b","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"target","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":318.5124053955078,"y":316.3194332122803,"wires":[["1bc3fd3f7de03fa3","b2413d7f8b4d15f4"]]},{"id":"abc25e24e1b4cb8d","type":"change","z":"ac59f7e267fc3d4b","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"ambient","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":757.052197155208,"y":94.76121456668139,"wires":[["1bc3fd3f7de03fa3","e913d344fc808ab8"]]},{"id":"e913d344fc808ab8","type":"debug","z":"ac59f7e267fc3d4b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":867.3881687268442,"y":38.22753664712071,"wires":[]},{"id":"b2413d7f8b4d15f4","type":"debug","z":"ac59f7e267fc3d4b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":484.57440681439056,"y":395.1973438339962,"wires":[]},{"id":"f5e2c3bf3d5a99b2","type":"ui_group","name":"Default","tab":"450ac4c9b5c2acc6","order":1,"disp":true,"width":"11","collapse":false},{"id":"450ac4c9b5c2acc6","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

yes your correct, there is in issue with msg.
debugs

Hang on - where are the values coming from - it looks to me like you are trying to operate on two messages and then moving their values where you want them ?

Take a step back to start with and explain what you are trying to achieve and where the data is coming from and going to - typically most of this can be done with switch and change nodes - which is where i tell everyone to start (i.e. as much graphical stuff as you can) and then later if you think it is worth streamlining you can come back later and write some function nodes

Craig

So the target value is coming from a ui slidebar, I then was using a change node to change to msg.target to keep from mixing things up in function node. The ambient temp is coming from mqtt, I used change mode to change msg.payload to msg.ambient

Yeah so the issue is that (from what you describe) you are trying to work on two different messages in the one function node ?

You can not do this - the function node sees each individual message as a distinct entity

So ways around this are to use the join node to join two messages together then you can manipulate them through change nodes etc or use context variables to store the values temporarily between messages

Personally i would use context variables to start with and then try and do it all with graphical nodes rather than functions to start with

Craig

Remember something like an IF/Else statement in a function node is really just a branch in a switch node, multiple IF else tests are just additional branches and nested if else blocks and just nested Switch nodes.

Its defintiely (for a beginner) much easier to follow the switch nodes if you label them all properly

Craig

So , im really not following what your saying .
I have ambient temp coming in from mqtt.
I have a change node setting topic to ambient
I have the target temp being set by a ui slide bar
Change node there is setting topic to target.
topicchangedebug

i got the join node going however the target temp is created by the slide bar. and doesnt resend message, so it shows them joined together then the target temp drops off.
joindebug

Yep so thats your problem - the slider sets the message only once - when you move it - from then on it is just dead air, whereas your MQTT messages are coming in continually.

Think of the messages as a stream of cars

Your slider control is a blue car - you set it and then you see the blue car drive past on the road until it is out of sight

You then receive an MQTT message - lets call this a yellow car - this message comes in and you grab it and do what you want - however the Blue car is already long gone and aint coming back

So what you need to do is grab the value from the slider - use a change node to put it into a context variable and then when you want to use it in your function node access the context variable and read it in

(but again you could equally access the context variable from a switch node etc)

Craig

Have a look at this simple example of setting flow variables with a change node

[{"id":"ab09c398.80dd5","type":"inject","z":"78b87e52.c9819","name":"Pretend Slider In","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"53","payloadType":"num","x":230,"y":160,"wires":[["64788784.091658"]]},{"id":"e21a514e.9c28d","type":"inject","z":"78b87e52.c9819","name":"Pretend MQTT in","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"15","payloadType":"num","x":220,"y":240,"wires":[["7db2d7e3.0fc2d8"]]},{"id":"64788784.091658","type":"change","z":"78b87e52.c9819","name":"setup Flow Variables","rules":[{"t":"set","p":"SliderIn","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":160,"wires":[[]]},{"id":"7db2d7e3.0fc2d8","type":"change","z":"78b87e52.c9819","name":"setup Flow Variables","rules":[{"t":"set","p":"AmbientTemp","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":240,"wires":[[]]}]

Craig

lol just got home from brewery was watching a video on context.
going to try to do before i look at your example.

Fair Enough - and here is an extension with a couple of switch nodes

[{"id":"78b87e52.c9819","type":"tab","label":"Flow 4","disabled":false,"info":""},{"id":"ab09c398.80dd5","type":"inject","z":"78b87e52.c9819","name":"Pretend Slider In","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"53","payloadType":"num","x":230,"y":160,"wires":[["64788784.091658"]]},{"id":"e21a514e.9c28d","type":"inject","z":"78b87e52.c9819","name":"Pretend MQTT in","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"15","payloadType":"num","x":220,"y":240,"wires":[["7db2d7e3.0fc2d8"]]},{"id":"64788784.091658","type":"change","z":"78b87e52.c9819","name":"setup Flow Variables","rules":[{"t":"set","p":"SliderIn","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":160,"wires":[[]]},{"id":"7db2d7e3.0fc2d8","type":"change","z":"78b87e52.c9819","name":"setup Flow Variables","rules":[{"t":"set","p":"AmbientTemp","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":240,"wires":[["19e21491.090b1b"]]},{"id":"19e21491.090b1b","type":"switch","z":"78b87e52.c9819","name":"Check Ambient and Branch","property":"AmbientTemp","propertyType":"flow","rules":[{"t":"gt","v":"15","vt":"num"},{"t":"lt","v":"15","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":780,"y":240,"wires":[["9e94abdb.f02148"],[]]},{"id":"9e94abdb.f02148","type":"switch","z":"78b87e52.c9819","name":"Ambient > 15 - Check Set Point","property":"SliderIn","propertyType":"flow","rules":[{"t":"gt","v":"AmbientTemp","vt":"flow"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":1030,"y":140,"wires":[["b3ed101f.00dea"],["4c9f119d.5154b"]]},{"id":"b3ed101f.00dea","type":"debug","z":"78b87e52.c9819","name":"Need Heat","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":1290,"y":80,"wires":[]},{"id":"4c9f119d.5154b","type":"debug","z":"78b87e52.c9819","name":"Otherwise Condition","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1300,"y":200,"wires":[]}]

Craig

just when I thought I was understanding things a little, you destroyed my mind @craigcurtin .
so I didn't realize how to set the flow...... even after the video I was trying to do it backwards.
however what is this doing.

also nothing shows up in debug bars when I hit inject.
and thankyou @E1cid and @Colin you really have me looking over my should and I'm trying to spell check, punctuation check, and trying to use caps in all the right places.

read this wrong earlier , in the function node i was trying to set these 2 entities as 2 different variables.
and depending if one was greater or lesser than would depend on the payload sent out by the function node. fyi

THis is using a switch node to check if the value that has been set in the Flow Context variable is > 15 - if it is it will go out to Output 1 - < 15 then output 2

Craig

Yeah but comes back to the same issue the function node only sees a single message at a time (as does all the other nodes) so you can not set a payload value like that

Craig