Determine if it's day or night at startup using Astro binding

So last time I wrote in java was in college about 10 years ago. I’m a little hazy on specific functions.

I’m trying to get openhab to postupdate a switch that acts like a boolean for daytime. There seems to be a shorter way to do this straight through the Astro binding (Apparently it calculates day/night) but I don’t know how to do that.

Anywho, I just need the proper function to return a current timestamp… like now.currenttime() or somesuch.

.items
Switch Sunrise_Event “” { astro=“planet=sun, type=rise, property=start” }
Switch Sunset_Event “” { astro=“planet=sun, type=set, property=end” }
DateTime Sunrise_Time “Sunrise [%1$tH:%1$tM]” {astro=“planet=sun, type=rise, property=start”}
DateTime Sunset_Time “Sunrise [%1$tH:%1$tM]” {astro=“planet=sun, type=set, property=end”}
Switch Daylight “Daylight”

.rules
rule "Set Daylight"
when
Item Sunrise_Event changed
then
postUpdate(Daylight, ON)
end

rule "Set night"
when
Item Sunset_Event changed
then
postUpdate(Daylight, OFF)
end

rule "Start Daylight"
when
System started
then
if (Currenttime <= Sunrise_Time && Currenttime > Sunset_Time) {
postUpdate(Daylight, ON)
} else {
postUpdate(Daylight, OFF)
}
end

Hi Marcus

may be you need to change the condition in the rules like:
rule "Set Daylight"
when
Item Sunrise_Event received update ON

regarding calculation i use:
if (!now.isAfter((Sunset_Time.state as DateTimeType).calendar.timeInMillis))

also i have the following imports in the rule
import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.openhab.core.types.Command
import org.joda.time.*
import java.util.Calendar
import java.util.Date
import java.text.SimpleDateFormat

hope you get an idea how to adapt it to your needs

Sebastian

Update I missed your calculation Thanks! I’m trying it now!

Update 2

2016-05-24 23:33:27 - Daylight state updated to ON <-This tells me it worked
2016-05-24 20:20:49 - Daylight state updated to ON
2016-05-24 20:22:08 - Daylight received command ON
2016-05-24 20:22:11 - Daylight received command OFF
2016-05-24 20:23:54 - Daylight state updated to ON

Here’s the current code:

rule "Start Daylight"
when
System started
then
if (now.isAfter((Sunrise_Time.state as DateTimeType).calendar.timeInMillis)&& !now.isAfter((Sunset_Time.state as DateTimeType).calendar.timeInMillis)) {
postUpdate(Daylight, ON)
} else {
postUpdate(Daylight, OFF)
}
end

Original reply

Good catch, normally I think you’d be right but the astro binding toggles the item switch on/off when the current time reaches “sunrise” or “sunset”. Any change on or off would trigger rules dependent on that item. Unfortunately, because it toggles “off” regardless, it can’t be used for current status. Most of the time it would be off.

My problem is after an unexpected reboot. When the system boots up how can I tell if it’s day or night.

Specifically, how do I call a timestamp for the current time to compare to sunrise and sunset.

import java.util.Calendar
import java.util.Date
import java.text.SimpleDateFormat

I have a feeling my answer is in one of those libraries.

You can use the calendar object like so:

if( calendar1.after(calendar2) )
   ...
else
  ...

How would I declare Calendar2 as the current date/time.

I have to mention, adding received update ON was the right call. Simply having it triggered on a change forces it to be triggered twice sunrise or sunset.

Nothing terrible, but it is a little sloppy. Might have gone completely unnoticed.

Thanks!

You may wish to reference this thread, which shows:

val morning = now.withTimeAtStartOfDay.plusHours(5).millis // 5 AM
val sunrise = new DateTime((Sunrise_Time.state as DateTimeType).calendar.timeInMillis)
val twilight = new DateTime((Twilight_Time.state as DateTimeType).calendar.timeInMillis)
val night = now.withTimeAtStartOfDay.plusHours(23).millis // 11 PM

if(now.isAfter(morning) && now.isBefore(sunrise)) {
    Morning.sendCommand(ON)
    Day.sendCommand(OFF)
    Twilight.sendCommand(OFF)
    Night.sendCommand(OFF)
}
3 Likes