From 73beb0231f3fc12f7f69c81f732494d268c68b2b Mon Sep 17 00:00:00 2001 From: Olex S Date: Tue, 20 Apr 2021 12:58:28 +0200 Subject: [PATCH] Correctly parse system load on locales with "," as decimal point On my RPi with German locale the output of `uptime` looks like this: ` 12:56:38 up 4 days, 15:35, 3 users, load average: 0,24, 0,24, 0,16` Default implementation cuts off the decimal point during parsing, resulting in very low CPU load accuracy being reported. Fix is a single character. Corrected version works both on German and English locales. --- src/rpi-cpu2mqtt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rpi-cpu2mqtt.py b/src/rpi-cpu2mqtt.py index fb5c8e6..cbab306 100644 --- a/src/rpi-cpu2mqtt.py +++ b/src/rpi-cpu2mqtt.py @@ -25,7 +25,7 @@ def check_cpu_load(): # bash command to get cpu load from uptime command p = subprocess.Popen("uptime", shell=True, stdout=subprocess.PIPE).communicate()[0] cores = subprocess.Popen("nproc", shell=True, stdout=subprocess.PIPE).communicate()[0] - cpu_load = str(p).split("average:")[1].split(",")[0].replace(' ', '').replace(',', '.') + cpu_load = str(p).split("average:")[1].split(", ")[0].replace(' ', '').replace(',', '.') cpu_load = float(cpu_load)/int(cores)*100 cpu_load = round(float(cpu_load), 1) return cpu_load