# Cron-plus and next event

**URL:** <https://discourse.nodered.org/t/cron-plus-and-next-event/47746>\
**Category:** General\
**Created:** [28 June 2021 12:16 UTC](https://discourse.nodered.org/t/cron-plus-and-next-event/47746 "2021-06-28T12:16:43Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Simon01011](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@Simon01011](https://discourse.nodered.org/u/Simon01011)\
**Post date:** [28 June 2021 12:16 UTC](https://discourse.nodered.org/t/cron-plus-and-next-event/47746/1 "2021-06-28T12:16:43Z")

</div>

Hi!

I'm using the cron-plus node. My question: is there a way to get the time and date of the event that will execute next? Like in the status shown in the editor?

Background: I'd like to use the cron-pus node in a subflow and have the same status. That's why I need to feed the next execution time into the status node of the subflow. The coloured circle/dot would be nice but is not a "must have".

My approach would be to get all `nextDate` fields by using command `status-all`, then sorting and taking the first/next one... but maybe there's an easier solution.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [28 June 2021 12:44 UTC](https://discourse.nodered.org/t/cron-plus-and-next-event/47746/2 "2021-06-28T12:44:06Z")

</div>

if it is shown in the existing status - then the status node should be able to get it.

---

<div class="post-metadata">

**Author:** ![Simon01011](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@Simon01011](https://discourse.nodered.org/u/Simon01011)\
**Post date:** [28 June 2021 12:54 UTC](https://discourse.nodered.org/t/cron-plus-and-next-event/47746/3 "2021-06-28T12:54:00Z")

</div>

Hmmm, not really. The flow looks like  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/f/8f313f820a8242f1713c82c6bab28812efe0581e.png)

The subflow is  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/f/dff93ec5c3355706284dc849a220a68a984dcc0d.png)

So, what I see in the flow is - after execution - the trigger time stamp. But even when cron is waiting for the next execution, I cannot see the status  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/a/ea2e1952cd8988129e47933ecf3a80d87c65bfc6.png)

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [28 June 2021 13:10 UTC](https://discourse.nodered.org/t/cron-plus-and-next-event/47746/4 "2021-06-28T13:10:38Z")

</div>

Hi .. maybe something like this ?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/2/c25e47d4ea88afdc12522c27502e68135d9588ee.png)

**Example Flow :**

```auto
[{"id":"2626dc64a84cde56","type":"subflow","name":"Subflow 1","info":"","in":[{"x":60,"y":80,"wires":[{"id":"b1945ef169f97d71"}]}],"out":[{"x":340,"y":80,"wires":[{"id":"b1945ef169f97d71","port":0}]}],"status":{"x":340,"y":200,"wires":[{"id":"d45f812abe56c395","port":0}]}},{"id":"b1945ef169f97d71","type":"cronplus","z":"2626dc64a84cde56","name":"","outputField":"payload","timeZone":"","persistDynamic":false,"commandResponseMsgOutput":"output1","outputs":1,"options":[{"name":"schedule1","topic":"schedule1","payloadType":"default","payload":"","expressionType":"cron","expression":"0 0 11 * * ? *","location":"","offset":"0","solarType":"all","solarEvents":"sunrise,sunset"},{"name":"schedule2","topic":"schedule2","payloadType":"default","payload":"","expressionType":"cron","expression":"0 0 7 * * ? *","location":"","offset":"0","solarType":"all","solarEvents":"sunrise,sunset"},{"name":"schedule3","topic":"schedule3","payloadType":"default","payload":"","expressionType":"cron","expression":"0 0 4 * * ? *","location":"","offset":"0","solarType":"all","solarEvents":"sunrise,sunset"},{"name":"schedule4","topic":"schedule4","payloadType":"default","payload":"","expressionType":"cron","expression":"0 0 8 * * ? *","location":"","offset":"0","solarType":"all","solarEvents":"sunrise,sunset"}],"x":200,"y":80,"wires":[[]]},{"id":"d45f812abe56c395","type":"status","z":"2626dc64a84cde56","name":"","scope":["b1945ef169f97d71"],"x":140,"y":200,"wires":[[]]},{"id":"0ed13df44b941f86","type":"inject","z":"af25975e21b17ff4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"command\":\"status-all\"}","payloadType":"json","x":130,"y":820,"wires":[["2298c4c3d9190ceb"]]},{"id":"03a93b36c1d1f757","type":"debug","z":"af25975e21b17ff4","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":410,"y":760,"wires":[]},{"id":"861798c9e03a035c","type":"change","z":"af25975e21b17ff4","name":"Jsonata sort","rules":[{"t":"set","p":"payload","pt":"msg","to":"$sort(**.nextDateTZ)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":590,"y":820,"wires":[["e79ed1e27d32b02f","35de6d23a0650fe0"]]},{"id":"e79ed1e27d32b02f","type":"debug","z":"af25975e21b17ff4","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":730,"y":760,"wires":[]},{"id":"2298c4c3d9190ceb","type":"subflow:2626dc64a84cde56","z":"af25975e21b17ff4","name":"","env":[],"x":320,"y":820,"wires":[["03a93b36c1d1f757","861798c9e03a035c"]]},{"id":"35de6d23a0650fe0","type":"debug","z":"af25975e21b17ff4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload[0]","targetType":"msg","statusVal":"","statusType":"auto","x":810,"y":820,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [28 June 2021 13:28 UTC](https://discourse.nodered.org/t/cron-plus-and-next-event/47746/5 "2021-06-28T13:28:56Z")

</div>

> [@Simon01011](#):
>
> is there a way to get the time and date of the event that will execute next? Like in the status shown in the editor?

Yes. No need to read the status text (which changes as processes are run - so is unreliable)

Use the command topic `status-all` then sort the results and return the newest entry...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/5/35c6c732d5281085d06dbe1fe1522d80b3c45a50.png)

```auto
[{"id":"591a56224f88062c","type":"cronplus","z":"bec69dbd.8d622","name":"4 schedules","outputField":"payload","timeZone":"","persistDynamic":false,"commandResponseMsgOutput":"output2","outputs":2,"options":[{"name":"schedule1","topic":"schedule1","payloadType":"default","payload":"","expressionType":"cron","expression":"0 0 11 * * ? *","location":"","offset":"0","solarType":"all","solarEvents":"sunrise,sunset"},{"name":"schedule2","topic":"schedule2","payloadType":"default","payload":"","expressionType":"cron","expression":"0 0 7 * * ? *","location":"","offset":"0","solarType":"all","solarEvents":"sunrise,sunset"},{"name":"schedule3","topic":"schedule3","payloadType":"default","payload":"","expressionType":"cron","expression":"0 0 4 * * ? *","location":"","offset":"0","solarType":"all","solarEvents":"sunrise,sunset"},{"name":"schedule4","topic":"schedule4","payloadType":"default","payload":"","expressionType":"cron","expression":"0 0 8 * * ? *","location":"","offset":"0","solarType":"all","solarEvents":"sunrise,sunset"}],"x":950,"y":1700,"wires":[[],["48a136f34f3ca57b"]]},{"id":"9dc73aeb28848ea0","type":"inject","z":"bec69dbd.8d622","name":"status-all","props":[{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"status-all","x":760,"y":1700,"wires":[["591a56224f88062c"]]},{"id":"eac3f4b63cf580ee","type":"function","z":"bec69dbd.8d622","name":"get next to run","func":"if (msg.payload.result && msg.payload.result.length) {\n\n const sorted = msg.payload.result.sort(function (a, b) {\n if (a.status.nextDate < b.status.nextDate) return -1;\n if (a.status.nextDate > b.status.nextDate) return 1;\n return 0;\n });\n\n if (sorted && sorted.length) {\n msg.payload = {\n config: sorted[0].config,\n status: sorted[0].status\n } \n return msg;\n }\n\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1140,"y":1760,"wires":[["31de359bc693742e"]]},{"id":"31de359bc693742e","type":"debug","z":"bec69dbd.8d622","name":"Next to run","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1330,"y":1760,"wires":[]},{"id":"48a136f34f3ca57b","type":"switch","z":"bec69dbd.8d622","name":"status-all command response?","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"status-all","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":890,"y":1760,"wires":[["eac3f4b63cf580ee"]]}]

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/6/366d7bf52da1e44c3f1ec8dac68a3da9cf867fbd.png)

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [28 June 2021 16:00 UTC](https://discourse.nodered.org/t/cron-plus-and-next-event/47746/6 "2021-06-28T16:00:41Z")

</div>

> [@Steve-Mcl](#):
>
> No need to read the status text (which changes as processes are run - so is unreliable)

apologies - I thought that showed the next event to run - and if it changed that would be a good thing rather than having to poll...

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [28 June 2021 16:10 UTC](https://discourse.nodered.org/t/cron-plus-and-next-event/47746/7 "2021-06-28T16:10:37Z")

</div>

> [@dceejay](#):
>
> apologies - I thought that showed the next event to run

It does Dave, but it also (momentarily) shows other info like `Done: xxxxx` (see GIF below)

> [@dceejay](#):
>
> and if it changed that would be a good thing rather than having to poll

This is (relatively) easy to capture also (without polling)...

![MbJgPp9zH6](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/f/ffca7177aeec61798f58e8f5a55be1e694abe6fb.gif)

```auto
[{"id":"591a56224f88062c","type":"cronplus","z":"bec69dbd.8d622","name":"4 schedules","outputField":"payload","timeZone":"","persistDynamic":false,"commandResponseMsgOutput":"output2","outputs":2,"options":[{"name":"schedule1","topic":"schedule1","payloadType":"default","payload":"","expressionType":"cron","expression":"0 0 11 * * ? *","location":"","offset":"0","solarType":"all","solarEvents":"sunrise,sunset"},{"name":"schedule2","topic":"schedule2","payloadType":"default","payload":"","expressionType":"cron","expression":"0 0 7 * * ? *","location":"","offset":"0","solarType":"all","solarEvents":"sunrise,sunset"},{"name":"schedule3","topic":"schedule3","payloadType":"default","payload":"","expressionType":"cron","expression":"0 0 4 * * ? *","location":"","offset":"0","solarType":"all","solarEvents":"sunrise,sunset"},{"name":"schedule4","topic":"schedule4","payloadType":"default","payload":"","expressionType":"cron","expression":"0 0 8 * * ? *","location":"","offset":"0","solarType":"all","solarEvents":"sunrise,sunset"}],"x":950,"y":1700,"wires":[["3fd8d85705e239a6","28d9c59aa18e7efc"],["48a136f34f3ca57b"]]},{"id":"9dc73aeb28848ea0","type":"inject","z":"bec69dbd.8d622","name":"status-all","props":[{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"status-all","payloadType":"str","x":520,"y":1700,"wires":[["dad662673acb025a"]]},{"id":"eac3f4b63cf580ee","type":"function","z":"bec69dbd.8d622","name":"get next to run","func":"if (msg.payload.result && msg.payload.result.length) {\n\n const sorted = msg.payload.result.sort(function (a, b) {\n if (a.status.nextDate < b.status.nextDate) return -1;\n if (a.status.nextDate > b.status.nextDate) return 1;\n return 0;\n });\n\n if (sorted && sorted.length) {\n msg.payload = {\n config: sorted[0].config,\n status: sorted[0].status\n } \n return msg;\n }\n\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1020,"y":1780,"wires":[["31de359bc693742e"]]},{"id":"31de359bc693742e","type":"debug","z":"bec69dbd.8d622","name":"Next to run","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload.config.name","statusType":"msg","x":1210,"y":1780,"wires":[]},{"id":"48a136f34f3ca57b","type":"switch","z":"bec69dbd.8d622","name":"status-all command response?","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"status-all","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":770,"y":1780,"wires":[["eac3f4b63cf580ee"]]},{"id":"b74ae0acba34c49a","type":"inject","z":"bec69dbd.8d622","name":"","props":[{"p":"topic","vt":"str"},{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"trigger","payload":"schedule1","payloadType":"str","x":740,"y":1640,"wires":[["591a56224f88062c"]]},{"id":"dad662673acb025a","type":"change","z":"bec69dbd.8d622","name":"set topic status-all","rules":[{"t":"set","p":"topic","pt":"msg","to":"status-all","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":730,"y":1700,"wires":[["591a56224f88062c"]]},{"id":"e6fc5cfe51bdf45a","type":"link in","z":"bec69dbd.8d622","name":"","links":["3fd8d85705e239a6"],"x":555,"y":1660,"wires":[["dad662673acb025a"]]},{"id":"3fd8d85705e239a6","type":"link out","z":"bec69dbd.8d622","name":"","links":["e6fc5cfe51bdf45a"],"x":1075,"y":1660,"wires":[]},{"id":"28d9c59aa18e7efc","type":"debug","z":"bec69dbd.8d622","name":"Schedule","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1200,"y":1700,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Simon01011](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@Simon01011](https://discourse.nodered.org/u/Simon01011)\
**Post date:** [29 June 2021 08:51 UTC](https://discourse.nodered.org/t/cron-plus-and-next-event/47746/8 "2021-06-29T08:51:47Z")

</div>

So I understand it like this:

1. I issue a status-all command, sort and show the first entry
2. Whenever the node triggers, I issue this command again and get the next event

To use it in a subflow with input (e.g. to add a schedule), I had to add some logic to the flow, now it looks like that:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/b/8b1cf75f1ffe4b5917abc33a81c3749be9752b41.png)  
Otherwise it didn't work as expected. After an input there is nothing in the "output" debug node. Thus there is no status update. Or did I miss something?

And one additional question: Do I need a delay before my "update status after input received" link? Or is a topic input to the cron-plus node always processed after the payload? Or should the change node even remove the payload before setting the topic to status-all? I'm a little bit confused...

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [29 June 2021 09:00 UTC](https://discourse.nodered.org/t/cron-plus-and-next-event/47746/9 "2021-06-29T09:00:05Z")

</div>

Looking at what you have there, your only intention is to display the next event on the status of the subflow instance - am i correct?

If that is all you need then instead of querying the schedules and sorting etc, simply use a `status` node (pointed at the cron node) and feed that to the `status` subflow output.

* * *

> [@Simon01011](#):
>
> Do I need a delay before my "update status after input received" link? Or is a topic input to the cron-plus node always processed after the payload?

If you are inputting a schedule and requesting status at the same time, it will depend on which message arrives first. This is easy to counter with a short delay (50ms is more than enough)

---

<div class="post-metadata">

**Author:** ![Simon01011](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@Simon01011](https://discourse.nodered.org/u/Simon01011)\
**Post date:** [29 June 2021 09:29 UTC](https://discourse.nodered.org/t/cron-plus-and-next-event/47746/10 "2021-06-29T09:29:29Z")

</div>

Ahhh, the status node!!! Sometimes I just don't see the easy things! Thanks a lot!

Maybe I need some more coffee... A few moments ago I fed the output of the status node to the inbput of the cron node instead of to the status output of the subflow, bad idea... 😬

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [29 June 2021 09:31 UTC](https://discourse.nodered.org/t/cron-plus-and-next-event/47746/11 "2021-06-29T09:31:02Z")

</div>

could swear that is what I suggested yesterday 😉

---

<div class="post-metadata">

**Author:** ![Simon01011](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@Simon01011](https://discourse.nodered.org/u/Simon01011)\
**Post date:** [29 June 2021 09:38 UTC](https://discourse.nodered.org/t/cron-plus-and-next-event/47746/12 "2021-06-29T09:38:24Z")

</div>

My bad! You're right of course!

Must be my English, I'm from Germany. I just didn't understand your answer

> [@dceejay](#):
>
> if it is shown in the existing status - then the status node should be able to get it.

When I read "status node" I thought of the status output node of the subflow, but you were talking about the status node from the palette... apparently I need to read answers more carefully 😳

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [29 June 2021 09:40 UTC](https://discourse.nodered.org/t/cron-plus-and-next-event/47746/13 "2021-06-29T09:40:25Z")

</div>

not a problem - I was just teasing Steve

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [28 August 2021 09:40 UTC](https://discourse.nodered.org/t/cron-plus-and-next-event/47746/14 "2021-08-28T09:40:43Z")

</div>

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