Condition between x time and y time

I’d like to fire a rule but only if the time is between 6:00 AM and 6:30 AM. Something like:

    Item AnyoneHome received command OFF
    if  [[time is between 6:00 and 6:30]] // **insert answer here**

I found one (2 years old) solution in the forums but I’m wondering if anyone has a second to give me a new and improved one-liner for this?

I’m expecting it to be something like:

(now.isAfter(6, 0) && now.isBefore(6, 30))

it would rather need to be something like this (beware, untested …)
now.isAfter(now.withTimeAtStartOfDay.plusHours(6)) && now.isBefore(now.withTimeAtStartOfDay.plusHours(6).plusMinutes(30))

A more simple way would be not to use .isBefore and .isAfter when we are talking of time of day:

    Item AnyoneHome received command OFF
    if (now.getMinuteOfDay >= 6*60 && now.getMinuteOfDay <= 6*60+30)

You could even use

if(now.getHourOfDay == 6 && now.getMinuteOfHour <= 30)

but this would be less variable :slight_smile:

These look like good options. Now the bigger question: where can I find ALL of the things that can go after “now.” ?

So far I know:

Surely there are more. Where is the documentation on this?

Well, I’m not sure if openHAB supports all of them (never tried), but take a look at joda.time

At least you can use

  • getSecondOfMinute
  • minusSeconds()
  • minusMinutes()
  • minusHours()
  • minusDays()
  • plusSeconds()
  • plusMinutes()
  • plusHours()
  • plusDays()

And it’s possible to combine them, as every object method will be of the same type:


would be now() + 31 Minutes. and as it’s nearly 23:24:00 -> 23 * 60 + 55 = 1435

1 Like

There’s many options but no definite or recommended one.
I’m actually using now.millis which is long type, that’s easier to work with particularly when you don’t want to code fixed times into your rules but want to use items (of Number type) instead and have the times changeable via GUI.
Docs: Something’s here, more if you google for Joda just as Udo just replied.

1 Like

It could be better to use:

[quote=“mstormi, post:2, topic:46379”]
now.isAfter(now.withTimeAtStartOfDay.plusDays(1).minusHours(18)) &amp;&amp; now.isBefore(now.withTimeAtStartOfDay.plusDays(1).minusHours(17).minusMinutes(30))

This way we go to the end of the day and go back for the hours
This will avoid edge cases like summer time changes

This is a very good point. In my case however it’s a Monday-Friday only situation (turning off my morning lights when I leave the house). I don’t ever work on Sunday so daylight saving on/off will not affect this rule.

I’m going to save the idea though - just in case I want to make another automation that might fire on Sunday.

Here’s another way to do it (I added in your day of week requirement too)…

    Item AnyoneHome received command OFF
    if ((1..5).contains(now.getDayOfWeek) && (new Interval(now.withTime(6,0,0,0),now.withTime(6,30,0,0)).contains(now))) {

2020-12-22 14:59:37.820 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘Activatie-2’ failed: ‘withTimeAtStartOfDay’ is not a member of ‘java.time.ZonedDateTime’; line 24, column 71, length 24 in Activatie

if(Gem_perc_MastRol.state >= 0.85 && now.isAfter(now.withTimeAtStartOfDay.plusHours(20).plusMinutes(30)) || now.isBefore(now.withTimeAtStartOfDay.plusHours(5).plusMinutes(00))){
logInfo(“regel 2”, “voorwaarde 2 slapen gaan”)
postUpdate(Script1, ON)

What is now (openhab3) the correct syntax? :see_no_evil: :grinning:

In the release notes, scroll down to Rules.

thx for the hint. I tried to change the rule above but i get an error or nothing happens, can you give me a hint how to? Because i cant wrap my head around it.

Can you show us what you have now?

Please use code fences when showing code - sandwich your code in-between three backticks: ```


like this


        Time cron "0/20 * * 1/1 * ? *"
    if((Dom_Nacht_Ems.state == ON || Timer_Lamp.state == OFF) &&
        (now.getHour.isAfter(8) && now.getMinute.isAfter(59))){
        sendCommand(Dom_Nacht_Ems, OFF)
        sendCommand(Timer_Lamp, ON)
        sendCommand(Kids_Slapen, OFF)
        if((!Kinderkamer_Binary_Switch.changedSince(now.minusMinutes(10))) && Gem_perc_KidsRol.state != 0 && Gem_Buiten_Licht.state >= 10000){
            if(Liam_Rolluik.state != 0){
                sendCommand(Liam_Rolluik, 0)
            if( Emily_Rolluik.state != 0) {
                sendCommand(Emily_Rolluik, 0)

this is the fault :

2020-12-23 17:44:00.945 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'KidsSlaap-1' failed: 'isAfter' is not a member of 'int'; line 12, column 10, length 22 in KidsSlaap

tried a lot of things but here i stopped...
thx for the ''' tip btw!

original it was this


There is no such thing as a or “the” correct syntax. There’s many correct syntaxes and a gazillion possibilities. To select from these remains to be your task (and yours alone).
Check out this thread (there’s an OH3 one, too)