node-red-contrib-octopus-intelligent 1.4.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.
Files changed (37) hide show
  1. package/CHANGELOG.md +487 -408
  2. package/README.md +1004 -980
  3. package/examples/basic-monitoring.json +1 -1
  4. package/examples/ha-dashboard.yaml +602 -486
  5. package/examples/mqtt-integration.json +1 -1
  6. package/examples/node-red-dashboard-flow.json +1287 -0
  7. package/examples/set-preferences.json +1 -1
  8. package/lib/api-metrics.js +31 -31
  9. package/lib/auth.js +20 -20
  10. package/lib/categories/account.js +32 -32
  11. package/lib/categories/applicable-rates.js +127 -91
  12. package/lib/categories/completed-dispatches.js +35 -35
  13. package/lib/categories/electricity.js +146 -133
  14. package/lib/categories/flex-planned-dispatches.js +36 -36
  15. package/lib/categories/free-electricity.js +61 -61
  16. package/lib/categories/gas.js +95 -95
  17. package/lib/categories/home-mini.js +37 -37
  18. package/lib/categories/intelligent.js +155 -124
  19. package/lib/categories/octoplus.js +32 -32
  20. package/lib/categories/saving-sessions.js +64 -64
  21. package/lib/categories/wheel-of-fortune.js +49 -49
  22. package/lib/commands.js +47 -0
  23. package/lib/discovery-retry.js +13 -0
  24. package/lib/discovery.js +92 -85
  25. package/lib/graphql-errors.js +18 -0
  26. package/lib/graphql.js +44 -44
  27. package/lib/http-get-json.js +22 -0
  28. package/lib/payload.js +89 -77
  29. package/lib/poll-tick.js +44 -44
  30. package/lib/preferences.js +32 -0
  31. package/lib/scheduler.js +47 -47
  32. package/lib/sensor-definitions.js +219 -0
  33. package/lib/timezone.js +49 -49
  34. package/lib/update-check.js +38 -0
  35. package/octopus-intelligent.html +218 -205
  36. package/octopus-intelligent.js +1687 -1685
  37. package/package.json +54 -54
