Skip to content

Commit 287a3ca

Browse files
committed
Update / Modification from the production live system: 2026-03-13 00:30
1 parent 714d9d1 commit 287a3ca

1 file changed

Lines changed: 42 additions & 15 deletions

File tree

iobroker/50_Energie_Verbrauch/Haushaltsgeräte/haushaltsgeraete_monitor.js

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/**
2-
* Haushaltsgeräte-Überwachung PRO (v4.1.0 - Next Level)
2+
* Haushaltsgeräte-Überwachung PRO (v4.2.0 - Auto-Influx)
33
* - Prädiktive Zeitvorhersage (InfluxDB Durchschnitt)
44
* - Intelligentes Benachrichtigungs-Management (Quiet Hours & Reminder)
55
* - Anomalie-Erkennung (Sicherheits-Abschaltung/Alarm)
6-
* - Auto-State Creation & InfluxDB 2.x Support
6+
* - Auto-State Creation & AUTO-INFLUX CONFIGURATION
77
*/
88

99
(async function() {
@@ -72,7 +72,39 @@
7272
}
7373
];
7474

75-
// --- 3. AKTIONEN (Smart Notifications) ---
75+
// --- 3. HILFSFUNKTIONEN ---
76+
77+
// Automatische InfluxDB Aktivierung
78+
async function enableInfluxLogging(id) {
79+
try {
80+
const obj = await getObjectAsync(id);
81+
if (obj && obj.common) {
82+
const custom = obj.common.custom || {};
83+
if (!custom[INFLUXDB_INSTANCE] || !custom[INFLUXDB_INSTANCE].enabled) {
84+
log(`${id}: Aktiviere InfluxDB-Logging automatisch.`);
85+
await extendObjectAsync(id, {
86+
common: {
87+
custom: {
88+
[INFLUXDB_INSTANCE]: {
89+
enabled: true,
90+
changesOnly: true,
91+
debounce: 0,
92+
retention: 0,
93+
changesRelativ: 0,
94+
changesMinDelta: 0,
95+
storageType: "",
96+
aliasId: ""
97+
}
98+
}
99+
}
100+
});
101+
}
102+
}
103+
} catch (e) {
104+
log(`Fehler beim Aktivieren von InfluxDB für ${id}: ${e}`, 'warn');
105+
}
106+
}
107+
76108
function isQuietTime() {
77109
const now = new Date();
78110
const currentTime = now.getHours() * 60 + now.getMinutes();
@@ -89,8 +121,6 @@
89121
log("Nachtruhe aktiv: Benachrichtigung nur im Log.");
90122
return;
91123
}
92-
// Beispiel für Telegram Integration:
93-
// sendTo('telegram.0', { text: `*${title}*\n${message}`, parse_mode: 'Markdown' });
94124
}
95125

96126
// --- 4. SKRIPT-LOGIK ---
@@ -107,11 +137,9 @@
107137
CURRENT_ENERGY: `${SCRIPT_DP_PATH}.Aktueller_Verbrauch_kWh`,
108138
RUN_START_TIME: `${SCRIPT_DP_PATH}.Aktueller_Startzeitpunkt`,
109139
RUN_START_ENERGY: `${SCRIPT_DP_PATH}.Aktueller_Energie_Startwert`,
110-
// Next Level DPs
111140
PREDICTED_END: `${SCRIPT_DP_PATH}.Voraussichtliches_Ende`,
112141
REMAINING_TIME: `${SCRIPT_DP_PATH}.Restlaufzeit_Minuten`,
113142
ANOMALY_ALARM: `${SCRIPT_DP_PATH}.Anomalie_Alarm`,
114-
// Analyse
115143
ANALYSE_START: `${SCRIPT_DP_PATH}.Analyse.Starten`,
116144
ANALYSE_STATUS: `${SCRIPT_DP_PATH}.Analyse.Status`,
117145
VORSCHLAG_START: `${SCRIPT_DP_PATH}.Analyse.Vorschlag_startingThreshold`,
@@ -139,13 +167,17 @@
139167
await createStateAsync(DP.VORSCHLAG_FINISH, 0, { type: 'number', unit: 'W' });
140168
})();
141169

