-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from DerOetzi/import-historic-data-from-monito…
…ring Import old historic data csv from monitoring site to influxdb
- Loading branch information
Showing
9 changed files
with
274 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import asyncio | ||
from datetime import timezone | ||
from pandas import read_csv, to_datetime | ||
|
||
from solaredge2mqtt.models import ( | ||
BatteryPowerflow, | ||
ConsumerPowerflow, | ||
GridPowerflow, | ||
InverterPowerflow, | ||
Powerflow, | ||
) | ||
from solaredge2mqtt.service.influxdb import InfluxDB, Point | ||
from solaredge2mqtt.settings import LOCAL_TZ, service_settings | ||
|
||
settings = service_settings() | ||
STRINGS = 2 | ||
|
||
|
||
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 = InverterPowerflow( | ||
power=round(row["inverter_production"]), | ||
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: | ||
point = Point("battery_import") | ||
point.field("state_of_charge", round(row["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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
solaredge2mqtt/flux/fixes/import-historic-monitoring_data_1.0.8.flux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import "date" | ||
|
||
|
||
startTime = date.sub(from: date.truncate(t: now(), unit: 1y), d: 1y) | ||
stopTime = date.add(to: date.truncate(t: now(), unit: 1h), d: 1d) | ||
|
||
bucket = "solaredgedev" | ||
|
||
from(bucket: bucket) | ||
|> range(start: startTime, stop: stopTime) | ||
|> filter(fn: (r) => r["_measurement"] == "powerflow") | ||
|> set(key: "_measurement", value: "powerflow_import") | ||
|> to(bucket: bucket) | ||
|
||
from(bucket: bucket) | ||
|> range(start: startTime, stop: stopTime) | ||
|> filter(fn: (r) => r["_measurement"] == "energy") | ||
|> set(key: "_measurement", value: "energy_import") | ||
|> to(bucket: bucket) | ||
|
||
from(bucket: bucket) | ||
|> range(start: startTime, stop: stopTime) | ||
|> filter(fn: (r) => r["_measurement"] == "battery") | ||
|> set(key: "_measurement", value: "battery_import") | ||
|> to(bucket: bucket) | ||
|
||
exclude = ["inverter_power", "grid_power", "battery_power"] | ||
|
||
from(bucket: bucket) | ||
|> range(start: startTime, stop: stopTime) | ||
|> filter(fn: (r) => r["_measurement"] == "powerflow_import") | ||
|> filter(fn: (r) => not contains(value: r._field, set: exclude)) | ||
|> set(key: "_measurement", value: "powerflow") | ||
|> to(bucket: bucket) | ||
|
||
from(bucket: bucket) | ||
|> range(start: startTime, stop: stopTime) | ||
|> filter(fn: (r) => r["_measurement"] == "energy_import") | ||
|> filter(fn: (r) => not contains(value: r._field, set: exclude)) | ||
|> set(key: "_measurement", value: "energy") | ||
|> to(bucket: bucket) | ||
|
||
from(bucket: bucket) | ||
|> range(start: startTime, stop: stopTime) | ||
|> filter(fn: (r) => r["_measurement"] == "battery_import") | ||
|> set(key: "_measurement", value: "battery") | ||
|> to(bucket: bucket) |
29 changes: 29 additions & 0 deletions
29
solaredge2mqtt/flux/fixes/refactor_money_fields_1.0.8.flux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import "date" | ||
|
||
startTime = date.sub(from: date.truncate(t: now(), unit: 1y), d: 1y) | ||
stopTime = date.add(to: date.truncate(t: now(), unit: 1h), d: 1d) | ||
|
||
bucket = "solaredgedev" | ||
price_in = 0.3644 | ||
|
||
from(bucket: bucket) | ||
|> range(start: startTime, stop: stopTime) | ||
|> filter(fn: (r) => r["_measurement"] == "energy") | ||
|> filter(fn: (r) => r["_field"] == "money_earnings") | ||
|> set(key: "_field", value: "money_delivered") | ||
|> to(bucket: bucket) | ||
|
||
from(bucket: bucket) | ||
|> range(start: startTime, stop: stopTime) | ||
|> filter(fn: (r) => r["_measurement"] == "energy") | ||
|> filter(fn: (r) => r["_field"] == "money_savings") | ||
|> set(key: "_field", value: "money_saved") | ||
|> to(bucket: bucket) | ||
|
||
from(bucket: bucket) | ||
|> range(start: startTime, stop: stopTime) | ||
|> filter(fn: (r) => r["_measurement"] == "energy") | ||
|> filter(fn: (r) => r["_field"] == "grid_consumption") | ||
|> map(fn: (r) => ({r with _value: r._value * price_in})) | ||
|> set(key: "_field", value: "money_consumed") | ||
|> to(bucket: bucket) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.