Cron expression

Dear Community,

I have action to trigger every minute between 6:00 AM to 14 (2:00 PM), first I made cron expression every minutes as “0 * * ? * *”. However my concern is not to exhaust the system for comparison every minutes for 24 hours which is unnecessary.

So, here I would like to take your opinions

  • Does this cron expression “0 * 6-14 ? * * *” do the job (triggers every minutes only between 6:00 am to 14:00 pm) ?

  • Is my concern needed, because I am thinking when the system uses cron it compares all the time any way ?

  • Is Comparison two DateTime variables better than two String variables (for example: DateTime variable to be converted into String), in term of performance ?

  • Also in term of performance: in (Design Pattern: Time Of Day), and when using it for every minutes triggering; will using timers better than comparing end of periods ?

Yes, this should work. But why are you triggering every minute instead of triggering at the crossover times of the intervals?

IMO, your concern is valid. The performance hit should be negligible, but the fix is simple! If you’re concerned about performance, only trigger at the crossover times of the intervals.

IMO, comparing DateTime would be negligibly faster.

I’m not clear on what you’re asking. Do you have an example to show, or are you just thinking about using timers?

No, this will trigger every minute at the 0 second from 6:00 until 14:59

A minute is nearly an eternity when it comes to computers. I wouldn’t worry about that. Of course, it is always cleaner not to poll when you don’t have to, and even better not to poll at all.

I don’t know the specifics, but the library that is used to do the cron implementation is I’m sure as efficient as can be done. I suspect that the library pretty much uses the epoch for all of its calculations meaning its just doing some simple math.

As Scott says, it would be negligibly faster. But you shouldn’t be worrying about performance. You should focus on writing rules that make sense to YOU. Worry about performance when you actually encounter a problem and when you do encounter a problem, come back here and ask for help. Your primary focus should be on making Rules that are simple and easy for you and others to understand. That is way more important than the performance difference between comparing Strings and DateTime Objects or the performance impact of triggering a Rule once minute all day.

Your time is way more important than the computers. Don’t waste it solving things that are not really a problem.

The only exception to this is you should pay attention to long running Rules and avoid them where possible. So use Timers instead of Thread::sleep.

I’m with Scott. I don’t understand what you are asking.

I am trying to do is applying the tutorial (Islamic Prayers - Time calculations and rules with audio and video). For learning and it is helpful.
So instead of checking every minutes, I try to stop checking after Isha (the last prayer of the day) and before Fajr (first prayer of the day), it is considerable period of time.

I can see some delay to getting my log message, especially when I add more devices

I mean first I made timers to be triggered when currrentTime == specific prayerTime, and when timer be off -> it will reset the action.
Then I see the method is not fine in term of scalability, so I tried to apply the DP, but I see some delay of getting my log messages !

Ah… this makes more sense now. Do you have a log at the beginning and end of the rule? This will help determine if it is inaccuracy in the cron trigger, or (more likely) due to the rule taking a while to complete. How much delay are you seeing? If you would post your rule, maybe there is something that can be done to speed it up.

Also, if you want the rule to trigger specifically from 0600 to 1400, inclusively, you could use two triggers…

“0 * 6-13 ? * * *”// the first second of every minute from 0600 to 1359
“0 0 14 ? * * *”// the first second of 1400
1 Like

If you are seeing some specific problems with that code you should ask for help from there, or at a minimum tag @vzorglub.

actually I meant my code

The cron expression are string that are fixed in code so you wouldn’t be able to do that.
The check every minute takes a mere milliseconds in one of the threads and won’t affect the performance of your OH.