Calculate rainfall per 24h

So you will need to store the value. You can store values using context variables see:

https://nodered.org/docs/writing-functions#storing-data

Try importing this flow and see how you go

[{"id":"9a549ce5.8953","type":"inject","z":"9726fcf.8cbf3","name":"Start of the day","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"00 00 * * *","once":false,"onceDelay":0.1,"x":170,"y":400,"wires":[["4cdf847.94f0afc"]]},{"id":"4cdf847.94f0afc","type":"change","z":"9726fcf.8cbf3","name":"Get last days rain, and reset dailyrain","rules":[{"t":"set","p":"payload","pt":"msg","to":"dailyrain","tot":"global"},{"t":"set","p":"dailyrain","pt":"global","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":400,"wires":[["1905712c.98e9f7"]]},{"id":"96447045.2e2438","type":"debug","z":"9726fcf.8cbf3","name":"Last Days Rain","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":900,"y":380,"wires":[]},{"id":"6e94878e.cf0e38","type":"debug","z":"9726fcf.8cbf3","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":890,"y":200,"wires":[]},{"id":"ccb4cf8b.5dd2c","type":"inject","z":"9726fcf.8cbf3","name":"","topic":"","payload":"5","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":240,"wires":[["4cf1d5d4.d4a894"]]},{"id":"bc23345e.cd6118","type":"function","z":"9726fcf.8cbf3","name":"","func":"\n//grab previous value from global variable for total rain\npreviousrainfall = global.get(\"totalrainfall\")||0;\n\n//how much rain since the last update\nperiodrainfall = msg.payload - previousrainfall;\n\n//grab previous value from global variable for daily rain\ndailyrainfall = global.get(\"dailyrain\")||0;\n\ndailyrainfall = dailyrainfall + periodrainfall;\n\n//update the global variables.\nglobal.set(\"dailyrain\",dailyrainfall );\nglobal.set(\"totalrainfall\", msg.payload);\n\nmsg.payload = \" Total Rain =\"+msg.payload+\" Period Rain\"+periodrainfall+\" Rain Today\"+dailyrainfall\n\nreturn msg;","outputs":1,"noerr":0,"x":710,"y":240,"wires":[["6e94878e.cf0e38"]]},{"id":"4cf1d5d4.d4a894","type":"function","z":"9726fcf.8cbf3","name":"total rainfall input","func":"\n//mimicing teh total rainfall input\n\npreviousrainfall = global.get(\"previousrainfall\")||0;\nmsg.payload = msg.payload + previousrainfall;\nglobal.set(\"previousrainfall\", msg.payload)\nreturn msg;","outputs":1,"noerr":0,"x":290,"y":240,"wires":[["bc23345e.cd6118","5721e159.00dc4"]]},{"id":"5721e159.00dc4","type":"debug","z":"9726fcf.8cbf3","name":"\"Total Rainfall input\"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":500,"y":280,"wires":[]},{"id":"a98eaf45.542de","type":"comment","z":"9726fcf.8cbf3","name":"Click input to simulate rain guage total rainfall input","info":"","x":230,"y":180,"wires":[]},{"id":"5d4f1eaa.04b2b8","type":"comment","z":"9726fcf.8cbf3","name":"Click to simulate start of day calculation","info":"","x":210,"y":360,"wires":[]},{"id":"1905712c.98e9f7","type":"function","z":"9726fcf.8cbf3","name":"msg.payload = \"Total rain today is \"+msg.payload","func":"msg.payload = \"Total rain today is \"+msg.payload\nreturn msg;","outputs":1,"noerr":0,"x":900,"y":480,"wires":[["96447045.2e2438"]]}]
1 Like