142-
// Durchschnittliche Laufzeit aus InfluxDB ermitteln
170+
// Automatisches Logging für Quellen und notwendige Statistik-DPs aktivieren
171+
await enableInfluxLogging(config.powerSensorId);
172+
await enableInfluxLogging(config.energySensorId);
173+
await enableInfluxLogging(DP.LAST_RUN_DURATION);
174+
143175
async function getAverageDuration() {
144176
return new Promise((resolve) => {
145177
const end = Date.now();
146178
const start = end - (30 * 24 * 60 * 60 * 1000);
147179
getHistory(INFLUXDB_INSTANCE, { id: DP.LAST_RUN_DURATION, start: start, end: end, aggregate: 'none' }, (err, result) => {
148-
if (err || !result || result.length === 0) resolve(120); // Fallback 2h
180+
if (err || !result || result.length === 0) resolve(120);
149181
const avg = result.reduce((a, b) => a + b.val, 0) / result.length;
150182
resolve(Math.round(avg));
151183
});
@@ -154,6 +186,7 @@
154186

155187
function updatePrediction(avgDuration) {
156188
const startTime = getState(DP.RUN_START_TIME).val;
189+
if (!startTime) return;
157190
const elapsed = (Date.now() - startTime) / 60000;
158191
const remaining = Math.max(0, Math.round(avgDuration - elapsed));
159192
const end = new Date(Date.now() + remaining * 60000);
@@ -177,12 +210,10 @@
177210
setState(DP.RUN_START_ENERGY, energyNow, true);
178211
setState(DP.ANOMALY_ALARM, false, true);
179212

180-
// Vorhersage starten
181213
const avg = await getAverageDuration();
182214
updatePrediction(avg);
183215
predictionInterval = setInterval(() => updatePrediction(avg), 60000);
184216

185-
// Sicherheits-Timer
186217
safetyTimer = setTimeout(() => {
187218
setState(DP.ANOMALY_ALARM, true, true);
188219
sendSmartNotification("ALARM", `${config.deviceName} läuft ungewöhnlich lange (> ${SAFETY_TIMEOUT_MIN} Min)! Bitte prüfen.`, 2);
@@ -208,7 +239,6 @@
208239

209240
sendSmartNotification(config.deviceName, `${config.deviceName} ist fertig! Dauer: ${durationMin.toFixed(0)} Min, Verbrauch: ${energyKWh.toFixed(2)} kWh.`, 1);
210241

211-
// Reminder-Timer starten
212242
reminderTimer = setInterval(() => {
213243
sendSmartNotification(config.deviceName, "Erinnerung: Die Wäsche liegt noch im Gerät.", 1);
214244
}, REMINDER_INTERVAL_MIN * 60000);
@@ -219,14 +249,12 @@
219249
const power = obj.state.val;
220250
const state = getState(DP.STATUS).val;
221251

222-
// Aktuellen Verbrauch während des Laufs berechnen
223252
if (state !== STATES.IDLE && state !== STATES.STARTING) {
224253
const startE = getState(DP.RUN_START_ENERGY).val;
225254
const currE = getState(config.energySensorId).val || 0;
226255
setState(DP.CURRENT_ENERGY, parseFloat((currE - startE).toFixed(3)), true);
227256
}
228257

229-
// Türöffnung / Entleerung erkennen (Power > 0.5W aber Status IDLE -> Reminder stoppen)
230258
if (power > 0.5 && state === STATES.IDLE && reminderTimer) {
231259
clearInterval(reminderTimer);
232260
reminderTimer = null;
@@ -250,7 +278,6 @@
250278
if (ns === STATES.PAUSED) pauseTimeout = setTimeout(() => setApplianceState(STATES.IDLE), runtimeConfig.pauseHysteresisMin * 60000);
251279
});
252280

253-
// Analyse bleibt wie im Original-Script (Histogramm-Methode)
254281
on({id: DP.ANALYSE_START, val: true}, () => {
255282
log(`Analyse für ${config.deviceName} gestartet...`);
256283
const end = Date.now();

0 commit comments

Comments
 (0)