Trouble with a counter function node

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

When you press the reset inject node, what do you expect it to do in the function node?

Here is a function node debugging trick add the following in your function node where you need to see what is happening
node.warn('debug01')
to display a variable use
node.warn('x='+x)
that should help you discover what is happening.

When the growth state is 1 I want the first counter to start counting from 0 On output 1. When growth state is 0 I want the first counter to stop and the second counter to start from 0 on output 2. When the reset button is pressed I want both counters to reset to 0 And the third counter is counter 1 + counter 2 on output 3.

Cheers,

Place a node.warn in every IF statement and after every IF statement so you can see what is happening

You return before the creating Total_Count.

Use node.send instead. Reference

Try this...

var Veg_Count = global.get('Veg_Count', 'file')||0;
var Flower_Count = global.get('Flower_Count', 'file')||0;
var Growth_State = global.get('Growth_State', 'file');
var Total_Count = global.get('Total_Count', 'file')||0;
var topic = msg.topic;

if (topic == 'Reset'){
    Flower_Count = 0;
    Veg_Count = 0;
    Total_Count = 0;
    global.set('Veg_Count',Veg_Count,'file');
    global.set('Flower_Count',Flower_Count,'file');
    global.set('Total_Count',Total_Count,'file');
}

if (Growth_State === 0){
    Flower_Count += 1;
    global.set('Flower_Count',Flower_Count, 'file');
    msg.payload = global.get('Flower_Count', 'file');
    //return [null,msg,null];
    node.send([null,msg,null]);  // <<<< SEND INSTEAD OF RETURN
}

if (Growth_State == 1){
    Veg_Count += 1;
    global.set('Veg_Count',Veg_Count,'file');
    msg.payload = global.get('Veg_Count', 'file');
    //return [msg,null,null];
    node.send([msg,null,null]);  // <<<< SEND INSTEAD OF RETURN
}

Total_Count = Veg_Count += Flower_Count
global.set('Total_Count', Total_Count, 'file')
msg.payload = global.get('Total_Count', 'file');
return [null,null,msg];

Thanks Steve the totaliser is now working. I also ended up getting the whole counter function node working with the reset, Once I had a better think about it I realised I was resetting the count to early by having the reset statement first, so the node was resetting and then adding a count so I moved the reset statement to the end so it adds a count then resets to 0.


[{"id":"ae78f972.88e408","type":"inject","z":"c7d21a37.0a8c08","name":"Switch - position 1","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":330,"y":560,"wires":[["4ac304ce.ce5cfc"]]},{"id":"baf03824.375ba","type":"inject","z":"c7d21a37.0a8c08","name":"Switch - position 2","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":330,"y":600,"wires":[["4ac304ce.ce5cfc"]]},{"id":"4ac304ce.ce5cfc","type":"change","z":"c7d21a37.0a8c08","name":"Set Growth State","rules":[{"t":"set","p":"Growth_State","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":530,"y":580,"wires":[[]]},{"id":"21bb277f.167638","type":"inject","z":"c7d21a37.0a8c08","name":"","topic":"Reset","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":370,"y":660,"wires":[["2bd94e5d.7e5702"]]},{"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":350,"y":700,"wires":[["2bd94e5d.7e5702"]]},{"id":"fefb0dbe.4f5ae","type":"inject","z":"c7d21a37.0a8c08","name":"Check Counter","topic":"","payload":"","payloadType":"str","repeat":"1","crontab":"","once":false,"onceDelay":0.1,"x":340,"y":740,"wires":[["d8841178.64bdc"]]},{"id":"2bd94e5d.7e5702","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 (Growth_State === 0){\n    Flower_Count += 1;\n    global.set('Flower_Count',Flower_Count, 'file');\n\nmsg.payload = global.get('Flower_Count', 'file');\nnode.send([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');\nnode.send([msg,null,null]);\n}\n\nif (Growth_State === 0 || Growth_State == 1){\n    Total_Count = Veg_Count += Flower_Count\n    global.set('Total_Count', Total_Count, 'file')\n\nmsg.payload = global.get('Total_Count', 'file');\nnode.send([null,null,msg]);\n}\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}","outputs":0,"noerr":0,"x":500,"y":680,"wires":[]},{"id":"d8841178.64bdc","type":"function","z":"c7d21a37.0a8c08","name":"Check Counter","func":"var Veg_Count = global.get('Veg_Count', 'file');\nvar Flower_Count = global.get('Flower_Count', 'file');\nvar Total_Count = global.get('Total_Count', 'file');\n\nvar msg1 = { payload: Veg_Count };\nvar msg2 = { payload: Flower_Count };\nvar msg3 = { payload: Total_Count };\n\nreturn [ msg1, msg2, msg3 ];","outputs":3,"noerr":0,"x":520,"y":740,"wires":[["98acd0f.68de13"],["fccd4399.a124"],["3d85ae2d.39636a"]]},{"id":"98acd0f.68de13","type":"debug","z":"c7d21a37.0a8c08","name":"veg","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":670,"y":700,"wires":[]},{"id":"fccd4399.a124","type":"debug","z":"c7d21a37.0a8c08","name":"flower","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":670,"y":740,"wires":[]},{"id":"3d85ae2d.39636a","type":"debug","z":"c7d21a37.0a8c08","name":"total","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":670,"y":780,"wires":[]}]
1 Like

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