OH3 Cron Trigger 1 day out

I created a rule that triggers on a specific day of the week but when testing found that the trigger did not work. So I created a pair of rules (one to switch a device ON and another to switch it back OFF a minute later) which which have triggers for each day of the week, with each trigger having a different time. In the UI the ON rule shows as:


Today being Thursday, which the machine running the rule confirms via the date cmd, I would expect the ON rule to have triggered at 16:48 and the OFF rule at 16:49 however the log shows that they triggered at 16:50 and 16:51 respectively, that is the time one would expect if it were Friday.


So unless I’ve done something stupid, which of course is possible, it would appear that the Cron is triggering on the wrong day. Is this a known issue, if so is there a workaround?

Don’t post screen shots of code and logs. Post the actual text. Screen shots are almost impossible to read, can’t be searched, and are generally not complete. Use code fences to preserve formatting.

```
code and logs go here
```

The Timer Switch ON screen shot doesn’t do anything. You’ve added a Script Action and the Script Action doesn’t do anything. It’s blank. Not that I could have really read what was in there if it wasn’t blank. Screen shots are impossible to read on a phone.

If the cron expressions are off, you can adjust by shifting your day fields in the cron expression by one. I’ve not heard of there being a problem with cron triggers but that would be a work around.

I think there is a bug in the ui because 1 should be Sunday and the ui sets Monday to 1. The fix here is to just set it to the correct number or use the abbreviation of the day, like SUN.

Here is the “code” and log within code fences as suggested:

triggers:
  - id: SwitchON00
    configuration:
      cronExpression: 0 40 16 * * 7 *
    type: timer.GenericCronTrigger
  - id: SwitchON10
    configuration:
      cronExpression: 0 42 16 * * 1 *
    type: timer.GenericCronTrigger
  - id: SwitchON20
    configuration:
      cronExpression: 0 44 16 * * 2 *
    type: timer.GenericCronTrigger
  - id: SwitchON30
    configuration:
      cronExpression: 0 46 16 * * 3 *
    type: timer.GenericCronTrigger
  - id: SwitchON40
    configuration:
      cronExpression: 0 48 16 * * 4 *
    type: timer.GenericCronTrigger
  - id: SwitchON50
    configuration:
      cronExpression: 0 50 16 * * 5 *
    type: timer.GenericCronTrigger
  - id: SwitchON60
    configuration:
      cronExpression: 0 52 16 * * 6 *
    type: timer.GenericCronTrigger
conditions: []
actions:
  - inputs: {}
    id: "1"
    configuration:
      privId: i49
    type: jsr223.ScriptedAction
2021-02-04 16:50:00.320 [INFO ] [org.openhab.rule.8e5a5882b0         ] - NOW IN TURN ON RULE

2021-02-04 16:50:00.323 [INFO ] [org.openhab.rule.8e5a5882b0         ] - NOW Leaving TURN ON RULE

==> /var/log/openhab/events.log <==

2021-02-04 16:50:00.343 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'InLineSwitch01_Switch' received command ON

2021-02-04 16:50:00.352 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item 'InLineSwitch01_Switch' predicted to become ON

2021-02-04 16:50:00.367 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'InLineSwitch01_Switch' changed from OFF to ON

==> /var/log/openhab/openhab.log <==

2021-02-04 16:51:00.338 [INFO ] [org.openhab.rule.8e5a5882b0         ] - NOW IN TURN OFF RULE

2021-02-04 16:51:00.341 [INFO ] [org.openhab.rule.8e5a5882b0         ] - Trigger was: {}

2021-02-04 16:51:00.344 [INFO ] [org.openhab.rule.8e5a5882b0         ] - NOW Leaving TURN OFF RULE

The script actions, which run as expected just on the wrong day are:

var ONRule = new SimpleRule() {
    
    execute: function( module, input) {
    var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);
    logger.info("NOW IN TURN ON RULE");
    events.sendCommand("InLineSwitch01_Switch", "ON");
    logger.info("NOW Leaving TURN ON RULE");
    }};
ONRule.triggers = ONTriggers;
ONRule.name = "Multi-Period Timer Switch ON";
ONRule.description = "Turn Item ON according to timer schedule";
UID=automationManager.addRule(ONRule);

var OFFRule = new SimpleRule() {

    execute: function( module, input) {
    var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);
    logger.info("NOW IN TURN OFF RULE");
//   var Trigger = input['event'].itemName;
  logger.info("Trigger was: " + input);
    events.sendCommand("InLineSwitch01_Switch", "OFF");
    logger.info("NOW Leaving TURN OFF RULE");

    }};
OFFRule.triggers = OFFTriggers;

OFFRule.name = "Multi-Period Timer Switch OFF";
OFFRule.description = "Turn Item OFF according to timer schedule";
UID=automationManager.addRule(OFFRule);

I didn’t post them originally since, as far as I can see, there is no reason to believe that they have any relevance to the issue. The issue being that as the first screen shot shows the rule provides a list of Cron triggers and when they will run, both in Human Readable and Cron Format, but as the log shows even though it is Thursday it is the Friday trigger that fires.

I agree that is a work around however not a particualarly good one as the UI will be providing misleading information.

@hilbrand I agree I think there is a bug, and it is probably because of the day to number translation. But I believe that Monday should be 1. The reason I say this is that:

  1. I am sure I’ve seen it stated in the OH documentation/code - although I can’t find it at the moment.

  2. A number of wesites, eg IBM, Wikipedia, Google Cloud, the Ubuntu documentation etc all show the week running from Sunday(0) to Saturday(6) which seems to be the consensus of opinion. Although that said the Quartz Tutorial that the OH documentation links to states 1=Sunday.

I did originally try using the day abbreviation of the form ‘Sun’ rather than ‘SUN’ and the Cron trigger creation failed which is why I went to using the numbers. I have now just tried the upper case version ‘SUN’ and it is now working properly :grinning: Many thanks for the pointer.

The implementation in openHAB used the quartz library in the past. This would explain why it has Sunday as 1. I guess it’s probably to late to change it to use Monday as 1 as that could break lots of cronjobs. So I would suggest we should change the UI.

Based on your original post I’ve moved to using day name abbriviations in caps (i.e. SUN MON TUE WED THU FRI SAT), rather than mixed case (i.e. Sun Mon etc) which is what I used originally, and the timer works as expected.