Time Events - if schedule type of triggers just not enough

If schedule type of triggering nodes does not have enough flexibility, there is easy way to create time based events and build your own logic to control your flows based on time.

[{"id":"c05fa29a.5620a","type":"tab","label":"TimeEvents","disabled":false,"info":""},{"id":"2623d1d6.35d9ce","type":"function","z":"c05fa29a.5620a","name":"Time Events","func":"var stamp = msg.payload;\n\nfunction pretty(num) {\n    if (num < 10) {\n        return \"0\" + num;\n    }\n    return \"\" + num;\n}\nvar updated = false;\nvar o_second = null;\nvar o_minute = null;\nvar o_hour = null;\nvar o_date = null;\nvar o_month = null;\nvar o_year = null;\nvar timeupdate = new Date(stamp);\n\nvar currenttime = global.get(\"currenttime\") || {second:0,minute:0,hour:0,date:0,month:0,year:0};\n\nif (currenttime.second !== timeupdate.getSeconds()) {\n    currenttime.second = timeupdate.getSeconds();\n    updated = true;\n    o_second = {\n        topic: \"TimeEventSecond\",\n        payload: currenttime.second,\n        timestamp: stamp\n    };\n}\nif (currenttime.minute !== timeupdate.getMinutes()) {\n    currenttime.minute = timeupdate.getMinutes();\n    o_minute = {\n        topic: \"TimeEventMinute\",\n        payload: currenttime.minute,\n        timestamp: stamp\n    };\n}\nif (currenttime.hour !== timeupdate.getHours()) {\n    currenttime.hour = timeupdate.getHours();\n    o_hour = {\n        topic: \"TimeEventHour\",\n        payload: currenttime.hour,\n        timestamp: stamp\n    };\n}\nif (currenttime.date !== timeupdate.getDate()) {\n    currenttime.date = timeupdate.getDate();\n    o_date = {\n        topic: \"TimeEventDate\",\n        payload: currenttime.date,\n        timestamp: stamp\n    };\n}\nif (currenttime.month !== timeupdate.getMonth() + 1) {\n    currenttime.month = timeupdate.getMonth() + 1;\n    o_month = {\n        topic: \"TimeEventMonth\",\n        payload: currenttime.month,\n        timestamp: stamp\n    };\n}\nif (currenttime.year !== timeupdate.getFullYear()) {\n    currenttime.year = timeupdate.getFullYear();\n    o_year = {\n        topic: \"TimeEventYear\",\n        payload: currenttime.year,\n        timestamp: stamp\n    };\n}\nif (updated) {\n    currenttime.timestamp = stamp;\n    // can hold prettyfied date-time values in currenttime if needed \n   /*\n    var displaytime = pretty(currenttime.hour) + \":\" + pretty(currenttime.minute) + \":\" + pretty(currenttime.second);\n    currenttime.displaytime = displaytime;\n    currenttime.displaydate = pretty(currenttime.date) + \".\" + pretty(currenttime.month);\n    */\n    global.set(\"currenttime\", currenttime);\n   \n   \n    return [o_second, o_minute, o_hour, o_date, o_month, o_year];\n    \n}","outputs":6,"noerr":0,"x":330,"y":200,"wires":[["5fa6b5e3.d0a64c"],["7932dcc7.413764"],["681fa20a.a7dc2c"],["620bd1d2.2428f"],["9dd4ab5a.069768"],["8abd1001.7e637"]]},{"id":"81aca5f3.294678","type":"inject","z":"c05fa29a.5620a","name":"","topic":"","payload":"","payloadType":"date","repeat":".5","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":200,"wires":[["2623d1d6.35d9ce"]]},{"id":"5fa6b5e3.d0a64c","type":"link out","z":"c05fa29a.5620a","name":"te-second","links":[],"x":495,"y":100,"wires":[]},{"id":"7932dcc7.413764","type":"link out","z":"c05fa29a.5620a","name":"te-minute","links":["20d1748a.e94adc"],"x":495,"y":140,"wires":[]},{"id":"681fa20a.a7dc2c","type":"link out","z":"c05fa29a.5620a","name":"te-hour","links":["10e78da4.a39652"],"x":495,"y":180,"wires":[]},{"id":"620bd1d2.2428f","type":"link out","z":"c05fa29a.5620a","name":"te-date","links":[],"x":495,"y":220,"wires":[]},{"id":"9dd4ab5a.069768","type":"link out","z":"c05fa29a.5620a","name":"te-month","links":[],"x":495,"y":260,"wires":[]},{"id":"8abd1001.7e637","type":"link out","z":"c05fa29a.5620a","name":"te-year","links":[],"x":495,"y":300,"wires":[]},{"id":"20d1748a.e94adc","type":"link in","z":"c05fa29a.5620a","name":"listen minute","links":["7932dcc7.413764"],"x":105,"y":700,"wires":[["d5454085.2d56d"]]},{"id":"d5454085.2d56d","type":"switch","z":"c05fa29a.5620a","name":"hour 10","property":"currenttime.hour","propertyType":"global","rules":[{"t":"eq","v":"10","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":230,"y":700,"wires":[["8f2a79ae.f957b8","df137e30.d0879"]]},{"id":"8f2a79ae.f957b8","type":"switch","z":"c05fa29a.5620a","name":"minute 15","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"15","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":440,"y":680,"wires":[["5d994a09.d7d294"]]},{"id":"df137e30.d0879","type":"switch","z":"c05fa29a.5620a","name":"minute 17","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"17","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":440,"y":720,"wires":[["c7a5224a.fea09"]]},{"id":"5d994a09.d7d294","type":"debug","z":"c05fa29a.5620a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":590,"y":680,"wires":[]},{"id":"c7a5224a.fea09","type":"debug","z":"c05fa29a.5620a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":590,"y":720,"wires":[]},{"id":"f0099959.c6aa28","type":"comment","z":"c05fa29a.5620a","name":"Do something every day at 10:15AM","info":"","x":520,"y":640,"wires":[]},{"id":"c2cdc8c2.999628","type":"comment","z":"c05fa29a.5620a","name":"Do something every day at 10:17AM","info":"","x":520,"y":760,"wires":[]},{"id":"785b3c0a.3c3ae4","type":"comment","z":"c05fa29a.5620a","name":"Connected to listen TimeEventMinute","info":"","x":190,"y":640,"wires":[]},{"id":"10e78da4.a39652","type":"link in","z":"c05fa29a.5620a","name":"listen hour","links":["681fa20a.a7dc2c"],"x":135,"y":510,"wires":[["186ea83c.a58058"]]},{"id":"186ea83c.a58058","type":"switch","z":"c05fa29a.5620a","name":"hour 12","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"12","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":230,"y":510,"wires":[["44c22a0c.922a94"],["6ac7cd18.cbb1f4"]]},{"id":"44c22a0c.922a94","type":"debug","z":"c05fa29a.5620a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":430,"y":490,"wires":[]},{"id":"e40e6981.9454c8","type":"comment","z":"c05fa29a.5620a","name":"Do something every day at midday","info":"","x":520,"y":450,"wires":[]},{"id":"ff396600.8540e8","type":"comment","z":"c05fa29a.5620a","name":"Do something else on every other hour","info":"","x":530,"y":570,"wires":[]},{"id":"6ac7cd18.cbb1f4","type":"debug","z":"c05fa29a.5620a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":430,"y":530,"wires":[]},{"id":"f1f3e6c8.281a78","type":"comment","z":"c05fa29a.5620a","name":"Connected to listen TimeEventHour","info":"","x":200,"y":460,"wires":[]}]

Flow Info

Created 4 years, 11 months ago
Updated 4 years, 10 months ago
Rating: not yet rated

Owner

Actions

Rate:

Node Types

Core
  • comment (x6)
  • debug (x4)
  • function (x1)
  • inject (x1)
  • link in (x2)
  • link out (x6)
  • switch (x4)
Other
  • tab (x1)

Tags

  • Time
  • Events
  • Trigger
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option