]> git.sven.stormbind.net Git - sven/scripts.git/commitdiff
Add a state tracking to envertech portalmonitor
authorSven Hoexter <sven@stormbind.net>
Mon, 31 Jan 2022 10:38:24 +0000 (11:38 +0100)
committerSven Höxter <sven.hoexter@paymenttools.com>
Mon, 31 Jan 2022 10:43:20 +0000 (11:43 +0100)
Instead of sending a mail by generating output on every execution
we track the state of setup we receive from the portal. Output
is only created whenever we have a state change. That also enables
us to create "resolve" messages when the state changes back to be
ok.

Introduces a new configuration option `stateFile`.

home/portalmonitor.ini
home/portalmonitor.py

index 93b78ffb472e098f149920b48f1c35f33745df43..984460b0e06414abedb9c34dfc8bf4b65a9b6fae 100644 (file)
@@ -3,5 +3,6 @@ lat = 51.1234
 lon = 3.1234
 toleranceSeconds = 3600
 stationId = C0FFE123456789
+stateFile = /tmp/portalmonitorstate
 userName = yourname
 password = yourpassword
\ No newline at end of file
index 1316b2f6807d50233c13f511c81767d56e040091..3acd3f1766e677469fff2021e60f853af0b1253a 100755 (executable)
@@ -63,6 +63,25 @@ def getCurrentPower(userName, password, stationId):
     return float(power)
 
 
+# use our stateFile to determine if we have a state change
+# used to decide if we print something - thus generate a mail - later on
+def stateCheck(newState, stateFile):
+    try:
+        with open(stateFile, "r") as f:
+            oldState = f.read()
+    except FileNotFoundError:
+        oldState = "NULL"
+
+    if newState == oldState:
+        change = False
+    else:
+        change = True
+        with open(stateFile, "w") as f:
+            f.write(newState)
+
+    return change
+
+
 # read configuration file
 conf = configparser.ConfigParser()
 conf.read('portalmonitor.ini')
@@ -78,5 +97,9 @@ if isDaylight(conf['config'].getfloat('lat'), conf['config'].getfloat('lon'),
         print(f"Current Power: {currentPower}")
 
     if currentPower == 0:
-        print('Error: Power dropped to 0 but we should have daylight!')
+        if stateCheck('FAILED', conf['config']['stateFile']):
+            print('Error: Power dropped to 0 but we should have daylight!')
         sys.exit(1)
+    else:
+        if stateCheck('OK', conf['config']['stateFile']):
+            print(f"Resolved: Inverter reports {currentPower}W")