# HG changeset patch # User Daniel O'Connor # Date 1612868406 -37800 # Node ID 393a879db9ebffe8c00eaf08e061c32c25d3e424 # Parent 7edf54ec37f20d3a370810c12e36e12a5a00c3e2 Update to latest tesla_powerwall code changes. diff -r 7edf54ec37f2 -r 393a879db9eb pw2log.py --- a/pw2log.py Tue Feb 18 13:18:25 2020 +1030 +++ b/pw2log.py Tue Feb 09 21:30:06 2021 +1030 @@ -9,7 +9,8 @@ import psycopg2 import requests import sys -import tesla_powerwall # https://github.com/jrester/tesla_powerwall +# https://github.com/jrester/tesla_powerwall +from tesla_powerwall import APIError, Powerwall import time # Standard in 3.7.. @@ -45,9 +46,10 @@ if not cp.has_section('pw'): print('Config file missing pw section', file = sys.stderr) sys.exit(1) - if not cp.has_option('pw', 'ip'): - print('pw section missing ip parameter', file = sys.stderr) - sys.exit(1) + for opt in ('ip', 'username', 'password'): + if not cp.has_option('pw', opt): + print('pw section missing %s parameter' % (opt,), file = sys.stderr) + sys.exit(1) if cp.has_option('pw2log', 'logfile'): logfile = cp.get('pw2log', 'logfile') @@ -87,17 +89,16 @@ with ctx: logger.critical('Starting') try: - collectdata(cp.get('pw', 'ip'), cp.get('db', 'dsn'), cp.getint('db', 'logtime')) + collectdata(cp.get('pw', 'ip'), cp.get('pw', 'username'), cp.get('pw', 'password'), cp.get('db', 'dsn'), cp.getint('db', 'logtime')) except Exception as e: logger.critical('Unable to collect data: ' + str(e)) except Exception as e: logger.critical('Unable to enter daemon context: ' + str(e)) -def collectdata(pwip, dsn, logtime): +def collectdata(pwip, username, password, dsn, logtime): dbh = psycopg2.connect(dsn) cur = dbh.cursor() - - pw = tesla_powerwall.PowerWall(pwip) + pw = None while True: try: @@ -107,18 +108,27 @@ # | Positive | Supply house | Drawing from grid | Drawing from battery | Solar generation | # | Negative | n/a | Feeding grid | Charging battery | n/a | # - grid = pw.grid - load = pw.load - battery = pw.battery - solar = pw.solar - charge = pw.charge - except requests.ConnectionError as e: + if not pw: + first = True + pw = Powerwall(pwip) + pw.login(username, password) + pw.detect_and_pin_version() + meters = pw.get_meters() + grid_volts = meters.site.avarage_voltage + grid_freq = meters.site.avarage_voltage + grid_power = meters.site.instant_power + load_power = meters.load.instant_power + battery_power = meters.battery.instant_power + solar_power = meters.solar.instant_power + charge = pw.get_charge() + except APIError as e: + pw = None logger.error('Error communicating with Powerwall: ' + str(e)) time.sleep(300) continue try: cur.execute('INSERT INTO pw2 (date, grid_voltage, grid_freq, grid_power, load_power, battery_power, battery_charge, solar_power) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)', - (datetime.datetime.now(), grid.instant_average_voltage, grid.frequency, grid.instant_power, load.instant_power, battery.instant_power, charge, solar.instant_power)) + (datetime.datetime.now(), grid_volts, grid_freq, grid_power, load_power, battery_power, charge, solar_power)) dbh.commit() except psycopg2.OperationalError as e: logger.error('Reconnecting after database error:' + str(e)) @@ -126,6 +136,9 @@ dbh = psycopg2.connect(dsn) cur = dbh.cursor() continue + if first: + logger.error('Logged OK') + first = False time.sleep(logtime)