Show openHAB uptime

perfect! learnt another thing :slight_smile:
thanks a lot!

Alternative way without any external scripts System Uptime Info and an Automatic Reboot

Hi

I got this aswell:
Uptime openHAB aN:aN, whats wrong?

No one?

No one will answer a what’s wrong question like that.
Post your items, sitemap and rule file

My itemsfile:
/* Network information*/
String Network_AdapterName { channel=‚Äúsysteminfo:computer:openHABianPi:network#networkDisplayName‚ÄĚ }
String Network_Name { channel=‚Äúsysteminfo:computer:openHABianPi:network#networkName‚ÄĚ }
String Network_IP { channel=‚Äúsysteminfo:computer:openHABianPi:network#ip‚ÄĚ }
String Network_Mac { channel=‚Äúsysteminfo:computer:openHABianPi:network#mac‚ÄĚ }
Number Network_DataSent { channel=‚Äúsysteminfo:computer:openHABianPi:network#dataSent‚ÄĚ }
Number Network_DataRecevied { channel=‚Äúsysteminfo:computer:openHABianPi:network#dataReceived‚ÄĚ }
Number Network_PacketsSent { channel=‚Äúsysteminfo:computer:openHABianPi:network#packetsSent‚ÄĚ }
Number Network_PacketsRecevied { channel=‚Äúsysteminfo:computer:openHABianPi:network#packetsReceived‚ÄĚ }

/* CPU information*/
String CPU_Name { channel=‚Äúsysteminfo:computer:openHABianPi:cpu#name‚ÄĚ }
String CPU_Description { channel=‚Äúsysteminfo:computer:openHABianPi:cpu#description‚ÄĚ }
Number CPU_Load { channel=‚Äúsysteminfo:computer:openHABianPi:cpu#load‚ÄĚ}
Number CPU_Load1 { channel=‚Äúsysteminfo:computer:openHABianPi:cpu#load1‚ÄĚ }
Number CPU_Load5 { channel=‚Äúsysteminfo:computer:openHABianPi:cpu#load5‚ÄĚ }
Number CPU_Load15 { channel=‚Äúsysteminfo:computer:openHABianPi:cpu#load15‚ÄĚ }
Number CPU_Threads { channel=‚Äúsysteminfo:computer:openHABianPi:cpu#threads‚ÄĚ }
Number CPU_Uptime { channel=‚Äúsysteminfo:computer:openHABianPi:cpu#uptime‚ÄĚ }
Number System_CPU_Uptime ‚ÄúUptime server [JS(duration.js):%s]‚ÄĚ (gSystem) { channel=‚Äúsysteminfo:computer:openHABianPi:cpu#uptime‚ÄĚ }
String System_openHAB_Uptime_output ‚ÄúUptime openHAB [%s]‚ÄĚ (gSystem) { channel=‚Äúexec:command:uptime:output‚ÄĚ }
Number System_openHAB_Uptime ‚ÄúUptime openHAB [JS(duration.js):%s]‚ÄĚ (gSystem) // updated in exec.rules

/* Drive information*/
String Drive_Name { channel=‚Äúsysteminfo:computer:openHABianPi:drive#name‚ÄĚ }
String Drive_Model { channel=‚Äúsysteminfo:computer:openHABianPi:drive#model‚ÄĚ }
String Drive_Serial { channel=‚Äúsysteminfo:computer:openHABianPi:drive#serial‚ÄĚ }

/* Storage information*/
String Storage_Name { channel=‚Äúsysteminfo:computer:openHABianPi:storage#name‚ÄĚ }
String Storage_Type { channel=‚Äúsysteminfo:computer:openHABianPi:storage#type‚ÄĚ }
String Storage_Description { channel=‚Äúsysteminfo:computer:openHABianPi:storage#description‚ÄĚ }
Number Storage_Available { channel=‚Äúsysteminfo:computer:openHABianPi:storage#available‚ÄĚ }
Number Storage_Used { channel=‚Äúsysteminfo:computer:openHABianPi:storage#used‚ÄĚ }
Number Storage_Total { channel=‚Äúsysteminfo:computer:openHABianPi:storage#total‚ÄĚ }
Number Storage_Available_Percent { channel=‚Äúsysteminfo:computer:openHABianPi:storage#availablePercent‚ÄĚ }
Number Storage_Used_Percent { channel=‚Äúsysteminfo:computer:openHABianPi:storage#usedPercent‚ÄĚ }

/* Memory information*/
Number Memory_Available { channel=‚Äúsysteminfo:computer:openHABianPi:memory#available‚ÄĚ }
Number Memory_Used { channel=‚Äúsysteminfo:computer:openHABianPi:memory#used‚ÄĚ }
Number Memory_Total { channel=‚Äúsysteminfo:computer:openHABianPi:memory#total‚ÄĚ }
Number Memory_Available_Percent { channel=‚Äúsysteminfo:computer:openHABianPi:memory#availablePercent‚ÄĚ }
Number Memory_Used_Percent { channel=‚Äúsysteminfo:computer:openHABianPi:memory#usedPercent‚ÄĚ }

/* Swap memory information*/
Number Swap_Available { channel=‚Äúsysteminfo:computer:openHABianPi:swap#available‚ÄĚ }
Number Swap_Used { channel=‚Äúsysteminfo:computer:openHABianPi:swap#used‚ÄĚ }
Number Swap_Total { channel=‚Äúsysteminfo:computer:openHABianPi:swap#total‚ÄĚ }
Number Swap_Available_Percent { channel=‚Äúsysteminfo:computer:openHABianPi:swap#availablePercent‚ÄĚ }
Number Swap_Used_Percent { channel=‚Äúsysteminfo:computer:openHABianPi:swap#usedPercent‚ÄĚ }

/* Battery information*/
String Battery_Name { channel=‚Äúsysteminfo:computer:work:battery#name‚ÄĚ }
Number Battery_RemainingCapacity { channel=‚Äúsysteminfo:computer:work:battery#remainingCapacity‚ÄĚ }
Number Battery_RemainingTime { channel=‚Äúsysteminfo:computer:work:battery#remainingTime‚ÄĚ }

/* Display information*/
String Display_Description { channel=‚Äúsysteminfo:computer:work:display#information‚ÄĚ }

/* Sensors information*/
Number Sensor_CPUTemp { channel=‚Äúsysteminfo:computer:openHABianPi:sensors#cpuTemp‚ÄĚ }
Number Sensor_CPUVoltage { channel=‚Äúsysteminfo:computer:openHABianPi:sensors#cpuVoltage‚ÄĚ }
Number Sensor_FanSpeed { channel=‚Äúsysteminfo:computer:openHABianPi:sensors#fanSpeed‚ÄĚ }

/* Process information*/
Number Process_load { channel=‚Äúsysteminfo:computer:openHABianPi:process#load‚ÄĚ }
Number Process_used { channel=‚Äúsysteminfo:computer:openHABianPi:process#used‚ÄĚ }
String Process_name { channel=‚Äúsysteminfo:computer:openHABianPi:process#name‚ÄĚ }
Number Process_threads { channel=‚Äúsysteminfo:computer:openHABianPi:process#threads‚ÄĚ }
String Process_path { channel=‚Äúsysteminfo:computer:openHABianPi:process#path‚ÄĚ }

My .sitemap:
sitemap our_home label=‚ÄúV√•rt hem‚ÄĚ {

Frame label="Datum" {
    Text item=CurrentDate
}
Frame {  

    Group item=Datarum icon="office"
    Group item=Sovrum icon="bedroom"
}

Frame {
    
    Group item=Vardagsrum
    Group item=Kok label="Kök" icon="kitchen"

}

Frame {
    
    Group item=Hall icon="corridor"
    Group item=Badrum icon="bath"
}

Frame {
    
    Group item=Kladkammare label="Klädkammare" icon="wardrobe"
    Group item=Balkong
}

   
Frame label="Vädret" {

        Text label="Solen" icon="sun"{
        Text item=Sunrise_Time icon="sunrise"
	    Text item=Sunset_Time icon="sunset"
        
}

    Text label="Vädret" icon="sun"{

        Text item=Temperature icon="temperature"
        Text item=Temp_Min icon="temperature"
        Text item=Temp_Max icon="temperature"
        Text item=Temp_Feel icon="temperature"
        Text item=Temp_Dewpoint icon="temperature"
        Text item=Humidity icon="humidity"
        Text item=Pressure icon="pressure"
        Text item=Clouds icon="sun_clouds"
        Text item=Condition
        Text item=ObservationTime
        Text item=LastUpdate
        Text item=CommonId
        Text item=Rain icon="rain"
        Text item=Snow icon="snow"
        Text item=Precip_Probability
        Text item=Wind_Speed icon="wind"
        Text item=Wind_Speed_Mps icon="wind"
        Text item=Temp_MinF icon="temperature"
        Text item=Temp_MaxF icon="temperature"
        
    }
}
Frame label="System information" icon="pie" {

    Group item=RaspberryPI {
        
        Text item=Network_IP label="IP" icon="network"
        Text item=Network_DataSent label="Data sänt" icon="network"
        Text item=Network_DataRecevied label="Data mottaget" icon="network"
                  
        Text item=CPU_Load1 icon="processor"
        Text item=CPU_Load5 icon="processor"
        Text item=System_CPU_Uptime icon="time"
        Text item=System_openHAB_Uptime_output
        Text item=System_openHAB_Uptime icon="time"

        Text item=Storage_Total icon="hard_drive"
        Text item=Storage_Available_Percent icon="hard_drive"

        Text item=Memory_Total label="Minne Totalt" icon="ram_memory"
        Text item=Memory_Available label="Minne tillgängligt" icon="ram_memory"
        Text item=Memory_Used label="Minne använt" icon="ram_memory"
        Text item=Memory_Used_Percent label="Minne använt %" icon="ram_memory"        
        
        }
        
    
    Group item=Energi label="Energiförbrukning" icon="solarplant"

    }
}

My .rulefile:
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

You should mention that one also needs the regex binding installed :wink:

I just implemented @johfeu’s solution and noticed that it triggers when I add an item using Paper UI. (OH 2.4)
Have not tested other use cases yet, but my system might behave the same as yours. So I might switch to your solution.

Edit: Just tried to reproduce. Still working fine for me. No change when manually adding items via paper UI. took some time but now it changed also for me…
Does it mean, the system fires ‚ÄúSystem started‚ÄĚ rule trigger on item changes via paper UI? sounds strange to me

Looks like it.

As I migrate all my rules to JavaScript, I might just put the code directly in my rule file without even using the system start trigger.
Because with JS rules, this code is executed when the script is loaded and this I saw only at system start or when I change the script itself.

This probably works for all jsr223 scripting languages. Maybe it works for the classic rules as well.

Just gave it a quick test: Does not work for classic rules. postUpdate does not work outside the rule block.

Maybe it can be done with different commands. Otherwise we could check if the value is still null/undefined and only then update it, as it means the values have never been set and therefore it has to be the actual first call after the system start. Should be quite easy.

As I persist all item values, so I do not have to bother with null/undefined, this is a little bit more effort in my setup. So I guess I will not bother and just move it to the next-gen rules.

Me once more: I now built the JS ‚Äúrule‚ÄĚ and it works quite well.
I make use of the openHAB Helper Libraries. They also have instruction on how to enable the next-gen rule engine on the website.

JS-Code:

'use strict';

var OPENHAB_CONF = Java.type("java.lang.System").getenv("OPENHAB_CONF");
load(OPENHAB_CONF+'/automation/lib/javascript/core/utils.js');

try {
    postUpdate("OH_StartTime",now());
} catch (err) {
    console.error(err);
}

Note: This is not the actual code I use. I simplified it a bit, as I also send a notification at start to me and use a logger library I wrote, which is not mandatory to use.

Items file:

DateTime OH_StartTime "Openhab start time" <time>

I am not interested in the uptime, so I do not calculate it. But it would be just a matter of making a time triggered rule which is called every minute or so and calculates the duration back to OH_StartTime.

And for completion, the sitemap entry:

Text item=OH_StartTime  label="openHAB start time [%1$tA %1$tR]"

I currently restart openHAB daily, so I just display the weekday and time, but you can also show the full date, if preferred.

Hi,

I was wondering why it doesn’t work, but it has something do with the exec.whitelist:

/etc/openhab2/things/uptime.things

Thing exec:command:uptime [command="/etc/openhab2/scripts/uptime.sh", interval=60, timeout=2]
Thing systeminfo:computer:local [interval_high=2, interval_medium=60]

/etc/openhab2/misc/exec.whitelist

# For security reasons all commands that are used by the exec binding or transformation need to be whitelisted.
# Every command needs to be listed on a separate line below.
/etc/openhab2/scripts/uptime.sh

@rtvb I hope you can update your tutorial.
@Gargam3L0 this should solve your problem

Is the $OPENHAB_CONF/scripts/uptime.sh path really a good solution? Because openHAB tries to convert each file within the scripts folder. It works but maybe it was smarter to use another location. If you look inside the karaf console you get something similar like this:

09:14:37.388 [DEBUG] [pse.smarthome.model.script.rules.exec] - Item 'System_openHAB_Uptime' state: 1442
09:15:05.732 [WARN ] [arthome.model.script.actions.BusEvent] - Cannot convert 'sh: 1: /etc/openhab2/scripts/uptime.sh: Permission denied
sh: 1: /etc/openhab2/scripts/uptime.sh: Permission denied' to a state type which item 'System_openHAB_Uptime' accepts: [DecimalType, QuantityType, UnDefType].
09:15:05.742 [DEBUG] [pse.smarthome.model.script.rules.exec] - Item 'System_openHAB_Uptime' state: sh: 1: /etc/openhab2/scripts/uptime.sh: Permission denied
sh: 1: /etc/openhab2/scripts/uptime.sh: Permission denied

The permission denied error occurs because an .rules file was expected in this folder and he tries to convert or read it. Well the exec binding can read it but not the ‚Äúsystem reader‚ÄĚ from openHAB. (Sorry for my bad english. I hope you can understand what I mean. I have no idea what it‚Äôs called, which ultimately reads the files.)

I hope I was able to impart a little more knowledge to the whole thing here.