Calculating payloads in a function, to adjust a time payload

[{"id":"1adf96e7.285aa9","type":"debug","z":"c23bf0c8.c3a58","name":"Test1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":770,"y":260,"wires":[]},{"id":"5ae806d1.371dc8","type":"inject","z":"c23bf0c8.c3a58","name":"BP(10sec)","topic":"GPIO/Delta","payload":"10000","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":440,"y":220,"wires":[["ac0bb6a1.e604f8"]]},{"id":"a1f821c3.fd2bd","type":"inject","z":"c23bf0c8.c3a58","name":"PD(5sec)","topic":"PauseDelta","payload":"5000","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":440,"y":260,"wires":[["ac0bb6a1.e604f8"]]},{"id":"5d3ae14c.f83d1","type":"inject","z":"c23bf0c8.c3a58","name":"PD(2.5sec)","topic":"PauseDelta","payload":"2500","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":440,"y":300,"wires":[["ac0bb6a1.e604f8"]]},{"id":"ac0bb6a1.e604f8","type":"function","z":"c23bf0c8.c3a58","name":"PauseAdjust","func":"var g = context.get(\"GPIO/Delta\") || 0;\nvar p = context.get(\"PauseDelta\") || 0;\n\nif (p >= 0 && g === 0){\n    msg.topic = \"Bye\";\n    p = msg.payload;\n    return [msg];\n} else if (p >= 0 && g >= 0){\n    msg.topic = \"Hi\";\n    g = msg.payload;\n    return [msg];\n} else if (p === 0 && p >= 0){\n    msg.topic = \"sum\";\n    r = x-y;\n    r = msg.payload;\n    return [msg];\n}\n//if (g.length === 2 && p.length === 0){\n//    msg.payload = g;\n//} else if (g.length === 2 && p.length === 2){\n//    msg.payload = g-p;\n//} else if (g.length === 2 && g >= 0){\n//    msg.payload = Math.abs(g-p);\n// }else {\n //   msg.payload = g;\n//}\n//msg.topic = \"Tester\"\n//eturn msg;\n\n//if (p >= 0 && g === 0){\n//    msg.topic = \"Bye\";\n//    p = msg.payload;\n//    return [msg, null, null];\n//} else if (p >= 0 && g >= 0){\n//    msg.topic = \"Hi\";\n//    g = msg.payload;\n//    return [null, msg, null];\n//} else if (p === 0 && p >= 0){\n//    msg.topic = \"sum\";\n//    x = Number(g);\n//    y = Number(p);\n//    r = x-y;\n//    r = msg.payload;\n//   return [null, null, msg];\n//}","outputs":1,"noerr":0,"x":630,"y":260,"wires":[["1adf96e7.285aa9"]]}]

Hi all,
I'm struggling with the function node(another noob move), have read numerous topics, but find i seem to get myself even more befuddled.
My aim is to use the function node to subtract a pause delta from a GPIO delta & send the adjusted payload, or just continue sending the GPIO delta, if no pause is detected.
I've managed to achieved the goal using the switch, change, calculate & simplequeue node...but It's a mess of wires(including the rest of my flow), so i want to streamline. I'm also getting more confused, when reading about using 'CalculateSum' & 'Math' methods, as i was having some success without adding them to my scrambled brain.

The code I posted has some iterations i was attempting further below the code. Hopefully I can be nudged down the right alley, as i had great responses the last time i posted. Thanks

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