I am new to openhab2 and I am a bit stuck. I’m trying to get some of my zwave lights to turn on at sunset and off at sunrise. Should be simple right? I think I am mixing up openhab1 and openhab2 methods and it is not working
In my items file I have:
Switch Sunrise_Event {astro=“planet=sun, type=rise, property=start, offset=0”}
Switch Sunset_Event {astro=“planet=sun, type=set, property=end, offset=0”}
an example zwave lamp is: Dimmer BedroomLamp "Bedroom Lamp" (Garden,Lights) { channel="zwave:device:3e29eba6:node6:switch_dimmer:switch_dimmer" }
in my rules file I have:
rule "Bedroom Light On At Sunset"
when
Item Sunset_Event received update ON
then
postUpdate(BedroomLamp, 90)
end
but this rule never fires. In the openhab.log and events.log I don’t even see the Sunrise_Event or Sunset_Event fire.
You can use the 1.9 astro binding in OH2 instead of the native OH2 one. This works in the same way as for OH 1.8, and events based on sunrise/sunset etc trigger fine.
The events you are looking for are not available with openHAB2 anymore, as a better solution is underway but not available yet. Here’s yet another workaround:
Here’s what I did to generate sunrise/sunset events. It relies on the NTP binding being set up to refresh the time every 60 seconds. I think there may still be a boundary condition in my math, but so far I haven’t seen it occur in over a couple months… I use the same approach for several other events, such as start of Day, Night, Dawn, Dusk, etc., which is why I didn’t use Sun Elevation as mentioned above. However, I’ve removed all those other items and rules for the sake of brevity.
// Sunrise & Sunset
DateTime SunriseStart_Time "Sunrise Start [%1$tH:%1$tM]" { channel="astro:sun:local:rise#start" }
DateTime SunsetStart_Time "Sunset Start [%1$tH:%1$tM]" { channel="astro:sun:local:set#start" }
Switch SunriseStart_Event "Sunrise Event"
Switch SunsetStart_Event "Sunset Event"
DateTime CurrentTime "Date [%1$tA, %1$tm/%1$td/%1$tY %1$tT]" <calendar> { channel="ntp:ntp:local:dateTime" }
rule "Generate Time of Day Events"
when
Item CurrentTime received update
then
val now = new Date()
val sunset = new Date((SunsetStart_Time.state as DateTimeType).calendar.timeInMillis)
val sunrise = new Date((SunriseStart_Time.state as DateTimeType).calendar.timeInMillis)
if((now.getTime-(now.getTime%60000)) == (sunrise.getTime-(sunrise.getTime%60000))) {
logInfo("time-of-day.rules", "Start of Sunrise!")
SunriseStart_Event.postUpdate(ON)
}
else if((now.getTime-(now.getTime%60000)) == (sunset.getTime-(sunset.getTime%60000))) {
logInfo("time-of-day.rules", "Start of Sunset!")
SunsetStart_Event.postUpdate(ON)
}
end
rule "Sunrise started"
when
Item SunriseStart_Event received update ON
then
logInfo("time-of-day.rules", "Its Sunrise")
end
rule "Sunset started"
when
Item SunsetStart_Event received update ON
then
logInfo("time-of-day.rules", "Its Sunset")
end
Its still not working. Not sure why @mhilbush the log statement logInfo(“time-of-day.rules”, “Its Sunrise”) should go to my openhab.log in the userdata/logs folder right? If so its not going. I even did a final else saying “its neither sunrise or sunset” and it didn’t work. I get minute by minutes update of time like:
2016-09-25 05:43:07.973 [ItemStateChangedEvent ] - ntp_ntp_local_dateTime changed from 2016-09-25T05:42:07.954+0000 to 2016-09-25T05:43:07.954+0000
2016-09-25 05:43:07.977 [ItemStateChangedEvent ] - ntp_ntp_local_string changed from 2016-09-25 05:42:07 UTC to 2016-09-25 05:43:07 UTC
rule "Generate Time of Day Events"
when
Item ntp_ntp_local_dateTime received update
then
val now = new Date()
val sunset = new Date((SunsetStart_Time.state as DateTimeType).calendar.timeInMillis)
val sunrise = new Date((SunriseStart_Time.state as DateTimeType).calendar.timeInMillis)
if((now.getTime-(now.getTime%60000)) == (sunrise.getTime-(sunrise.getTime%60000))) {
logInfo("time-of-day.rules", "Start of Sunrise!")
SunriseStart_Event.postUpdate(ON)
}
else if((now.getTime-(now.getTime%60000)) == (sunset.getTime-(sunset.getTime%60000))) {
logInfo("time-of-day.rules", "Start of Sunset!")
SunsetStart_Event.postUpdate(ON)
}
end
ok I made a separate .rules and .items file for these things. But I am getting an error: Rule 'Generate Time of Day Events': An error occured during the script execution: null
Seems to be happening at: val now = new Date()
(I put a debug line in before this line and it fires, but after does not fire)
Any idea why?
Is there a language reference for whatever scripting language the .rules files are in?
Hey Kevin,
just wanted to remind you: Marks rule sure has more to offer but if you are looking for a simple “at sunset / at sunrise” solution, mine is way smaller and less resource hungry.
I didn’t take a look at the OH2 docs for weeks, even months … because the pages were mostly empty.
As I can see now this has changed significantly! Thanks for that
ok, still having problems. In my rule I am printing SunsetStart_Time and it doesn’t seem to be set. Like: logInfo("sunrisesunset.rules","SunsetStart_Time is " + SunsetStart_Time)
and it prints: (Type=DateTimeItem, State=NULL, Label=Sunset Start, Category=null)
so seems it is not being initialized.
i see events:
2016-09-26 05:40:02.835 [INFO ] [ding.astro.handler.AstroThingHandler] - Scheduled astro DailyJob at midnight for thing astro:sun:home
2016-09-26 05:40:02.885 [INFO ] [ding.astro.handler.AstroThingHandler] - Scheduled astro DailyJob at midnight for thing astro:moon:home
at startup so maybe eventually Astro will be initialized