openHAB uptime in an item

This is how I grep the time since start of openHAB service.

Based on a bash file, @Udo_Hartmann created in a discussion in the german community.

By the way: sorry for perfect “denglish” in naming the varibles :wink:

.things: (don’t forget to whitelist!)

Thing exec:command:OH_uptime    "opebHAB uptime"        [command="systemctl status openhab.service | grep since", interval=30] 

. items:

String      OH_UptimeText     "Uptime"                              
String      UptimeRaw            {channel="exec:command:OH_uptime:output" [profile="transform:REGEX",function=".*([0-9]{4}-[0-9]{2}-[0-9]{2} ([0-9]+(:[0-9]+)+) [A-Z]+).*"]}

.rules:

rule "openHAB uptime formatieren"
when
    Item UptimeRaw received update
then
    val input    = newState.toString.split(" ")
    val datum    = input.get(0)
    val uhrzeit  = input.get(1)
    val tz       = input.get(2)
    var st       = "+0100"
    if (tz == "CEST") st = "+0200"
    val dtOnline = DateTimeType.valueOf(datum + "T" + uhrzeit + st)
    val uptime   = Duration.between(dtOnline.getZonedDateTime(ZoneId.systemDefault), now).toSeconds
    val Minutes  = (uptime / 60) % 60
    val Hours    = (uptime / 60 / 60) % 24
    val Days     = (uptime / 60 / 60 / 24).intValue
    var zeit     = Days.toString + " Tage "
    switch Days.intValue {
      case 0 : zeit = ""
      case 1 : zeit = "1 Tag " }
    zeit += Hours.toString + " Std. " + Minutes.toString + " Min. "
    OH_UptimeText.postUpdate(zeit) 
end

Just for your information - there is also a rest api endpoint that returns the uptime in seconds.

1 Like

I use the “Systeminfo” Binding for this. There is a channel that is called “uptime” (of the system of course).

1 Like

But wouldn’t that be the system uptime?
OPs script and the rest api return the openHAB uptime.

Yes, the ideas was not to get system uptime but OH’s uptime.

Oh, I didn’t kown this yet.

Found it:

curl -X 'GET'   'http://192.168.178.60:8080/rest/systeminfo'   -H 'accept: application/json'   -H 'Authorization: Bearer .........

Thanks for the hint!

Java provides this information. See java - time since JVM started - Stack Overflow

In JRuby:

logger.info java.lang.management.ManagementFactory.runtime_mx_bean.uptime # in milliseconds

# Convert it to a Duration object:
uptime_duration = java.lang.management.ManagementFactory.runtime_mx_bean.uptime.milliseconds
logger.info uptime_duration

logger.info "openHAB was started less than an hour ago" if uptime_duration < 1.hour
1 Like

That’s definitively the leanest approach!

Modified it for DSL rules:

val uptime = (java.lang.management.ManagementFactory.getRuntimeMXBean.getUptime / 1000).intValue

I think DSL lets you write it as

java.lang.management.ManagementFactory.runtimeMXBean.uptime

Yes, but for me it is the same :slight_smile:
There is no reason for me to only restart OpenHab.
When I do a system update I also restart the whole system to finalize the procedure and see that everything comes up properly.

both do it.

A couple other ways to get the openHAB uptime:

  • MainUI → Help & About → View Details → Uptime

  • Create a simple rule that triggers at a system runtime level and post an update to a DateTime Item. You can use and expression like =dayjs(items.SystemStart.state).fromNow() in a MainUI widget and you’ll get a text like “3 days ago”. This will give you how long OH has been operational, not just when it was started.

1 Like

Yes, obviously there are many roads that lead to Rome.

The approach “systemstart” have I tried before but rejected it, because it triggers again when editing rules.

I’m now fine with java.lang.management… The only thing is where to get this knowlegde from.
I think, this is not run-of-the-mill.

Is there also a way to use that with a js rule? :upside_down_face:

This gives you the uptime in seconds:

var uptime = java.lang.management.ManagementFactory.getRuntimeMXBean().getUptime() / 1000;

In JS:

Java.type("java.lang.management.ManagementFactory").getRuntimeMXBean().getUptime()