I’m struggling with a Groovy script to run in a system startup rule.
I want to set DAY and NIGHT flags dependent on where the current time is with respect to sunrise (using Astro binding). I started putting this together for when the current time is after sunrise:
I’m using the OH3 M2 UI for setting this up and scripting the rule action.
var DateTime sunRise = new DateTime(ir.getItem("LocalSunrise_StartTime").state.toString)
if (now.isafter(sunRise)) {
events.sendCommand("openHAB2Server_ItemFlagDay","ON")
events.sendCommand("openHAB2Server_ItemFlagNight","OFF")
}
I get the following error:
Script44.groovy: 1: unable to resolve class DateTime
@ line 1, column 24.
var DateTime sunRise = new DateTime(ir.getItem("LocalSunrise_StartTime").state.toString)
Since Joda Time is removed in OH3, you should use ZonedDateTime…
var sunRise = items["LocalSunrise_StartTime"].zonedDateTime
if (new DateTimeType().zonedDateTime.isAfter(sunRise)) {
// import java.time.ZonedDateTime
// if (ZonedDateTime.now().isAfter(sunRise)) {
events.sendCommand("openHAB2Server_ItemFlagDay","ON")
events.sendCommand("openHAB2Server_ItemFlagNight","OFF")
}
If you get an error, comment out the first line of the if statement and uncomment the two under it. I’m curious of the result.
Cool… so you got an error due to DateTimeType missing? If so, this means that the GroovyScriptEngineFactory add-on in OH3 is not properly including DateTimeType and the other classes from the DefaultScriptScopeProvider. Hmmm…