iobroker.utility-monitor 1.5.0 → 1.6.1

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.
@@ -0,0 +1,136 @@
1
+ 'use strict';
2
+
3
+ const STATE_ROLES = require('./roles');
4
+
5
+ /**
6
+ * Creates the totals state structure for a utility type
7
+ * Totals show the sum of all meters (main + additional)
8
+ *
9
+ * @param {object} adapter - The adapter instance
10
+ * @param {string} type - Utility type: 'gas', 'water', 'electricity', 'pv'
11
+ * @returns {Promise<void>}
12
+ */
13
+ async function createTotalsStructure(adapter, type) {
14
+ const labels = {
15
+ gas: { name: 'Gas (Gesamt)', unit: 'kWh' },
16
+ water: { name: 'Wasser (Gesamt)', unit: 'm³' },
17
+ electricity: { name: 'Strom (Gesamt)', unit: 'kWh' },
18
+ pv: { name: 'PV (Gesamt)', unit: 'kWh' },
19
+ };
20
+
21
+ const label = labels[type];
22
+ if (!label) {
23
+ adapter.log.error(`MISSING LABEL for type "${type}" in createTotalsStructure!`);
24
+ return;
25
+ }
26
+ const basePath = `${type}.totals`;
27
+
28
+ // Create main channel
29
+ await adapter.setObjectNotExistsAsync(basePath, {
30
+ type: 'channel',
31
+ common: { name: `${label.name} - Summe aller Zähler` },
32
+ native: {},
33
+ });
34
+
35
+ // --- CONSUMPTION STATES (totals) ---
36
+ await adapter.setObjectNotExistsAsync(`${basePath}.consumption`, {
37
+ type: 'channel',
38
+ common: { name: 'Gesamtverbrauch' },
39
+ native: {},
40
+ });
41
+
42
+ const periods = [
43
+ { id: 'daily', name: 'Tagesverbrauch Gesamt' },
44
+ { id: 'monthly', name: 'Monatsverbrauch Gesamt' },
45
+ { id: 'yearly', name: 'Jahresverbrauch Gesamt' },
46
+ { id: 'weekly', name: 'Wochenverbrauch Gesamt' },
47
+ ];
48
+
49
+ for (const p of periods) {
50
+ await adapter.setObjectNotExistsAsync(`${basePath}.consumption.${p.id}`, {
51
+ type: 'state',
52
+ common: {
53
+ name: `${p.name} (${label.unit})`,
54
+ type: 'number',
55
+ role: STATE_ROLES.consumption,
56
+ read: true,
57
+ write: false,
58
+ unit: label.unit,
59
+ def: 0,
60
+ },
61
+ native: {},
62
+ });
63
+ }
64
+
65
+ if (type === 'gas') {
66
+ await adapter.setObjectNotExistsAsync(`${basePath}.consumption.weeklyVolume`, {
67
+ type: 'state',
68
+ common: {
69
+ name: 'Wochenverbrauch Gesamt (m³)',
70
+ type: 'number',
71
+ role: STATE_ROLES.consumption,
72
+ read: true,
73
+ write: false,
74
+ unit: 'm³',
75
+ def: 0,
76
+ },
77
+ native: {},
78
+ });
79
+
80
+ if (adapter.config.gasHtNtEnabled) {
81
+ const hntVolumeStates = [
82
+ { id: 'weeklyVolumeHT', name: 'Wochenverbrauch Gesamt Haupttarif (HT) (m³)' },
83
+ { id: 'weeklyVolumeNT', name: 'Wochenverbrauch Gesamt Nebentarif (NT) (m³)' },
84
+ ];
85
+ for (const s of hntVolumeStates) {
86
+ await adapter.setObjectNotExistsAsync(`${basePath}.consumption.${s.id}`, {
87
+ type: 'state',
88
+ common: {
89
+ name: s.name,
90
+ type: 'number',
91
+ role: STATE_ROLES.consumption,
92
+ read: true,
93
+ write: false,
94
+ unit: 'm³',
95
+ def: 0,
96
+ },
97
+ native: {},
98
+ });
99
+ }
100
+ }
101
+ }
102
+
103
+ // --- COST STATES (totals) ---
104
+ await adapter.setObjectNotExistsAsync(`${basePath}.costs`, {
105
+ type: 'channel',
106
+ common: { name: 'Gesamtkosten' },
107
+ native: {},
108
+ });
109
+
110
+ const costPeriods = [
111
+ { id: 'daily', name: 'Tageskosten Gesamt' },
112
+ { id: 'monthly', name: 'Monatskosten Gesamt' },
113
+ { id: 'weekly', name: 'Wochenkosten Gesamt' },
114
+ { id: 'totalYearly', name: 'Jahreskosten Gesamt' },
115
+ ];
116
+
117
+ for (const p of costPeriods) {
118
+ await adapter.setObjectNotExistsAsync(`${basePath}.costs.${p.id}`, {
119
+ type: 'state',
120
+ common: {
121
+ name: `${p.name} (€)`,
122
+ type: 'number',
123
+ role: STATE_ROLES.cost,
124
+ read: true,
125
+ write: false,
126
+ unit: '€',
127
+ def: 0,
128
+ },
129
+ native: {},
130
+ });
131
+ }
132
+
133
+ adapter.log.debug(`Totals state structure created for ${type}`);
134
+ }
135
+
136
+ module.exports = createTotalsStructure;