From 09a116984c72ba4ff116b0b89006c33165cb1b0e Mon Sep 17 00:00:00 2001 From: Hauke Petersen Date: Fri, 2 Sep 2022 10:19:11 +0200 Subject: [PATCH] verbump to 0.0.3 --- setup.py | 2 +- steckie/steckie.py | 78 +++++++++++++++++++++++++++++++++++++++------- 2 files changed, 67 insertions(+), 13 deletions(-) diff --git a/setup.py b/setup.py index f0fb812..1455894 100644 --- a/setup.py +++ b/setup.py @@ -21,7 +21,7 @@ import setuptools name = "steckie" -ver = "0.0.2" +ver = "0.0.3" desc = 'Read state from Tasmota smart plugs' setuptools.setup( diff --git a/steckie/steckie.py b/steckie/steckie.py index f08eb87..ae928fb 100755 --- a/steckie/steckie.py +++ b/steckie/steckie.py @@ -26,6 +26,7 @@ from deichapp import Deichapp from deichflux import Deichflux from apscheduler.schedulers.blocking import BlockingScheduler +import dateutil CONFIG_DEFAULT = { "interval": 5.0, @@ -39,6 +40,32 @@ class Steckie(Deichapp): self.scheduler = BlockingScheduler() self.db = Deichflux(self.cfg["db"]) + logging.warning("Steckie V0.72 - loaded...") + + self.counters = {} + for name in self.cfg["devs"]: + self.counters[name] = { + "last": None, + "year": 0.0, + "month": 0.0, + "day": 0.0, + } + + # read latest known values from database + query = f'SELECT e_total_kwh, e_year_kwh, e_month_kwh, e_day_kwh ' \ + f'FROM "{self.cfg["measurement"]}" WHERE "name"=\'{name}\' ' \ + f'ORDER BY DESC LIMIT 1' + res = self.db.query(query, None) + for p in res.get_points(): + print(p) + last = pytz.UTC.localize(datetime.utcfromtimestamp(p["time"] / 1e9)) + self.counters[name]["last"] = last + total = p["e_total_kwh"] + self.counters[name]["year"] = total - p["e_year_kwh"] + self.counters[name]["month"] = total - p["e_month_kwh"] + self.counters[name]["day"] = total - p["e_day_kwh"] + logging.warning(f'Found {name}: current total is {total}kWh') + def run(self): for name, url in self.cfg["devs"].items(): self.scheduler.add_job(self.query, @@ -48,30 +75,57 @@ class Steckie(Deichapp): self.scheduler.start() def query(self, name, url): - logging.warning(f'query {name} -> {url}') + cnt = self.counters[name] + now = pytz.UTC.localize(datetime.utcnow()) try: with urllib.request.urlopen(f'{url}/cm?cmnd=Status%208') as raw: resp = json.loads(raw.read().decode("utf-8")) - logging.warning(resp) except Exception as e: - logging.warning(f'no response from {name} -> {e}') + logging.warning(f'{name} - no response: {e}') return - point = [{ + total = float(resp["StatusSNS"]["ENERGY"]["Total"]) + + # initialize values if this is the first time we read them + if cnt["last"] is None: + cnt["last"] = now + for f in ("year", "month", "day"): + cnt[f] = total + + # reset values if a when new time frame starts + now_local = now.astimezone(pytz.timezone("Europe/Berlin")) + last_local = cnt["last"].astimezone(pytz.timezone("Europe/Berlin")) + if now_local.date().year != last_local.date().year: + self.cnt["year"] = total + self.cnt["month"] = total + self.cnt["day"] = total + elif now_local.date().month != last_local.date().month: + self.cnt["month"] = total + self.cnt["day"] = total + elif now_local.date().day != last_local.date().day: + self.cnt["day"] = total + cnt["last"] = now + + # write data to DB + point = { "measurement": self.cfg["measurement"], "tags": { "name": name, }, - "time": pytz.UTC.localize(datetime.utcnow()).isoformat(), + "time": now.isoformat(), "fields": { - "voltage": float(resp['StatusSNS']['ENERGY']['Voltage']), - "current": float(resp['StatusSNS']['ENERGY']['Current']), - "pwr": float(resp['StatusSNS']['ENERGY']['ApparentPower']), - "pwr_r": float(resp['StatusSNS']['ENERGY']['ReactivePower']), - "factor": float(resp['StatusSNS']['ENERGY']['Factor']), - "e_daily": float(resp['StatusSNS']['ENERGY']['Today']), + "power_w": float(resp["StatusSNS"]["ENERGY"]["Power"]), + "voltage_v": float(resp["StatusSNS"]["ENERGY"]["Voltage"]), + "current_a": float(resp["StatusSNS"]["ENERGY"]["Current"]), + "factor": float(resp["StatusSNS"]["ENERGY"]["Factor"]), + "e_total_kwh": total, + "e_year_kwh": total - cnt["year"], + "e_month_kwh": total - cnt["month"], + "e_day_kwh": total - cnt["day"], } - }] + } + logging.warning(f'{name} - power:{point["fields"]["power_w"]:.1f}W ' + f'total:{point["fields"]["e_total_kwh"]}kWh') self.db.write(point)