How to efficiently trigger events at the time defined by the user in the UI?

Hi, everyone.

I have been looking for a long time for an efficient way to trigger actions at a time defined by the user in the UI using the rules DSL engine. A use case, but not the only one, is the alarm clock (Individual Alarm clock for each Day of week, with adjustable duration). Another use case would be allowing users to define a time to turn things on or off, e.g., smart plugs.

I know that, unfortunately, we cannot dynamically edit the cron time in a rule, so the only option I know so far is creating rules that get triggered periodically (e.g., every 5 minutes).

when
Time cron "0 0/5 * ? * *"
then

I’m not sure about the performance and resource impact of that option.

However, I have noticed that such a rule runs exactly at 07:05:00, 07:10:00, etc., not exactly every 5 minutes since the service started (e.g., 07:02:48, 07:07:48), so I’m wondering whether OpenHAB has already something implemented to avoid performance degradation by cron-based rules.

If that is the case, I also wonder if creating X (e.g., 30) number of similar rules running every 5 minutes wouldn’t have a big performance impact just because they are triggered so frequently. Of course, it’s understandable that there will be a performance overhead caused by the logic inside the rule, so the simpler the better.

If there isn’t a better alternative and creating many rules drastically decreases the performance and resources, I believe implementing an efficient solution to handle this case would be an extremely useful and long overdue feature.

I found the same topic from 2018: Can I create a time based triggered rule that is based on items value?

You can trigger a rule based on a DateTime item. In DSL it would be the following:

when
    Time is YourDateTimeItem
then
1 Like

That’s awesome! Thanks, @mhilbush
I’ve been looking for years for that!

I’ll test it as soon as I can.

There is no performance degradation from cron based rules. On any machine as powerful or more powerful than the recommended minimum (an RPi 4 BTW according to the latest docs) you couldn’t schedule a rule to run fast enough to matter.

The reason that rule runs exactly at 07:05:00 is because that’s what the cron expression is defined to do. The first 0 means “at second 0” and the 0/5 means every five minutes starting and minute 0. To construct a cron expression relative to system start instead of the clock, use *. * */5 * ? * *

This has long since been improved in OH 3. In addition to the Time is trigger @mhilbush points out see:

Hahaha you’re right! Dumb me!