2024-10-16 16:50:16 +11:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import statistics
|
|
|
|
|
|
|
|
from pyfroniusreg import gen24_registers
|
|
|
|
from pyfroniusreg.froniusreg import RegisterReadError
|
|
|
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
from pymodbus.client.tcp import ModbusTcpClient
|
|
|
|
|
2024-10-25 11:13:33 +11:00
|
|
|
# ModBUS connection to the inverter
|
2024-10-16 16:50:16 +11:00
|
|
|
fronius1 = ModbusTcpClient("172.19.107.211", port=502, timeout=10)
|
|
|
|
fronius1.connect()
|
|
|
|
|
2024-10-25 11:13:33 +11:00
|
|
|
# Info Registers
|
2024-10-17 20:52:47 +11:00
|
|
|
soc_percent = gen24_registers.scaledChaState.get(fronius1)
|
|
|
|
total_wh = gen24_registers.scaledWHRtg.get(fronius1)
|
2024-10-16 16:50:16 +11:00
|
|
|
|
2024-10-25 11:13:33 +11:00
|
|
|
# Charge Registers
|
|
|
|
discharge = gen24_registers.scaledOutWRte.get(fronius1)
|
|
|
|
charge = gen24_registers.scaledInWRte.get(fronius1)
|
|
|
|
mode = gen24_registers.StorCtl_Mod.get(fronius1)
|
|
|
|
timer = gen24_registers.InOutWRte_RvrtTms.get(fronius1)
|
|
|
|
|
|
|
|
current_time = datetime.now()
|
|
|
|
|
2024-10-16 16:50:16 +11:00
|
|
|
charge_data = []
|
|
|
|
discharge_data = []
|
|
|
|
|
2024-10-18 17:13:09 +11:00
|
|
|
print("Gathering 5 second average of battery power")
|
2024-10-17 20:52:47 +11:00
|
|
|
while( (datetime.now() - current_time).seconds < 5):
|
2024-10-16 21:33:28 +11:00
|
|
|
charge_data.append(gen24_registers.scaledmodule_3_DCW.get(fronius1))
|
|
|
|
discharge_data.append(gen24_registers.scaledmodule_4_DCW.get(fronius1))
|
2024-10-16 16:50:16 +11:00
|
|
|
|
|
|
|
avg_charge = statistics.mean(charge_data)
|
|
|
|
avg_discharge = statistics.mean(discharge_data)
|
|
|
|
print(" Charge: %.2f" % avg_charge)
|
|
|
|
print("DisCharge: %.2f" % avg_discharge)
|
2024-10-17 20:52:47 +11:00
|
|
|
print(" SoC: %.2f" % soc_percent)
|
|
|
|
print("Total WH: %.2f" % total_wh)
|
2024-10-25 11:13:33 +11:00
|
|
|
print(" Timer: %d" % timer)
|
2024-10-16 16:50:16 +11:00
|
|
|
|
2024-10-17 20:52:47 +11:00
|
|
|
wh_remain = (soc_percent / 100.0) * total_wh
|
2024-10-20 06:34:44 +11:00
|
|
|
h_estim = wh_remain - (avg_discharge - avg_charge)
|
2024-10-17 20:52:47 +11:00
|
|
|
h_estpct = (h_estim / total_wh) * 100
|
|
|
|
|
|
|
|
print("WH remain: %.2f" % wh_remain)
|
|
|
|
print("1h est WH: %.2f" % h_estim)
|
|
|
|
print("1hr est %%: %.2f" % h_estpct)
|
2024-10-16 16:50:16 +11:00
|
|
|
|
2024-10-18 17:13:09 +11:00
|
|
|
|
2024-10-23 07:34:12 +11:00
|
|
|
time_check = (current_time.hour == 5 or current_time.hour == 15)
|
2024-10-18 17:13:09 +11:00
|
|
|
estimate_check = (h_estpct < 18.0)
|
|
|
|
|
|
|
|
if (time_check and estimate_check):
|
|
|
|
print("Full BEEEEEAAAAANS!")
|
2024-10-26 15:02:54 +11:00
|
|
|
# Set settings timer to 55 minutes
|
|
|
|
err = gen24_registers.InOutWRte_RvrtTms.set(fronius1, 3300)
|
2024-10-25 11:13:33 +11:00
|
|
|
# This should be 'limit discharge' mode
|
|
|
|
print("Setting control mode to limit discharge")
|
|
|
|
err = gen24_registers.StorCtl_Mod.set(fronius1, 2)
|
|
|
|
# Charge battery at a rate from -2% discharge to 100% charge
|
|
|
|
# as a percentage of the MaxChaRte, which in our case is 25600W
|
|
|
|
print("Setting discharge rate to -10%")
|
|
|
|
err = gen24_registers.scaledOutWRte.set(fronius1, int(-20))
|
|
|
|
print("Setting charge rate to 100%")
|
|
|
|
err = gen24_registers.scaledInWRte.set(fronius1, int(100))
|
|
|
|
|
|
|
|
|
2024-10-18 17:13:09 +11:00
|
|
|
else:
|
|
|
|
print("Nothing doing")
|