So I got your code partially working. Didn’t realize the import has to go at the beginning based on your code fragment although being a VB.Net programmer I should have known so that was 30 minutes of fighting. Now the issue is I can get my lastUpdatedDate attribute to hold the date but it keeps saying it’s null and I don’t know why and maybe someone else can figure it out. Here is a snippet of the code so far:
def currentDate = new Date()
if (lastUpdatedDate == null) {sendEvent(name: 'lastUpdatedDate', value: currentDate, isStateChange: true)} // THIS IS EXECUTING EVERY TIME SINCE IT THINKS ITS NULL
// Update runtime
if (device.latestValue("contact") == "open") {
// If last value was open then we add to the runtime. Doesn't matter if its still
// open or closed as if our latestValue was closed we wouldn't do anything.
use (groovy.time.TimeCategory) {
// Get the number of seconds difference
log.debug("currentDate is: " + currentDate)
log.debug("lastUpdatedDate is currently: " + lastUpdatedDate) // THIS SAYS ITS NULL
def myDuration = currentDate - lastUpdatedDate
def mySeconds = myDuration.seconds
// Then update the runtime
log.debug("Current totalRuntime is " + totalRuntime + " and we are adding in " + mySeconds + " seconds")
def myTotal = Integer.parseInt(totalRuntime) + mySeconds
sendEvent(name: "totalRuntimeInSeconds",value: myTotal, displayed: false)
def myTotalInMinutes = myTotal / 60
sendEvent(name: "totalRuntimeInMinutes", value: String.valueOf(myTotalInMinutes), displayed: false)
}
}
and here is the logging:
9:11:05 AM: error groovy.lang.GroovyRuntimeException: Ambiguous method overloading for method java.util.Date#minus.
Cannot resolve which method to invoke for [null] due to overlapping prototypes between:
[class java.util.Date]
[class groovy.time.BaseDuration] @line 139 (doCall)
9:11:05 AM: debug lastUpdatedDate is currently: null
9:11:05 AM: debug currentDate is: Fri Mar 16 13:11:04 UTC 2018
So it’s failing because lastUpdatedDate is null which makes sense why its failing but I don’t understand why its null. The “if null then sendevent” part is executing every run. So I thought maybe it’s because I didn’t call the full “device.currentValue(‘lastUpdatedDate’)” so I tried that and it’s still null. In the device properties however it is updating:
contact: open
lastUpdated: Mar 15 at 8:11 AM
lastUpdatedDate: Fri Mar 16 13:21:04 UTC 2018
Why can’t I pull the value in?
EDIT: I started to think maybe I need to convert it to a date first so I tried:
def lastDate = new Date().parse("E MMM dd H:m:s z yyyy", device.currentValue("lastUpdatedDate"))
but the lastUpdatedDate still comes back null even though the value is properly stored in the device.
SECOND EDIT:
Now I’m super confused. I put this in for troubleshooting:
log.debug("device.currentvalue(lastUpdatedDate) using single quotes: " + device.currentValue('lastUpdatedDate'))
log.debug("device.currentvalue(lastUpdatedDate) using double quotes: " + device.currentValue("lastUpdatedDate"))
log.debug("device.latestValue(lastUpdatedDate) using single quotes: " + device.latestValue('lastUpdatedDate'))
log.debug("device.latestValue(lastUpdatedDate) using double quotes: " + device.latestValue("lastUpdatedDate"))
log.debug("device.lastUpdatedDate: " + device.lastUpdatedDate)
log.debug("lastUpdatedDate: " + lastUpdatedDate)
Every one comes back Null but device properties shows a valid date.