Evaluating a Cron Expression inside of a Rule - OH3

Hi,

I am trying to evaluate a cron expression inside of an OH3 rule. I have created a rule to trigger every 30 minutes based on a cron expression (this is working fine). I am successfully persisting and restoring (using MapDB) a string item containing a different cron expression that I want to evaluate inside of the rule triggering every 30 minutes. I am stuck at this point and cannot find anything to point me in the right direction. Is this even possible? Any advice or guidance would be greatly appreciated.

What’s the point? If it’s only for checking the expression, I guess you would have to do it manually (i.e. reduce consecutive spaces to one, split string on spaces, read and analyze every position.

This sounds very much like an XY Problem. What are you attempting to achieve?

If you are trying to change the trigger for this rule to use a different cron expression, only in certain very advanced cases is this even possible at all and almost certainly not the best approach.

If you are just trying to schedule something to occur some time, that’s what Timers are for.

Thanks for your responses.

I am making timer control sitemaps for my devices where I have:

  • A switch to set if the timer is active or not.
  • A set of switches for days of the week.
  • Selection items to set an on and off times, these could possibly be Astro events or time values (in 30 minute intervals – this is what the 30 minute cron rule is for. It should check if the timers are active, and if the on or off triggers are not Astro events, if not extract the persisted variable cron expressions for on and off to check if an action should occur).

I want to be able to set these from the sitemap only. I have got it working by breaking up the string manually, I’m not sure how else to achieve it yet, I’m sure there is a simpler way though. Its all working but now having issues with mapdb not persisting values after abrupt power losses, values are persisting after proper shutdowns without issue, however still looking into this and alternatives.

Have you seen Time Based State Machine [3.2.0;3.4.9] or Time Based State Machine [4.0.0.0;4.1.0.0) if on OH 4? You don’t need to write the logic for this nor do you necessarily need all these Items and such. This provides a rule that will drive a time based state machine that allows you to set up different start times for each state given a type of day (weekend, holiday, custom day type, etc). Sounds like exactly what you are trying to do.

If that’s more than you are after, there is a Time is <Item> rule trigger that will trigger the rule at the date time or just the time ignoring the date.

If you are stuck on sitemaps, you’ll have to wait for OH 4 which has a new input element that lets you input arbitrary text.

On MainUI (any version) you can use a Date Time or just a Time picker.

And here’s your XY Problem. I’m going to guess that you are running openHABian with the default zram configuration. The way zram works is all the changes you make to OH configs, persistence, logs, etc. only exist in RAM. It’s not until zramd is normally shutdown that all the contents of that zram volume are written to the SD card. Consequently, if you pull the plug, any changes made since the last startup, including to MapDB, are lost.

So you can disable zram, though then you need to watch out for SD card wea-out and you increase the likelihood that the file system will become corrupted on loss of power or you need to take actions to make sure that power loss is a rare event and come up with recovery procedures.

What makes this an XY Problem is non of the above involves messing with cron expressions.

Apologies for the delayed response, haven’t been able to come back to this project for a while.

Thanks @rlkoshak yes that seems to be the proper way to do it. The issue is that this needs to be used by my grandmother, who definitely will not be changing any rules or using the Main UI. The solution I have used isn’t optimal but works perfectly for this case.

Regarding the persistence. I am still currently using MapDB, as it persists every hour atleast. The issue with this is that in South Africa the power is almost guaranteed to go off at least once a day and the times are not very accurate. I would prefer not to disable zram to persist a few values. I attempted to use MongoDB but I do not want to use a local db and the binding is not compatible with the latest version of Atlas.

But thanks so much for the assistance.

Another option would be a HAT UPS for the Pi which will provide Power at least for a proper shutdown. These modules are cheap, as there is no need to generate Voltage other than 5 V :slight_smile:
The modules come with software to shutdown the Pi (and even to start the Pi when Power comes back).

All the database save Item states as often as you configure then too. By default, Mapadab stores values on ever change.

But if you have zram, it doesn’t matter how often you save values, on a sudden power loss you’ll lose all data since there last power loss. The time is database doesn’t matter.