Throwing in the towel. What I'm I missing here?

I've been banging my head on this one for a couple of days now. I've even gone back and looked at some of my other node flows that use the same type of logic and are working fine.

I have a flow (code below) controlling a sprinkler pump. The logic is as follows:

Sprinkler controller sends signal 24vAC signal to relay -> Relay closes and sets GPIO on RPi -> RPi turns on pump and waits 10 seconds before checking the pipe pressure. After 10 seconds have passed the code checks the line pressure every 5 seconds, if the pressure falls out of range the pump is turned off.

The issue I'm having is with the code below. No matter the payload the flow variable StartupTime always stays "OFF". At this point, I'm starting to believe I'm either losing my grasp on reality or there is a bug with flow variables when running version 1.1.2

var now = new Date(); 

//Set a start time when the payload is 1 meaning the pump needs to be started
//this time is used by the pressure function to ignore the pressure for 10 seconds
//to allow the pressure to normalize before taking a reading

//If the payload is 1 and the previous value was OFF it means the pump was off
//on the previous iteration and the start time variable should be populated
if (msg.payload == 1)
{
    
    if (flow.get("StartupTime") == "OFF")
    {
        flow.set("StartUpTime", now);
    }
    
} 
//if the payload is 0 it means the pump needs to be turned off and the start time 
//variable needs to be set to OFF
else if (msg.payload === 0)
{
    flow.set("StartUpTime", "OFF");
}

node.status({text:"state: " + msg.payload +" "+ (flow.get("StartUpTime")) })

return msg;

Full Flow:

