[SOLVED] Need help with Groovy script to compare current time with sunrise time

Hi All,

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.

Item:

DateTime LocalSunrise_StartTime {channel="astro:sun:local:rise#start"}

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)

Thanks for your help.

Cheers,
Randy

  1. You will need to import org.joda.time.DateTime, since it is not included in the default script scope
  2. Methods and functions require parenthesis, even if they do not have any parameters
  3. There is a typo… not isafter, but isAfter
  4. Rather than getting the Item and then getting its state, you can just use items (included in the default script scope) to get the state of an Item
import org.slf4j.LoggerFactory
import org.joda.time.DateTime
def LOG = LoggerFactory.getLogger('jsr223.groovy.test_script_1')
// LOG.warn("Test slf4j")

// import org.eclipse.smarthome.model.script.actions.LogAction
// LogAction.logWarn("Rules", "Test LogAction")

LOG.warn("LocalSunrise_StartTime: {}", items["LocalSunrise_StartTime"])

var sunRise = new DateTime(items["LocalSunrise_StartTime"].toString())

if (DateTime.now().isAfter(sunRise)) {
    events.sendCommand("openHAB2Server_ItemFlagDay","ON")
    events.sendCommand("openHAB2Server_ItemFlagNight","OFF")
}
  1. 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.

Thanks a million @5iver!

Here is the Groovy code that worked for me:

import java.time.ZonedDateTime

var sunRise = items["LocalSunrise_StartTime"].zonedDateTime
var sunSet = items["LocalSunset_StartTime"].zonedDateTime

if (ZonedDateTime.now().isAfter(sunRise) && ZonedDateTime.now().isBefore(sunSet)) {
    events.sendCommand("openHAB2Server_ItemFlagDay","ON")
    events.sendCommand("openHAB2Server_ItemFlagNight","OFF")
} else {
    events.sendCommand("openHAB2Server_ItemFlagDay","OFF")
    events.sendCommand("openHAB2Server_ItemFlagNight","ON")
}

Cheers,
Randy

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…

That is correct.

1 Like