Create Timer for Sunset and Sunrise

I like to create a Timer (createTier) for Sunset and Sunrise_Time

Items:
DateTime Sunrise_Time “Sonnenaufgang [%1$tH:%1$tM]” {astro=“planet=sun, type=rise, property=start, offset=15”}
DateTime Sunset_Time “Sonnenuntergang [%1$tH:%1$tM]” {astro=“planet=sun, type=set, property=end, offset=-20”}

Rules:
var DateTime Sunrise = parse(now.getYear() + “-” + now.getMonthOfYear() + “-” + now.getDayOfMonth() + “T” + Sunrise_Time.state)
var DateTime Sunset = parse(now.getYear() + “-” + now.getMonthOfYear() + “-” + now.getDayOfMonth() + “T” + Sunset_Time.state)
createTimer(Sunrise) [| sendCommand(Schalter0_Switch, OFF) ]
createTimer(Sunset) [| sendCommand(Schalter0_Switch, ON) ]

Unfortunately it is not working, any hints?

F Hatz

I am sure you already know this, but just in case…

You can associate the astro binding with a Switch item (instead of a DateTime item), in which case the binding will handle the timer-stuff for you. When the event happens (i.e. sunrise, sunset, etc.) the switch will “fire” (briefly toggle from OFF to ON and then OFF again) and this can be used as trigger condition in rules (e.g. for turning lights ON or OFF).

Alternatively you can use it to toggle a “daylight” switch and then use this to control your lights, something like this:

Switch Sunrise_Event {astro="planet=sun, type=rise, property=start, offset=15"}
Switch Sunset_Event {astro="planet=sun, type=set, property=end, offset=-20"}
Switch Daylight

rule "Daylight - Sunrise"
when
	Item Sunrise_Event changed to ON
then
	logInfo("daylight", "End of sunrise. It is now daylight outside.")
	Daylight.postUpdate(ON)
end

rule "Daylight - Sunset"
when
	Item Sunset_Event changed to ON
then
	logInfo("daylight", "Start of sunset. It is now dark outside.")
	Daylight.postUpdate(OFF)
end

Hope this helps.

BR
Kjetil

4 Likes

I need to use the createTimer function as the script is used for a presence simulation. Using the Caldav-binding a switch ‘Gang’ is toggled on/off. In respect to sunrise and sunset time the switch ‘Licht_Gang’ is either immediately executed or delayed until the sunrise/sunset event.

Items:

DateTime Sunrise_Time “Sonnenaufgang [%1$tH:%1$tM]” {astro=“planet=sun, type=rise, property=start, offset=15”}
DateTime Sunset_Time “Sonnenuntergang [%1$tH:%1$tM]” {astro=“planet=sun, type=set, property=end, offset=-20”}
Switch Licht_Gang “Licht Gang” (Licht, Schalter) {homematic=“address=MEQ1510934, channel=1, parameter=STATE” }
Switch Gang // <- this item is changed by the caldav-binding

Rule:

import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.openhab.core.transform.actions.Transformation
import org.joda.time.*
import org.openhab.model.script.actions.Timer

var Timer Gang_Begin
var Timer Gang_End

var DateTime Gang_StartTime = null
var DateTime Gang_StopTime = null

rule "Gang"
when
Item Gang changed
then
if (Toggle_Absence.state == ON) {
if (Gang.state == ON) {
var DateTime Sunrise = parse(now.getYear() + “-” + now.getMonthOfYear() + “-” + now.getDayOfMonth() + “T” + Sunrise_Time.state)
var DateTime Sunset = parse(now.getYear() + “-” + now.getMonthOfYear() + “-” + now.getDayOfMonth() + “T” + Sunset_Time.state)
Gang_StartTime = now
if (Gang_StartTime.isBefore(Sunset) && Gang_StartTime.isAfter(Sunrise)) {
Gang_StartTime = Sunset
}
Gang_Begin = createTimer(Gang_StartTime) [| sendCommand(Licht_Gang, ON) ]
} else {
var DateTime Sunrise = parse(now.getYear() + “-” + now.getMonthOfYear() + “-” + now.getDayOfMonth() + “T” + Sunrise_Time.state)
var DateTime Sunset = parse(now.getYear() + “-” + now.getMonthOfYear() + “-” + now.getDayOfMonth() + “T” + Sunset_Time.state)
Gang_StopTime = now
if (Gang_StopTime.isAfter(Sunrise) && Gang_StartTime.isBefore(Sunset)) {
Gang_StopTime = Sunrise
}
if (Gang_StartTime != null && Gang_StopTime.isAfter(Gang_StartTime)) {
Gang_StopTime = Gang_StopTime.plusSeconds(2)
Gang_End = createTimer(Gang_StopTime) [| sendCommand(Licht_Gang, OFF) ]
} else {
Gang_Begin.cancel
Gang_Begin = null
if (Licht_Gang.state == ON) {
sendCommand(Licht_Gang, OFF)
}
}
}
}
end

This is exactly what I need … unfortunately it doesn’t work for me :frowning:

The rules never fire.

.items

    Switch Sunrise_Event    {astro="planet=sun, type=rise, property=start, offset=15"}
    Switch Sunset_Event     {astro="planet=sun, type=set, property=end, offset=-20"}
    Switch Daylight      

.rules

rule "Daylight - Sunrise"
when
	Item Sunrise_Event changed to ON
then
	logInfo("daylight", "End of sunrise. It is now daylight outside.")
	Daylight.postUpdate(ON)
	postUpdate(EventLog, "Daylight - Sunrise")        // eventlog.rules
	sendPushoverMessage(pushoverBuilder("End of sunrise. It is now daylight outside.").withSound("pushover"))
end


rule "Daylight - Sunset"
when
	Item Sunset_Event changed to ON
then
	logInfo("daylight", "Start of sunset. It is now dark outside.")
	Daylight.postUpdate(OFF)
	postUpdate(EventLog, "Daylight - Sunset")        // eventlog.rules
	sendPushoverMessage(pushoverBuilder("Start of sunset. It is now dark outside.").withSound("pushover"))
end

The Astro-Binding is installed and shows values in my sitemap.
But is the code above still compatible with oH2.4 - especially this astro line:
{astro="planet=sun, type=rise, property=start, offset=15"}

Thanks in advance

No, it is not. Please take a look at the documentation for the Astro v2 binding:

The concept has changed a bit with the introduction of things, event channels, etc.

My working setup is based on the following:

astro.things

Thing astro:sun:home [ geolocation="54.1362,05.9753" ]

(Location is randomized for privacy, :-))

general.rules

rule "general.rules - Daylight - Start"
when
  Channel "astro:sun:home:daylight#event" triggered START
then
  vDaylight.sendCommand(ON)
end

rule "general.rules - Daylight - End"
when
  Channel "astro:sun:home:daylight#event" triggered END
then
  vDaylight.sendCommand(OFF)
end

Let me know if it works out for you.

Great - many thanks for the really quick reply.

I’ll try this one and get back to you!

It works :slight_smile: