Getting and Displaying Time

Have been running openhab 1.8 for months now and have been able to resolve all issues, but today I’m stumped on a seemingly simple issue…

In my UI I display the last time my zwave front door sensor experienced a change in state. Based on a review of the sample rules, it seems the way to do this is as I show below - i.e. to get each component of the time and then concatenate them together. This works, but I have two issues:

  1. This seems rather convoluted - is there not a way to simply bring back the full time stamp?
  2. If the number of minutes or seconds is less than 10, the display is awkward, for example 16:3:21 instead of 16:03:21.

I’ve tried to enhance the code by using the length method to calculate the length of the string, such as second.length, but that syntax is not accepted.

Appreciate any help with finding a more elegant way to capture and display the time of an event and or to calculate the length of a string.

    rule "Front Door Check"
        when
	       Item ZWave_FrontDoor changed
       then
   
    var month	= now.getMonthOfYear
    var day		= now.getDayOfMonth
    var hour	        = now.getHourOfDay
    var minute	= now.getMinuteOfHour
    var second	= now.getSecondOfMinute
    
   var CurrentTime = month+"/"+day+" @ "+hour+":"+minute+":"+second

	postUpdate(strLastFrontDoorTime, CurrentTime)
   end

I was able to answer my first question after some research. Posting back here for reference:

  1. Declare the item against which you want to post your time as an item of type DateTime:

    DateTime strLastFrontDoorTime “Last Door [%1$tm/%1$td %1$tH:%1$tM:%1$tS]”

  2. Identify the current time as follows:

    val DateTime MoveMoment = now

  3. Post the time as follows:

    strLastMotionTime.postUpdate(new DateTimeType(MoveMoment.toString))

Still haven’t figured out how to calculate the length of a string, but now I don’t have to. But if anyone reading this can clarify, much appreciated.

strLastMotionTime.postUpdate(new DateTimeType())

should suffice, as long as it’s the current time.