If (between time A and time B)

Open Openhab3.1, in a script rule… how do I do an “IF in between 7am and 9pm - do this” … i tried using

if(now.plusHours(7).getHour > 23) {

but that did not work…

any ideas?

In openhab2.5 I have this:

if (((now.getHourOfDay) >= 7) && ((now.getHourOfDay) <= 21)) {

} 

I think in openhab3 the ‘getHourofDay’ is a bit different.
But you’ll find out for sure.
Grüße,
Baschtlwaschtl

1 Like

ZonedDateTime (Java SE 11 & JDK 11 ) has the full documentation for ZonedDateTime, which is what now is.

Note that when writing rules in the UI there are other options for controlling the execution of a rule based on time. The “But only if…” section, also called Conditions, lets you define a set of conditions that need to be true in order for the rule to run. Time and Ephemeris are among the conditions that could be defined (e.g. between 07:00 and 09:00 on weekdays).

1 Like
if (now.getHour() >5 && now.getHour() <10) {

}

works in OH 3.1 for me

Comment; you have to think about these things, you’re instructing a machine and it is pretty dumb.
At time 23:55, the hour part is still not greater than 23, so this won’t be true.
At midnight, the hour rolls over to 00:00 … so >23 will never be true.

Put it another way, hour >7 means 8 o’clock and after in practice.

i fixed it by using a manual switch - defined in the items document… then @7:00 ON… @23:00 OFF… and then using an IF statement in my rules… it works now …