[{"id":"c64f78bb.691d88","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"448ac54c.fa26fc","type":"rpi-gpio out","z":"c64f78bb.691d88","name":"Contactor Relay","pin":"40","set":true,"level":"0","freq":"","out":"out","x":820,"y":500,"wires":[]},{"id":"e1f2e7ae.3f8cc8","type":"inject","z":"c64f78bb.691d88","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":590,"y":660,"wires":[["448ac54c.fa26fc","a510cb3.6280138"]]},{"id":"b65e4b92.638808","type":"inject","z":"c64f78bb.691d88","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":590,"y":620,"wires":[["448ac54c.fa26fc","a510cb3.6280138"]]},{"id":"12614fe1.94196","type":"rpi-gpio in","z":"c64f78bb.691d88","name":"RainMachine Signal","pin":"16","intype":"up","debounce":"25","read":true,"x":110,"y":100,"wires":[["613fb33.1e4984c"]]},{"id":"df2d2ea2.39da8","type":"function","z":"c64f78bb.691d88","name":"Validate States","func":"//if (flow.get(\"RainMachineState\") == \"ON\")\nif (flow.get(\"RainMachineState\") == \"ON\" && flow.get(\"PressureState\") == \"ON\")\n{\n    msg.payload = 1;\n    \n} \nelse\n{\n    msg.payload = 0;\n}\nnode.status({text:msg.payload});\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":360,"y":520,"wires":[["e9975ded.4ae26"]]},{"id":"e9975ded.4ae26","type":"switch","z":"c64f78bb.691d88","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":530,"y":520,"wires":[["448ac54c.fa26fc","a510cb3.6280138","3af48d3b.777242"],["448ac54c.fa26fc","a510cb3.6280138","3af48d3b.777242"]]},{"id":"14632060.69151","type":"function","z":"c64f78bb.691d88","name":"Check Pressure","func":"//turns off the pump if the pressure is too low meaning a leak has occurred\n//turns off the pump if the pressure is too high meaning a valve has failed\n\npsi = msg.payload.psi;\n\nif (psi > 70 || psi < 20)\n{\n    msg.payload = \"OFF\"\n    \n} else {\n    msg.payload = \"ON\"\n}\n\n//Sets the flow variable that will be used in the main loop\nflow.set(\"PressureState\", msg.payload);\n\nnode.status({text:psi + \" psi, state \" + msg.payload })\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":980,"y":340,"wires":[[]]},{"id":"c774d4a1.c473e8","type":"inject","z":"c64f78bb.691d88","name":"Resent every .1 sec","props":[{"p":"payload"}],"repeat":".1","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":520,"wires":[["df2d2ea2.39da8"]]},{"id":"613fb33.1e4984c","type":"function","z":"c64f78bb.691d88","name":"Set State","func":"//Because the signal from the controller is 24vAC and the AC relay being used\n//does not have a NC pole this logic converts the irregular ON=0 to the normal ON=1\n//the flow variable rainmachine state is then used by the main thread to detect when\n//the pump is being told to turn off by the sprinkler controller\nif (msg.payload === 0)\n{\n    msg.payload = \"ON\";\n    \n} else if (msg.payload == 1)\n{\n    msg.payload = \"OFF\";\n}\nflow.set(\"RainMachineState\", msg.payload);\nnode.status({text:msg.payload});\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":320,"y":100,"wires":[[]]},{"id":"3af48d3b.777242","type":"debug","z":"c64f78bb.691d88","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":710,"y":440,"wires":[]},{"id":"6ae4575a.30a0b8","type":"serial in","z":"c64f78bb.691d88","name":"","serial":"15086b85.3bd874","x":110,"y":340,"wires":[["842dab0c.b70ca8"]]},{"id":"106caf4b.697021","type":"debug","z":"c64f78bb.691d88","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":450,"y":280,"wires":[]},{"id":"842dab0c.b70ca8","type":"json","z":"c64f78bb.691d88","name":"","property":"payload","action":"","pretty":false,"x":250,"y":340,"wires":[["106caf4b.697021","4f3946a1.8c81e8"]]},{"id":"4f3946a1.8c81e8","type":"function","z":"c64f78bb.691d88","name":"Pressure Gate","func":"if (flow.get(\"RainMachineState\") == \"OFF\")\n{\n    //If the controller doesn't have the pump on\n    //Set pressure state to allow start and do not \n    //send a message to the downstream function which\n    //checks pressure.\n    //This overrides the downstream function checking\n    //for low pressure and shutting off the pump\n    msg.payload = \"ON\"\n    flow.set(\"PressureState\", msg.payload);\n    node.status({text:\"state: \" + msg.payload })\n    \n} else {\n    \n    //If the controller wants the pump on\n    //send the message to the downstream node \n    //after being delayed 10 seconds to allow pressure on the pipe to normalize \n    //the downstream function will verify pressure and shut off the pump if the pressures are out of range\n    startTime = flow.get(\"StartUpTime\");\n    now = new Date();\n\n    //prevents hunting by waiting 10 seconds before checking pressure after pump startup\n    timeElapsed = (now - startTime)/1000;\n    if (timeElapsed > 10)\n    {\n        return msg;\n    }\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":480,"y":340,"wires":[["8236d0a4.77d48","742a554f.1576cc"]]},{"id":"8236d0a4.77d48","type":"debug","z":"c64f78bb.691d88","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":650,"y":280,"wires":[]},{"id":"742a554f.1576cc","type":"delay","z":"c64f78bb.691d88","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"5","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"x":740,"y":340,"wires":[["14632060.69151","cf8fec2b.1067b"]]},{"id":"a510cb3.6280138","type":"function","z":"c64f78bb.691d88","name":"Set Pump Start Time","func":"var now = new Date(); \n\n//Set a start time when the payload is 1 meaning the pump needs to be started\n//this time is used by the pressure function to ignore the pressure for 10 seconds\n//to allow the pressure to normalize before taking a reading\n\n//If the payload is 1 and the previous value was OFF it means the pump was off\n//on the previous iteration and the start time variable should be populated\nif (msg.payload == 1)\n{\n    \n    if (flow.get(\"StartupTime\") == \"OFF\")\n    {\n        flow.set(\"StartUpTime\", now);\n    }\n    \n} \n//if the payload is 0 it means the pump needs to be turned off and the start time \n//variable needs to be set to OFF\nelse if (msg.payload === 0)\n{\n    flow.set(\"StartUpTime\", \"OFF\");\n}\n\nnode.status({text:\"state: \" + msg.payload +\" \"+ (flow.get(\"StartUpTime\")) })\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":840,"y":580,"wires":[[]]},{"id":"cf8fec2b.1067b","type":"debug","z":"c64f78bb.691d88","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":870,"y":280,"wires":[]},{"id":"d481f9f6.3457c8","type":"comment","z":"c64f78bb.691d88","name":"Signal from Sprinkler Controller","info":"","x":150,"y":60,"wires":[]},{"id":"766a25f9.1d20cc","type":"comment","z":"c64f78bb.691d88","name":"Pressure on the pipe","info":"","x":130,"y":300,"wires":[]},{"id":"1bc471b2.99a01e","type":"comment","z":"c64f78bb.691d88","name":"Main Loop","info":"","x":120,"y":480,"wires":[]},{"id":"15086b85.3bd874","type":"serial-port","z":"","serialport":"/dev/ttyUSB0","serialbaud":"9600","databits":"8","parity":"none","stopbits":"1","waitfor":"","dtr":"none","rts":"none","cts":"none","dsr":"none","newline":"\\n","bin":"false","out":"char","addchar":"","responsetimeout":"10000"}]

Are you sure that msg.payload is actually a number? Rather than true/false or "0"/"1"? Or indeed, something else entirely? You have no final else in that outer if so you may wish to add one that outputs a warning msg. You might also want to put an else on the inner if as well so that you can see exactly what is happening.

You've also mixed up == and === which is rarely a good idea. Stick with === for an exact match unless you are really sure of what you need.

Also in you switch node you are checking if payload equals 0 a number , and "1" a string. Is the switch node required, i can not see it does anything?

Are you sure that msg.payload is actually a number? Rather than true/false or "0"/"1"? Or indeed, something else entirely? You have no final else in that outer if so you may wish to add one that outputs a warning msg. You might also want to put an else on the inner if as well so that you can see exactly what is happening.

Yep, I attached a debug node prior to writing the code and the output is an int

image

You're correct, I just changed the output from a string to an int type but it didn't make any changes. I think node was automatically converting it to an int as debug showed it as an int output (See screenshot on my last reply).

As to why it is there. Leftover from troubleshooting when I had 2 separate functions in trying to figure out the issue but you're right. it is not needed.

You also have a typo in "StartupTime" no capital U

I can't believe I missed that after looking at the code for multiple days. Fixed the typo and logic is working as expected now. Thank you.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.