Add uptime information (on linux)
I want to display the uptime, which is quite a common information to display on such devices.
I tried by myself, adding stuff in library/sensors/sensors.py and library/stats.py, but I failed miserably. That sounds quite easy to do if you know the code better than I do.
I couldn't find any pythonish way of getting the current uptime, but this code does the trick on linux:
from datetime import timedelta
with open('/proc/uptime', 'r') as f: uptime_s = float(f.readline().split()[0])
uptime_string = str(timedelta(seconds=uptime_s))
I found an other solution.
You need the
pip3 install uptime
dependency.
Then u just can cast:
from uptime import uptime
import time
uptime()
It will give u the uptime in seconds. Then u can change the code to
from uptime import uptime
import datetime
sec = uptime()
convert = str(datetime.timedelta(seconds = sec))
convert = convert.split(".")[0] # entferne Millisekunden
print(convert)
and it will give you a output like "49 days, 16:47:16".
Would be happy if someone could build this in. I've testet it on Windows and Debian with Python 3.11.