Checking if time is between 6 and 0 (06:00 and 00:00)

Hi there,

i´m setting up a rule to turn on my hue for 5 minutes when motion is detected between 6 and 0 (06:00 / 00:00).
But i´m not sure what time format OH is using.

    if(now.getHourOfDay() >6 && now.getHourOfDay() <0)
        logInfo("RuleFlurNacht", "Aborting, it´s not night.")
        return (false)

Would this work ?


OH uses 24 hour format, so 0-23 for the hours. That statement won’t work, because you can’t have a number that is both bigger than 6 and less than 0, and the hours will never be less than 0 anyway. If you want to trigger between 6pm and midnight, you’d check if getHourOfDay is greater than 18. If you want to trigger before 6am, you’d check if it was less than 6. Hope that helps.

I assume you are referring to 6pm. I believe you need >17 (i.e. greater than the 5pm hour).

If you have more things you want to do based on the time of day, see my Time of Day Design Pattern for a more modular and easier to maintain way to keep track of time periods and trigger or control behaviors based on time like this.

For example, I keep track of four time periods: Night, Morning, Day, and Evening. Some are based on clock time and others are based on sunrise/sunset. Some things trigger when a time of day period starts or stops and some rules change their behavior based on what the current time of day is.

You should use

if (now.getHourOfDay > 5)

From 12:00:00am to 05:59:59am getHourOfDay() will return 0 to 5, from 06:00:00am to 11:59:59pm it will return 6 to 23.

For using upper and lower limit, I use

if(now.plusHours(4).getHourOfDay > 9) //this is 6am to 8pm aka 06:00 to 20:00

Also, the testing for now.getHourOfDay() > 17 would be the simplest test, you can also do the following:

if(now.millis >= now.withTimeAtStartOfDay.plusHours(18).millis && now.millis < now.withTimeAtStartOfDay.plusHours(24).millis)