Inject node runs late sometimes

The root cause of the problem seems to be that once in place, inject time triggers in Node Red are never revisited, even if the operating system receives a network time update that skips forward due to the PI being switched off for some time.

The solution could be that if there is a sudden Raspbian OS time change detected (e.g. when your PI successfully connects to a network time server) THEN all inject node time triggers need to be deleted and re-created.

OK, I have a workaround that checks to see if the system time has changed and restarts the node red instance (not the Pi) if a time change is detected, so your timers can be correct. It also waits one minute before restarting because if your logic is somehow wrong you at least have 60 seconds to fix it before your flows restart. Enjoy!

[{"id":"201be917.c5f456","type":"inject","z":"f72ea602.fbc808","name":"Inject every second","topic":"","payload":"","payloadType":"date","repeat":"1","crontab":"","once":true,"onceDelay":0.1,"x":160,"y":4220,"wires":[["822796b7.7db868"]]},{"id":"c07557af.3ee838","type":"comment","z":"f72ea602.fbc808","name":"NNTP update detection","info":"","x":160,"y":4180,"wires":[]},{"id":"822796b7.7db868","type":"function","z":"f72ea602.fbc808","name":"Check if NNTP update","func":"var TIME_CHECKED = global.get(\"TIME_CHECKED\") || 0;\nvar TIME_CHECK_LAST_RUN = global.get(\"TIME_CHECK_LAST_RUN\") || msg.payload;\n\n// TIME_CHECK is used to make sure we have run at least once before we trigger a restart.\n// This is because the global variables might not have been saved in time before a restart happens\n// and we'll get the old value.\nif (TIME_CHECKED > 0)\n{\n    global.set(\"TIME_CHECK_LAST_RUN\",msg.payload);\n\n    var timeSinceLastRun = msg.payload - TIME_CHECK_LAST_RUN;\n    if (timeSinceLastRun > 10000)\n    {\n        // Timer triggers every second yet the system time has jumped more than 10 seconds.\n        // Need to restart the node red flows to fix all the timers.\n        msg.payload = \"RESTART\";\n    }\n}\nglobal.set(\"TIME_CHECKED\",msg.payload);\n\nreturn msg;","outputs":1,"noerr":0,"x":380,"y":4220,"wires":[["6c6a897c.875d78"]]},{"id":"48f2c8f3.162888","type":"change","z":"f72ea602.fbc808","name":"\"Pi system time updated - Node Red flows will restart in 60 seconds\"","rules":[{"t":"set","p":"payload","pt":"msg","to":"Pi system time updated - Node Red flows will restart in 60 seconds","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":970,"y":4220,"wires":[["c5e1afa5.0a454","af8fbc26.98b78","3dcc11fe.a196ee","e450ebbb.3f3d08"]]},{"id":"6c6a897c.875d78","type":"switch","z":"f72ea602.fbc808","name":"payload=RESTART?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"RESTART","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":600,"y":4220,"wires":[["48f2c8f3.162888"]]},{"id":"66341252.8394fc","type":"exec","z":"f72ea602.fbc808","command":"sudo ","addpay":false,"append":"node-red-restart","useSpawn":"false","timer":"","oldrc":false,"name":"Node Red Restart","x":1610,"y":4320,"wires":[[],[],[]]},{"id":"cf50a6dc.d83108","type":"inject","z":"f72ea602.fbc808","name":"Test Restart node-RED","topic":"","payload":"node-red-restart","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":1340,"y":4320,"wires":[["66341252.8394fc"]]},{"id":"af8fbc26.98b78","type":"delay","z":"f72ea602.fbc808","name":"","pauseType":"delay","timeout":"60","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1340,"y":4280,"wires":[["66341252.8394fc"]]},{"id":"e450ebbb.3f3d08","type":"debug","z":"f72ea602.fbc808","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1350,"y":4240,"wires":[]},{"id":"7e435524.4fd43c","type":"inject","z":"f72ea602.fbc808","name":"Run instantly on startup","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":"0","x":170,"y":4260,"wires":[["1b37bbe2.a6c674"]]},{"id":"1b37bbe2.a6c674","type":"change","z":"f72ea602.fbc808","name":"TIME_CHECKED=0","rules":[{"t":"set","p":"TIME_CHECKED","pt":"global","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":4260,"wires":[[]]}]

Nice job however a bit overkill (IMHO)

I have updated the cron-plus node (which does pretty much everything the inject does) which will simply update its internal schedules an continue to perform the inject as normal (no restarting of node-red required)

1 Like

Nice work, I'll replace mine with the cron-plus node. :wink:

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