From 990de7fbb929c4cc580522ff78056aac8930198e Mon Sep 17 00:00:00 2001 From: "Dustin C. Hatch" Date: Fri, 18 Jun 2021 18:31:34 -0500 Subject: [PATCH] sensor: Improve reported value precision Casting the measured values to `int` loses a lot of precision. Sending the raw values, however, results in uselessly over-precise values. Thus, we need to compromise by truncating the values at the tenths place. --- src/thermostat/sensor.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/thermostat/sensor.py b/src/thermostat/sensor.py index 96f6864..7ef1c0d 100644 --- a/src/thermostat/sensor.py +++ b/src/thermostat/sensor.py @@ -96,9 +96,9 @@ class Daemon: while 1: data = bme280.sample(bus, SENSOR_ADDR, params) values = { - "temperature": int(data.temperature), - "pressure": int(data.pressure), - "humidity": int(data.humidity), + "temperature": adj(data.temperature), + "pressure": adj(data.pressure), + "humidity": adj(data.humidity), } client.publish(TOPIC, json.dumps(values)) ready = select.select((self.quitpipe[0],), (), (), 10)[0] @@ -142,6 +142,10 @@ class Daemon: print("Message", client, userdata, msg) +def adj(value, p=10): + return int(value * p) / p + + def main(): logging.basicConfig(level=logging.DEBUG) daemon = Daemon()