Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1ce98df596 | ||
|
|
8f5106e3a0 | ||
|
|
202759cc84 | ||
|
|
0f564a561f | ||
|
|
c524a0189a | ||
|
|
4ff91a2674 | ||
|
|
117a2d2beb | ||
|
|
b7dc008cd7 |
@@ -80,6 +80,10 @@ Konfigurierbar pro Gerät im Web UI:
|
||||
>
|
||||
> Die EMS-Funktion muss zusätzlich in der Kathrein easyOperate-App aktiviert sein.
|
||||
|
||||
**Prognosebasierter Zwangslade-Timeout (optional):** Mit Standort (Breitengrad/Längengrad, Einstellungen-Tab) passt sich der Zwangslade-Timeout an die Solarstrahlungs-Prognose (Open-Meteo, kostenlos) an — bei bald zu erwartender Sonne wird länger gewartet (gedeckelt), bei aussichtsloser Prognose eher vom Netz geladen statt sinnlos den vollen Timeout abzuwarten. Ohne Standort unverändertes Verhalten (fester Timeout).
|
||||
|
||||
**Batterie-Schutz beim PV-Überschussladen:** Bei Anlagen mit Hausbatterie (z. B. Goodwe) wird die aktuelle Batterie-Entladeleistung vom berechneten PV-Überschuss abgezogen — ein Netzexport, der eigentlich aus dem Speicher kommt (z. B. bei bewölktem Wetter), wird so nicht fälschlich als PV-Überschuss fürs Auto verwendet. Betrifft nur PV-Überschussladen, Zwangsladen bleibt unverändert netzbasiert.
|
||||
|
||||
## Netzleistung (grid_power)
|
||||
|
||||
Der Sensor `grid_power` zeigt die Netzleistung intuitiv:
|
||||
|
||||
+6
-4
@@ -4,7 +4,7 @@
|
||||
|
||||
## Bekannte Einschränkungen
|
||||
|
||||
- [ ] Firma: Wallbox lädt in einem ~6,5-Minuten-Zyklus (Laden→Abgeschlossen→Pausiert→Laden) statt durchzuladen — Root Cause noch offen, live-Diagnose folgt beim nächsten Ladevorgang
|
||||
- [x] Firma: Wallbox lädt in einem ~6,5-Minuten-Zyklus (Laden→Abgeschlossen→Pausiert→Laden) statt durchzuladen — **Ursache endgültig gefunden 2026-08-26: Schütz in der Wallbox hat nicht angezogen.** Live-Diagnose zeigte Wallbox meldete korrekt "lädt, 16A frei" (`gewährter Strom`/`ems_stromsollwert` 16000mA), aber 0A auf allen drei Phasen — Steuerungs-/Kommunikationsebene lief einwandfrei, nur der Leistungskontakt selbst hat nie geschlossen. Erklärt auch den Loop: Auto negiert Ladevorgang, bekommt nie echten Strom, setzt immer wieder neu an. **Reines Hardware-Problem, ShineBridge zu 100% ausgeschlossen.** Nach Wallbox-Neustart Status offen — falls Schütz weiter nicht anzieht, braucht es einen Elektriker/Ersatzteil.
|
||||
- [ ] Growatt-only: Netzbezug nicht direkt messbar — nur Einspeisung via `power_to_grid`
|
||||
- [ ] Kathrein EVSE-Register 0x0060 (Charging-State) nur lesbar wenn Auto angeschlossen
|
||||
|
||||
@@ -14,11 +14,13 @@
|
||||
|
||||
Reihenfolge nach Aufwand/Abhängigkeit, nicht nach Wichtigkeit.
|
||||
|
||||
1. ~~**Automatischer täglicher Config-Export**~~ — erledigt (v1.11.0): täglicher Export nach `/share/shinebridge-backups/`, 14 Tage Aufbewahrung, sofort beim Start plus alle 24h.
|
||||
2. **Discord-Alerts** — bei Zwangsladen-Start, Fehlerzuständen (unbekannter Ladezustand), besonders günstigen Spot-Preisfenstern. Nutzt den bestehenden Homelab-Discord-Bot.
|
||||
1. ~~**Automatischer täglicher Config-Export**~~ — erledigt (v1.11.0–1.11.1): täglicher Export nach `/share/shinebridge-backups/`, Aufbewahrung konfigurierbar (Default 14 Tage), sofort beim Start plus alle 24h. Backup-Liste + Download + Wiederherstellen direkt im Einstellungen-Tab.
|
||||
2. **Discord-Alerts** — bei Zwangsladen-Start, Fehlerzuständen (unbekannter Ladezustand), besonders günstigen Spot-Preisfenstern. Nutzt den bestehenden Homelab-Discord-Bot. War zurückgestellt bis zur Klärung des 6,5-Minuten-Zyklus — **ist jetzt geklärt** (siehe "Bekannte Einschränkungen", war kein ShineBridge-Bug), Punkt kann wieder angegangen werden. Braucht trotzdem generelles Debounce (Mindestabstand pro Alert-Typ, Zustand muss erst eine Weile stabil sein, günstige Preise als tägliche Zusammenfassung statt Live-Alert) — normales Alert-Design, keine Sonderbehandlung mehr nötig.
|
||||
3. **Sessions pro Fahrzeug trennen** — falls mehrere Fahrer/Autos an einer Wallbox laden, aktuell nur eine Gesamt-Statistik.
|
||||
4. **Lastspitzenkappung (Peak Shaving)** — Ladestrom drosseln, wenn Netzbezug (mehrere Autos + Haushalt gleichzeitig) einen Grenzwert überschreitet. Eigenes Sicherheits-Design nötig (was passiert bei Messfehler/Ausfall — nie ungebremst weiterladen).
|
||||
5. **Prognosebasiertes Laden** — PV-Wetterprognose (z.B. Open-Meteo/Solcast) + Spot-Preis kombinieren, damit Zwangsladen nicht stur nach Timeout entscheidet, sondern z.B. "morgen wird's sonnig, noch etwas warten" berücksichtigt. Größter Umfang, externe API-Anbindung nötig.
|
||||
5. **Prognosebasiertes Laden** — in zwei Teilen, hintereinander gebaut:
|
||||
- ~~**Teil A: Batteriebewusster PV-Überschuss**~~ — erledigt (v1.11.2): Batterie-Entladeleistung wird vom PV-Überschuss abgezogen, damit PV-Überschussladen nie versehentlich den Hausspeicher anzapft (Zwangsladen bleibt unverändert netzbasiert).
|
||||
- ~~**Teil B: Wetterprognose**~~ — erledigt (v1.12.0): Standort (Breitengrad/Längengrad) im Einstellungen-Tab, optional. Open-Meteo-Solarstrahlungsprognose (kostenlos, kein Sonnenstand extra nötig — steckt implizit in der stündlichen Prognose) passt den Zwangslade-Timeout adaptiv an: bald Sonne zu erwarten → länger warten (gedeckelt); aussichtslose Prognose → früher vom Netz laden. Ohne Standort unverändertes Verhalten.
|
||||
6. **Chart-Feintuning** — läuft nebenbei mit, keine feste Priorität (z.B. Live-Kosten-Ticker statt nur Rückblick).
|
||||
7. **Auto-Cleanup verwaister MQTT-Geräte** — wenn ein Gerät aus ShineBridge entfernt wird, dessen MQTT-Discovery-Topics aktiv abmelden statt sie als tote HA-Geräte-Leichen zurückzulassen (siehe Fund unten).
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
name: ShineBridge
|
||||
version: "1.11.0"
|
||||
version: "1.12.1"
|
||||
slug: shinebridge
|
||||
description: Energiemanagement für Growatt/Goodwe-Wechselrichter — PV-Überschussladen, Wallbox-EMS, Tarifvergleich, Zigbee2MQTT-Überschussgeräte
|
||||
url: https://gitea.bitfire.work/retr0/shinebridge
|
||||
|
||||
@@ -76,14 +76,18 @@ class EmsController:
|
||||
return max(6000, min(self.max_charge_ma, ma))
|
||||
|
||||
def update(self, wallbox_reader, pv_surplus_w: float, charging_state: int,
|
||||
wallbox_power_w: float = 0.0) -> str:
|
||||
wallbox_power_w: float = 0.0, timeout_override_h: Optional[float] = None) -> str:
|
||||
"""
|
||||
Wird vom Poll-Loop aufgerufen.
|
||||
pv_surplus_w: positiv = PV exportiert ins Netz (verbleibender Überschuss)
|
||||
wallbox_power_w: aktuell von der Wallbox gezogene Leistung (aus Meter-Daten)
|
||||
timeout_override_h: falls gesetzt, ersetzt self.pv_timeout_h für diesen Aufruf
|
||||
(Prognose-basierte Anpassung, siehe weather_forecast.adjusted_timeout_h —
|
||||
wird vom Poll-Loop berechnet, EmsController kennt selbst keine Standortdaten)
|
||||
Gibt eine kurze Status-Beschreibung zurück.
|
||||
"""
|
||||
now = time.time()
|
||||
effective_timeout_h = timeout_override_h if timeout_override_h is not None else self.pv_timeout_h
|
||||
|
||||
# EMS einmalig aktivieren wenn Auto angesteckt
|
||||
if charging_state in (STATE_CONNECTED, STATE_CHARGING, STATE_PAUSED):
|
||||
@@ -132,7 +136,7 @@ class EmsController:
|
||||
|
||||
no_pv_h = (now - self._no_pv_since) / 3600.0
|
||||
|
||||
if no_pv_h >= self.pv_timeout_h:
|
||||
if no_pv_h >= effective_timeout_h:
|
||||
# Zwangsladen bis Zielzeit — Strom bei JEDEM Update erneut setzen (nicht nur beim
|
||||
# ersten Eintritt), sonst bleibt ein einzelner fehlgeschlagener/verworfener Write
|
||||
# (Modbus-Hänger, Fahrzeug-Störung) unbemerkt für den Rest der Nacht stehen.
|
||||
@@ -140,8 +144,9 @@ class EmsController:
|
||||
mins_left = (target - datetime.now()).total_seconds() / 60
|
||||
if not self._forced_charging:
|
||||
self._forced_charging = True
|
||||
log.info("[EMS] Zwangsladen gestartet — %dmin bis %02d:00, max %.0fA",
|
||||
int(mins_left), self.target_hour, self.max_charge_ma / 1000)
|
||||
log.info("[EMS] Zwangsladen gestartet — %dmin bis %02d:00, max %.0fA (Timeout %.1fh%s)",
|
||||
int(mins_left), self.target_hour, self.max_charge_ma / 1000, effective_timeout_h,
|
||||
" prognose-angepasst" if timeout_override_h is not None else "")
|
||||
wallbox_reader.set_current(self.max_charge_ma, self.phases)
|
||||
return f"Zwangsladen ({no_pv_h:.1f}h kein PV, {int(mins_left)}min bis {self.target_hour:02d}:00)"
|
||||
|
||||
|
||||
+119
-15
@@ -15,6 +15,7 @@ from modbus_client import ModbusReader
|
||||
from goodwe_client import GoodweReader
|
||||
from wallbox_client import WallboxReader
|
||||
from ha_entity_client import HAEntityReader
|
||||
import weather_forecast
|
||||
from ems_controller import EmsController
|
||||
from mqtt_publisher import MqttPublisher
|
||||
from surplus_devices import SurplusDeviceController
|
||||
@@ -124,6 +125,9 @@ def _defaults() -> Dict[str, Any]:
|
||||
"inverters": [],
|
||||
"surplus_devices": [],
|
||||
"z2m_base": "zigbee2mqtt",
|
||||
"config_backup_retention_days": 14,
|
||||
"site_lat": None,
|
||||
"site_lon": None,
|
||||
}
|
||||
|
||||
def _load_json_safe(path: str) -> Optional[Dict]:
|
||||
@@ -174,6 +178,9 @@ def save_config():
|
||||
"inverters": State.inverters_cfg,
|
||||
"surplus_devices": State.surplus_devices_cfg,
|
||||
"z2m_base": State.z2m_base,
|
||||
"config_backup_retention_days": State.mqtt_cfg.get("config_backup_retention_days", 14),
|
||||
"site_lat": State.mqtt_cfg.get("site_lat"),
|
||||
"site_lon": State.mqtt_cfg.get("site_lon"),
|
||||
}
|
||||
# Backup der letzten guten Config anlegen
|
||||
if os.path.exists(CONFIG_PATH):
|
||||
@@ -221,9 +228,16 @@ def _compute_aggregates(allow_stale: bool = False) -> Dict[str, float]:
|
||||
# ── EMS Hilfsfunktionen ───────────────────────────────────────
|
||||
|
||||
def _get_pv_surplus() -> float:
|
||||
"""PV-Überschuss in Watt. Nutzt grid_power-Aggregat (negativ = Einspeisung)."""
|
||||
"""PV-Überschuss in Watt für PV-Überschussladen (nicht für Zwangsladen — das
|
||||
bleibt unverändert netzbasiert). Netzexport (grid_power negativ = Einspeisung)
|
||||
abzüglich aktueller Batterie-Entladeleistung, damit ein Überschuss, der
|
||||
tatsächlich aus dem Hausspeicher kommt (z.B. bei bewölktem Wetter), nicht
|
||||
fälschlich als PV-Überschuss fürs Auto verwendet wird. Anlagen ohne Batterie
|
||||
(bat_discharge_power fehlt) verhalten sich unverändert wie zuvor."""
|
||||
agg = _compute_aggregates(allow_stale=True)
|
||||
return max(0.0, -agg.get("grid_power", 0.0))
|
||||
grid_export = max(0.0, -agg.get("grid_power", 0.0))
|
||||
bat_discharge = agg.get("bat_discharge_power", 0.0)
|
||||
return max(0.0, grid_export - bat_discharge)
|
||||
|
||||
|
||||
# ── Poll Loop ─────────────────────────────────────────────────
|
||||
@@ -357,7 +371,10 @@ def _poll_loop(inv_cfg: Dict[str, Any], stop: threading.Event):
|
||||
# charging_state aus 0x0060 (solo-Read); fehlt bei kein Fahrzeug → 0 (Idle)
|
||||
charging_state = int(values.get("charging_state", 0))
|
||||
wallbox_power = values.get("total_power", 0.0)
|
||||
ems_status = ems.update(reader, pv_surplus, charging_state, wallbox_power)
|
||||
site_lat = State.mqtt_cfg.get("site_lat")
|
||||
site_lon = State.mqtt_cfg.get("site_lon")
|
||||
timeout_override = weather_forecast.adjusted_timeout_h(ems.pv_timeout_h, site_lat, site_lon)
|
||||
ems_status = ems.update(reader, pv_surplus, charging_state, wallbox_power, timeout_override)
|
||||
values["ems_status_code"] = float(charging_state)
|
||||
log.info("[%s] EMS: %s | PV-Überschuss: %.0fW", inv_id, ems_status, pv_surplus)
|
||||
|
||||
@@ -494,6 +511,19 @@ def _restart_all():
|
||||
|
||||
# ── REST API ──────────────────────────────────────────────────
|
||||
|
||||
@app.get("/api/weather-forecast")
|
||||
def api_weather_forecast():
|
||||
with State.lock:
|
||||
lat = State.mqtt_cfg.get("site_lat")
|
||||
lon = State.mqtt_cfg.get("site_lon")
|
||||
# Beispiel-Basiswert: erstes konfigurierte Kathrein-Timeout, sonst 2h-Default
|
||||
base = next(
|
||||
(inv.get("ems_timeout") for inv in State.inverters_cfg if inv.get("ems_timeout")),
|
||||
2.0,
|
||||
)
|
||||
return jsonify(weather_forecast.summary(lat, lon, float(base)))
|
||||
|
||||
|
||||
@app.get("/api/config")
|
||||
def api_get_config():
|
||||
with State.lock:
|
||||
@@ -518,7 +548,16 @@ def api_save_config():
|
||||
State.mqtt_cfg[k] = float(data[k])
|
||||
except (ValueError, TypeError):
|
||||
pass
|
||||
for k in ("billing_day", "billing_month"):
|
||||
for k in ("site_lat", "site_lon"):
|
||||
if k in data:
|
||||
if data[k] in (None, ""):
|
||||
State.mqtt_cfg[k] = None
|
||||
else:
|
||||
try:
|
||||
State.mqtt_cfg[k] = float(data[k])
|
||||
except (ValueError, TypeError):
|
||||
pass
|
||||
for k in ("billing_day", "billing_month", "config_backup_retention_days"):
|
||||
if k in data and data[k] is not None:
|
||||
try:
|
||||
State.mqtt_cfg[k] = int(data[k])
|
||||
@@ -824,7 +863,7 @@ def _build_export_data() -> Dict[str, Any]:
|
||||
|
||||
|
||||
CONFIG_BACKUP_DIR = "/share/shinebridge-backups"
|
||||
CONFIG_BACKUP_RETENTION_DAYS = 14
|
||||
CONFIG_BACKUP_RETENTION_DAYS_DEFAULT = 14
|
||||
|
||||
|
||||
def _export_config_backup():
|
||||
@@ -839,7 +878,8 @@ def _export_config_backup():
|
||||
json.dump(_build_export_data(), f, indent=2, ensure_ascii=False)
|
||||
log.info("Config-Backup geschrieben: %s", path)
|
||||
|
||||
cutoff = time.time() - CONFIG_BACKUP_RETENTION_DAYS * 86400
|
||||
retention_days = State.mqtt_cfg.get("config_backup_retention_days", CONFIG_BACKUP_RETENTION_DAYS_DEFAULT)
|
||||
cutoff = time.time() - retention_days * 86400
|
||||
for name in os.listdir(CONFIG_BACKUP_DIR):
|
||||
fpath = os.path.join(CONFIG_BACKUP_DIR, name)
|
||||
if name.startswith("config-") and name.endswith(".json") and os.path.getmtime(fpath) < cutoff:
|
||||
@@ -870,27 +910,28 @@ def api_export_config():
|
||||
)
|
||||
|
||||
|
||||
@app.post("/api/import-config")
|
||||
def api_import_config():
|
||||
data = request.get_json(force=True) or {}
|
||||
def _apply_import_data(data: Dict[str, Any]) -> "tuple[bool, str, int]":
|
||||
"""Validiert + übernimmt eine ShineBridge-Export-Struktur. Gemeinsam genutzt
|
||||
von /api/import-config (Datei-Upload) und /api/config-backups/.../restore
|
||||
(automatisches Backup). Gibt (ok, error, anzahl_geräte) zurück."""
|
||||
if not data.get("shinebridge_export"):
|
||||
return jsonify({"error": "Keine gültige ShineBridge-Export-Datei"}), 400
|
||||
return False, "Keine gültige ShineBridge-Export-Datei", 0
|
||||
|
||||
inverters = data.get("inverters", [])
|
||||
if not isinstance(inverters, list):
|
||||
return jsonify({"error": "inverters ungültig"}), 400
|
||||
return False, "inverters ungültig", 0
|
||||
for inv in inverters:
|
||||
model_id = inv.get("inverter_model")
|
||||
if model_id not in INVERTERS:
|
||||
return jsonify({"error": f"Unbekanntes Modell: {model_id}"}), 400
|
||||
return False, f"Unbekanntes Modell: {model_id}", 0
|
||||
protocol = INVERTERS[model_id].protocol
|
||||
# modbus_ip wird als generisches Host-Feld fuer alle IP-basierten Protokolle
|
||||
# wiederverwendet (modbus, goodwe_udp, kathrein) — nur ha_entity braucht keins.
|
||||
if protocol == "ha_entity":
|
||||
if not inv.get("ha_entity_prefix"):
|
||||
return jsonify({"error": f"ha_entity_prefix fehlt in Gerät {inv.get('name', '?')}"}), 400
|
||||
return False, f"ha_entity_prefix fehlt in Gerät {inv.get('name', '?')}", 0
|
||||
elif not inv.get("modbus_ip"):
|
||||
return jsonify({"error": f"modbus_ip fehlt in Gerät {inv.get('name', '?')}"}), 400
|
||||
return False, f"modbus_ip fehlt in Gerät {inv.get('name', '?')}", 0
|
||||
|
||||
with State.lock:
|
||||
if mqtt := data.get("mqtt"):
|
||||
@@ -904,7 +945,69 @@ def api_import_config():
|
||||
save_config()
|
||||
|
||||
threading.Thread(target=_restart_all, daemon=True).start()
|
||||
return jsonify({"ok": True, "inverters": len(inverters)})
|
||||
return True, "", len(inverters)
|
||||
|
||||
|
||||
@app.post("/api/import-config")
|
||||
def api_import_config():
|
||||
data = request.get_json(force=True) or {}
|
||||
ok, error, count = _apply_import_data(data)
|
||||
if not ok:
|
||||
return jsonify({"error": error}), 400
|
||||
return jsonify({"ok": True, "inverters": count})
|
||||
|
||||
|
||||
def _safe_backup_filename(filename: str) -> Optional[str]:
|
||||
"""Nur Dateinamen aus CONFIG_BACKUP_DIR selbst zulassen, kein Path-Traversal."""
|
||||
name = os.path.basename(filename)
|
||||
if name != filename or not name.startswith("config-") or not name.endswith(".json"):
|
||||
return None
|
||||
path = os.path.join(CONFIG_BACKUP_DIR, name)
|
||||
return path if os.path.isfile(path) else None
|
||||
|
||||
|
||||
@app.get("/api/config-backups")
|
||||
def api_list_config_backups():
|
||||
try:
|
||||
files = [f for f in os.listdir(CONFIG_BACKUP_DIR) if f.startswith("config-") and f.endswith(".json")]
|
||||
except OSError:
|
||||
files = []
|
||||
backups = []
|
||||
for name in files:
|
||||
path = os.path.join(CONFIG_BACKUP_DIR, name)
|
||||
try:
|
||||
st = os.stat(path)
|
||||
backups.append({"filename": name, "modified_at": int(st.st_mtime), "size_bytes": st.st_size})
|
||||
except OSError:
|
||||
continue
|
||||
backups.sort(key=lambda b: b["modified_at"], reverse=True)
|
||||
return jsonify({
|
||||
"backups": backups,
|
||||
"retention_days": State.mqtt_cfg.get("config_backup_retention_days", CONFIG_BACKUP_RETENTION_DAYS_DEFAULT),
|
||||
})
|
||||
|
||||
|
||||
@app.get("/api/config-backups/<path:filename>/download")
|
||||
def api_download_config_backup(filename):
|
||||
path = _safe_backup_filename(filename)
|
||||
if not path:
|
||||
return jsonify({"error": "Datei nicht gefunden"}), 404
|
||||
from flask import send_file
|
||||
return send_file(path, mimetype="application/json", as_attachment=True, download_name=os.path.basename(path))
|
||||
|
||||
|
||||
@app.post("/api/config-backups/<path:filename>/restore")
|
||||
def api_restore_config_backup(filename):
|
||||
path = _safe_backup_filename(filename)
|
||||
if not path:
|
||||
return jsonify({"error": "Datei nicht gefunden"}), 404
|
||||
data = _load_json_safe(path)
|
||||
if data is None:
|
||||
return jsonify({"error": "Backup-Datei ist beschädigt"}), 400
|
||||
ok, error, count = _apply_import_data(data)
|
||||
if not ok:
|
||||
return jsonify({"error": error}), 400
|
||||
return jsonify({"ok": True, "inverters": count})
|
||||
|
||||
_hist_spot_cache: Dict[str, Any] = {} # key → {"ts", "avg_ct"}
|
||||
|
||||
@@ -976,6 +1079,7 @@ if __name__ == "__main__":
|
||||
"price_import", "price_export", "billing_day", "billing_month",
|
||||
"tariff_type", "spot_country", "spot_markup", "spot_chart",
|
||||
"billing_tracker_enabled", "monthly_rate_eur", "grundpreis_eur_per_month",
|
||||
"config_backup_retention_days", "site_lat", "site_lon",
|
||||
) if k in cfg}
|
||||
State.inverters_cfg = cfg.get("inverters", [])
|
||||
State.surplus_devices_cfg = cfg.get("surplus_devices", [])
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
"""Solarstrahlungs-Prognose über Open-Meteo (kostenlos, kein API-Key nötig).
|
||||
|
||||
Wird genutzt, um den Zwangslade-Timeout im EMS-Controller adaptiv zu machen:
|
||||
bei noch zu erwartender Sonne wird länger gewartet, bei aussichtsloser Prognose
|
||||
wird eher vom Netz geladen statt sinnlos den vollen Timeout abzuwarten.
|
||||
"""
|
||||
|
||||
import logging
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
import json
|
||||
from typing import List, Optional, Tuple
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
_CACHE_TTL_S = 3 * 3600 # Prognose alle 3h neu holen, nicht bei jedem Poll
|
||||
_cache: dict = {} # key: (lat, lon) -> {"ts": float, "hourly": [(epoch_s, ghi_w_m2), ...]}
|
||||
|
||||
|
||||
def _fetch(lat: float, lon: float) -> Optional[List[Tuple[int, float]]]:
|
||||
url = (
|
||||
"https://api.open-meteo.com/v1/forecast"
|
||||
f"?latitude={lat}&longitude={lon}"
|
||||
"&hourly=shortwave_radiation&forecast_days=2&timezone=auto"
|
||||
)
|
||||
try:
|
||||
with urllib.request.urlopen(url, timeout=10) as r:
|
||||
data = json.loads(r.read())
|
||||
times = data["hourly"]["time"]
|
||||
values = data["hourly"]["shortwave_radiation"]
|
||||
# ISO-Zeitstrings (lokale Zeit dank timezone=auto) -> Unix-Timestamp
|
||||
import datetime
|
||||
out = []
|
||||
for t, v in zip(times, values):
|
||||
dt = datetime.datetime.fromisoformat(t)
|
||||
out.append((int(dt.timestamp()), float(v or 0.0)))
|
||||
return out
|
||||
except (urllib.error.URLError, urllib.error.HTTPError, KeyError, ValueError) as e:
|
||||
log.warning("[Weather] Open-Meteo Abfrage fehlgeschlagen: %s", e)
|
||||
return None
|
||||
|
||||
|
||||
def get_hourly_ghi(lat: float, lon: float) -> Optional[List[Tuple[int, float]]]:
|
||||
"""Liefert [(unix_ts, ghi_w_m2), ...] für die nächsten ~48h, gecacht."""
|
||||
key = (round(lat, 3), round(lon, 3))
|
||||
cached = _cache.get(key)
|
||||
now = time.time()
|
||||
if cached and now - cached["ts"] < _CACHE_TTL_S:
|
||||
return cached["hourly"]
|
||||
|
||||
hourly = _fetch(lat, lon)
|
||||
if hourly is None:
|
||||
return cached["hourly"] if cached else None # alten Stand weiterverwenden statt nichts
|
||||
|
||||
_cache[key] = {"ts": now, "hourly": hourly}
|
||||
return hourly
|
||||
|
||||
|
||||
def adjusted_timeout_h(base_timeout_h: float, lat: Optional[float], lon: Optional[float]) -> float:
|
||||
"""Passt den Zwangslade-Timeout an die Prognose an. Ohne Standort (lat/lon
|
||||
nicht gesetzt) unverändert den Basiswert zurückgeben — Feature ist Opt-in."""
|
||||
if lat is None or lon is None:
|
||||
return base_timeout_h
|
||||
|
||||
hourly = get_hourly_ghi(lat, lon)
|
||||
if not hourly:
|
||||
return base_timeout_h
|
||||
|
||||
now = time.time()
|
||||
# Strahlung in den naechsten 3h (kurzfristig: lohnt es sich, gleich noch zu warten?)
|
||||
near = [ghi for ts, ghi in hourly if now <= ts < now + 3 * 3600]
|
||||
# Strahlung im Rest des "Tagesfensters" (grob 12h voraus, deckt auch noch
|
||||
# kommende Vormittagssonne ab, falls schon frueh am Morgen geprueft wird)
|
||||
horizon = [ghi for ts, ghi in hourly if now <= ts < now + 12 * 3600]
|
||||
|
||||
SUN_THRESHOLD = 80.0 # W/m² — deutlich mehr als Daemmerung, aber kein voller Sonnentag noetig
|
||||
|
||||
if near and max(near) >= SUN_THRESHOLD:
|
||||
# Bald noch nennenswerte Sonne zu erwarten -> mehr Geduld, gedeckelt
|
||||
return min(base_timeout_h * 1.75, base_timeout_h + 4.0)
|
||||
if horizon and max(horizon) < SUN_THRESHOLD:
|
||||
# Auch im Rest des Fensters keine nennenswerte Sonne zu erwarten ->
|
||||
# nicht sinnlos den vollen Timeout abwarten, aber nie unter 0.5h
|
||||
return max(base_timeout_h * 0.5, 0.5)
|
||||
return base_timeout_h
|
||||
|
||||
|
||||
def summary(lat: Optional[float], lon: Optional[float], base_timeout_h: float = 2.0) -> dict:
|
||||
"""Für die UI: aktueller Prognose-Stand + Beispiel, wie sich ein Timeout
|
||||
dadurch veraendern wuerde. Nicht fuers eigentliche EMS-Update genutzt
|
||||
(das ruft adjusted_timeout_h direkt auf) — nur zur Anzeige/Diagnose."""
|
||||
if lat is None or lon is None:
|
||||
return {"configured": False}
|
||||
|
||||
hourly = get_hourly_ghi(lat, lon)
|
||||
if not hourly:
|
||||
return {"configured": True, "error": "Prognose aktuell nicht abrufbar"}
|
||||
|
||||
now = time.time()
|
||||
near = [ghi for ts, ghi in hourly if now <= ts < now + 3 * 3600]
|
||||
horizon = [ghi for ts, ghi in hourly if now <= ts < now + 12 * 3600]
|
||||
next_3h_max = round(max(near), 0) if near else 0
|
||||
horizon_12h_max = round(max(horizon), 0) if horizon else 0
|
||||
|
||||
adjusted = adjusted_timeout_h(base_timeout_h, lat, lon)
|
||||
if adjusted > base_timeout_h:
|
||||
verdict = "verlängert"
|
||||
elif adjusted < base_timeout_h:
|
||||
verdict = "verkürzt"
|
||||
else:
|
||||
verdict = "unverändert"
|
||||
|
||||
return {
|
||||
"configured": True,
|
||||
"next_3h_max_ghi": next_3h_max,
|
||||
"horizon_12h_max_ghi": horizon_12h_max,
|
||||
"sun_threshold": SUN_THRESHOLD,
|
||||
"example_base_h": base_timeout_h,
|
||||
"example_adjusted_h": round(adjusted, 2),
|
||||
"verdict": verdict,
|
||||
}
|
||||
@@ -233,6 +233,19 @@
|
||||
<button class="btn btn-primary" onclick="saveMqtt()">Speichern & Neu starten</button>
|
||||
</div>
|
||||
|
||||
<div class="settings-section">
|
||||
<h3>Standort</h3>
|
||||
<p style="color:var(--text-dim);font-size:.85rem;margin:0 0 .75rem">Für die prognosebasierte Anpassung des Zwangslade-Timeouts (Solarstrahlungs-Vorhersage via Open-Meteo). Optional — leer lassen deaktiviert die Anpassung, Zwangsladen nutzt dann weiter den festen Timeout.</p>
|
||||
<div style="display:flex;gap:10px;flex-wrap:wrap">
|
||||
<div class="field" style="max-width:180px"><label>Breitengrad</label>
|
||||
<input type="number" id="cfg-site-lat" step="0.0001" placeholder="z.B. 52.5200"></div>
|
||||
<div class="field" style="max-width:180px"><label>Längengrad</label>
|
||||
<input type="number" id="cfg-site-lon" step="0.0001" placeholder="z.B. 13.4050"></div>
|
||||
</div>
|
||||
<button class="btn btn-primary" onclick="saveLocation()">Speichern</button>
|
||||
<div id="weather-preview" style="margin-top:.75rem;font-size:.85rem"></div>
|
||||
</div>
|
||||
|
||||
<div class="settings-section">
|
||||
<h3>Stromtarif</h3>
|
||||
<div class="field">
|
||||
@@ -321,6 +334,15 @@
|
||||
</div>
|
||||
<div id="import-result" style="margin-top:.5rem;font-size:.85rem"></div>
|
||||
</div>
|
||||
|
||||
<div class="settings-section">
|
||||
<h3>Automatische Backups</h3>
|
||||
<p style="color:var(--text-dim);font-size:.85rem;margin:0 0 .75rem">Täglich automatisch nach <code style="font-size:11px;background:var(--surface2);padding:1px 5px;border-radius:3px">/share/shinebridge-backups/</code> gesichert — übersteht auch einen Add-on-Uninstall+Reinstall.</p>
|
||||
<div class="field" style="max-width:200px"><label>Aufbewahrung (Tage)</label>
|
||||
<input type="number" id="cfg-backup-retention" min="1" max="365" value="14"></div>
|
||||
<button class="btn btn-primary" onclick="saveBackupRetention()">Speichern</button>
|
||||
<div id="backup-list" style="margin-top:1rem;display:flex;flex-direction:column;gap:6px"></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Flash/OTA-Panel ausgelagert nach growatt-diagnose-tool (v1.9.0) -->
|
||||
@@ -477,6 +499,7 @@ function switchTab(name) {
|
||||
});
|
||||
if (name === "energy" || name === "live") startRefresh(); else stopRefresh();
|
||||
if (name === "finance") loadFinance();
|
||||
if (name === "settings") loadBackupList();
|
||||
}
|
||||
|
||||
// ── Energy Dashboard ──────────────────────────────────────────
|
||||
@@ -1227,6 +1250,43 @@ async function loadSettings() {
|
||||
document.getElementById("billing-tracker-fields").style.display = trackerEnabled ? "" : "none";
|
||||
document.getElementById("cfg-monthly-rate").value = cfg.monthly_rate_eur ?? 0;
|
||||
document.getElementById("cfg-grundpreis").value = cfg.grundpreis_eur_per_month ?? 0;
|
||||
document.getElementById("cfg-backup-retention").value = cfg.config_backup_retention_days ?? 14;
|
||||
document.getElementById("cfg-site-lat").value = cfg.site_lat ?? "";
|
||||
document.getElementById("cfg-site-lon").value = cfg.site_lon ?? "";
|
||||
loadWeatherPreview();
|
||||
}
|
||||
|
||||
async function saveLocation() {
|
||||
const lat = document.getElementById("cfg-site-lat").value;
|
||||
const lon = document.getElementById("cfg-site-lon").value;
|
||||
try {
|
||||
await fetchJSON(api("api/config"), {
|
||||
method: "POST", headers: {"Content-Type": "application/json"},
|
||||
body: JSON.stringify({ site_lat: lat === "" ? null : lat, site_lon: lon === "" ? null : lon }),
|
||||
});
|
||||
showToast("Gespeichert!", "ok");
|
||||
setTimeout(loadWeatherPreview, 500);
|
||||
} catch(e) { showToast("Fehler beim Speichern", "err"); }
|
||||
}
|
||||
|
||||
async function loadWeatherPreview() {
|
||||
const el = document.getElementById("weather-preview");
|
||||
let data;
|
||||
try { data = await fetchJSON(api("api/weather-forecast")); }
|
||||
catch(e) { el.innerHTML = ""; return; }
|
||||
|
||||
if (!data.configured) {
|
||||
el.innerHTML = `<span style="color:var(--text-dim)">Kein Standort gesetzt — Zwangslade-Timeout bleibt fest.</span>`;
|
||||
return;
|
||||
}
|
||||
if (data.error) {
|
||||
el.innerHTML = `<span style="color:var(--red)">${esc(data.error)}</span>`;
|
||||
return;
|
||||
}
|
||||
const verdictColor = data.verdict === "verlängert" ? "var(--green)" : data.verdict === "verkürzt" ? "var(--orange, #ffa657)" : "var(--text-dim)";
|
||||
el.innerHTML = `
|
||||
<div style="color:var(--text-dim)">Solarstrahlung nächste 3h: <strong style="color:var(--text)">${data.next_3h_max_ghi} W/m²</strong> · nächste 12h: <strong style="color:var(--text)">${data.horizon_12h_max_ghi} W/m²</strong></div>
|
||||
<div style="margin-top:4px;color:${verdictColor}">Beispiel bei ${data.example_base_h}h Basis-Timeout: <strong>${data.example_adjusted_h}h</strong> (${data.verdict})</div>`;
|
||||
}
|
||||
|
||||
async function savePrices() {
|
||||
@@ -1304,6 +1364,61 @@ async function importConfig(input) {
|
||||
input.value = "";
|
||||
}
|
||||
|
||||
// ── Automatische Backups ─────────────────────────────────────
|
||||
|
||||
async function saveBackupRetention() {
|
||||
const days = parseInt(document.getElementById("cfg-backup-retention").value) || 14;
|
||||
try {
|
||||
await fetchJSON(api("api/config"), {
|
||||
method: "POST", headers: {"Content-Type": "application/json"},
|
||||
body: JSON.stringify({ config_backup_retention_days: days }),
|
||||
});
|
||||
showToast("Gespeichert!", "ok");
|
||||
} catch(e) { showToast("Fehler beim Speichern", "err"); }
|
||||
}
|
||||
|
||||
function fBytes(n) {
|
||||
if (n < 1024) return `${n} B`;
|
||||
if (n < 1024*1024) return `${(n/1024).toFixed(1)} KB`;
|
||||
return `${(n/1024/1024).toFixed(1)} MB`;
|
||||
}
|
||||
|
||||
async function loadBackupList() {
|
||||
const el = document.getElementById("backup-list");
|
||||
let data;
|
||||
try { data = await fetchJSON(api("api/config-backups")); }
|
||||
catch(e) { el.innerHTML = '<div class="no-data">Fehler beim Laden</div>'; return; }
|
||||
|
||||
const backups = data.backups || [];
|
||||
if (backups.length === 0) {
|
||||
el.innerHTML = `<div style="font-size:12px;color:var(--text-dim)">Noch keine Backups — das erste entsteht beim nächsten Add-on-Start oder spätestens morgen.</div>`;
|
||||
return;
|
||||
}
|
||||
el.innerHTML = backups.map(b => {
|
||||
const date = new Date(b.modified_at * 1000).toLocaleString('de-DE');
|
||||
return `<div style="display:flex;align-items:center;justify-content:space-between;gap:10px;background:var(--surface2);border:1px solid var(--border);border-radius:6px;padding:8px 12px;font-size:13px">
|
||||
<div><strong>${esc(b.filename)}</strong><br><span style="color:var(--text-dim);font-size:11px">${date} · ${fBytes(b.size_bytes)}</span></div>
|
||||
<div style="display:flex;gap:6px;flex-shrink:0">
|
||||
<button class="btn" style="padding:4px 10px;font-size:12px" onclick="downloadBackup('${esc(b.filename)}')">Download</button>
|
||||
<button class="btn btn-danger" style="padding:4px 10px;font-size:12px" onclick="restoreBackup('${esc(b.filename)}')">Wiederherstellen</button>
|
||||
</div>
|
||||
</div>`;
|
||||
}).join("");
|
||||
}
|
||||
|
||||
function downloadBackup(filename) {
|
||||
window.location.href = api(`api/config-backups/${encodeURIComponent(filename)}/download`);
|
||||
}
|
||||
|
||||
async function restoreBackup(filename) {
|
||||
if (!confirm(`"${filename}" wiederherstellen? Das überschreibt die aktuelle Konfiguration (Geräte, MQTT) komplett.`)) return;
|
||||
try {
|
||||
const res = await fetchJSON(api(`api/config-backups/${encodeURIComponent(filename)}/restore`), { method: "POST" });
|
||||
showToast(`${res.inverters} Gerät(e) wiederhergestellt`, "ok");
|
||||
setTimeout(() => { loadSettings(); loadInverters(); }, 3000);
|
||||
} catch(e) { showToast("Wiederherstellen fehlgeschlagen", "err"); }
|
||||
}
|
||||
|
||||
// ── Surplus Devices ───────────────────────────────────────────
|
||||
|
||||
let surplusDevices = [];
|
||||
|
||||
Reference in New Issue
Block a user