-
Notifications
You must be signed in to change notification settings - Fork 2
/
import_historic.py
115 lines (85 loc) · 3.25 KB
/
import_historic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import asyncio
from datetime import timezone
from pandas import read_csv, to_datetime
from tzlocal import get_localzone_name
from solaredge2mqtt.core.influxdb import InfluxDB, Point
from solaredge2mqtt.core.settings import service_settings
from solaredge2mqtt.services.powerflow.models import (
BatteryPowerflow,
ConsumerPowerflow,
GridPowerflow,
InverterPowerflow,
Powerflow,
)
settings = service_settings()
STRINGS = 2
LOCAL_TZ = get_localzone_name()
async def main():
data = read_csv("data.csv")
data["time"] = to_datetime(data["time"], format="%d.%m.%Y %H:%M").dt.tz_localize(
LOCAL_TZ
)
for i in range(1, STRINGS + 1):
data[f"string{i}"] = data[f"string{i}"].fillna(0)
data["evcharger"] = data["evcharger"].fillna(0)
data.info()
points = []
columns = data.columns.to_list()
for _, row in data.iterrows():
grid = GridPowerflow(
power=round(row["grid_delivery"] - row["grid_consumption"])
)
if "battery_charge" in columns and "battery_discharge" in columns:
battery = BatteryPowerflow(
power=round(row["battery_charge"] - row["battery_discharge"])
)
else:
battery = BatteryPowerflow(power=0)
pv_production = 0.0
for i in range(1, STRINGS + 1):
pv_production += row[f"string{i}"]
dc_power = pv_production - battery.power
inverter_power = round(row["inverter_production"] - row["inverter_consumption"])
inverter = InverterPowerflow(
power=inverter_power,
dc_power=round(dc_power),
battery_discharge=battery.discharge,
)
if "evcharger" in columns:
evcharger = round(row["evcharger"])
else:
evcharger = 0
consumer = ConsumerPowerflow(inverter=inverter, grid=grid, evcharger=evcharger)
pv_production = round(pv_production)
powerflow = Powerflow(
pv_production=pv_production,
inverter=inverter,
grid=grid,
battery=battery,
consumer=consumer,
)
print(
f"Time: {row['time']}, PV: {pv_production} W, Inverter: {inverter.power} W, "
+ f"House: {consumer.house} W, Grid: {grid.power} W, "
+ f"Battery: {battery.power} W, Wallbox: {consumer.evcharger} W"
)
point = powerflow.prepare_point("powerflow_import")
point.tag("agg_type", "mean")
point.time(row["time"].astimezone(timezone.utc))
points.append(point)
point = powerflow.prepare_point_energy("energy_import", settings.prices)
point.time(row["time"].astimezone(timezone.utc))
points.append(point)
if "battery_soc" in columns:
battery_soc = row["battery_soc"]
if battery_soc < 100:
battery_soc += 0.01
point = Point("battery_import")
point.field("state_of_charge", round(battery_soc, 2))
point.time(row["time"].astimezone(timezone.utc))
point.tag("agg_type", "mean")
points.append(point)
influxdb = InfluxDB(settings.influxdb, settings.prices)
await influxdb.write_points_async(points)
if __name__ == "__main__":
asyncio.run(main())