Passing state timestamp as an argument

I’m trying to do something really simple but am having some issues. Maybe I’m going about this the wrong way, not sure.

In my smart app, my app is storing when it last performed various actions. I then want to show that date/time on the settings page. Here is the basic function that is designed to take the timestamp and convert it into a printable date:

def showLastDate(Long timestamp) {
log.debug “Date: ‘${timestamp}’”
def lastDate = new Date(timestamp).format(“yyyy-MM-dd HH:mm”)
return lastDate
}

This function is then used in my preferences section:

preferences {

section(“Last Alert: ${showLastDate(atomicState.lastActionTimeStamp)}”)

}

With this I always get a compile error java.lang.IllegalArgumentException on the “def lastDate” line. That seemed odd so I verified that the timestamp (i.e. 1521573853473) is pulled from the state value and passed into showLastDate() and is logged as expected.

If I put a static numeric value in the code, it works. If I try to do a conversion on the passed value to a Long, it also gets the same error so it has to be related to what is being passed. I’m guessing there is some typecasting issue here but for the life of me I just don’t see it.

Actually, I am not sure if the value is passed in. Something odd here.

Basically, as noted previously the value is logged per the call to the logger but that executes at runtime. However, at compile time, it seems that the argument passed in is “null”. So, I guess it is acceptable to log null at compile but not to the Date or other functions? I can’t explain that.

So this works:

def showLastDate(timestamp) {
if (timestamp == null) {
timestamp = 0
}
def lastDate = new Date(timestamp).format(“EEE, MMMMM d yyyy HH:mm:ss z”)
return lastDate
}

So weird. Unless anyone else has some ideas, I guess you just have to fake out the compiler. Nuts.