HAOS Add-on v1.1.1: Sparkline-Graphen (letzte 5 Minuten)
- SVG-Sparkline pro Sensor-Karte, farbkodiert nach device_class - Backend: (timestamp, value) deque pro Sensor, API filtert auf 300s - Kein Datenverlust bei Neustart (In-Memory, reicht für Trendanzeige) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -4,6 +4,7 @@ import os
|
||||
import threading
|
||||
import time
|
||||
import uuid
|
||||
from collections import deque
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from flask import Flask, jsonify, request, send_from_directory
|
||||
@@ -112,6 +113,12 @@ def _poll_loop(inv_cfg: Dict[str, Any], stop: threading.Event):
|
||||
d["last_update"] = time.time()
|
||||
d["modbus_ok"] = True
|
||||
d["poll_count"] = d.get("poll_count", 0) + 1
|
||||
# History: (timestamp, value) pro Sensor, maximal 5 Minuten
|
||||
hist = d.setdefault("history", {})
|
||||
now = time.time()
|
||||
for sid, val in values.items():
|
||||
q = hist.setdefault(sid, deque(maxlen=300))
|
||||
q.append((now, val))
|
||||
if _publisher:
|
||||
_publisher.publish_data(values, prefix)
|
||||
_publisher.publish_status("online", prefix)
|
||||
@@ -209,10 +216,17 @@ def api_get_data():
|
||||
model_id = inv_cfg.get("inverter_model", "MIC_1500_TL_X")
|
||||
inverter = INVERTERS.get(model_id, INVERTERS["MIC_1500_TL_X"])
|
||||
d = State.inv_data.get(inv_id, {})
|
||||
cutoff = time.time() - 300 # letzte 5 Minuten
|
||||
raw_hist = d.get("history", {})
|
||||
history = {
|
||||
sid: [v for (t, v) in q if t >= cutoff]
|
||||
for sid, q in raw_hist.items()
|
||||
}
|
||||
result[inv_id] = {
|
||||
"name": inv_cfg.get("name", inverter.name),
|
||||
"inverter_name": inverter.name,
|
||||
"values": d.get("values", {}),
|
||||
"history": history,
|
||||
"sensors": [
|
||||
{"id": s.id, "name": s.name, "unit": s.unit,
|
||||
"icon": s.icon, "device_class": s.device_class}
|
||||
|
||||
Reference in New Issue
Block a user