Conditional logic to trigger On/Off

Greetings,

I have been struggling to put together this logic.

If pvwatts >= 800 and soc >= 80 then 2 (turn on inverter) else 3 (turn off inverter). I have tried with the javascript below but it never works...as it never combines both inputs.

var soc = Number(msg.payload.soc);
var pvwatts = Number(msg.payload.pvwatts);

if (pvwatts >= 800 && soc >= 80) {
    msg.payload = 2;
}
else {
    msg.payload = 3;
}
return msg;

Then i tried with switch nodes and change nodes but i dont know how to merge the two, like if SoC and PVwatts match then true.

[{"id":"eec3780d.de5d48","type":"switch","z":"fbf7f30b.ec577","name":"SoC Switch","property":"payload.value","propertyType":"msg","rules":[{"t":"gt","v":"80","vt":"num"},{"t":"lt","v":"30","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":610,"y":1300,"wires":[["c2b5ec31.cc86b"],["b2fe8568.be5b08"]]},{"id":"c2b5ec31.cc86b","type":"change","z":"fbf7f30b.ec577","name":"Turn Inverter On","rules":[{"t":"set","p":"payload","pt":"msg","to":"2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":880,"y":1280,"wires":[["6198a44a.3b204c"]]},{"id":"b2fe8568.be5b08","type":"change","z":"fbf7f30b.ec577","name":"Turn Inverter Off","rules":[{"t":"set","p":"payload","pt":"msg","to":"3","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":880,"y":1320,"wires":[[]]},{"id":"73f256c0.29d388","type":"switch","z":"fbf7f30b.ec577","name":"PVWatts Switch","property":"payload.value","propertyType":"msg","rules":[{"t":"gt","v":"800","vt":"num"},{"t":"lt","v":"800","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":620,"y":1340,"wires":[["764190a2.3832b"],["ba10e458.b736b8"]]},{"id":"764190a2.3832b","type":"change","z":"fbf7f30b.ec577","name":"Turn Inverter On","rules":[{"t":"set","p":"payload","pt":"msg","to":"2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":880,"y":1380,"wires":[["6198a44a.3b204c"]]},{"id":"ba10e458.b736b8","type":"change","z":"fbf7f30b.ec577","name":"Turn Inverter Off","rules":[{"t":"set","p":"payload","pt":"msg","to":"3","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":880,"y":1420,"wires":[[]]}]

There's nothing wrong with your code. so I would try to check that the numbers coming in are what you think they are. Add the line in the code below so that you can see what the incoming data is.

var soc = Number(msg.payload.soc);
var pvwatts = Number(msg.payload.pvwatts);

node.status({text:soc+" "+pvwatts});

if (pvwatts >= 800 && soc >= 80) {
    msg.payload = 2;
}
else {
    msg.payload = 3;
}
return msg;

Are the two bus of data contained in one message or are you expecting node-red to merge two different messages?

if i understand it correctly there two inbound messages.

msg.payload.soc and msg.payload.pvwatts, heres the export.

[{"id":"b46dc22c.22401","type":"modbus-write","z":"fbf7f30b.ec577","name":"Turn Inverter On/OFF","showStatusActivities":false,"showErrors":false,"unitid":"237","dataType":"HoldingRegister","adr":"33","quantity":"1","server":"b6c5d7eb.2e0208","x":1120,"y":1300,"wires":[[],[]]},{"id":"b1d95ad4.04b2b8","type":"delay","z":"fbf7f30b.ec577","name":"","pauseType":"rate","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"15","rateUnits":"minute","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"x":900,"y":1300,"wires":[["b46dc22c.22401"]]},{"id":"16753ff1.c9db6","type":"function","z":"fbf7f30b.ec577","name":"","func":"var soc = Number(msg.payload.soc);\nvar pvwatts = Number(msg.payload.pvwatts);\n\nnode.status({text:soc+\" \"+pvwatts});\n\nif (pvwatts >= 800 && soc >= 80) {\n    msg.payload = 2;\n}\nelse {\n    msg.payload = 3;\n}\nreturn msg;","outputs":1,"noerr":0,"x":710,"y":1300,"wires":[["b1d95ad4.04b2b8"]]},{"id":"f806732d.fede5","type":"function","z":"fbf7f30b.ec577","name":"6","func":"msg.payload.pvwatts = Number(msg.payload.value.toFixed(2));\nreturn msg;","outputs":1,"noerr":0,"x":770,"y":400,"wires":[["69f80bd9.c4a064","16753ff1.c9db6"]]},{"id":"36f66ead.10b382","type":"function","z":"fbf7f30b.ec577","name":"4","func":"msg.payload.soc = msg.payload.value.toFixed(2);\nreturn msg;","outputs":1,"noerr":0,"x":770,"y":100,"wires":[["95e52825.fec8d8","16753ff1.c9db6"]]},{"id":"8b70634d.44e87","type":"switch","z":"fbf7f30b.ec577","name":"Splitter","property":"topic","propertyType":"msg","rules":[{"t":"cont","v":"/Ac/ActiveIn/Source","vt":"str"},{"t":"cont","v":"/Dc/Battery/State","vt":"str"},{"t":"cont","v":"/Soc","vt":"str"},{"t":"cont","v":"/Dc/0/Current","vt":"str"},{"t":"cont","v":"/Dc/Battery/Power","vt":"str"},{"t":"cont","v":"/Yield/Power","vt":"str"},{"t":"cont","v":"/Pv/I","vt":"str"},{"t":"cont","v":"/Pv/V","vt":"str"},{"t":"cont","v":"/Dc/0/Voltage","vt":"str"},{"t":"cont","v":"/Ac/Grid/L1/Power","vt":"str"},{"t":"cont","v":"/Ac/Consumption/L1/Power","vt":"str"},{"t":"cont","v":"/TimeToGo","vt":"str"}],"checkall":"true","repair":false,"outputs":12,"x":463,"y":99,"wires":[["e22ce84c.777f38"],["f4fae6f2.335cb8"],["36f66ead.10b382"],["a62558c9.5c0d48"],["beb46b66.f104f8"],["f806732d.fede5"],["84c294e8.6ead68"],["8029c8d5.bbd138"],["7c104b86.684e24","30e267f2.55cde"],["cef3739.244bb9"],["f5f1056d.469d68"],["2810c6ee.84df7a"]],"outputLabels":["1","2","3","4","5","6","7","8","9","","",""]},{"id":"b6c5d7eb.2e0208","type":"modbus-client","z":"","name":"VictronVenus","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":false,"tcpHost":"192.168.3.30","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":"1","commandDelay":"1","clientTimeout":"1000","reconnectTimeout":"2000"}]

If there are two separate inbound messages you will need to merge them into one message BEFORE the function node. Generally nodes in Node-RED have no memory, they get a message, process it and pass it on.

There are however ways to join messages, take a look at the example from the cookbook
https://cookbook.nodered.org/basic/join-streams

That makes sense but im having difficulty ingesting in the javascript node. This is what i attempted:

[{"id":"8b70634d.44e87","type":"switch","z":"fbf7f30b.ec577","name":"Splitter","property":"topic","propertyType":"msg","rules":[{"t":"cont","v":"/Ac/ActiveIn/Source","vt":"str"},{"t":"cont","v":"/Dc/Battery/State","vt":"str"},{"t":"cont","v":"/Soc","vt":"str"},{"t":"cont","v":"/Dc/0/Current","vt":"str"},{"t":"cont","v":"/Dc/Battery/Power","vt":"str"},{"t":"cont","v":"/Yield/Power","vt":"str"},{"t":"cont","v":"/Pv/I","vt":"str"},{"t":"cont","v":"/Pv/V","vt":"str"},{"t":"cont","v":"/Dc/0/Voltage","vt":"str"},{"t":"cont","v":"/Ac/Grid/L1/Power","vt":"str"},{"t":"cont","v":"/Ac/Consumption/L1/Power","vt":"str"},{"t":"cont","v":"/TimeToGo","vt":"str"}],"checkall":"true","repair":false,"outputs":12,"x":463,"y":99,"wires":[["e22ce84c.777f38"],["f4fae6f2.335cb8"],["36f66ead.10b382"],["a62558c9.5c0d48"],["beb46b66.f104f8"],["f806732d.fede5"],["84c294e8.6ead68"],["8029c8d5.bbd138"],["7c104b86.684e24","30e267f2.55cde"],["cef3739.244bb9"],["f5f1056d.469d68"],["2810c6ee.84df7a"]],"outputLabels":["1","2","3","4","5","6","7","8","9","","",""]},{"id":"36f66ead.10b382","type":"function","z":"fbf7f30b.ec577","name":"4","func":"msg.payload.soc = msg.payload.value.toFixed(2);\nmsg.topic = \"soc\";\nreturn msg;","outputs":1,"noerr":0,"x":770,"y":100,"wires":[["95e52825.fec8d8","a0ea1d7.45765e","42834c7f.6dea04"]]},{"id":"f806732d.fede5","type":"function","z":"fbf7f30b.ec577","name":"6","func":"msg.payload.pvwatts = Number(msg.payload.value.toFixed(2));\nmsg.topic = \"pvwatts\";\nreturn msg;","outputs":1,"noerr":0,"x":770,"y":400,"wires":[["69f80bd9.c4a064","42834c7f.6dea04"]]},{"id":"42834c7f.6dea04","type":"join","z":"fbf7f30b.ec577","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":710,"y":1360,"wires":[["16753ff1.c9db6","15458f8b.54575"]]},{"id":"16753ff1.c9db6","type":"function","z":"fbf7f30b.ec577","name":"","func":"var soc = Number(msg.payload.soc);\nvar pvwatts = Number(msg.payload.pvwatts);\n\nnode.status({text:soc+\" \"+pvwatts});\n\nif (pvwatts >= 800 && soc >= 80) {\n    msg.payload = 2;\n}\nelse {\n    msg.payload = 3;\n}\nreturn msg;","outputs":1,"noerr":0,"x":890,"y":1460,"wires":[[]]},{"id":"15458f8b.54575","type":"debug","z":"fbf7f30b.ec577","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.soc","targetType":"msg","x":1150,"y":1400,"wires":[]}]