iobroker.nexowatt-ui 0.7.28 → 0.7.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -192,6 +192,20 @@ class PeakShavingModule extends BaseModule {
192
192
  /** @type {Map<string, {mode:string, phases:number, baseline:number|null, baselineEnabled:boolean|null}>} */
193
193
  this._baselines = new Map();
194
194
  this._wasActive = false;
195
+
196
+ // Atypische Nachkontrolle: laufende Jahres-/HLZF-Maxima werden über States persistiert.
197
+ this._atypicalReviewLoaded = false;
198
+ this._atypicalReviewState = {
199
+ resetIdentity: '',
200
+ pAbsMaxW: 0,
201
+ pHlzfMaxW: 0,
202
+ lastUpdate: 0,
203
+ };
204
+
205
+ // Nachweis/Audit: throttled samples for Influx/CSV/PDF export.
206
+ this._atypicalAuditLastSampleMs = 0;
207
+ this._atypicalAuditLastReason = '';
208
+ this._atypicalAuditLastHistorySetupMs = 0;
195
209
  }
196
210
 
197
211
  _isEnabled() {
@@ -208,7 +222,7 @@ class PeakShavingModule extends BaseModule {
208
222
  native: {},
209
223
  });
210
224
 
211
- for (const ch of ['measure', 'calc', 'control', 'dynamic', 'actuators', 'atypical']) {
225
+ for (const ch of ['measure', 'calc', 'control', 'dynamic', 'actuators', 'atypical', 'atypical.review', 'atypical.audit']) {
212
226
  await this.adapter.setObjectNotExistsAsync(`peakShaving.${ch}`, {
213
227
  type: 'channel',
214
228
  common: { name: ch },
@@ -265,7 +279,54 @@ class PeakShavingModule extends BaseModule {
265
279
  await mk('peakShaving.atypical.deltaPercent', 'Verlagerung gegenüber P_abs_ref (%)', 'number', 'value');
266
280
  await mk('peakShaving.atypical.eligiblePotential', '§19-Potenzial erfüllt Zielparameter', 'boolean', 'indicator');
267
281
  await mk('peakShaving.atypical.binding', 'Bindendes Limit / Quelle', 'string', 'text');
282
+ await mk('peakShaving.atypical.gridOperator', 'Netzbetreiber Hochlastzeitfenster', 'string', 'text');
283
+ await mk('peakShaving.atypical.year', 'Gültigkeitsjahr Hochlastzeitfenster', 'number', 'value');
284
+ await mk('peakShaving.atypical.sourceDocument', 'HLZF Quelle / Dokument', 'string', 'text');
285
+ await mk('peakShaving.atypical.sourcePublishedAt', 'HLZF Veröffentlichungsdatum', 'string', 'text');
286
+ await mk('peakShaving.atypical.sourceUrl', 'HLZF Quell-URL / Ablage', 'string', 'text');
287
+ await mk('peakShaving.atypical.sourceNote', 'HLZF Bemerkung', 'string', 'text');
268
288
  await mk('peakShaving.atypical.snapshotJson', 'Atypische Spitzenkappung Snapshot (JSON)', 'string', 'text');
289
+
290
+ await mk('peakShaving.atypical.review.enabled', 'Nachkontrolle aktiv', 'boolean', 'indicator');
291
+ await mk('peakShaving.atypical.review.status', 'Nachkontrolle Status', 'string', 'text');
292
+ await mk('peakShaving.atypical.review.year', 'Nachkontrolle Jahr', 'number', 'value');
293
+ await mk('peakShaving.atypical.review.resetIdentity', 'Nachkontrolle Reset-Identität', 'string', 'text');
294
+ await mk('peakShaving.atypical.review.pAbsMaxW', 'Gemessene Jahreshöchstlast P_abs_max (W)', 'number', 'value.power');
295
+ await mk('peakShaving.atypical.review.pHlzfMaxW', 'Gemessene HLZF-Höchstlast P_HLZF_max (W)', 'number', 'value.power');
296
+ await mk('peakShaving.atypical.review.pAbsEvalW', 'Bewertete Jahreshöchstlast P_abs (W)', 'number', 'value.power');
297
+ await mk('peakShaving.atypical.review.pHlzfEvalW', 'Bewertete HLZF-Höchstlast P_HLZF (W)', 'number', 'value.power');
298
+ await mk('peakShaving.atypical.review.deltaW', 'Nachkontrolle Verlagerung (W)', 'number', 'value.power');
299
+ await mk('peakShaving.atypical.review.deltaPercent', 'Nachkontrolle Verlagerung (%)', 'number', 'value');
300
+ await mk('peakShaving.atypical.review.thresholdOk', 'Erheblichkeitsschwelle erfüllt', 'boolean', 'indicator');
301
+ await mk('peakShaving.atypical.review.minShiftOk', 'Mindestverlagerung erfüllt', 'boolean', 'indicator');
302
+ await mk('peakShaving.atypical.review.savingsEur', 'Geschätzte Ersparnis (€)', 'number', 'value');
303
+ await mk('peakShaving.atypical.review.savingsOk', 'Bagatellgrenze erfüllt', 'boolean', 'indicator');
304
+ await mk('peakShaving.atypical.review.eligible', 'Nachkontrolle erfüllt', 'boolean', 'indicator');
305
+ await mk('peakShaving.atypical.review.lastUpdate', 'Nachkontrolle letzte Aktualisierung', 'number', 'value.time');
306
+ await mk('peakShaving.atypical.review.snapshotJson', 'Nachkontrolle Snapshot (JSON)', 'string', 'text');
307
+
308
+ await mk('peakShaving.atypical.audit.lastSampleTs', 'Nachweis letzter Influx-Sample-Zeitpunkt', 'number', 'value.time');
309
+ await mk('peakShaving.atypical.audit.gridImportW', 'Nachweis Netzbezug Sample (W)', 'number', 'value.power');
310
+ await mk('peakShaving.atypical.audit.activeWindow', 'Nachweis HLZF aktiv', 'boolean', 'indicator');
311
+ await mk('peakShaving.atypical.audit.windowLabel', 'Nachweis Hochlastzeitfenster', 'string', 'text');
312
+ await mk('peakShaving.atypical.audit.effectiveLimitW', 'Nachweis wirksames Limit (W)', 'number', 'value.power');
313
+ await mk('peakShaving.atypical.audit.pAbsMaxW', 'Nachweis P_abs_max laufend (W)', 'number', 'value.power');
314
+ await mk('peakShaving.atypical.audit.pHlzfMaxW', 'Nachweis P_HLZF_max laufend (W)', 'number', 'value.power');
315
+ await mk('peakShaving.atypical.audit.deltaW', 'Nachweis Verlagerung (W)', 'number', 'value.power');
316
+ await mk('peakShaving.atypical.audit.deltaPercent', 'Nachweis Verlagerung (%)', 'number', 'value');
317
+ await mk('peakShaving.atypical.audit.thresholdOk', 'Nachweis Erheblichkeit erfüllt', 'boolean', 'indicator');
318
+ await mk('peakShaving.atypical.audit.minShiftOk', 'Nachweis Mindestverlagerung erfüllt', 'boolean', 'indicator');
319
+ await mk('peakShaving.atypical.audit.savingsEur', 'Nachweis Ersparnis (€)', 'number', 'value');
320
+ await mk('peakShaving.atypical.audit.eligible', 'Nachweis §19 erfüllt', 'boolean', 'indicator');
321
+ await mk('peakShaving.atypical.audit.sampleReason', 'Nachweis Sample-Grund', 'string', 'text');
322
+ await mk('peakShaving.atypical.audit.snapshotJson', 'Nachweis Audit-Snapshot (JSON)', 'string', 'text');
323
+ await mk('peakShaving.atypical.audit.historyEnabled', 'Nachweis Historie/Influx aktiv', 'boolean', 'indicator');
324
+ await mk('peakShaving.atypical.audit.historyInstance', 'Nachweis Historien-Instanz', 'string', 'text');
325
+ await mk('peakShaving.atypical.audit.historyProvisionState', 'Nachweis Historienstatus', 'string', 'text');
326
+ await mk('peakShaving.atypical.audit.historyProvisionError', 'Nachweis Historienhinweis/Fehler', 'string', 'text');
327
+ await mk('peakShaving.atypical.audit.retentionTargetDays', 'Nachweis Retention-Ziel (Tage)', 'number', 'value.interval');
328
+
329
+ try { await this._setupAtypicalAuditHistory(); } catch { /* optional */ }
269
330
  }
270
331
 
271
332
 
@@ -433,10 +494,331 @@ class PeakShavingModule extends BaseModule {
433
494
  };
434
495
  }
435
496
 
497
+ _getAtypicalSourceInfo(atypicalCfg) {
498
+ const cfg = (atypicalCfg && typeof atypicalCfg === 'object') ? atypicalCfg : {};
499
+ const yearRaw = Number(cfg.year ?? cfg.validityYear ?? cfg.calendarYear ?? 0);
500
+ return {
501
+ gridOperator: String(cfg.gridOperatorName || cfg.gridOperator || cfg.networkOperator || cfg.netzbetreiber || '').trim(),
502
+ year: Number.isFinite(yearRaw) && yearRaw > 0 ? Math.round(yearRaw) : 0,
503
+ sourceDocument: String(cfg.sourceDocument || cfg.sourceName || cfg.source || '').trim(),
504
+ sourcePublishedAt: String(cfg.sourcePublishedAt || cfg.publishedAt || cfg.publicationDate || '').trim(),
505
+ sourceUrl: String(cfg.sourceUrl || cfg.sourceLink || cfg.sourcePath || '').trim(),
506
+ sourceNote: String(cfg.sourceNote || cfg.note || cfg.notes || '').trim(),
507
+ };
508
+ }
509
+
510
+ async _readOwnNumber(id, fallback = 0) {
511
+ try {
512
+ const st = await this.adapter.getStateAsync(id);
513
+ const n = st ? Number(st.val) : NaN;
514
+ return Number.isFinite(n) ? n : fallback;
515
+ } catch {
516
+ return fallback;
517
+ }
518
+ }
519
+
520
+ async _readOwnString(id, fallback = '') {
521
+ try {
522
+ const st = await this.adapter.getStateAsync(id);
523
+ if (!st || st.val === null || st.val === undefined) return fallback;
524
+ return String(st.val);
525
+ } catch {
526
+ return fallback;
527
+ }
528
+ }
529
+
530
+ async _ensureAtypicalReviewLoaded(resetIdentity) {
531
+ const identity = String(resetIdentity || '');
532
+ const st = this._atypicalReviewState || { resetIdentity: '', pAbsMaxW: 0, pHlzfMaxW: 0, lastUpdate: 0 };
533
+ if (this._atypicalReviewLoaded && st.resetIdentity === identity) return;
534
+
535
+ const persistedIdentity = await this._readOwnString('peakShaving.atypical.review.resetIdentity', '');
536
+ if (persistedIdentity && persistedIdentity === identity) {
537
+ st.resetIdentity = persistedIdentity;
538
+ st.pAbsMaxW = Math.max(0, await this._readOwnNumber('peakShaving.atypical.review.pAbsMaxW', 0));
539
+ st.pHlzfMaxW = Math.max(0, await this._readOwnNumber('peakShaving.atypical.review.pHlzfMaxW', 0));
540
+ st.lastUpdate = Math.max(0, await this._readOwnNumber('peakShaving.atypical.review.lastUpdate', 0));
541
+ } else {
542
+ st.resetIdentity = identity;
543
+ st.pAbsMaxW = 0;
544
+ st.pHlzfMaxW = 0;
545
+ st.lastUpdate = 0;
546
+ }
547
+ this._atypicalReviewState = st;
548
+ this._atypicalReviewLoaded = true;
549
+ }
550
+
551
+ async _buildAtypicalReviewContext({ atypicalCfg, schedule, target, gridPowerRaw, effPower, staleMeter, now }) {
552
+ const cfg = (atypicalCfg && typeof atypicalCfg === 'object') ? atypicalCfg : {};
553
+ const review = (cfg.review && typeof cfg.review === 'object') ? cfg.review : ((cfg.nachkontrolle && typeof cfg.nachkontrolle === 'object') ? cfg.nachkontrolle : {});
554
+ const source = this._getAtypicalSourceInfo(cfg);
555
+ const year = Math.round(num(review.year ?? source.year, (new Date(now || Date.now())).getFullYear()) || (new Date(now || Date.now())).getFullYear());
556
+ const resetToken = String(review.resetToken || review.resetKey || '').trim();
557
+ const resetIdentity = `${year}|${resetToken}`;
558
+ const enabled = cfg.enabled === true && review.enabled !== false;
559
+
560
+ await this._ensureAtypicalReviewLoaded(resetIdentity);
561
+ const st = this._atypicalReviewState || { resetIdentity, pAbsMaxW: 0, pHlzfMaxW: 0, lastUpdate: 0 };
562
+ st.resetIdentity = resetIdentity;
563
+
564
+ const rawW = Number.isFinite(Number(gridPowerRaw)) ? Number(gridPowerRaw) : (Number.isFinite(Number(effPower)) ? Number(effPower) : null);
565
+ const importW = (rawW !== null && Number.isFinite(rawW)) ? Math.max(0, rawW) : null;
566
+ const measurementUsable = enabled && !staleMeter && importW !== null;
567
+
568
+ if (measurementUsable) {
569
+ if (importW > st.pAbsMaxW) st.pAbsMaxW = Math.round(importW);
570
+ if (schedule && schedule.active && importW > st.pHlzfMaxW) st.pHlzfMaxW = Math.round(importW);
571
+ st.lastUpdate = now || Date.now();
572
+ }
573
+
574
+ const manualPAbs = num(review.pAbsActualW ?? review.actualAnnualPeakW ?? review.pAbsIstW, null);
575
+ const manualHlzf = num(review.pHlzfMaxW ?? review.hlzfPeakW ?? review.pHlzfIstW, null);
576
+ const pAbsEvalW = (typeof manualPAbs === 'number' && manualPAbs > 0) ? manualPAbs : st.pAbsMaxW;
577
+ const pHlzfEvalW = (typeof manualHlzf === 'number' && manualHlzf >= 0) ? manualHlzf : st.pHlzfMaxW;
578
+
579
+ const thresholdPercent = Math.max(0, num(target && target.thresholdPercent, atypicalThresholdPercent(target && target.voltageLevel, 20)) || 0);
580
+ const minShiftW = Math.max(0, num(target && target.minShiftW, 100000) || 0);
581
+ const complete = pAbsEvalW > 0 && pHlzfEvalW >= 0;
582
+ const deltaW = complete ? Math.max(0, pAbsEvalW - pHlzfEvalW) : 0;
583
+ const deltaPercent = complete ? (deltaW / pAbsEvalW * 100) : 0;
584
+ const thresholdOk = complete && deltaPercent + 1e-9 >= thresholdPercent;
585
+ const minShiftOk = complete && deltaW + 1e-9 >= minShiftW;
586
+
587
+ const powerPrice = num(review.powerPriceEurPerKwYear ?? review.powerPriceEurPerKwA ?? review.leistungspreisEurProKwJahr, null);
588
+ const bagatelleEur = Math.max(0, num(review.savingsBagatelleEur ?? review.bagatelleEur ?? 500, 500) || 0);
589
+ const generalGridFeeEur = num(review.generalGridFeeEur ?? review.generalGridFee ?? review.allgemeinesNetzentgeltEur, null);
590
+ const maxReductionPercent = Math.min(100, Math.max(0, num(review.maxReductionPercent ?? 80, 80) || 0));
591
+ const grossSavingsEur = (complete && typeof powerPrice === 'number' && powerPrice > 0) ? ((deltaW / 1000) * powerPrice) : null;
592
+ const reductionCapEur = (grossSavingsEur !== null && typeof generalGridFeeEur === 'number' && generalGridFeeEur > 0) ? (generalGridFeeEur * maxReductionPercent / 100) : null;
593
+ const savingsEur = grossSavingsEur === null ? null : (reductionCapEur === null ? grossSavingsEur : Math.min(grossSavingsEur, reductionCapEur));
594
+ const savingsOk = savingsEur === null ? null : savingsEur + 1e-9 >= bagatelleEur;
595
+ const technicalEligible = complete && thresholdOk && minShiftOk;
596
+ const eligible = enabled && technicalEligible && (savingsOk === null ? true : savingsOk);
597
+
598
+ let status = 'disabled';
599
+ if (enabled) {
600
+ if (!complete) status = 'collecting';
601
+ else if (!thresholdOk) status = 'threshold-missing';
602
+ else if (!minShiftOk) status = 'min-shift-missing';
603
+ else if (savingsOk === false) status = 'bagatelle-missing';
604
+ else if (savingsOk === null) status = 'technical-ok-savings-open';
605
+ else status = 'eligible';
606
+ }
607
+
608
+ return {
609
+ enabled,
610
+ status,
611
+ year,
612
+ resetIdentity,
613
+ measurementUsable,
614
+ influxLogEnabled: review.influxLogEnabled !== false,
615
+ auditIntervalMinutes: Math.max(1, Math.min(1440, Math.round(num(review.auditIntervalMinutes ?? review.influxSampleIntervalMinutes ?? cfg.auditIntervalMinutes ?? 15, 15) || 15))),
616
+ pAbsMaxW: st.pAbsMaxW,
617
+ pHlzfMaxW: st.pHlzfMaxW,
618
+ pAbsEvalW,
619
+ pHlzfEvalW,
620
+ manualPAbsW: (typeof manualPAbs === 'number' && manualPAbs > 0) ? manualPAbs : null,
621
+ manualHlzfW: (typeof manualHlzf === 'number' && manualHlzf >= 0) ? manualHlzf : null,
622
+ thresholdPercent,
623
+ minShiftW,
624
+ deltaW,
625
+ deltaPercent,
626
+ thresholdOk,
627
+ minShiftOk,
628
+ powerPriceEurPerKwYear: typeof powerPrice === 'number' ? powerPrice : null,
629
+ bagatelleEur,
630
+ grossSavingsEur,
631
+ reductionCapEur,
632
+ savingsEur,
633
+ savingsOk,
634
+ technicalEligible,
635
+ eligible,
636
+ lastUpdate: st.lastUpdate || 0,
637
+ };
638
+ }
639
+
640
+ async _setupAtypicalAuditHistory() {
641
+ let historyInstance = '';
642
+ let provisionState = 'missing';
643
+ let provisionError = '';
644
+
645
+ try {
646
+ if (this.adapter && typeof this.adapter._nwDetectInfluxInstance === 'function') {
647
+ const detected = await this.adapter._nwDetectInfluxInstance();
648
+ if (detected) provisionState = 'detected';
649
+ }
650
+ } catch (e) {
651
+ provisionState = 'detect_error';
652
+ provisionError = String(e && e.message ? e.message : e);
653
+ }
654
+
655
+ try {
656
+ if (this.adapter && typeof this.adapter._nwGetHistoryInstance === 'function') {
657
+ historyInstance = String(this.adapter._nwGetHistoryInstance() || '').trim();
658
+ if (historyInstance && provisionState === 'missing') provisionState = 'configured_or_default';
659
+ }
660
+ } catch (e) {
661
+ historyInstance = '';
662
+ provisionState = 'history_instance_error';
663
+ provisionError = String(e && e.message ? e.message : e);
664
+ }
665
+
666
+ const historyReady = !!historyInstance && !!(this.adapter && typeof this.adapter._nwEnsureInfluxCustom === 'function');
667
+ const set = async (id, val) => {
668
+ try { await this.adapter.setStateAsync(id, val, true); } catch { /* optional */ }
669
+ };
670
+
671
+ await set('peakShaving.atypical.audit.historyEnabled', historyReady);
672
+ await set('peakShaving.atypical.audit.historyInstance', historyInstance);
673
+ await set('peakShaving.atypical.audit.historyProvisionState', historyReady ? provisionState : (provisionState || 'missing'));
674
+ await set('peakShaving.atypical.audit.historyProvisionError', provisionError || '');
675
+ await set('peakShaving.atypical.audit.retentionTargetDays', 730);
676
+
677
+ if (!historyReady) return;
678
+
679
+ const metaIds = [
680
+ 'peakShaving.atypical.audit.historyEnabled',
681
+ 'peakShaving.atypical.audit.historyInstance',
682
+ 'peakShaving.atypical.audit.historyProvisionState',
683
+ 'peakShaving.atypical.audit.historyProvisionError',
684
+ 'peakShaving.atypical.audit.retentionTargetDays',
685
+ ];
686
+ const auditIds = [
687
+ 'peakShaving.atypical.audit.lastSampleTs',
688
+ 'peakShaving.atypical.audit.gridImportW',
689
+ 'peakShaving.atypical.audit.activeWindow',
690
+ 'peakShaving.atypical.audit.windowLabel',
691
+ 'peakShaving.atypical.audit.effectiveLimitW',
692
+ 'peakShaving.atypical.audit.pAbsMaxW',
693
+ 'peakShaving.atypical.audit.pHlzfMaxW',
694
+ 'peakShaving.atypical.audit.deltaW',
695
+ 'peakShaving.atypical.audit.deltaPercent',
696
+ 'peakShaving.atypical.audit.thresholdOk',
697
+ 'peakShaving.atypical.audit.minShiftOk',
698
+ 'peakShaving.atypical.audit.savingsEur',
699
+ 'peakShaving.atypical.audit.eligible',
700
+ 'peakShaving.atypical.audit.sampleReason',
701
+ 'peakShaving.atypical.audit.snapshotJson',
702
+ ];
703
+ const reviewIds = [
704
+ 'peakShaving.atypical.review.status',
705
+ 'peakShaving.atypical.review.year',
706
+ 'peakShaving.atypical.review.pAbsMaxW',
707
+ 'peakShaving.atypical.review.pHlzfMaxW',
708
+ 'peakShaving.atypical.review.pAbsEvalW',
709
+ 'peakShaving.atypical.review.pHlzfEvalW',
710
+ 'peakShaving.atypical.review.deltaW',
711
+ 'peakShaving.atypical.review.deltaPercent',
712
+ 'peakShaving.atypical.review.thresholdOk',
713
+ 'peakShaving.atypical.review.minShiftOk',
714
+ 'peakShaving.atypical.review.savingsEur',
715
+ 'peakShaving.atypical.review.savingsOk',
716
+ 'peakShaving.atypical.review.eligible',
717
+ ];
718
+
719
+ for (const id of metaIds) {
720
+ await this.adapter._nwEnsureInfluxCustom(id, historyInstance, { changesOnly: true });
721
+ }
722
+ // Audit samples are deliberately logged per write so the CSV/PDF proof can reconstruct
723
+ // a time series even if values stay constant over a longer period.
724
+ for (const id of auditIds) {
725
+ await this.adapter._nwEnsureInfluxCustom(id, historyInstance, { changesOnly: false });
726
+ }
727
+ // Review states are maxima/status values; logging only changes keeps Influx lean.
728
+ for (const id of reviewIds) {
729
+ await this.adapter._nwEnsureInfluxCustom(id, historyInstance, { changesOnly: true });
730
+ }
731
+ }
732
+
733
+ _getAtypicalAuditIntervalMs(atypicalCfg) {
734
+ const cfg = (atypicalCfg && typeof atypicalCfg === 'object') ? atypicalCfg : {};
735
+ const review = (cfg.review && typeof cfg.review === 'object') ? cfg.review : ((cfg.nachkontrolle && typeof cfg.nachkontrolle === 'object') ? cfg.nachkontrolle : {});
736
+ const minutes = num(review.auditIntervalMinutes ?? review.influxSampleIntervalMinutes ?? cfg.auditIntervalMinutes ?? 15, 15);
737
+ const clamped = clamp(Number(minutes), 1, 1440);
738
+ return Math.round((Number.isFinite(clamped) ? clamped : 15) * 60 * 1000);
739
+ }
740
+
741
+ async _publishAtypicalAuditSample(ctx) {
742
+ const c = (ctx && typeof ctx === 'object') ? ctx : {};
743
+ const review = (c.review && typeof c.review === 'object') ? c.review : {};
744
+ const cfg = (c.atypicalCfg && typeof c.atypicalCfg === 'object') ? c.atypicalCfg : {};
745
+ const reviewCfg = (cfg.review && typeof cfg.review === 'object') ? cfg.review : ((cfg.nachkontrolle && typeof cfg.nachkontrolle === 'object') ? cfg.nachkontrolle : {});
746
+ if (!review.enabled || reviewCfg.influxLogEnabled === false) return;
747
+
748
+ const now = Number.isFinite(Number(c.now)) ? Math.round(Number(c.now)) : Date.now();
749
+ const intervalMs = this._getAtypicalAuditIntervalMs(c.atypicalCfg || {});
750
+ const schedule = (c.schedule && typeof c.schedule === 'object') ? c.schedule : {};
751
+ const activeWindow = !!schedule.active;
752
+ const status = String(review.status || '');
753
+ const reasonSig = `${activeWindow ? 1 : 0}|${status}|${String(c.binding || '')}|${review.eligible ? 1 : 0}`;
754
+ const due = !this._atypicalAuditLastSampleMs || (now - this._atypicalAuditLastSampleMs) >= intervalMs;
755
+ const changedImportant = this._atypicalAuditLastReason && this._atypicalAuditLastReason !== reasonSig;
756
+ if (!due && !changedImportant) return;
757
+
758
+ this._atypicalAuditLastSampleMs = now;
759
+ this._atypicalAuditLastReason = reasonSig;
760
+
761
+ const rawW = Number.isFinite(Number(c.gridPowerRaw)) ? Number(c.gridPowerRaw) : (Number.isFinite(Number(c.effPower)) ? Number(c.effPower) : 0);
762
+ const gridImportW = Math.max(0, rawW);
763
+ const set = async (id, val) => {
764
+ try { await this.adapter.setStateAsync(id, val, true); } catch { /* audit only */ }
765
+ };
766
+ const sampleReason = due ? 'interval' : 'state-change';
767
+ const source = (c.source && typeof c.source === 'object') ? c.source : {};
768
+ const target = (c.target && typeof c.target === 'object') ? c.target : {};
769
+ const snapshot = {
770
+ ts: now,
771
+ sampleReason,
772
+ gridImportW: Math.round(gridImportW),
773
+ activeWindow,
774
+ windowLabel: String(schedule.windowLabel || ''),
775
+ effectiveLimitW: Number.isFinite(Number(c.effectiveLimitW)) ? Math.round(Number(c.effectiveLimitW)) : 0,
776
+ binding: String(c.binding || ''),
777
+ status,
778
+ gridOperator: String(source.gridOperator || ''),
779
+ sourceDocument: String(source.sourceDocument || ''),
780
+ sourcePublishedAt: String(source.sourcePublishedAt || ''),
781
+ sourceUrl: String(source.sourceUrl || ''),
782
+ voltageLevel: String(target.voltageLevel || ''),
783
+ thresholdPercent: Number.isFinite(Number(target.thresholdPercent)) ? Number(target.thresholdPercent) : (Number.isFinite(Number(review.thresholdPercent)) ? Number(review.thresholdPercent) : 0),
784
+ minShiftW: Number.isFinite(Number(target.minShiftW)) ? Math.round(Number(target.minShiftW)) : (Number.isFinite(Number(review.minShiftW)) ? Math.round(Number(review.minShiftW)) : 0),
785
+ year: Number.isFinite(Number(review.year)) ? Math.round(Number(review.year)) : 0,
786
+ pAbsMaxW: Number.isFinite(Number(review.pAbsMaxW)) ? Math.round(Number(review.pAbsMaxW)) : 0,
787
+ pHlzfMaxW: Number.isFinite(Number(review.pHlzfMaxW)) ? Math.round(Number(review.pHlzfMaxW)) : 0,
788
+ pAbsEvalW: Number.isFinite(Number(review.pAbsEvalW)) ? Math.round(Number(review.pAbsEvalW)) : 0,
789
+ pHlzfEvalW: Number.isFinite(Number(review.pHlzfEvalW)) ? Math.round(Number(review.pHlzfEvalW)) : 0,
790
+ deltaW: Number.isFinite(Number(review.deltaW)) ? Math.round(Number(review.deltaW)) : 0,
791
+ deltaPercent: Number.isFinite(Number(review.deltaPercent)) ? Math.round(Number(review.deltaPercent) * 100) / 100 : 0,
792
+ thresholdOk: !!review.thresholdOk,
793
+ minShiftOk: !!review.minShiftOk,
794
+ savingsEur: Number.isFinite(Number(review.savingsEur)) ? Math.round(Number(review.savingsEur) * 100) / 100 : null,
795
+ savingsOk: review.savingsOk === null ? null : review.savingsOk === true,
796
+ eligible: !!review.eligible,
797
+ };
798
+
799
+ await set('peakShaving.atypical.audit.lastSampleTs', now);
800
+ await set('peakShaving.atypical.audit.gridImportW', snapshot.gridImportW);
801
+ await set('peakShaving.atypical.audit.activeWindow', activeWindow);
802
+ await set('peakShaving.atypical.audit.windowLabel', snapshot.windowLabel);
803
+ await set('peakShaving.atypical.audit.effectiveLimitW', snapshot.effectiveLimitW);
804
+ await set('peakShaving.atypical.audit.pAbsMaxW', snapshot.pAbsMaxW);
805
+ await set('peakShaving.atypical.audit.pHlzfMaxW', snapshot.pHlzfMaxW);
806
+ await set('peakShaving.atypical.audit.deltaW', snapshot.deltaW);
807
+ await set('peakShaving.atypical.audit.deltaPercent', snapshot.deltaPercent);
808
+ await set('peakShaving.atypical.audit.thresholdOk', snapshot.thresholdOk);
809
+ await set('peakShaving.atypical.audit.minShiftOk', snapshot.minShiftOk);
810
+ await set('peakShaving.atypical.audit.savingsEur', snapshot.savingsEur === null ? 0 : snapshot.savingsEur);
811
+ await set('peakShaving.atypical.audit.eligible', snapshot.eligible);
812
+ await set('peakShaving.atypical.audit.sampleReason', sampleReason);
813
+ await set('peakShaving.atypical.audit.snapshotJson', JSON.stringify(snapshot));
814
+ }
815
+
436
816
  async _publishAtypicalDiagnostics(ctx) {
437
817
  const c = (ctx && typeof ctx === 'object') ? ctx : {};
438
818
  const schedule = (c.schedule && typeof c.schedule === 'object') ? c.schedule : {};
439
819
  const target = (c.target && typeof c.target === 'object') ? c.target : {};
820
+ const source = (c.source && typeof c.source === 'object') ? c.source : {};
821
+ const review = (c.review && typeof c.review === 'object') ? c.review : {};
440
822
  const set = async (id, val) => {
441
823
  try { await this.adapter.setStateAsync(id, val, true); } catch { /* diagnostics only */ }
442
824
  };
@@ -456,6 +838,31 @@ class PeakShavingModule extends BaseModule {
456
838
  await set('peakShaving.atypical.deltaPercent', Number.isFinite(Number(target.deltaPercent)) ? Math.round(Number(target.deltaPercent) * 100) / 100 : 0);
457
839
  await set('peakShaving.atypical.eligiblePotential', !!target.eligiblePotential);
458
840
  await set('peakShaving.atypical.binding', String(c.binding || ''));
841
+ await set('peakShaving.atypical.gridOperator', String(source.gridOperator || ''));
842
+ await set('peakShaving.atypical.year', Number.isFinite(Number(source.year)) ? Math.round(Number(source.year)) : 0);
843
+ await set('peakShaving.atypical.sourceDocument', String(source.sourceDocument || ''));
844
+ await set('peakShaving.atypical.sourcePublishedAt', String(source.sourcePublishedAt || ''));
845
+ await set('peakShaving.atypical.sourceUrl', String(source.sourceUrl || ''));
846
+ await set('peakShaving.atypical.sourceNote', String(source.sourceNote || ''));
847
+
848
+ await set('peakShaving.atypical.review.enabled', !!review.enabled);
849
+ await set('peakShaving.atypical.review.status', String(review.status || ''));
850
+ await set('peakShaving.atypical.review.year', Number.isFinite(Number(review.year)) ? Math.round(Number(review.year)) : 0);
851
+ await set('peakShaving.atypical.review.resetIdentity', String(review.resetIdentity || ''));
852
+ await set('peakShaving.atypical.review.pAbsMaxW', Number.isFinite(Number(review.pAbsMaxW)) ? Math.round(Number(review.pAbsMaxW)) : 0);
853
+ await set('peakShaving.atypical.review.pHlzfMaxW', Number.isFinite(Number(review.pHlzfMaxW)) ? Math.round(Number(review.pHlzfMaxW)) : 0);
854
+ await set('peakShaving.atypical.review.pAbsEvalW', Number.isFinite(Number(review.pAbsEvalW)) ? Math.round(Number(review.pAbsEvalW)) : 0);
855
+ await set('peakShaving.atypical.review.pHlzfEvalW', Number.isFinite(Number(review.pHlzfEvalW)) ? Math.round(Number(review.pHlzfEvalW)) : 0);
856
+ await set('peakShaving.atypical.review.deltaW', Number.isFinite(Number(review.deltaW)) ? Math.round(Number(review.deltaW)) : 0);
857
+ await set('peakShaving.atypical.review.deltaPercent', Number.isFinite(Number(review.deltaPercent)) ? Math.round(Number(review.deltaPercent) * 100) / 100 : 0);
858
+ await set('peakShaving.atypical.review.thresholdOk', !!review.thresholdOk);
859
+ await set('peakShaving.atypical.review.minShiftOk', !!review.minShiftOk);
860
+ await set('peakShaving.atypical.review.savingsEur', Number.isFinite(Number(review.savingsEur)) ? Math.round(Number(review.savingsEur) * 100) / 100 : 0);
861
+ await set('peakShaving.atypical.review.savingsOk', review.savingsOk === true);
862
+ await set('peakShaving.atypical.review.eligible', !!review.eligible);
863
+ await set('peakShaving.atypical.review.lastUpdate', Number.isFinite(Number(review.lastUpdate)) ? Math.round(Number(review.lastUpdate)) : 0);
864
+ await set('peakShaving.atypical.review.snapshotJson', JSON.stringify(review || {}));
865
+
459
866
  await set('peakShaving.atypical.snapshotJson', JSON.stringify({
460
867
  ts: Date.now(),
461
868
  mode: String(c.mode || ''),
@@ -463,10 +870,21 @@ class PeakShavingModule extends BaseModule {
463
870
  enforce: !!c.enforce,
464
871
  schedule,
465
872
  target,
873
+ source,
874
+ review,
466
875
  limitCandidateW: Number.isFinite(Number(c.limitCandidateW)) ? Math.round(Number(c.limitCandidateW)) : 0,
467
876
  effectiveLimitW: Number.isFinite(Number(c.effectiveLimitW)) ? Math.round(Number(c.effectiveLimitW)) : 0,
468
877
  binding: String(c.binding || ''),
469
878
  }));
879
+
880
+ try {
881
+ const nowSetup = Date.now();
882
+ if (!this._atypicalAuditLastHistorySetupMs || (nowSetup - this._atypicalAuditLastHistorySetupMs) >= 5 * 60 * 1000) {
883
+ this._atypicalAuditLastHistorySetupMs = nowSetup;
884
+ await this._setupAtypicalAuditHistory();
885
+ }
886
+ } catch { /* optional */ }
887
+ await this._publishAtypicalAuditSample(c);
470
888
  }
471
889
 
472
890
  async tick() {
@@ -677,12 +1095,29 @@ class PeakShavingModule extends BaseModule {
677
1095
  limitW = Math.max(0, limitW - Math.max(0, safetyMarginW));
678
1096
  }
679
1097
 
1098
+ const atypicalSource = this._getAtypicalSourceInfo(atypicalCfg);
1099
+ const atypicalReview = await this._buildAtypicalReviewContext({
1100
+ atypicalCfg: { ...atypicalCfg, enabled: atypicalRuntimeEnabled },
1101
+ schedule: atypicalSchedule,
1102
+ target: atypicalTarget,
1103
+ gridPowerRaw,
1104
+ effPower,
1105
+ staleMeter,
1106
+ now,
1107
+ });
1108
+
680
1109
  await this._publishAtypicalDiagnostics({
681
1110
  enabled: !!atypicalRuntimeEnabled,
682
1111
  mode: atypicalMode,
683
1112
  enforce: atypicalEnforce,
684
1113
  schedule: atypicalSchedule,
685
1114
  target: atypicalTarget,
1115
+ source: atypicalSource,
1116
+ review: atypicalReview,
1117
+ atypicalCfg: { ...atypicalCfg, enabled: atypicalRuntimeEnabled },
1118
+ now,
1119
+ gridPowerRaw,
1120
+ effPower,
686
1121
  limitCandidateW: atypicalLimitCandidateW,
687
1122
  effectiveLimitW: atypicalWantsPowerLimit ? limitW : 0,
688
1123
  binding: atypicalWantsPowerLimit ? (limitSource || 'atypical') : '',
package/io-package.json CHANGED
@@ -1,8 +1,16 @@
1
1
  {
2
2
  "common": {
3
3
  "name": "nexowatt-ui",
4
- "version": "0.7.28",
4
+ "version": "0.7.30",
5
5
  "news": {
6
+ "0.7.30": {
7
+ "en": "Atypical peak-shaving after-control proof export added. The adapter now mirrors §19 HLZF review values into canonical Influx history states under historie.peakShaving.atypical.* and provides App-Center CSV/PDF exports for the current proof and Influx time series.",
8
+ "de": "Nachweisexport für atypische Lastspitzenkappung ergänzt. Der Adapter spiegelt die §19-HLZF-Nachkontrollwerte jetzt in kanonische Influx-Historie-States unter historie.peakShaving.atypical.* und bietet im App-Center CSV-/PDF-Exporte für aktuelle Prüfung und Influx-Zeitreihe."
9
+ },
10
+ "0.7.29": {
11
+ "en": "Peak-shaving App-Center now documents grid-operator high-load-window source data and adds §19 after-control diagnostics. The runtime tracks annual peak and HLZF peak, calculates shift percentage, minimum-shift status and estimated savings under peakShaving.atypical.review.*.",
12
+ "de": "Peak-Shaving App-Center erweitert: Netzbetreiber-/Quellnachweis für Hochlastzeitfenster sowie §19-Nachkontrolle ergänzt. Die Runtime führt Jahreshöchstlast und HLZF-Höchstlast mit, berechnet Verlagerung, Mindestverlagerung und grobe Ersparnis unter peakShaving.atypical.review.*."
13
+ },
6
14
  "0.7.28": {
7
15
  "en": "Peak-Shaving App-Center tab added: installers can now choose standard peak shaving, atypical high-load-window capping, hybrid mode or monitoring, configure §19 reference peak/threshold/minimum shift, calendar exceptions and high-load windows directly in the UI.",
8
16
  "de": "Peak-Shaving-Reiter im App-Center ergänzt: Installateure können jetzt normale Lastspitzenkappung, atypische Hochlastzeitfenster-Kappung, Hybrid-Modus oder Monitoring wählen und §19-Referenzpeak, Schwelle, Mindestverlagerung, Kalenderausnahmen sowie Hochlastzeitfenster direkt in der UI konfigurieren."