# Get last missed event with cron block after reboot

**URL:** https://discourse.nodered.org/t/get-last-missed-event-with-cron-block-after-reboot/76301
**Category:** General
**Created:** [8 March 2023 14:52 UTC](https://discourse.nodered.org/t/get-last-missed-event-with-cron-block-after-reboot/76301 "2023-03-08T14:52:18Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![tenti](https://avatars.discourse-cdn.com/v4/letter/t/5daacb/32.png) [@tenti](https://discourse.nodered.org/u/tenti)
#### Post date: [8 March 2023 14:52 UTC](https://discourse.nodered.org/t/get-last-missed-event-with-cron-block-after-reboot/76301/1 "2023-03-08T14:52:18Z")

</div>

Hi,

I've used in my project a cron-plus block. While node-red is running is all ok, but my problem is when node-red was inactive for a long period and then turn on. The cron-plus have to set a display with different message according to the shceduled task on cron but i don't know the state of the display should to be when the system is turned on.

I can get the next event but i've to get the previous one, but i don't know how to compare in the past the cron configuration.

thank's

Gian-Luca

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [11 March 2023 15:16 UTC](https://discourse.nodered.org/t/get-last-missed-event-with-cron-block-after-reboot/76301/2 "2023-03-11T15:16:49Z")

</div>

One solution is use repeating events, so that rather than sending the new state only at the start of a period, it sends it every minute until the next event is reached. This can be done using the technique described in [this post](https://www.baeldung.com/linux/cron-time-ranges). It is easy enough for simple periods such as every minute between 09:00 and 10:59, but it can get messy for complex time periods such as every minute between 09:06 and 11:03.

What we need is for someone to come up with an algorithm for calculating the schedule for any given time range, which can then be used to generate a dynamic schedule and feed it to the node.

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [11 March 2023 15:40 UTC](https://discourse.nodered.org/t/get-last-missed-event-with-cron-block-after-reboot/76301/3 "2023-03-11T15:40:47Z")

</div>

Can you export your cron-plus node and share it so we can see your schedules?

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [11 March 2023 15:59 UTC](https://discourse.nodered.org/t/get-last-missed-event-with-cron-block-after-reboot/76301/4 "2023-03-11T15:59:01Z")

</div>

I don't currently use cron-plus in that way. I have been thinking how to move over from big timer (which has a built in repeat every minute option), but have not done it yet as what I have does work.

---

<div class="post-metadata">

### Author: ![juntiedt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/juntiedt/32/58357_2.png) [@juntiedt](https://discourse.nodered.org/u/juntiedt)
#### Post date: [11 March 2023 16:05 UTC](https://discourse.nodered.org/t/get-last-missed-event-with-cron-block-after-reboot/76301/5 "2023-03-11T16:05:26Z")

</div>

store the output of cronplus in persited contexts.  
when you restart your system, load the context first and then start cron plus.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [11 March 2023 16:34 UTC](https://discourse.nodered.org/t/get-last-missed-event-with-cron-block-after-reboot/76301/6 "2023-03-11T16:34:18Z")

</div>

> [@juntiedt](#):
>
> output of cronplus in persited contexts.

That only works for an immediate restart. If you wait until after the next state change then it will be restored to the state at shutdown, not whatever the new state should be. Think of heating on/off state dependant on time of day for example. Shutdown while it is off, wait till it should be on and then restart, will restore the Off state.

---

<div class="post-metadata">

### Author: ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)
#### Post date: [11 March 2023 17:08 UTC](https://discourse.nodered.org/t/get-last-missed-event-with-cron-block-after-reboot/76301/7 "2023-03-11T17:08:58Z")

</div>

Depending on the way that the schedules are arranged you may be able to work out the previous schedule from `result.nextDates` obtained from a `list-all` command

i.e. If you have a daily schedule run through the `nextDates` array to find one with the time previous to the current time and then send it as a one-off dynamic schedule. If the schedules are a bit more sporadic you may be able to work out the previous schedule using the day of the week.

Never tried this but it should be possible. (you obviously have to know the name of the schedule you are looking for)

---

<div class="post-metadata">

### Author: ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)
#### Post date: [11 March 2023 17:38 UTC](https://discourse.nodered.org/t/get-last-missed-event-with-cron-block-after-reboot/76301/8 "2023-03-11T17:38:49Z")

</div>

The chronos package has a settable repeat node I use a lot. That only works after it has been fired once so there is still the need to fire the missed event but it does help a little.

---

<div class="post-metadata">

### Author: ![juntiedt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/juntiedt/32/58357_2.png) [@juntiedt](https://discourse.nodered.org/u/juntiedt)
#### Post date: [11 March 2023 17:42 UTC](https://discourse.nodered.org/t/get-last-missed-event-with-cron-block-after-reboot/76301/9 "2023-03-11T17:42:51Z")

</div>

Yes, you are right. Will see what other ideas come up

---

<div class="post-metadata">

### Author: ![tenti](https://avatars.discourse-cdn.com/v4/letter/t/5daacb/32.png) [@tenti](https://discourse.nodered.org/u/tenti)
#### Post date: [11 March 2023 18:29 UTC](https://discourse.nodered.org/t/get-last-missed-event-with-cron-block-after-reboot/76301/10 "2023-03-11T18:29:41Z")

</div>

Hi,

Thank's for your interest !!! My problem is that I use dynamic task, so i can't export. Let me explain my use's case:

Task 1 : every day at 12:00 am -\> action 1  
Task 2 : every day at 10:00 pm -\> action 3  
Task 3 : every day at 04:00 pm -\> action 2

Suppose that at 01:00 pm of today the system go down and tomorrow restart at 05:00 pm .

In the mean i've lost 3 task, and not set the relative message out, so when it redtart i can read the next task but i dont know the previous, so i can't set the right message.

The block should tell me the last task before now, so i could trigger it and fire the action.

Sorry for my bad english 😁

I hope to have explained you better this time my problem

Thank"s

Gianluca

---

<div class="post-metadata">

### Author: ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)
#### Post date: [11 March 2023 18:35 UTC](https://discourse.nodered.org/t/get-last-missed-event-with-cron-block-after-reboot/76301/11 "2023-03-11T18:35:30Z")

</div>

deleted post

---

<div class="post-metadata">

### Author: ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)
#### Post date: [11 March 2023 20:03 UTC](https://discourse.nodered.org/t/get-last-missed-event-with-cron-block-after-reboot/76301/12 "2023-03-11T20:03:11Z")

</div>

A couple of questions, you say you can't share the flow your schedule is dynamic. How does that get set? How do you restore it after a power cycle?

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [11 March 2023 20:15 UTC](https://discourse.nodered.org/t/get-last-missed-event-with-cron-block-after-reboot/76301/13 "2023-03-11T20:15:11Z")

</div>

It's unfortunate that cron-plus does not log the schedules it executes to the syslog (at least not unless you have logging set to "debug") I think @Steve-Mcl originally had it logging everything and was persuaded to crank it back a bit.  
In theory you could track down the latest log entry with an exec node running

```auto
grep '\[cronplus.*running' /var/log/syslog` | grep -v grep | tail -1

```

The name of the system log might well change. On my Raspberry Pi I think it is renamed syslog.1 at startup, so would have to search that file.

This is what it shows me just now  
`Mar 11 20:12:00 ZeroTwoPink Node-RED[366070]: 11 Mar 20:12:00 - [debug] [cronplus:6574dc33dea16033] running 'schedule1' ~ 'topic1'`

I can't really see how to get from that to a list of schedules missed while the device was down.

---

<div class="post-metadata">

### Author: ![tenti](https://avatars.discourse-cdn.com/v4/letter/t/5daacb/32.png) [@tenti](https://discourse.nodered.org/u/tenti)
#### Post date: [11 March 2023 20:23 UTC](https://discourse.nodered.org/t/get-last-missed-event-with-cron-block-after-reboot/76301/14 "2023-03-11T20:23:02Z")

</div>

Hi, the block can be set to persist the task, sratic or dynamic

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [11 March 2023 21:06 UTC](https://discourse.nodered.org/t/get-last-missed-event-with-cron-block-after-reboot/76301/15 "2023-03-11T21:06:03Z")

</div>

> [@tenti](#):
>
> Task 1 : every day at 12:00 am -\> action 1  
> Task 2 : every day at 10:00 pm -\> action 3  
> Task 3 : every day at 04:00 pm -\> action 2

For a simple schedule like that it is very easy to make it send the action every minute using the technique from my first post. Then if the repeats themselves cause problems a Filter node will strip them out.

---

<div class="post-metadata">

### Author: ![tenti](https://avatars.discourse-cdn.com/v4/letter/t/5daacb/32.png) [@tenti](https://discourse.nodered.org/u/tenti)
#### Post date: [11 March 2023 21:30 UTC](https://discourse.nodered.org/t/get-last-missed-event-with-cron-block-after-reboot/76301/16 "2023-03-11T21:30:59Z")

</div>

Hi,

I've use'd the repetition but after reboot it wait for next event anyway

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [11 March 2023 21:32 UTC](https://discourse.nodered.org/t/get-last-missed-event-with-cron-block-after-reboot/76301/17 "2023-03-11T21:32:52Z")

</div>

> [@tenti](#):
>
> I've use'd the repetition but after reboot it wait for next event anyway

In that case the repetition is not working. What was the full definition of the schedule that you used, including the repetition?

---

<div class="post-metadata">

### Author: ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)
#### Post date: [11 March 2023 22:10 UTC](https://discourse.nodered.org/t/get-last-missed-event-with-cron-block-after-reboot/76301/18 "2023-03-11T22:10:09Z")

</div>

> [@tenti](#):
>
> I can get the next event but i've to get the previous one

This is not a solution to your issue (and I'm no expert on cron), but there may be an enhancement of the cron-plus node that you could discuss with the author. The CronosJS library used by the node has the methods `nextDate(afterDate?)` and `nextNDates(afterDate?, n?)` that I think could be used to recreate missed outputs, assuming that you can remember when the last output was sent. It might take some effort to implement a command to send some or all of the missed events, but it would be a useful feature.

---

<div class="post-metadata">

### Author: ![tenti](https://avatars.discourse-cdn.com/v4/letter/t/5daacb/32.png) [@tenti](https://discourse.nodered.org/u/tenti)
#### Post date: [12 March 2023 08:51 UTC](https://discourse.nodered.org/t/get-last-missed-event-with-cron-block-after-reboot/76301/19 "2023-03-12T08:51:51Z")

</div>

Hi thank's for this tips.

I'll investigare about this.

Regard's

Gianluca

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [12 March 2023 09:49 UTC](https://discourse.nodered.org/t/get-last-missed-event-with-cron-block-after-reboot/76301/20 "2023-03-12T09:49:47Z")

</div>

> [@tenti](#):
>
> Task 1 : every day at 12:00 am -\> action 1  
> Task 2 : every day at 10:00 pm -\> action 3  
> Task 3 : every day at 04:00 pm -\> action 2

To make that auto repeat use a schedule that sends action 1 every minute where the hour is 12,13,14 or 15, sends action 2 every minute where the hour is 16 to 21, and action 3 when it is 22 or 23, or 0 to 9.

[Next page](https://discourse.nodered.org/t/get-last-missed-event-with-cron-block-after-reboot/76301.md?page=2)
