node-red-contrib-octopus-intelligent 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.
Files changed (37) hide show
  1. package/CHANGELOG.md +487 -442
  2. package/README.md +1004 -980
  3. package/examples/basic-monitoring.json +1 -1
  4. package/examples/ha-dashboard.yaml +602 -599
  5. package/examples/mqtt-integration.json +1 -1
  6. package/examples/node-red-dashboard-flow.json +1287 -1199
  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 -127
  12. package/lib/categories/completed-dispatches.js +35 -35
  13. package/lib/categories/electricity.js +146 -146
  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 -22
  28. package/lib/payload.js +89 -88
  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 -38
  35. package/octopus-intelligent.html +218 -205
  36. package/octopus-intelligent.js +1687 -1807
  37. package/package.json +54 -54
@@ -1,146 +1,146 @@
1
- // lib/categories/electricity.js
2
- 'use strict';
3
-
4
- const RATES_QUERY = `
5
- query getElectricityRates($account: String!) {
6
- account(accountNumber: $account) {
7
- electricityAgreements {
8
- validFrom validTo
9
- tariff {
10
- ... on TariffType { productCode standingCharge }
11
- ... on StandardTariff { productCode standingCharge unitRate }
12
- ... on HalfHourlyTariff { productCode standingCharge }
13
- ... on DayNightTariff { productCode standingCharge dayRate nightRate }
14
- ... on FourRateEvTariff { productCode standingCharge dayRate nightRate evDevicePeakRate evDeviceOffPeakRate }
15
- ... on PrepayTariff { productCode standingCharge unitRate }
16
- }
17
- }
18
- }
19
- }`;
20
-
21
- const CONSUMPTION_QUERY = `
22
- query getElectricityConsumption($account: String!, $startAt: DateTime!, $timezone: String!) {
23
- account(accountNumber: $account) {
24
- properties {
25
- electricityMeterPoints {
26
- mpan
27
- meters {
28
- consumption(startAt: $startAt, last: 1, grouping: DAY, timezone: $timezone) {
29
- edges { node { startAt endAt value } }
30
- }
31
- }
32
- }
33
- }
34
- }
35
- }`;
36
-
37
- function buildRatesQuery(account) {
38
- return { query: RATES_QUERY, variables: { account } };
39
- }
40
-
41
- function buildConsumptionQuery(account, timezone) {
42
- const tz = timezone || 'UTC';
43
- const startAt = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000).toISOString();
44
- return { query: CONSUMPTION_QUERY, variables: { account, startAt, timezone: tz } };
45
- }
46
-
47
- // Tariff productCodes containing "OUTGOING" are export agreements (e.g. AGILE-OUTGOING-19-05-13).
48
- // We surface both import and export tariff metadata as separate fields.
49
- function parseRatesResponse(data) {
50
- const all = (data.account || {}).electricityAgreements || [];
51
- const isExport = (a) => ((a.tariff || {}).productCode || '').includes('OUTGOING');
52
- const importAgreements = all.filter(a => !isExport(a));
53
- const exportAgreements = all.filter(isExport);
54
-
55
- const fieldsFor = (prefix, agreements) => {
56
- const latest = agreements[0];
57
- const tariff = (latest && latest.tariff) || {};
58
- // Kraken Float scalars can serialise as strings — coerce defensively
59
- return {
60
- [`${prefix}_unit_rate`]: tariff.unitRate != null ? parseFloat(tariff.unitRate) : null,
61
- [`${prefix}_standing_charge`]: tariff.standingCharge != null ? parseFloat(tariff.standingCharge) : null,
62
- [`${prefix}_tariff_code`]: tariff.productCode || null,
63
- [`${prefix}_valid_from`]: (latest && latest.validFrom) || null,
64
- [`${prefix}_valid_to`]: (latest && latest.validTo) || null,
65
- // v1.5 rate-band fields — null on tariffs that don't expose them
66
- [`${prefix}_day_rate`]: tariff.dayRate != null ? parseFloat(tariff.dayRate) : null,
67
- [`${prefix}_night_rate`]: tariff.nightRate != null ? parseFloat(tariff.nightRate) : null,
68
- [`${prefix}_ev_peak_rate`]: tariff.evDevicePeakRate != null ? parseFloat(tariff.evDevicePeakRate) : null,
69
- [`${prefix}_ev_off_peak_rate`]: tariff.evDeviceOffPeakRate != null ? parseFloat(tariff.evDeviceOffPeakRate) : null,
70
- };
71
- };
72
-
73
- return {
74
- ...fieldsFor('electricity', importAgreements),
75
- ...fieldsFor('electricity_export', exportAgreements),
76
- electricity_rates_error: null
77
- };
78
- }
79
-
80
- // Picks the latest consumption edge for a given MPAN out of the query response.
81
- function _findEntryForMpan(data, mpan) {
82
- if (!mpan) return null;
83
- const points = ((data.account || {}).properties || [])[0];
84
- const meterPoints = points ? (points.electricityMeterPoints || []) : [];
85
- const mp = meterPoints.find(p => p.mpan === mpan);
86
- if (!mp) return null;
87
- const meter = (mp.meters || [])[0];
88
- const edges = ((meter && meter.consumption) || {}).edges || [];
89
- return edges.length > 0 ? edges[edges.length - 1].node : null;
90
- }
91
-
92
- // Parses both import and export consumption from one response, given both MPANs.
93
- // Either MPAN may be null on accounts without that direction — the corresponding
94
- // fields will be null without affecting the other direction.
95
- function parseConsumptionResponse(data, importMpan, exportMpan) {
96
- const importEntry = _findEntryForMpan(data, importMpan);
97
- const exportEntry = _findEntryForMpan(data, exportMpan);
98
- return {
99
- electricity_consumption_kwh: importEntry ? parseFloat(importEntry.value) : null,
100
- electricity_consumption_cost: null,
101
- electricity_consumption_from: importEntry ? importEntry.startAt : null,
102
- electricity_consumption_to: importEntry ? importEntry.endAt : null,
103
- electricity_export_consumption_kwh: exportEntry ? parseFloat(exportEntry.value) : null,
104
- electricity_export_consumption_from: exportEntry ? exportEntry.startAt : null,
105
- electricity_export_consumption_to: exportEntry ? exportEntry.endAt : null,
106
- electricity_consumption_error: null
107
- };
108
- }
109
-
110
- const defaultData = {
111
- electricity_unit_rate: null,
112
- electricity_standing_charge: null,
113
- electricity_tariff_code: null,
114
- electricity_valid_from: null,
115
- electricity_valid_to: null,
116
- electricity_day_rate: null,
117
- electricity_night_rate: null,
118
- electricity_ev_peak_rate: null,
119
- electricity_ev_off_peak_rate: null,
120
- electricity_export_unit_rate: null,
121
- electricity_export_standing_charge: null,
122
- electricity_export_tariff_code: null,
123
- electricity_export_valid_from: null,
124
- electricity_export_valid_to: null,
125
- electricity_export_day_rate: null,
126
- electricity_export_night_rate: null,
127
- electricity_export_ev_peak_rate: null,
128
- electricity_export_ev_off_peak_rate: null,
129
- electricity_rates_error: null,
130
- electricity_consumption_kwh: null,
131
- electricity_consumption_cost: null,
132
- electricity_consumption_from: null,
133
- electricity_consumption_to: null,
134
- electricity_export_consumption_kwh: null,
135
- electricity_export_consumption_from: null,
136
- electricity_export_consumption_to: null,
137
- electricity_consumption_error: null
138
- };
139
-
140
- module.exports = {
141
- buildRatesQuery,
142
- buildConsumptionQuery,
143
- parseRatesResponse,
144
- parseConsumptionResponse,
145
- defaultData
146
- };
1
+ // lib/categories/electricity.js
2
+ 'use strict';
3
+
4
+ const RATES_QUERY = `
5
+ query getElectricityRates($account: String!) {
6
+ account(accountNumber: $account) {
7
+ electricityAgreements {
8
+ validFrom validTo
9
+ tariff {
10
+ ... on TariffType { productCode standingCharge }
11
+ ... on StandardTariff { productCode standingCharge unitRate }
12
+ ... on HalfHourlyTariff { productCode standingCharge }
13
+ ... on DayNightTariff { productCode standingCharge dayRate nightRate }
14
+ ... on FourRateEvTariff { productCode standingCharge dayRate nightRate evDevicePeakRate evDeviceOffPeakRate }
15
+ ... on PrepayTariff { productCode standingCharge unitRate }
16
+ }
17
+ }
18
+ }
19
+ }`;
20
+
21
+ const CONSUMPTION_QUERY = `
22
+ query getElectricityConsumption($account: String!, $startAt: DateTime!, $timezone: String!) {
23
+ account(accountNumber: $account) {
24
+ properties {
25
+ electricityMeterPoints {
26
+ mpan
27
+ meters {
28
+ consumption(startAt: $startAt, last: 1, grouping: DAY, timezone: $timezone) {
29
+ edges { node { startAt endAt value } }
30
+ }
31
+ }
32
+ }
33
+ }
34
+ }
35
+ }`;
36
+
37
+ function buildRatesQuery(account) {
38
+ return { query: RATES_QUERY, variables: { account } };
39
+ }
40
+
41
+ function buildConsumptionQuery(account, timezone) {
42
+ const tz = timezone || 'UTC';
43
+ const startAt = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000).toISOString();
44
+ return { query: CONSUMPTION_QUERY, variables: { account, startAt, timezone: tz } };
45
+ }
46
+
47
+ // Tariff productCodes containing "OUTGOING" are export agreements (e.g. AGILE-OUTGOING-19-05-13).
48
+ // We surface both import and export tariff metadata as separate fields.
49
+ function parseRatesResponse(data) {
50
+ const all = (data.account || {}).electricityAgreements || [];
51
+ const isExport = (a) => ((a.tariff || {}).productCode || '').includes('OUTGOING');
52
+ const importAgreements = all.filter(a => !isExport(a));
53
+ const exportAgreements = all.filter(isExport);
54
+
55
+ const fieldsFor = (prefix, agreements) => {
56
+ const latest = agreements[0];
57
+ const tariff = (latest && latest.tariff) || {};
58
+ // Kraken Float scalars can serialise as strings — coerce defensively
59
+ return {
60
+ [`${prefix}_unit_rate`]: tariff.unitRate != null ? parseFloat(tariff.unitRate) : null,
61
+ [`${prefix}_standing_charge`]: tariff.standingCharge != null ? parseFloat(tariff.standingCharge) : null,
62
+ [`${prefix}_tariff_code`]: tariff.productCode || null,
63
+ [`${prefix}_valid_from`]: (latest && latest.validFrom) || null,
64
+ [`${prefix}_valid_to`]: (latest && latest.validTo) || null,
65
+ // v1.5 rate-band fields — null on tariffs that don't expose them
66
+ [`${prefix}_day_rate`]: tariff.dayRate != null ? parseFloat(tariff.dayRate) : null,
67
+ [`${prefix}_night_rate`]: tariff.nightRate != null ? parseFloat(tariff.nightRate) : null,
68
+ [`${prefix}_ev_peak_rate`]: tariff.evDevicePeakRate != null ? parseFloat(tariff.evDevicePeakRate) : null,
69
+ [`${prefix}_ev_off_peak_rate`]: tariff.evDeviceOffPeakRate != null ? parseFloat(tariff.evDeviceOffPeakRate) : null,
70
+ };
71
+ };
72
+
73
+ return {
74
+ ...fieldsFor('electricity', importAgreements),
75
+ ...fieldsFor('electricity_export', exportAgreements),
76
+ electricity_rates_error: null
77
+ };
78
+ }
79
+
80
+ // Picks the latest consumption edge for a given MPAN out of the query response.
81
+ function _findEntryForMpan(data, mpan) {
82
+ if (!mpan) return null;
83
+ const points = ((data.account || {}).properties || [])[0];
84
+ const meterPoints = points ? (points.electricityMeterPoints || []) : [];
85
+ const mp = meterPoints.find(p => p.mpan === mpan);
86
+ if (!mp) return null;
87
+ const meter = (mp.meters || [])[0];
88
+ const edges = ((meter && meter.consumption) || {}).edges || [];
89
+ return edges.length > 0 ? edges[edges.length - 1].node : null;
90
+ }
91
+
92
+ // Parses both import and export consumption from one response, given both MPANs.
93
+ // Either MPAN may be null on accounts without that direction — the corresponding
94
+ // fields will be null without affecting the other direction.
95
+ function parseConsumptionResponse(data, importMpan, exportMpan) {
96
+ const importEntry = _findEntryForMpan(data, importMpan);
97
+ const exportEntry = _findEntryForMpan(data, exportMpan);
98
+ return {
99
+ electricity_consumption_kwh: importEntry ? parseFloat(importEntry.value) : null,
100
+ electricity_consumption_cost: null,
101
+ electricity_consumption_from: importEntry ? importEntry.startAt : null,
102
+ electricity_consumption_to: importEntry ? importEntry.endAt : null,
103
+ electricity_export_consumption_kwh: exportEntry ? parseFloat(exportEntry.value) : null,
104
+ electricity_export_consumption_from: exportEntry ? exportEntry.startAt : null,
105
+ electricity_export_consumption_to: exportEntry ? exportEntry.endAt : null,
106
+ electricity_consumption_error: null
107
+ };
108
+ }
109
+
110
+ const defaultData = {
111
+ electricity_unit_rate: null,
112
+ electricity_standing_charge: null,
113
+ electricity_tariff_code: null,
114
+ electricity_valid_from: null,
115
+ electricity_valid_to: null,
116
+ electricity_day_rate: null,
117
+ electricity_night_rate: null,
118
+ electricity_ev_peak_rate: null,
119
+ electricity_ev_off_peak_rate: null,
120
+ electricity_export_unit_rate: null,
121
+ electricity_export_standing_charge: null,
122
+ electricity_export_tariff_code: null,
123
+ electricity_export_valid_from: null,
124
+ electricity_export_valid_to: null,
125
+ electricity_export_day_rate: null,
126
+ electricity_export_night_rate: null,
127
+ electricity_export_ev_peak_rate: null,
128
+ electricity_export_ev_off_peak_rate: null,
129
+ electricity_rates_error: null,
130
+ electricity_consumption_kwh: null,
131
+ electricity_consumption_cost: null,
132
+ electricity_consumption_from: null,
133
+ electricity_consumption_to: null,
134
+ electricity_export_consumption_kwh: null,
135
+ electricity_export_consumption_from: null,
136
+ electricity_export_consumption_to: null,
137
+ electricity_consumption_error: null
138
+ };
139
+
140
+ module.exports = {
141
+ buildRatesQuery,
142
+ buildConsumptionQuery,
143
+ parseRatesResponse,
144
+ parseConsumptionResponse,
145
+ defaultData
146
+ };
@@ -1,36 +1,36 @@
1
- // lib/categories/flex-planned-dispatches.js
2
- 'use strict';
3
-
4
- const QUERY = `
5
- query getFlexPlannedDispatches($deviceId: String!) {
6
- flexPlannedDispatches(deviceId: $deviceId) {
7
- start end type energyAddedKwh
8
- }
9
- }`;
10
-
11
- function buildQuery(deviceId) {
12
- return { query: QUERY, variables: { deviceId } };
13
- }
14
-
15
- function parseResponse(data) {
16
- const dispatches = data.flexPlannedDispatches || [];
17
- return {
18
- flex_planned_dispatches: dispatches.map(d => ({
19
- start: d.start,
20
- end: d.end,
21
- type: d.type || null,
22
- energy_kwh: d.energyAddedKwh !== null && d.energyAddedKwh !== undefined
23
- ? parseFloat(d.energyAddedKwh) : null
24
- })),
25
- flex_planned_dispatches_count: dispatches.length,
26
- flex_planned_dispatches_error: null
27
- };
28
- }
29
-
30
- const defaultData = {
31
- flex_planned_dispatches: [],
32
- flex_planned_dispatches_count: 0,
33
- flex_planned_dispatches_error: null
34
- };
35
-
36
- module.exports = { buildQuery, parseResponse, defaultData };
1
+ // lib/categories/flex-planned-dispatches.js
2
+ 'use strict';
3
+
4
+ const QUERY = `
5
+ query getFlexPlannedDispatches($deviceId: String!) {
6
+ flexPlannedDispatches(deviceId: $deviceId) {
7
+ start end type energyAddedKwh
8
+ }
9
+ }`;
10
+
11
+ function buildQuery(deviceId) {
12
+ return { query: QUERY, variables: { deviceId } };
13
+ }
14
+
15
+ function parseResponse(data) {
16
+ const dispatches = data.flexPlannedDispatches || [];
17
+ return {
18
+ flex_planned_dispatches: dispatches.map(d => ({
19
+ start: d.start,
20
+ end: d.end,
21
+ type: d.type || null,
22
+ energy_kwh: d.energyAddedKwh !== null && d.energyAddedKwh !== undefined
23
+ ? parseFloat(d.energyAddedKwh) : null
24
+ })),
25
+ flex_planned_dispatches_count: dispatches.length,
26
+ flex_planned_dispatches_error: null
27
+ };
28
+ }
29
+
30
+ const defaultData = {
31
+ flex_planned_dispatches: [],
32
+ flex_planned_dispatches_count: 0,
33
+ flex_planned_dispatches_error: null
34
+ };
35
+
36
+ module.exports = { buildQuery, parseResponse, defaultData };
@@ -1,61 +1,61 @@
1
- // lib/categories/free-electricity.js
2
- // Detects upcoming and active "Free Electricity" sessions (0p/kWh windows).
3
- // Requires account number and MPAN from account discovery.
4
- 'use strict';
5
-
6
- const QUERY = `
7
- query getFreeElectricity($account: String!, $mpan: String!) {
8
- customerFlexibilityCampaignEvents(
9
- accountNumber: $account,
10
- supplyPointIdentifier: $mpan,
11
- campaignSlug: "free_electricity",
12
- first: 50
13
- ) {
14
- edges {
15
- node {
16
- code
17
- startAt
18
- endAt
19
- }
20
- }
21
- }
22
- }`;
23
-
24
- function buildQuery(account, mpan) {
25
- return { query: QUERY, variables: { account, mpan } };
26
- }
27
-
28
- function parseResponse(data) {
29
- const now = new Date();
30
-
31
- const edges = ((data.customerFlexibilityCampaignEvents || {}).edges) || [];
32
- const sessions = edges.map(e => ({ startAt: e.node.startAt, endAt: e.node.endAt }));
33
-
34
- // Filter to sessions not yet ended, sort by startAt ascending, take the first
35
- const future = sessions
36
- .filter(s => new Date(s.endAt) > now)
37
- .sort((a, b) => new Date(a.startAt) - new Date(b.startAt));
38
-
39
- const next = future[0] || null;
40
-
41
- const active = next !== null && new Date(next.startAt) <= now && new Date(next.endAt) > now;
42
- const available = next !== null;
43
-
44
- return {
45
- free_electricity_active: active,
46
- free_electricity_available: available,
47
- free_electricity_start: next ? next.startAt : null,
48
- free_electricity_end: next ? next.endAt : null,
49
- free_electricity_error: null
50
- };
51
- }
52
-
53
- const defaultData = {
54
- free_electricity_active: null,
55
- free_electricity_available: null,
56
- free_electricity_start: null,
57
- free_electricity_end: null,
58
- free_electricity_error: null
59
- };
60
-
61
- module.exports = { buildQuery, parseResponse, defaultData };
1
+ // lib/categories/free-electricity.js
2
+ // Detects upcoming and active "Free Electricity" sessions (0p/kWh windows).
3
+ // Requires account number and MPAN from account discovery.
4
+ 'use strict';
5
+
6
+ const QUERY = `
7
+ query getFreeElectricity($account: String!, $mpan: String!) {
8
+ customerFlexibilityCampaignEvents(
9
+ accountNumber: $account,
10
+ supplyPointIdentifier: $mpan,
11
+ campaignSlug: "free_electricity",
12
+ first: 50
13
+ ) {
14
+ edges {
15
+ node {
16
+ code
17
+ startAt
18
+ endAt
19
+ }
20
+ }
21
+ }
22
+ }`;
23
+
24
+ function buildQuery(account, mpan) {
25
+ return { query: QUERY, variables: { account, mpan } };
26
+ }
27
+
28
+ function parseResponse(data) {
29
+ const now = new Date();
30
+
31
+ const edges = ((data.customerFlexibilityCampaignEvents || {}).edges) || [];
32
+ const sessions = edges.map(e => ({ startAt: e.node.startAt, endAt: e.node.endAt }));
33
+
34
+ // Filter to sessions not yet ended, sort by startAt ascending, take the first
35
+ const future = sessions
36
+ .filter(s => new Date(s.endAt) > now)
37
+ .sort((a, b) => new Date(a.startAt) - new Date(b.startAt));
38
+
39
+ const next = future[0] || null;
40
+
41
+ const active = next !== null && new Date(next.startAt) <= now && new Date(next.endAt) > now;
42
+ const available = next !== null;
43
+
44
+ return {
45
+ free_electricity_active: active,
46
+ free_electricity_available: available,
47
+ free_electricity_start: next ? next.startAt : null,
48
+ free_electricity_end: next ? next.endAt : null,
49
+ free_electricity_error: null
50
+ };
51
+ }
52
+
53
+ const defaultData = {
54
+ free_electricity_active: null,
55
+ free_electricity_available: null,
56
+ free_electricity_start: null,
57
+ free_electricity_end: null,
58
+ free_electricity_error: null
59
+ };
60
+
61
+ module.exports = { buildQuery, parseResponse, defaultData };