package/lib/payload.js CHANGED
@@ -1,77 +1,89 @@
1
- // lib/payload.js
2
- 'use strict';
3
-
4
- function mergePayload(base, newData) {
5
- return Object.assign({}, base, newData);
6
- }
7
-
8
- function buildDefaultPayload(state) {
9
- if (!state || typeof state !== 'object') {
10
- throw new TypeError('buildDefaultPayload: state must be an object');
11
- }
12
- const { confirmedLimit, confirmedTime, pendingLimit, pendingTime, chargingNow, smartChargingSuspended } = state;
13
- return {
14
- // V1 fields — unchanged
15
- next_start: null, total_energy: 0, next_kwh: '0', next_source: 'unknown',
16
- confirmed_limit: confirmedLimit, confirmed_time: confirmedTime,
17
- pending_limit: pendingLimit, pending_time: pendingTime,
18
- charging_now: chargingNow,
19
- next_poll: null, next_poll_raw: null,
20
- refresh_available_at: null,
21
- api_requests_hour: 0, api_complexity_hour: 0, api_complexity_percent: 0,
22
- slot1_start: null, slot1_end: null, slot2_start: null, slot2_end: null,
23
- slot3_start: null, slot3_end: null,
24
- window_start: null, window_end: null,
25
- next_start_raw: null,
26
- slot1_start_raw: null, slot1_end_raw: null, slot2_start_raw: null, slot2_end_raw: null,
27
- slot3_start_raw: null, slot3_end_raw: null,
28
- window_start_raw: null, window_end_raw: null,
29
- timezone_detected: null, timezone_applied: null,
30
- next_start_locale: null,
31
- slot1_start_locale: null, slot1_end_locale: null,
32
- slot2_start_locale: null, slot2_end_locale: null,
33
- slot3_start_locale: null, slot3_end_locale: null,
34
- window_start_locale: null, window_end_locale: null,
35
- smart_charging: smartChargingSuspended === null ? null : !smartChargingSuspended,
36
- // V2 fields — new categories
37
- electricity_unit_rate: null, electricity_standing_charge: null,
38
- electricity_tariff_code: null, electricity_valid_from: null, electricity_valid_to: null,
39
- electricity_export_unit_rate: null, electricity_export_standing_charge: null,
40
- electricity_export_tariff_code: null, electricity_export_valid_from: null, electricity_export_valid_to: null,
41
- electricity_consumption_kwh: null, electricity_consumption_cost: null,
42
- electricity_consumption_from: null, electricity_consumption_to: null,
43
- electricity_export_consumption_kwh: null,
44
- electricity_export_consumption_from: null, electricity_export_consumption_to: null,
45
- electricity_export_rate: [], electricity_export_rate_count: 0,
46
- electricity_export_rate_current_pence: null, electricity_export_rate_current_gbp: null,
47
- electricity_export_rate_prev_pence: null, electricity_export_rate_prev_gbp: null, electricity_export_rate_prev_to: null,
48
- electricity_export_rate_next_pence: null, electricity_export_rate_next_gbp: null, electricity_export_rate_next_from: null,
49
- electricity_export_rate_error: null,
50
- electricity_rates_error: null, electricity_consumption_error: null,
51
- gas_unit_rate: null, gas_standing_charge: null,
52
- gas_tariff_code: null, gas_valid_from: null, gas_valid_to: null,
53
- gas_consumption_kwh: null, gas_consumption_cost: null,
54
- gas_consumption_from: null, gas_consumption_to: null,
55
- gas_rates_error: null, gas_consumption_error: null,
56
- wheel_of_fortune_electricity_spins: null, wheel_of_fortune_electricity_max: null,
57
- wheel_of_fortune_electricity_used: null,
58
- wheel_of_fortune_gas_spins: null, wheel_of_fortune_gas_max: null,
59
- wheel_of_fortune_gas_used: null, wheel_of_fortune_error: null,
60
- mini_demand_kw: null, mini_consumption_delta_kwh: null, mini_read_at: null, home_mini_error: null,
61
- saving_session_available: null, saving_session_start: null, saving_session_end: null,
62
- saving_session_joined: null, saving_session_window_active: null, saving_session_active: null,
63
- saving_session_points: null, saving_sessions_error: null,
64
- free_electricity_active: null, free_electricity_available: null,
65
- free_electricity_start: null, free_electricity_end: null, free_electricity_error: null,
66
- intelligent_error: null,
67
- completed_dispatches: [], completed_dispatches_count: 0, completed_dispatches_error: null,
68
- flex_planned_dispatches: [], flex_planned_dispatches_count: 0, flex_planned_dispatches_error: null,
69
- applicable_rates: [], applicable_rates_count: 0,
70
- applicable_rates_current_pence: null, applicable_rates_error: null,
71
- account_balance_pence: null, account_balance_pounds: null, account_error: null,
72
- octoplus_enrolled: null, octoplus_enrollment_status: null,
73
- octoplus_loyalty_points_user: null, octoplus_error: null
74
- };
75
- }
76
-
77
- module.exports = { mergePayload, buildDefaultPayload };
1
+ // lib/payload.js
2
+ 'use strict';
3
+
4
+ function mergePayload(base, newData) {
5
+ return Object.assign({}, base, newData);
6
+ }
7
+
8
+ function buildDefaultPayload(state) {
9
+ if (!state || typeof state !== 'object') {
10
+ throw new TypeError('buildDefaultPayload: state must be an object');
11
+ }
12
+ const { confirmedLimit, confirmedTime, pendingLimit, pendingTime, chargingNow, smartChargingSuspended } = state;
13
+ return {
14
+ // V1 fields — unchanged
15
+ next_start: null, total_energy: 0, next_kwh: '0', next_source: 'unknown',
16
+ confirmed_limit: confirmedLimit, confirmed_time: confirmedTime,
17
+ pending_limit: pendingLimit, pending_time: pendingTime,
18
+ charging_now: chargingNow,
19
+ next_poll: null, next_poll_raw: null,
20
+ refresh_available_at: null,
21
+ api_requests_hour: 0, api_complexity_hour: 0, api_complexity_percent: 0,
22
+ slot1_start: null, slot1_end: null, slot2_start: null, slot2_end: null,
23
+ slot3_start: null, slot3_end: null,
24
+ window_start: null, window_end: null,
25
+ next_start_raw: null,
26
+ slot1_start_raw: null, slot1_end_raw: null, slot2_start_raw: null, slot2_end_raw: null,
27
+ slot3_start_raw: null, slot3_end_raw: null,
28
+ window_start_raw: null, window_end_raw: null,
29
+ timezone_detected: null, timezone_applied: null,
30
+ next_start_locale: null,
31
+ slot1_start_locale: null, slot1_end_locale: null,
32
+ slot2_start_locale: null, slot2_end_locale: null,
33
+ slot3_start_locale: null, slot3_end_locale: null,
34
+ window_start_locale: null, window_end_locale: null,
35
+ smart_charging: smartChargingSuspended === null ? null : !smartChargingSuspended,
36
+ charge_cap: null,
37
+ // V2 fields — new categories
38
+ electricity_unit_rate: null, electricity_standing_charge: null,
39
+ electricity_tariff_code: null, electricity_valid_from: null, electricity_valid_to: null,
40
+ electricity_export_unit_rate: null, electricity_export_standing_charge: null,
41
+ electricity_export_tariff_code: null, electricity_export_valid_from: null, electricity_export_valid_to: null,
42
+ electricity_consumption_kwh: null, electricity_consumption_cost: null,
43
+ electricity_consumption_from: null, electricity_consumption_to: null,
44
+ electricity_export_consumption_kwh: null,
45
+ electricity_export_consumption_from: null, electricity_export_consumption_to: null,
46
+ electricity_export_rate: [], electricity_export_rate_count: 0,
47
+ electricity_export_rate_current_pence: null, electricity_export_rate_current_gbp: null,
48
+ electricity_export_rate_prev_pence: null, electricity_export_rate_prev_gbp: null, electricity_export_rate_prev_to: null,
49
+ electricity_export_rate_next_pence: null, electricity_export_rate_next_gbp: null, electricity_export_rate_next_from: null,
50
+ electricity_export_rate_error: null,
51
+ electricity_rates_error: null, electricity_consumption_error: null,
52
+ gas_unit_rate: null, gas_standing_charge: null,
53
+ gas_tariff_code: null, gas_valid_from: null, gas_valid_to: null,
54
+ gas_consumption_kwh: null, gas_consumption_cost: null,
55
+ gas_consumption_from: null, gas_consumption_to: null,
56
+ gas_rates_error: null, gas_consumption_error: null,
57
+ wheel_of_fortune_electricity_spins: null, wheel_of_fortune_electricity_max: null,
58
+ wheel_of_fortune_electricity_used: null,
59
+ wheel_of_fortune_gas_spins: null, wheel_of_fortune_gas_max: null,
60
+ wheel_of_fortune_gas_used: null, wheel_of_fortune_error: null,
61
+ mini_demand_kw: null, mini_consumption_delta_kwh: null, mini_read_at: null, home_mini_error: null,
62
+ saving_session_available: null, saving_session_start: null, saving_session_end: null,
63
+ saving_session_joined: null, saving_session_window_active: null, saving_session_active: null,
64
+ saving_session_points: null, saving_sessions_error: null,
65
+ free_electricity_active: null, free_electricity_available: null,
66
+ free_electricity_start: null, free_electricity_end: null, free_electricity_error: null,
67
+ intelligent_error: null,
68
+ completed_dispatches: [], completed_dispatches_count: 0, completed_dispatches_error: null,
69
+ flex_planned_dispatches: [], flex_planned_dispatches_count: 0, flex_planned_dispatches_error: null,
70
+ applicable_rates: [], applicable_rates_count: 0,
71
+ applicable_rates_current_pence: null, applicable_rates_error: null,
72
+ account_balance_pence: null, account_balance_pounds: null, account_error: null,
73
+ octoplus_enrolled: null, octoplus_enrollment_status: null,
74
+ octoplus_loyalty_points_user: null, octoplus_error: null,
75
+ // v1.5 — tariff rate-band fields (electricity)
76
+ electricity_day_rate: null, electricity_night_rate: null,
77
+ electricity_ev_peak_rate: null, electricity_ev_off_peak_rate: null,
78
+ // v1.5 — 24h schedule stats
79
+ applicable_rates_min_pence: null, applicable_rates_max_pence: null,
80
+ applicable_rates_median_pence: null, applicable_rates_avg_pence: null,
81
+ electricity_export_rate_min_pence: null, electricity_export_rate_max_pence: null,
82
+ electricity_export_rate_median_pence: null, electricity_export_rate_avg_pence: null,
83
+ // v1.5 — update check
84
+ installed_version: null, latest_version: null,
85
+ update_available: false, update_check_at: null, update_check_error: null
86
+ };
87
+ }
88
+
89
+ module.exports = { mergePayload, buildDefaultPayload };
package/lib/poll-tick.js CHANGED
@@ -1,44 +1,44 @@
1
- 'use strict';
2
-
3
- function simplePoll(queryFn, parseFn, graphqlPost) {
4
- return async function poll(token) {
5
- const { query, variables, hostname } = queryFn();
6
- const response = await graphqlPost({ query, variables }, token, hostname);
7
- if (response.data.errors) throw new Error(JSON.stringify(response.data.errors));
8
- if (!response.data.data) throw new Error('Response missing data');
9
- return parseFn(response.data.data);
10
- };
11
- }
12
-
13
- const { isDue } = require('./scheduler');
14
- const { mergePayload } = require('./payload');
15
-
16
- async function runTick({ categories, state, getToken, now = Date.now }) {
17
- const due = categories.filter((c) => c.enabled && isDue(c));
18
- if (due.length === 0) return { state, emitted: false };
19
- let token;
20
- try {
21
- token = await getToken();
22
- } catch (e) {
23
- let newState = state;
24
- for (const cat of due) {
25
- newState = mergePayload(newState, { [`${cat.id}_error`]: `Auth failed: ${e.message}` });
26
- cat.lastPolled = now();
27
- }
28
- return { state: newState, emitted: true };
29
- }
30
- let newState = state;
31
- for (const cat of due) {
32
- try {
33
- const partial = await cat.poll(token);
34
- newState = mergePayload(newState, partial);
35
- newState = mergePayload(newState, { [`${cat.id}_error`]: null });
36
- } catch (e) {
37
- newState = mergePayload(newState, { [`${cat.id}_error`]: e.message });
38
- }
39
- cat.lastPolled = now();
40
- }
41
- return { state: newState, emitted: true };
42
- }
43
-
44
- module.exports = { simplePoll, runTick };
1
+ 'use strict';
2
+
3
+ function simplePoll(queryFn, parseFn, graphqlPost) {
4
+ return async function poll(token) {
5
+ const { query, variables, hostname } = queryFn();
6
+ const response = await graphqlPost({ query, variables }, token, hostname);
7
+ if (response.data.errors) throw new Error(JSON.stringify(response.data.errors));
8
+ if (!response.data.data) throw new Error('Response missing data');
9
+ return parseFn(response.data.data);
10
+ };
11
+ }
12
+
13
+ const { isDue } = require('./scheduler');
14
+ const { mergePayload } = require('./payload');
15
+
16
+ async function runTick({ categories, state, getToken, now = Date.now }) {
17
+ const due = categories.filter((c) => c.enabled && isDue(c));
18
+ if (due.length === 0) return { state, emitted: false };
19
+ let token;
20
+ try {
21
+ token = await getToken();
22
+ } catch (e) {
23
+ let newState = state;
24
+ for (const cat of due) {
25
+ newState = mergePayload(newState, { [`${cat.id}_error`]: `Auth failed: ${e.message}` });
26
+ cat.lastPolled = now();
27
+ }
28
+ return { state: newState, emitted: true };
29
+ }
30
+ let newState = state;
31
+ for (const cat of due) {
32
+ try {
33
+ const partial = await cat.poll(token);
34
+ newState = mergePayload(newState, partial);
35
+ newState = mergePayload(newState, { [`${cat.id}_error`]: null });
36
+ } catch (e) {
37
+ newState = mergePayload(newState, { [`${cat.id}_error`]: e.message });
38
+ }
39
+ cat.lastPolled = now();
40
+ }
41
+ return { state: newState, emitted: true };
42
+ }
43
+
44
+ module.exports = { simplePoll, runTick };
@@ -0,0 +1,32 @@
1
+ 'use strict';
2
+
3
+ // Ready-by times offered by the Octopus app: every half hour, 00:00–23:30.
4
+ const TIME_OPTIONS = Array.from({ length: 48 }, (_, i) => {
5
+ const h = String(Math.floor(i / 2)).padStart(2, '0');
6
+ return `${h}:${i % 2 ? '30' : '00'}`;
7
+ });
8
+
9
+ function validateTime(time) {
10
+ if (typeof time === 'string' && TIME_OPTIONS.includes(time)) {
11
+ return { ok: true, time };
12
+ }
13
+ return {
14
+ ok: false,
15
+ error: `Invalid ready-by time '${time}' — must be HH:MM on the hour or half hour (00:00–23:30)`,
16
+ };
17
+ }
18
+
19
+ function validateLimit(limit) {
20
+ // Plain decimal numbers or numeric strings only: no truncating '8O' to 8 (parseInt) and no
21
+ // coercing true/[90]/'0x5A'/'1e2' (Number). 0 is never a meaningful target.
22
+ let n = NaN;
23
+ if (typeof limit === 'number') n = limit;
24
+ else if (typeof limit === 'string' && /^\s*\d+(\.\d+)?\s*$/.test(limit)) n = Number(limit);
25
+ if (!Number.isFinite(n) || n <= 0) {
26
+ return { ok: false, error: `Invalid charge limit '${limit}' — must be a number between 5 and 100` };
27
+ }
28
+ const clamped = Math.min(100, Math.max(5, n));
29
+ return { ok: true, limit: Math.round(clamped / 5) * 5 };
30
+ }
31
+
32
+ module.exports = { TIME_OPTIONS, validateTime, validateLimit };
package/lib/scheduler.js CHANGED
@@ -1,47 +1,47 @@
1
- 'use strict';
2
-
3
- function isDue(cat) {
4
- if (!cat.enabled) return false;
5
- if (cat.lastPolled === 0) return true;
6
- const slot = ts => Math.floor(ts / cat.intervalMs);
7
- return slot(Date.now()) > slot(cat.lastPolled);
8
- }
9
-
10
- function alignedStart(nowMs) {
11
- const ms = nowMs % 1000;
12
- return ms === 0 ? 1000 : 1000 - ms;
13
- }
14
-
15
- function wrapWithInFlightGuard(fn) {
16
- let inFlight = false;
17
- return async function guarded(...args) {
18
- if (inFlight) return;
19
- inFlight = true;
20
- try {
21
- return await fn(...args);
22
- } finally {
23
- inFlight = false;
24
- }
25
- };
26
- }
27
-
28
- function createScheduler(onTick) {
29
- const guarded = wrapWithInFlightGuard(onTick);
30
- let handle = null;
31
-
32
- function start() {
33
- const delay = alignedStart(Date.now());
34
- setTimeout(() => {
35
- guarded();
36
- handle = setInterval(guarded, 1000);
37
- }, delay);
38
- }
39
-
40
- function stop() {
41
- if (handle) { clearInterval(handle); handle = null; }
42
- }
43
-
44
- return { start, stop };
45
- }
46
-
47
- module.exports = { isDue, alignedStart, createScheduler, wrapWithInFlightGuard };
1
+ 'use strict';
2
+
3
+ function isDue(cat) {
4
+ if (!cat.enabled) return false;
5
+ if (cat.lastPolled === 0) return true;
6
+ const slot = ts => Math.floor(ts / cat.intervalMs);
7
+ return slot(Date.now()) > slot(cat.lastPolled);
8
+ }
9
+
10
+ function alignedStart(nowMs) {
11
+ const ms = nowMs % 1000;
12
+ return ms === 0 ? 1000 : 1000 - ms;
13
+ }
14
+
15
+ function wrapWithInFlightGuard(fn) {
16
+ let inFlight = false;
17
+ return async function guarded(...args) {
18
+ if (inFlight) return;
19
+ inFlight = true;
20
+ try {
21
+ return await fn(...args);
22
+ } finally {
23
+ inFlight = false;
24
+ }
25
+ };
26
+ }
27
+
28
+ function createScheduler(onTick) {
29
+ const guarded = wrapWithInFlightGuard(onTick);
30
+ let handle = null;
31
+
32
+ function start() {
33
+ const delay = alignedStart(Date.now());
34
+ setTimeout(() => {
35
+ guarded();
36
+ handle = setInterval(guarded, 1000);
37
+ }, delay);
38
+ }
39
+
40
+ function stop() {
41
+ if (handle) { clearInterval(handle); handle = null; }
42
+ }
43
+
44
+ return { start, stop };
45
+ }
46
+
47
+ module.exports = { isDue, alignedStart, createScheduler, wrapWithInFlightGuard };