Show openHAB uptime
This is an update to the example I posted earlier as a reply in someone else’s thread:
Purpose
The System Info binding provides the ‘CPU uptime’, which is the time in minutes since the last reboot of the machine running openHAB as a Number item.
I would also like to be able to see the time that openHAB is running. This is usually shorter, since I tend to restart the sudo systemctl restart openhab2.service
every now and then, because I experience instability due to some memory leaks.
Using similar values would make it possible to compare them, so the openHab uptime should also be stored as minutes in a Number item.
The system uptime and openhab uptime should both be shown as the number of elapsed days, hours en minutes in the compact format “5 day(s) 05:59”. Localization should be possible.
Prerequisites
- Exec binding is already installed
- System Info binding installed
- Javascript transformation installed
- openHAB is running on a Linux based host operating system (the example uses a bash script and the
bc
utility)
I have tested this setup on a Raspberry Pi 3 running openHabianPi on Debian Stretch.
Please note that I refer to $OPENHAB_CONF
, which is the root of my configuration folder in openHabian.
Configuration
Create a script
Create a bash script that gets the time that the openHAB process is running. I store the script in the scripts folder in my configuration.
$OPENHAB_CONF/scripts/uptime.sh
#!/usr/bin/env bash
PID=`ps aux --sort=start_time | grep openhab.*java | grep -v grep | awk '{print $2}' | tail -1`
UPTIME=`ps -o etimes= -p "${PID}"`
echo $UPTIME/60 | bc
Don’t forget to make the script executable
$ chmod a+x $OPENHAB_CONF/scripts/uptime.sh
Schedule the script
Schedule the script. I use the Exec binding, but you could also create a cron based rule. If you want get fancy you could also run a cron job on your machine that posts the value using the REST api.
$OPENHAB_CONF/things/uptime.things
Thing exec:command:uptime [command="/full/path/to/uptime.sh", interval=60, timeout=2]
The script will run every 1 minute and will timeout after 2 seconds if needed.
Unfortunately, the command does not accept the $OPENHAB_CONF variable. This raises an error. You need the absolute path (for openHABian this would be /etc/openhab2/scripts/uptime.sh
).
Convert exec output to Number
The new Exec binding does not output Number, but String only. A rule will take care of the conversion.
$OPENHAB_CONF/rules/exec.rules
val String _logger = "rules.exec"
// This rule is used to update the Number out of the returned string from the command line.
// Only Works since openhab 2.2.
rule "Convert String to Item Type"
when
Item System_openHAB_Uptime_output changed
then
// Get name of Number Item by removing "_output" from the name of the String Item
val toUpdate = triggeringItem.name.toString.split("_output").get(0)
// post the new value to the Number Item
postUpdate( toUpdate.toString , triggeringItem.state.toString )
logDebug(_logger, "Item '" + toUpdate + "' state: " + triggeringItem.state)
end
Credits for this rule:
Transform the output for the sitemap
Create a transformation
$OPENHAB_CONF/transform/duration.js
// computes nicely formatted duration from given minutes
(function(i){
var d = Math.floor(i / (24 * 60));
var h = Math.floor((i / 60) - (24 * d));
var m = Math.round(i - 60 * (24 * d + h));
var result = '';
// days
if (d > 0) {
result = result + d + ' day(s) ';
}
result = result + ("00" + h).substr(-2,2) + ":" + ("00" + m).substr(-2,2);
return result;
})(input)
Define items
$OPENHAB_CONF/items/uptime.items
Number System_CPU_Uptime "Uptime server [JS(duration.js):%s]" <clock> (gSystem) { channel="systeminfo:computer:local:cpu#uptime" }
String System_openHAB_Uptime_output "Uptime openHAB [%s]" <clock> (gSystem) { channel="exec:command:uptime:output" }
Number System_openHAB_Uptime "Uptime openHAB [JS(duration.js):%s]" <clock> (gSystem) // updated in exec.rules
Afterwards you can add the 2 Number items to your sitemap