Get last missed event with cron block after reboot

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

1 Like

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. 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.

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

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.

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

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.

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)

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.

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

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 :grin:

I hope to have explained you better this time my problem

Thank"s

Gianluca

deleted post

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?

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

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.

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

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.

Hi,

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?

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.

Hi thank's for this tips.

I'll investigare about this.

Regard's

Gianluca

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.