iobroker.pvnode 0.0.0-stage → 0.2.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.
package/lib/api.js ADDED
@@ -0,0 +1,109 @@
1
+ 'use strict';
2
+
3
+ const BASE_URL = 'https://api.pvnode.com';
4
+ const TIMEOUT_MS = 30000;
5
+
6
+ class PvnodeError extends Error {
7
+ /**
8
+ * @param {string} message error message
9
+ * @param {number} status HTTP status (0 = network error)
10
+ * @param {string} detail detail text from the API
11
+ * @param {object} quota parsed quota headers
12
+ */
13
+ constructor(message, status, detail, quota) {
14
+ super(message);
15
+ this.name = 'PvnodeError';
16
+ this.status = status;
17
+ this.detail = detail || '';
18
+ this.quota = quota || null;
19
+ }
20
+ }
21
+
22
+ /**
23
+ * Parse the RequestLimit-* headers. Limit/Remaining may be "unmetered".
24
+ *
25
+ * @param {Headers} headers fetch response headers
26
+ */
27
+ function parseQuota(headers) {
28
+ const limit = headers.get('requestlimit-limit');
29
+ if (limit === null) {
30
+ return null;
31
+ }
32
+ const num = v => (v !== null && v !== '' && !Number.isNaN(Number(v)) ? Number(v) : v);
33
+ return {
34
+ limit: num(limit),
35
+ used: num(headers.get('requestlimit-used')),
36
+ remaining: num(headers.get('requestlimit-remaining')),
37
+ reset: headers.get('requestlimit-reset'),
38
+ };
39
+ }
40
+
41
+ class PvnodeApi {
42
+ /**
43
+ * @param {string} apiKey pvnode API key
44
+ * @param {string} userAgent User-Agent header value
45
+ */
46
+ constructor(apiKey, userAgent) {
47
+ this.apiKey = apiKey;
48
+ this.userAgent = userAgent;
49
+ }
50
+
51
+ async _get(path) {
52
+ let res;
53
+ try {
54
+ res = await fetch(BASE_URL + path, {
55
+ headers: {
56
+ Authorization: `Bearer ${this.apiKey}`,
57
+ 'User-Agent': this.userAgent,
58
+ Accept: 'application/json',
59
+ },
60
+ signal: AbortSignal.timeout(TIMEOUT_MS),
61
+ });
62
+ } catch (err) {
63
+ const timedOut = err.name === 'TimeoutError' || err.name === 'AbortError';
64
+ throw new PvnodeError(`Network error: ${timedOut ? 'timeout' : err.message}`, 0, '', null);
65
+ }
66
+ const quota = parseQuota(res.headers);
67
+ const text = await res.text();
68
+ let body = null;
69
+ try {
70
+ body = text ? JSON.parse(text) : null;
71
+ } catch {
72
+ body = null;
73
+ }
74
+ if (!res.ok) {
75
+ const detail = body && typeof body.detail === 'string' ? body.detail : text.slice(0, 300);
76
+ throw new PvnodeError(`HTTP ${res.status}: ${detail}`, res.status, detail, quota);
77
+ }
78
+ if (body === null) {
79
+ throw new PvnodeError('Invalid JSON response', res.status, text.slice(0, 300), quota);
80
+ }
81
+ return { body, quota };
82
+ }
83
+
84
+ /** List sites. Does not consume forecast quota; validates the key (401 = invalid). */
85
+ async getSites() {
86
+ const { body } = await this._get('/v2/sites/');
87
+ return Array.isArray(body) ? body : [];
88
+ }
89
+
90
+ /**
91
+ * Get forecast for a saved site.
92
+ *
93
+ * @param {string} siteId site id
94
+ * @param {string[]} include include groups
95
+ * @param {number} [forecastDays] 0/undefined = plan maximum
96
+ */
97
+ async getForecast(siteId, include, forecastDays) {
98
+ const params = new URLSearchParams();
99
+ for (const g of include) {
100
+ params.append('include', g);
101
+ }
102
+ if (forecastDays && forecastDays > 0) {
103
+ params.set('forecast_days', String(forecastDays));
104
+ }
105
+ return this._get(`/v2/forecast/${encodeURIComponent(siteId)}?${params.toString()}`);
106
+ }
107
+ }
108
+
109
+ module.exports = { PvnodeApi, PvnodeError, parseQuota };
package/lib/compute.js ADDED
@@ -0,0 +1,288 @@
1
+ 'use strict';
2
+
3
+ const { parseTs, localDate, localTime, isoLocal } = require('./time');
4
+
5
+ const DEFAULT_SLOT_MS = 15 * 60 * 1000;
6
+
7
+ /** Fields from the weather / irradiance groups that are exposed for the current slot. */
8
+ const WEATHER_FIELDS = [
9
+ 'temp',
10
+ 'weather_code',
11
+ 'wind_speed',
12
+ 'relative_humidity',
13
+ 'precipitation_mm',
14
+ 'snow_water_equivalent',
15
+ 'ghi',
16
+ 'dhi',
17
+ 'bni',
18
+ ];
19
+
20
+ const r1 = v => (typeof v === 'number' && Number.isFinite(v) ? Math.round(v * 10) / 10 : null);
21
+ const r2 = v => (typeof v === 'number' && Number.isFinite(v) ? Math.round(v * 100) / 100 : null);
22
+ const r3 = v => (typeof v === 'number' && Number.isFinite(v) ? Math.round(v * 1000) / 1000 : null);
23
+
24
+ /**
25
+ * Parse a pvnode forecast payload into time series with epoch timestamps.
26
+ *
27
+ * @param {object} payload forecast response body
28
+ */
29
+ function prepare(payload) {
30
+ const tz = payload.timezone || 'UTC';
31
+ const values = (payload.values || [])
32
+ .map(v => ({ ...v, t: parseTs(v.timestamp, tz) }))
33
+ .filter(v => Number.isFinite(v.t))
34
+ .sort((a, b) => a.t - b.t);
35
+
36
+ let slotMs = DEFAULT_SLOT_MS;
37
+ if (values.length > 1) {
38
+ const d = values[1].t - values[0].t;
39
+ if (d > 0 && d <= 3600000) {
40
+ slotMs = d;
41
+ }
42
+ }
43
+
44
+ const strings = new Map();
45
+ for (const row of payload.strings || []) {
46
+ const idx = Number(row.string_index);
47
+ if (!Number.isInteger(idx)) {
48
+ continue;
49
+ }
50
+ let s = strings.get(idx);
51
+ if (!s) {
52
+ s = { index: idx, id: row.string_id || null, rows: [] };
53
+ strings.set(idx, s);
54
+ }
55
+ const t = parseTs(row.timestamp, tz);
56
+ if (Number.isFinite(t)) {
57
+ s.rows.push({ ...row, t });
58
+ }
59
+ }
60
+ for (const s of strings.values()) {
61
+ s.rows.sort((a, b) => a.t - b.t);
62
+ }
63
+
64
+ const hasVariability = values.some(v => typeof v.pv_power_min === 'number');
65
+ const hasClearsky = values.some(v => typeof v.pv_power_clearsky === 'number');
66
+ const hasPower = values.some(v => typeof v.pv_power === 'number');
67
+
68
+ return {
69
+ tz,
70
+ slotMs,
71
+ values,
72
+ strings: [...strings.values()].sort((a, b) => a.index - b.index),
73
+ daily: payload.daily || [],
74
+ hasVariability,
75
+ hasClearsky,
76
+ hasPower,
77
+ };
78
+ }
79
+
80
+ /**
81
+ * Energy in kWh of a series between from and to (ms). Each row is the mean power
82
+ * of the slot starting at its timestamp; partial slots are prorated.
83
+ *
84
+ * @param {Array} rows rows with t and the power field
85
+ * @param {number} from epoch ms
86
+ * @param {number} to epoch ms
87
+ * @param {number} slotMs slot length
88
+ * @param {string} [field] power field, default pv_power
89
+ */
90
+ function energy(rows, from, to, slotMs, field = 'pv_power') {
91
+ let wh = 0;
92
+ let any = false;
93
+ for (const r of rows) {
94
+ const p = r[field];
95
+ if (typeof p !== 'number') {
96
+ continue;
97
+ }
98
+ const a = Math.max(r.t, from);
99
+ const b = Math.min(r.t + slotMs, to);
100
+ if (b > a) {
101
+ wh += (p * (b - a)) / 3600000;
102
+ any = true;
103
+ }
104
+ }
105
+ return any ? wh / 1000 : null;
106
+ }
107
+
108
+ function currentRow(rows, now, slotMs) {
109
+ for (const r of rows) {
110
+ if (r.t <= now && now < r.t + slotMs) {
111
+ return r;
112
+ }
113
+ }
114
+ return null;
115
+ }
116
+
117
+ function dayBounds(rows, day, tz) {
118
+ const dayRows = rows.filter(r => localDate(r.t, tz) === day);
119
+ return dayRows;
120
+ }
121
+
122
+ function peak(rows, field = 'pv_power') {
123
+ let best = null;
124
+ for (const r of rows) {
125
+ if (typeof r[field] === 'number' && (best === null || r[field] > best[field])) {
126
+ best = r;
127
+ }
128
+ }
129
+ return best && best[field] > 0 ? best : null;
130
+ }
131
+
132
+ /**
133
+ * Compute all state values for "now" from a prepared forecast.
134
+ * Returns a flat object { relativeStateId: value }.
135
+ *
136
+ * @param {ReturnType<typeof prepare>} p prepared forecast
137
+ * @param {number} now epoch ms
138
+ */
139
+ function computeStates(p, now) {
140
+ const { tz, slotMs, values } = p;
141
+ const out = {};
142
+ const today = localDate(now, tz);
143
+ // "tomorrow" = first date in the series after today (robust against DST days)
144
+ const dates = [...new Set(values.map(v => localDate(v.t, tz)))].sort();
145
+ const nextDay = dates.find(d => d > today) || localDate(now + 24 * 3600000, tz);
146
+
147
+ // ---- now ----
148
+ const cur = currentRow(values, now, slotMs);
149
+ out['now.power'] = cur && p.hasPower ? r1(cur.pv_power) : null;
150
+ if (p.hasVariability) {
151
+ out['now.power_min'] = cur ? r1(cur.pv_power_min) : null;
152
+ out['now.power_max'] = cur ? r1(cur.pv_power_max) : null;
153
+ }
154
+ if (p.hasClearsky) {
155
+ out['now.power_clearsky'] = cur ? r1(cur.pv_power_clearsky) : null;
156
+ }
157
+ out['now.energy_next_hour'] = p.hasPower ? r3(energy(values, now, now + 3600000, slotMs)) : null;
158
+ out['now.slot_start'] = cur ? isoLocal(cur.t, tz) : null;
159
+
160
+ // ---- weather of current slot ----
161
+ if (cur) {
162
+ for (const f of WEATHER_FIELDS) {
163
+ if (f in cur) {
164
+ out[`weather.${f}`] = typeof cur[f] === 'number' ? r1(cur[f]) : cur[f];
165
+ }
166
+ }
167
+ }
168
+
169
+ // ---- today / tomorrow ----
170
+ const dailyByDate = new Map(p.daily.map(d => [d.date, d]));
171
+ const fillDay = (prefix, day, withRemaining) => {
172
+ const rows = dayBounds(values, day, tz);
173
+ const d = dailyByDate.get(day);
174
+ out[`${prefix}.date`] = day;
175
+ const calc = rows.length ? energy(rows, -Infinity, Infinity, slotMs) : null;
176
+ out[`${prefix}.energy`] = r2(d && typeof d.pv_energy_kwh === 'number' ? d.pv_energy_kwh : calc);
177
+ if (p.hasVariability) {
178
+ out[`${prefix}.energy_min`] = r2(
179
+ d ? d.pv_energy_kwh_min : energy(rows, -Infinity, Infinity, slotMs, 'pv_power_min'),
180
+ );
181
+ out[`${prefix}.energy_max`] = r2(
182
+ d ? d.pv_energy_kwh_max : energy(rows, -Infinity, Infinity, slotMs, 'pv_power_max'),
183
+ );
184
+ }
185
+ const pk = peak(rows);
186
+ out[`${prefix}.peak_power`] = pk ? r1(pk.pv_power) : null;
187
+ out[`${prefix}.peak_time`] = pk ? localTime(pk.t, tz) : null;
188
+ if (withRemaining) {
189
+ const rem = rows.length ? energy(rows, now, Infinity, slotMs) : null;
190
+ out[`${prefix}.remaining`] = r2(rem === null && rows.length ? 0 : rem);
191
+ const done = rows.length ? energy(rows, -Infinity, now, slotMs) : null;
192
+ out[`${prefix}.expected_so_far`] = r2(done === null && rows.length ? 0 : done);
193
+ if (p.hasVariability) {
194
+ out[`${prefix}.remaining_min`] = r2(energy(rows, now, Infinity, slotMs, 'pv_power_min') ?? 0);
195
+ out[`${prefix}.remaining_max`] = r2(energy(rows, now, Infinity, slotMs, 'pv_power_max') ?? 0);
196
+ }
197
+ }
198
+ };
199
+ fillDay('today', today, true);
200
+ fillDay('tomorrow', nextDay, false);
201
+
202
+ // ---- days.N (0 = today) ----
203
+ const futureDaily = p.daily.filter(d => d.date >= today).sort((a, b) => (a.date < b.date ? -1 : 1));
204
+ out._dayCount = futureDaily.length;
205
+ futureDaily.forEach((d, i) => {
206
+ const pre = `days.${i}`;
207
+ out[`${pre}.date`] = d.date;
208
+ out[`${pre}.energy`] = r2(d.pv_energy_kwh);
209
+ if (p.hasVariability) {
210
+ out[`${pre}.energy_min`] = r2(d.pv_energy_kwh_min);
211
+ out[`${pre}.energy_max`] = r2(d.pv_energy_kwh_max);
212
+ }
213
+ if ('pv_energy_kwh_clearsky' in d) {
214
+ out[`${pre}.energy_clearsky`] = r2(d.pv_energy_kwh_clearsky);
215
+ }
216
+ if ('temp_min' in d) {
217
+ out[`${pre}.temp_min`] = r1(d.temp_min);
218
+ }
219
+ if ('temp_max' in d) {
220
+ out[`${pre}.temp_max`] = r1(d.temp_max);
221
+ }
222
+ if ('weather_code' in d) {
223
+ out[`${pre}.weather_code`] = d.weather_code;
224
+ }
225
+ });
226
+
227
+ // ---- strings ----
228
+ for (const s of p.strings) {
229
+ const pre = `strings.${s.index}`;
230
+ const c = currentRow(s.rows, now, slotMs);
231
+ out[`${pre}.power`] = c ? r1(c.pv_power) : null;
232
+ if (p.hasVariability) {
233
+ out[`${pre}.power_min`] = c ? r1(c.pv_power_min) : null;
234
+ out[`${pre}.power_max`] = c ? r1(c.pv_power_max) : null;
235
+ }
236
+ out[`${pre}.gti`] = c ? r1(c.gti) : null;
237
+ const tRows = dayBounds(s.rows, today, tz);
238
+ const mRows = dayBounds(s.rows, nextDay, tz);
239
+ out[`${pre}.today_energy`] = r2(tRows.length ? energy(tRows, -Infinity, Infinity, slotMs) : null);
240
+ out[`${pre}.today_remaining`] = r2(tRows.length ? (energy(tRows, now, Infinity, slotMs) ?? 0) : null);
241
+ out[`${pre}.tomorrow_energy`] = r2(mRows.length ? energy(mRows, -Infinity, Infinity, slotMs) : null);
242
+ }
243
+
244
+ return out;
245
+ }
246
+
247
+ /**
248
+ * Compact JSON series for charts: [{t, pv, pv_min, pv_max, clearsky, s0, s1, ...}]
249
+ * t = epoch ms, power in W.
250
+ *
251
+ * @param {ReturnType<typeof prepare>} p prepared forecast
252
+ * @param {(row: object) => boolean} [filter] row filter
253
+ */
254
+ function chartSeries(p, filter) {
255
+ const byT = new Map();
256
+ for (const s of p.strings) {
257
+ for (const r of s.rows) {
258
+ let m = byT.get(r.t);
259
+ if (!m) {
260
+ m = {};
261
+ byT.set(r.t, m);
262
+ }
263
+ m[`s${s.index}`] = r1(r.pv_power);
264
+ }
265
+ }
266
+ const out = [];
267
+ for (const v of p.values) {
268
+ if (filter && !filter(v)) {
269
+ continue;
270
+ }
271
+ const e = { t: v.t };
272
+ if (typeof v.pv_power === 'number') {
273
+ e.pv = r1(v.pv_power);
274
+ }
275
+ if (p.hasVariability) {
276
+ e.pv_min = r1(v.pv_power_min);
277
+ e.pv_max = r1(v.pv_power_max);
278
+ }
279
+ if (p.hasClearsky) {
280
+ e.clearsky = r1(v.pv_power_clearsky);
281
+ }
282
+ Object.assign(e, byT.get(v.t) || {});
283
+ out.push(e);
284
+ }
285
+ return out;
286
+ }
287
+
288
+ module.exports = { prepare, computeStates, chartSeries, energy, WEATHER_FIELDS };
package/lib/states.js ADDED
@@ -0,0 +1,160 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * State definitions, looked up by the last segment of the state id.
5
+ * Names are given in English and German.
6
+ */
7
+ const DEFS = {
8
+ power: { name: { en: 'PV power forecast', de: 'PV-Leistung Prognose' }, unit: 'W', role: 'value.power' },
9
+ power_min: {
10
+ name: { en: 'PV power forecast (lower band)', de: 'PV-Leistung Prognose (unteres Band)' },
11
+ unit: 'W',
12
+ role: 'value.power',
13
+ },
14
+ power_max: {
15
+ name: { en: 'PV power forecast (upper band)', de: 'PV-Leistung Prognose (oberes Band)' },
16
+ unit: 'W',
17
+ role: 'value.power',
18
+ },
19
+ power_clearsky: {
20
+ name: { en: 'PV power at clear sky', de: 'PV-Leistung bei klarem Himmel' },
21
+ unit: 'W',
22
+ role: 'value.power',
23
+ },
24
+ energy_next_hour: {
25
+ name: { en: 'Expected energy next hour', de: 'Erwartete Energie nächste Stunde' },
26
+ unit: 'kWh',
27
+ role: 'value.energy',
28
+ },
29
+ slot_start: {
30
+ name: { en: 'Start of current forecast slot', de: 'Beginn des aktuellen Prognose-Intervalls' },
31
+ type: 'string',
32
+ role: 'text',
33
+ },
34
+
35
+ date: { name: { en: 'Date', de: 'Datum' }, type: 'string', role: 'text' },
36
+ energy: { name: { en: 'Energy forecast', de: 'Energie Prognose' }, unit: 'kWh', role: 'value.energy' },
37
+ energy_min: {
38
+ name: { en: 'Energy forecast (lower band)', de: 'Energie Prognose (unteres Band)' },
39
+ unit: 'kWh',
40
+ role: 'value.energy',
41
+ },
42
+ energy_max: {
43
+ name: { en: 'Energy forecast (upper band)', de: 'Energie Prognose (oberes Band)' },
44
+ unit: 'kWh',
45
+ role: 'value.energy',
46
+ },
47
+ energy_clearsky: {
48
+ name: { en: 'Energy at clear sky', de: 'Energie bei klarem Himmel' },
49
+ unit: 'kWh',
50
+ role: 'value.energy',
51
+ },
52
+ remaining: { name: { en: 'Remaining energy today', de: 'Restertrag heute' }, unit: 'kWh', role: 'value.energy' },
53
+ remaining_min: {
54
+ name: { en: 'Remaining energy today (lower band)', de: 'Restertrag heute (unteres Band)' },
55
+ unit: 'kWh',
56
+ role: 'value.energy',
57
+ },
58
+ remaining_max: {
59
+ name: { en: 'Remaining energy today (upper band)', de: 'Restertrag heute (oberes Band)' },
60
+ unit: 'kWh',
61
+ role: 'value.energy',
62
+ },
63
+ expected_so_far: {
64
+ name: { en: 'Expected energy until now', de: 'Erwarteter Ertrag bis jetzt' },
65
+ unit: 'kWh',
66
+ role: 'value.energy',
67
+ },
68
+ peak_power: { name: { en: 'Peak power', de: 'Spitzenleistung' }, unit: 'W', role: 'value.power' },
69
+ peak_time: { name: { en: 'Time of peak power', de: 'Uhrzeit der Spitzenleistung' }, type: 'string', role: 'text' },
70
+ temp_min: {
71
+ name: { en: 'Minimum temperature', de: 'Tiefsttemperatur' },
72
+ unit: '°C',
73
+ role: 'value.temperature.min',
74
+ },
75
+ temp_max: {
76
+ name: { en: 'Maximum temperature', de: 'Höchsttemperatur' },
77
+ unit: '°C',
78
+ role: 'value.temperature.max',
79
+ },
80
+ weather_code: { name: { en: 'Weather code (WMO)', de: 'Wettercode (WMO)' }, role: 'value' },
81
+
82
+ temp: { name: { en: 'Temperature', de: 'Temperatur' }, unit: '°C', role: 'value.temperature' },
83
+ wind_speed: { name: { en: 'Wind speed', de: 'Windgeschwindigkeit' }, role: 'value.speed.wind' },
84
+ relative_humidity: {
85
+ name: { en: 'Relative humidity', de: 'Relative Luftfeuchtigkeit' },
86
+ unit: '%',
87
+ role: 'value.humidity',
88
+ },
89
+ precipitation_mm: { name: { en: 'Precipitation', de: 'Niederschlag' }, unit: 'mm', role: 'value.precipitation' },
90
+ snow_water_equivalent: { name: { en: 'Snow water equivalent', de: 'Schnee-Wasser-Äquivalent' }, role: 'value' },
91
+ ghi: {
92
+ name: { en: 'Global horizontal irradiance', de: 'Globalstrahlung horizontal' },
93
+ unit: 'W/m²',
94
+ role: 'value',
95
+ },
96
+ dhi: {
97
+ name: { en: 'Diffuse horizontal irradiance', de: 'Diffusstrahlung horizontal' },
98
+ unit: 'W/m²',
99
+ role: 'value',
100
+ },
101
+ bni: { name: { en: 'Beam normal irradiance', de: 'Direktnormalstrahlung' }, unit: 'W/m²', role: 'value' },
102
+ gti: { name: { en: 'Irradiance on module plane', de: 'Einstrahlung auf Modulebene' }, unit: 'W/m²', role: 'value' },
103
+
104
+ today_energy: {
105
+ name: { en: 'Energy forecast today', de: 'Energie Prognose heute' },
106
+ unit: 'kWh',
107
+ role: 'value.energy',
108
+ },
109
+ today_remaining: {
110
+ name: { en: 'Remaining energy today', de: 'Restertrag heute' },
111
+ unit: 'kWh',
112
+ role: 'value.energy',
113
+ },
114
+ tomorrow_energy: {
115
+ name: { en: 'Energy forecast tomorrow', de: 'Energie Prognose morgen' },
116
+ unit: 'kWh',
117
+ role: 'value.energy',
118
+ },
119
+
120
+ string_id: { name: { en: 'String ID', de: 'String-ID' }, type: 'string', role: 'text' },
121
+ orientation: { name: { en: 'Orientation (azimuth)', de: 'Ausrichtung (Azimut)' }, unit: '°', role: 'value' },
122
+ slope: { name: { en: 'Tilt', de: 'Neigung' }, unit: '°', role: 'value' },
123
+ power_kw: { name: { en: 'Installed power', de: 'Installierte Leistung' }, unit: 'kWp', role: 'value' },
124
+ };
125
+
126
+ const CHANNELS = {
127
+ now: { en: 'Current forecast values', de: 'Aktuelle Prognosewerte' },
128
+ today: { en: 'Today', de: 'Heute' },
129
+ tomorrow: { en: 'Tomorrow', de: 'Morgen' },
130
+ days: { en: 'Daily forecast (0 = today)', de: 'Tagesprognose (0 = heute)' },
131
+ strings: { en: 'Strings / module areas', de: 'Strings / Modulflächen' },
132
+ weather: { en: 'Weather and irradiance (current slot)', de: 'Wetter und Einstrahlung (aktuelles Intervall)' },
133
+ json: { en: 'JSON data for charts', de: 'JSON-Daten für Diagramme' },
134
+ html: { en: 'Ready-made SVG charts for VIS', de: 'Fertige SVG-Diagramme für VIS' },
135
+ site: { en: 'Site', de: 'Standort' },
136
+ control: { en: 'Control', de: 'Steuerung' },
137
+ };
138
+
139
+ /**
140
+ * Common definition for a relative state id.
141
+ *
142
+ * @param {string} id relative id, e.g. "today.energy"
143
+ */
144
+ function commonFor(id) {
145
+ const last = id.split('.').pop();
146
+ const d = DEFS[last] || { name: last, role: 'value' };
147
+ const common = {
148
+ name: d.name,
149
+ type: d.type || 'number',
150
+ role: d.role,
151
+ read: true,
152
+ write: false,
153
+ };
154
+ if (d.unit) {
155
+ common.unit = d.unit;
156
+ }
157
+ return common;
158
+ }
159
+
160
+ module.exports = { DEFS, CHANNELS, commonFor };