Hourly, Daily and Monthly Payload

Hi guys,

I am looking at a way to get a reset signal generated on a couple of occasions :

  1. Hourly, on the hour.
  2. Daily at midnight.
  3. Monthly, at midnight on the last day of the month.

For the hourly, the inject node works perfectly fine, for the rest I do not know.

I do not need any On/Off time, I just want to get a payload to send trough MQTT and reset some counters. So I am not sure the scheduler or the bigtimer are good options for that.

Thanks for your insights.

Even more flexibility can be achieved by creating time events.

[{"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":[],"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":385,"y":450,"wires":[["d2f54c2b.cc955"]]},{"id":"d2f54c2b.cc955","type":"debug","z":"c05fa29a.5620a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":490,"y":450,"wires":[]}]
2 Likes

Beat me to it!
Good work!

1 Like

Thanks @hotNipi & @scottpageindysoft !

That certainly is one way of doing I never considered, but that's brilliant and doing the job perfectly.

Shared a little example also in flows https://flows.nodered.org/flow/986e43672b35b4329d58ceb3d987596b

1 Like

I realise this thread has a solution but as an alternative, no code solution, this is my contribution.

Screenshot...

The flow...

[{"id":"941d531a.9ebc3","type":"cronplus","z":"a39bd3b0.60173","name":"","outputField":"payload","timeZone":"","options":[{"topic":"Every1m","payload":"true","type":"bool","expression":"0 * * * * *"},{"topic":"EveryHour","payload":"true","type":"bool","expression":"0 0 * * * *"},{"topic":"Midnight","payload":"true","type":"bool","expression":"0 0 0 * * *"}],"x":80,"y":120,"wires":[["11814df4.399852"]]},{"id":"11814df4.399852","type":"debug","z":"a39bd3b0.60173","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":230,"y":120,"wires":[]}]

Disclaimer: I am the author of node-red-contrib-cron-plus

node-red-contrib-cron-plus - A flexible scheduler node for Node-RED (incl dynamic control and Timezone support)

FEATURES

  • Schedule by CRON, date and date sequences
    • Human readable descriptions of your CRON expression are provided as you type.
  • Send timestamp, string, number, boolean, flow variable, global variable, JSON, Buffer or Env variable as the output.
  • Settable output variable (normally msg.payload but it is up to you)
  • Multiple schedules can be entered by the UI
  • Additional info about the triggered scedule is always send in msg.cronplus
  • Ability to add, remove, list, stop, start, pause schedules by a payload input permitting full flexability & dynamic control
  • Full demo flow provided in node-red editors menu > import > examples.
  • Optional timezone setting suppoting UTC and Region/Area (e.g. Europe/London)

Info for anyone not familiar with CRON...

CRON Format...

    s  m  h  md m  wd y
    |  |  |  |  |  |  |
    *  *  *  *  *  *  *    Field              Allowed values    Special symbols
    |  |  |  |  |  |  |    -----------------  ---------------   ---------------
    `--|--|--|--|--|--|->  Second (optional)  0-59              * / , -
       `--|--|--|--|--|->  Minute             0-59              * / , -
          `--|--|--|--|->  Hour               0-23              * / , -
             `--|--|--|->  Day of Month       1-31              * / , - L W
                `--|--|->  Month              1-12 or JAN-DEC   * / , -
                   `--|->  Day of Week        0-7 or SUN-SAT    * / , - L #
                      `->  Year (optional)    1970-2099         * / , -

Examples...

  • * * * * * * Every Second
  • 0 * * * * * Every minute
  • 0 */10 * * * * Every 10 minutes
  • 0 */20 1 * * * Every 20 minutes, between 01:00 AM and 01:59 AM
  • 0 15,30,45 * * * * At 15, 30, and 45 minutes past the hour
  • 0 0 12 * * * Every day at noon - 12pm
  • 0 0 2 29 FEB * 2020-2040 At 02:00 AM, on day 29 of the month, only in February, every 4 years, 2020 through 2040
  • 0 0 7 * * MON#1 * At 07:00 AM, on the first Monday of the month
  • 0 0 12 * JAN,FEB,MAR,APR * Every day at noon in January, February, March and April
  • * * 1W * * Every minute, on the first weekday of the month
  • * * * * Tue#3 Every minute, on the third Tuesday of the month
  • 0 12 * * MONL At 12:00 PM, on the last Monday of the month

All of this info is present in the nodes built in help on the info panel.

5 Likes

Thanks @Steve-Mcl !

I was looking at this node and big timer as candidates to manage my counters.
I will try yours since you took the time to contribute to this post :+1:.

To be honest since I never used CRON before, by glancing at both solutions, the one using the function node directly make sense for me to understand, whereas with CRON it is not so direct, but if when I type the expression is explained then that is a huge help !