Fire rule when item changed between hours of midnight and 6

The rule below fails to save. I looked at Rlkoshaks design pattern for some inspiration. I see he is using or but i can’t find any examples like if the item changes announce it but only between midnight and 6. Please help! Much appreciated. Below is what I have tried under the french door

rule "Basement door"
when
        Item Basement_door_DoorSensor changed from CLOSED to OPEN
then
        {
        Kitchen_Speak.sendCommand("Basement door has been opened")
        JayBedroom_Speak.sendCommand("Basement door has been opened")
        }
end

rule "French door"
when
        Item FrenchDoor_DoorSensor changed from CLOSED to OPEN  && Time cron 0 0 0-6 ? * * *

then
        {
        Kitchen_Speak.sendCommand("Kitchen French door has been opened")
        JayBedroom_Speak.sendCommand("French door has been opened")
        }
end

  • Issue of the topic: please be detailed explaining your issue
  • Please post configurations (if applicable):
    • Items configuration related to the issue
    • Sitemap configuration related to the issue
    • Rules code related to the issue
    • Services configuration related to the issue
  • If logs where generated please post these here using code fences:

Rules are triggered by events. No two events occur at the same time so and makes no sense. Between the hours of midnight and 6am is a state, not an event.

So you can’t do this with the rule triggers.

But you can have an if statement as the first line of the rule and exit if it isn’t the right state. Using the timer if day do

if(vTimeOfDay.state == "BED") return;
1 Like

What you have wouldn’t work. Timecron generates an event itself. What you need is a condition check for the hour of the day. Use the now variable to do so. Something like this:

val hourOfDay = now.getHourOfDays()
if ( hourOfDay > 0 && hourOfDay < 6 ) {
    ...
}

http://joda-time.sourceforge.net/apidocs/index.html?org/joda/time/LocalDate.html

1 Like

As @rlkoshak and @yfaway mentionned it won’t work
The triggers in rules can only be “or” because 2 events will NEVER happen at the same time

rule "French door"
when
        Item FrenchDoor_DoorSensor changed from CLOSED to OPEN
then
    if (now.getHourOfHay <= 6) { // Between 12:00 and 6:59:59am
        Kitchen_Speak.sendCommand("Kitchen French door has been opened")
        JayBedroom_Speak.sendCommand("French door has been opened")
    }
end

But I recommend that you implement something like this:

1 Like

Should be

val hourOfDay = now.getHourOfDay

As .getHourOfDay is never less than 0 (it’s 0 to 23…) you can omit the first condition:

if (hourOfDay <=6)

and as you only need one condition, there is no need for a val at all:

if (now.getHourOfDay <= 6) {...

:wink:
Now how to test if it’s between 22:30 and 4:45? Without val?

if (now.plusMinutes(90).getMinuteOfDay <= 6*60+15) // 22:30 + 90 Minutes = 0:00, 04:45 + 90 Minutes = 06:15

:sunglasses:

1 Like

Yes, your design pattern would be the better strategy for sure. I need to bite the bullet and start converting to png for all of the icons.

Udo, thank you. I’m going to use this while I learn the design pattern.