Hi All,
Im having some trouble with this counter function node. Inside the function node are 3 counters, all share a common input and each have their own output.
[{"id":"841615d7.5593e8","type":"function","z":"c7d21a37.0a8c08","name":"Counter","func":"var Veg_Count = global.get('Veg_Count', 'file')||0;\nvar Flower_Count = global.get('Flower_Count', 'file')||0;\nvar Growth_State = global.get('Growth_State', 'file');\nvar Total_Count = global.get('Total_Count', 'file')||0;\nvar topic = msg.topic;\n\nif (topic == 'Reset'){\n Flower_Count = 0;\n Veg_Count = 0;\n Total_Count = 0;\n global.set('Veg_Count',Veg_Count,'file');\n global.set('Flower_Count',Flower_Count,'file');\n global.set('Total_Count',Total_Count,'file');\n}\n\nif (Growth_State === 0){\n Flower_Count += 1;\n global.set('Flower_Count',Flower_Count, 'file');\n\nmsg.payload = global.get('Flower_Count', 'file');\nreturn [null,msg,null];\n}\n\nif (Growth_State == 1){\n Veg_Count += 1;\n global.set('Veg_Count',Veg_Count,'file');\n \nmsg.payload = global.get('Veg_Count', 'file');\nreturn [msg,null,null];\n}\n\nTotal_Count = Veg_Count += Flower_Count\nglobal.set('Total_Count', Total_Count, 'file')\n\nmsg.payload = global.get('Total_Count', 'file');\nreturn [null,null,msg];\n","outputs":3,"noerr":0,"x":1320,"y":1220,"wires":[["98acd0f.68de13"],["fccd4399.a124"],["3d85ae2d.39636a"]]},{"id":"26b796c5.76e572","type":"inject","z":"c7d21a37.0a8c08","name":"Daily pulse","topic":"","payload":"","payloadType":"num","repeat":"","crontab":"00 05 * * *","once":false,"onceDelay":0.1,"x":1150,"y":1240,"wires":[["841615d7.5593e8"]]},{"id":"3d85ae2d.39636a","type":"debug","z":"c7d21a37.0a8c08","name":"total","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1470,"y":1260,"wires":[]},{"id":"fccd4399.a124","type":"debug","z":"c7d21a37.0a8c08","name":"flower","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1470,"y":1220,"wires":[]},{"id":"98acd0f.68de13","type":"debug","z":"c7d21a37.0a8c08","name":"veg","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1470,"y":1180,"wires":[]},{"id":"21bb277f.167638","type":"inject","z":"c7d21a37.0a8c08","name":"","topic":"Reset","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":1170,"y":1200,"wires":[["841615d7.5593e8"]]},{"id":"8fd53e07.bbcfc8","type":"change","z":"c7d21a37.0a8c08","name":"Set Growth State","rules":[{"t":"set","p":"#:(file)::Growth_State","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1510,"y":1120,"wires":[[]]},{"id":"2c871389.84228c","type":"inject","z":"c7d21a37.0a8c08","name":"","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":1170,"y":1100,"wires":[["8fd53e07.bbcfc8"]]},{"id":"df5ae2ec.5b4118","type":"inject","z":"c7d21a37.0a8c08","name":"","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":1170,"y":1140,"wires":[["8fd53e07.bbcfc8"]]}]
First is, the reset function won't quite reset all values to 0, It will only reset the value opposite of the switch position to 0 and the value in the current position to 1. The second is, the total count doesn't work.
Im sure it will be an easy fix but I'm still trying to learn javascript. Any help would be much appreciated.
Cheers,
Jezz