I think the key is “now”. The “now” variable is one that you get for free in the rules and it is a Joda DateTime object that represents the time right now. Also, the key to working with time in openHAB is the Joda library, particularly the “now” variable.
Note, if you were able to accept something a little more static (e.g. shutting off the lamp at a fixed time of night rather than half way between sunset and midnight) below could be WAY simpler, implemented without even needing the rules.
I would implement your rules as follows.
Items:
Switch Lampgroup1_ON { astro="planet=sun, type=set, property=start" }
Switch Lampgroup1_OFF
Switch Lampgroup2_ON
Switch Lampgroup2_OFF
Rules:
var Timer Lampgroup1_OFF_Timer = null
var Timer Lampgroup2_ON_Timer = null
rule "Sunset"
when
Item Lampgroup1_ON received command
then
val long midnight = now.plusHours(23).withTimeAtStartOfDay.millis // get time at midnight tomorrow in milliseconds epoc
val long halfway = (midnight - now.millis) / 2 // get number of milliseconds to halfway between now and midnight
Lampgroup1_OFF_Timer = createTimer(now.plusMillis(halfway + (10*60*1000)), [ | Lampgroup1_OFF.sendCommand(OFF) ]) // Timer to send OFF command at Sunset + ((Sunset-midnight)/2) + 10 mins
Lampgroup2_ON_Timer = createTimer(now.plusMillis(halfway), [| Lampgroup2_ON.sendCommand(ON) ]
end
rule "Midnight"
when
Time midnight
then
Lampgroup2_OFF.sendCommand(OFF)
Lampgroup1_OFF_Timer = null
Lampgroup2_ON_Timer = null
end
Actually I sort of misspoke as I would actually implement your rules following the design pattern I describe here. In short, I would have some time of day switches that get turned on and off based on certain times of day. Then I would trigger events or check what time I’m at by checking those switches. I’ve sort of done that here with how I’m triggering the Lampgroup switches, but I would make them generic (i.e. EarlyEvening, Evening, Night, Morning, etc) instead of having them tied to the lamps directly.