This was pulled together from many sources on the forum.
Items
// OH Up Time
//
DateTime OH_Uptime "Up Time [%1$tm.%1$td.%1$tY %1$tH:%1$tM]" <time> (HomeState)
String OH_Uptime_HumanReadable "Readable Up Time [%s]" (HomeState, Group_HabPanel_Dashboard)
Rules
Startup Area
OH_Uptime.postUpdate(new DateTimeType()) // Set Start Up Time
rule "OH Readable Up Time "
when
Item OH_Uptime changed or
Time cron "1 1 * * * ?"
then
if (systemStarted.state != ON && OH_Uptime.state != NULL) {
var DateTime dateTime_OH_Uptime = new DateTime((OH_Uptime.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli)
var diff = now.millis - dateTime_OH_Uptime.millis
var String tmp = null
// http://stackoverflow.com/questions/13018550/time-since-ago-library-for-android-java
val Number SECOND_MILLIS = 1000;
val Number MINUTE_MILLIS = 60 * SECOND_MILLIS;
val Number HOUR_MILLIS = 60 * MINUTE_MILLIS;
val Number DAY_MILLIS = 24 * HOUR_MILLIS;
if (diff < MINUTE_MILLIS) {
tmp = "just now";
} else if (diff < 2 * MINUTE_MILLIS) {
tmp = "a minute";
} else if (diff < 50 * MINUTE_MILLIS) {
tmp = String::format("%.2f", diff / MINUTE_MILLIS) + " Minutes";
} else if (diff < 90 * MINUTE_MILLIS) {
tmp = "an hour ago";
} else if (diff < 24 * HOUR_MILLIS) {
tmp = String::format("%.2f", diff / HOUR_MILLIS) + " Hours";
} else if (diff < 48 * HOUR_MILLIS) {
tmp = "since yesterday";
} else {
tmp = String::format("%.1f", diff / DAY_MILLIS) + " Days";
}
OH_Uptime_HumanReadable.postUpdate(tmp)
tmp = NULL
logInfo("OHUpTime", "-----------------------------------------------------------------------------")
logInfo("OHUpTime", "Human Readable Time is " + OH_Uptime_HumanReadable.state)
logInfo("OHUpTime", "-----------------------------------------------------------------------------")
}
end
Best, Jay