Time based Rules - Between times

  • Platform information:
    • Hardware:RPI3b+
    • OS: Debian 10 (Buster)
    • Java Runtime Environment: _Which comes with Openhabian / PI image
    • openHAB version: Openhab 3 / Openhabian 3.1.0
  • Issue of the topic: Make rule fire between certain times and triggers
  • 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:

Im trying to create a rule with the following things in mind:

When ITEM received command ON it should turn on and go off again in 15 minutes, except between these two time frames (07:00-09:00 & 16:00-19:00) In these time frames it should stay on.

The following is my current rule: (Which isn’t working)

var Timer timer = null

rule "Keukenlicht ochtend"
when
    Time cron "0 * 7-8 ? * * *" or
    Time cron "0 * 16-21 ? * * *"
then
    if (Mini006_Schakelaar === OFF)
        sendCommand(Mini006_Schakelaar, ON) else 
        (timer !== null) {timer.cancel()}

        timer = createTimer(now.plusMinutes(15)) [|
  		    sendCommand(Mini006_Schakelaar, OFF)
		    timer = null
		]

 end 

The rule doesn’t spit out an error, but isn’t working. (except for the validation issue: This expression is not allowed in this context, since it doesn’t cause any side effects.)

I’ve been at it for a couple of days now, trying different things, but im a bit lost. Could anybody shed a light (get it, light? :-P) on this to get me on my way?

Thanks for all the help!

Regards Onac

So that should be your rule trigger.

Within the body of your rule, you can look to see what time it is now and decide what action to take.

If the Item is ON between the specified times, you’ll need to decide what you want it to do (if anything) at the end of the magic period. Timers can be set up for fixed times, not just now+offset.

Let’s break it down. When having problems like this simplify and then gradually build it back up.

Part 1: When Item received command ON turn off in 15 minutes.

var Timer timer = null
rule "Keukenlicht ochtend"
when
    Item MyItem received command ON
then
    timer?.cancel()
    timer = createTimer(now.plusMinutes(15), [ | MyItem.sendCommand(OFF) ])
end

Make sure that works. If that works add in the next part, don’t schedule the timer if it’s between 07:00-09:00.

var Timer timer = null
rule "Keukenlicht ochtend"
when
    Item MyItem received command ON
then
    timer?.cancel()
    if(now.getHour() > 7 && now.getHour() < 9) return;
    timer = createTimer(now.plusMinutes(15), [ | MyItem.sendCommand(OFF) ])
end

Now add in the second time period

var Timer timer = null
rule "Keukenlicht ochtend"
when
    Item MyItem received command ON
then
    timer?.cancel()
    if(now.getHour() > 7 && now.getHour() < 9) return;
    if(now.getHour() > 16 && now.getHour() < 19) return;
    timer = createTimer(now.plusMinutes(15), [ | MyItem.sendCommand(OFF) ])
end

The return; immediately exists the rule.

So why did your rule not work?

  1. Rules are event driven. The “when” defined the event in which the rule is run. They are not conditions. Your two triggers say run the rule once a minute between 7 and 9 am and then run it once a minute between 4 pm and 9 pm. You’ve specified that you want to do something when the Item receives a command. That’s your event. That’s what you use to trigger the rule.

  2. Assuming your Item is Mini006_Schakelaar, an Item will never equal OFF. An Item carries a State and if the Item is a Switch Item it might carry a state OFF. You would need to compare to Mini006_Schakelaar.state == OFF if you wanted to see if that Item is OFF.

  3. You said you want to do something in response to the Item being commanded ON, not to command the Item to ON. There is no reason to sendCommand in this part of the rule.

Rlkoshak,

You are my saviour. :slight_smile: Thank you!
I first started the rule simply like your break down. And added stuff as I got along.

Points 2 and 3 where my struggle within the way the rules work.(The syntax) So indeed the “when the light is on turn it on” syntax was becaus I couldn’t figure out any other way.

One question tho:

The now.getHour() syntax, was that introduced into OH3? It’s not something I was fimiliar with?
Is there a site/information I could read upon about those syntaxes?

I got a really basic understanding of the if/else and do/loop stuff but haven’t seen that now.getHour() stuff before.

Anyway thanks for breaking it down, you have been a great help!
Ill post back if it all works like it should!

Onac

No, that sort of thing has always been there. However now used to be implemented by a Joda DateTime but now is implemented with a java.time.ZonedDateTime and the name of that function is slightly different. It used to be getHourOfDay().

See ZonedDateTime (Java SE 11 & JDK 11 ) for all that a ZonedDateTime can do.

1 Like

This sure did the trick, thanks again!