node-red-contrib-octopus-intelligent 1.4.0 → 1.5.0

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.
@@ -19,7 +19,29 @@ function buildQuery(account, mpan) {
19
19
  return { query: QUERY, variables: { account, mpxn: mpan, startAt, endAt } };
20
20
  }
21
21
 
22
- function _emit(prefix, slots, current, prev, next) {
22
+ function _stats(slots) {
23
+ const values = slots
24
+ .map(s => s.value_pence_per_kwh)
25
+ .filter(v => v !== null && v !== undefined);
26
+ if (values.length === 0) {
27
+ return { min: null, max: null, median: null, avg: null };
28
+ }
29
+ const sorted = values.slice().sort((a, b) => a - b);
30
+ const min = sorted[0];
31
+ const max = sorted[sorted.length - 1];
32
+ const sum = values.reduce((a, v) => a + v, 0);
33
+ const avg = parseFloat((sum / values.length).toFixed(4));
34
+ let median;
35
+ if (sorted.length % 2 === 1) {
36
+ median = sorted[Math.floor(sorted.length / 2)];
37
+ } else {
38
+ const mid = sorted.length / 2;
39
+ median = parseFloat(((sorted[mid - 1] + sorted[mid]) / 2).toFixed(4));
40
+ }
41
+ return { min, max, median, avg };
42
+ }
43
+
44
+ function _emit(prefix, slots, current, prev, next, stats) {
23
45
  const pence = current ? current.value_pence_per_kwh : null;
24
46
  const prevPence = prev ? prev.value_pence_per_kwh : null;
25
47
  const nextPence = next ? next.value_pence_per_kwh : null;
@@ -35,6 +57,11 @@ function _emit(prefix, slots, current, prev, next) {
35
57
  out[`${prefix}_next_gbp`] = nextPence !== null ? parseFloat((nextPence / 100).toFixed(4)) : null;
36
58
  out[`${prefix}_next_from`] = next ? next.valid_from : null;
37
59
  out[`${prefix}_error`] = null;
60
+ // v1.5 stats
61
+ out[`${prefix}_min_pence`] = stats.min;
62
+ out[`${prefix}_max_pence`] = stats.max;
63
+ out[`${prefix}_median_pence`] = stats.median;
64
+ out[`${prefix}_avg_pence`] = stats.avg;
38
65
  return out;
39
66
  }
40
67
 
@@ -60,7 +87,8 @@ function parseResponse(data, options) {
60
87
  const prev = currentIdx > 0 ? slots[currentIdx - 1] : null;
61
88
  const next = (currentIdx !== -1 && currentIdx < slots.length - 1) ? slots[currentIdx + 1] : null;
62
89
 
63
- return _emit(prefix, slots, current, prev, next);
90
+ const stats = _stats(slots);
91
+ return _emit(prefix, slots, current, prev, next, stats);
64
92
  }
65
93
 
66
94
  const defaultData = {
@@ -75,6 +103,10 @@ const defaultData = {
75
103
  applicable_rates_next_gbp: null,
76
104
  applicable_rates_next_from: null,
77
105
  applicable_rates_error: null,
106
+ applicable_rates_min_pence: null,
107
+ applicable_rates_max_pence: null,
108
+ applicable_rates_median_pence: null,
109
+ applicable_rates_avg_pence: null,
78
110
  electricity_export_rate: [],
79
111
  electricity_export_rate_count: 0,
80
112
  electricity_export_rate_current_pence: null,
@@ -85,7 +117,11 @@ const defaultData = {
85
117
  electricity_export_rate_next_pence: null,
86
118
  electricity_export_rate_next_gbp: null,
87
119
  electricity_export_rate_next_from: null,
88
- electricity_export_rate_error: null
120
+ electricity_export_rate_error: null,
121
+ electricity_export_rate_min_pence: null,
122
+ electricity_export_rate_max_pence: null,
123
+ electricity_export_rate_median_pence: null,
124
+ electricity_export_rate_avg_pence: null
89
125
  };
90
126
 
91
127
  module.exports = { buildQuery, parseResponse, defaultData };
@@ -10,8 +10,8 @@ query getElectricityRates($account: String!) {
10
10
  ... on TariffType { productCode standingCharge }
11
11
  ... on StandardTariff { productCode standingCharge unitRate }
12
12
  ... on HalfHourlyTariff { productCode standingCharge }
13
- ... on DayNightTariff { productCode standingCharge }
14
- ... on FourRateEvTariff { productCode standingCharge }
13
+ ... on DayNightTariff { productCode standingCharge dayRate nightRate }
14
+ ... on FourRateEvTariff { productCode standingCharge dayRate nightRate evDevicePeakRate evDeviceOffPeakRate }
15
15
  ... on PrepayTariff { productCode standingCharge unitRate }
16
16
  }
17
17
  }
@@ -62,6 +62,11 @@ function parseRatesResponse(data) {
62
62
  [`${prefix}_tariff_code`]: tariff.productCode || null,
63
63
  [`${prefix}_valid_from`]: (latest && latest.validFrom) || null,
64
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,
65
70
  };
66
71
  };
67
72
 
@@ -108,11 +113,19 @@ const defaultData = {
108
113
  electricity_tariff_code: null,
109
114
  electricity_valid_from: null,
110
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,
111
120
  electricity_export_unit_rate: null,
112
121
  electricity_export_standing_charge: null,
113
122
  electricity_export_tariff_code: null,
114
123
  electricity_export_valid_from: null,
115
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,
116
129
  electricity_rates_error: null,
117
130
  electricity_consumption_kwh: null,
118
131
  electricity_consumption_cost: null,
@@ -0,0 +1,22 @@
1
+ 'use strict';
2
+ const https = require('https');
3
+
4
+ function httpGetJson(url) {
5
+ return new Promise((resolve, reject) => {
6
+ const req = https.get(url, res => {
7
+ if (res.statusCode < 200 || res.statusCode >= 300) {
8
+ res.resume();
9
+ return reject(new Error(`HTTP ${res.statusCode}`));
10
+ }
11
+ let body = '';
12
+ res.on('data', chunk => { body += chunk; });
13
+ res.on('end', () => {
14
+ try { resolve(JSON.parse(body)); }
15
+ catch (e) { reject(new Error(`JSON parse: ${e.message}`)); }
16
+ });
17
+ });
18
+ req.on('error', reject);
19
+ });
20
+ }
21
+
22
+ module.exports = httpGetJson;
package/lib/payload.js CHANGED
@@ -70,7 +70,18 @@ function buildDefaultPayload(state) {
70
70
  applicable_rates_current_pence: null, applicable_rates_error: null,
71
71
  account_balance_pence: null, account_balance_pounds: null, account_error: null,
72
72
  octoplus_enrolled: null, octoplus_enrollment_status: null,
73
- octoplus_loyalty_points_user: null, octoplus_error: null
73
+ octoplus_loyalty_points_user: null, octoplus_error: null,
74
+ // v1.5 — tariff rate-band fields (electricity)
75
+ electricity_day_rate: null, electricity_night_rate: null,
76
+ electricity_ev_peak_rate: null, electricity_ev_off_peak_rate: null,
77
+ // v1.5 — 24h schedule stats
78
+ applicable_rates_min_pence: null, applicable_rates_max_pence: null,
79
+ applicable_rates_median_pence: null, applicable_rates_avg_pence: null,
80
+ electricity_export_rate_min_pence: null, electricity_export_rate_max_pence: null,
81
+ electricity_export_rate_median_pence: null, electricity_export_rate_avg_pence: null,
82
+ // v1.5 — update check
83
+ installed_version: null, latest_version: null,
84
+ update_available: false, update_check_at: null, update_check_error: null
74
85
  };
75
86
  }
76
87
 
@@ -0,0 +1,38 @@
1
+ 'use strict';
2
+
3
+ const NPM_LATEST_URL = 'https://registry.npmjs.org/node-red-contrib-octopus-intelligent/latest';
4
+
5
+ function compareSemver(a, b) {
6
+ // Strict numeric X.Y.Z with optional pre-release suffix.
7
+ // Pre-release (1.5.0-dev.1) is OLDER than the base release (1.5.0).
8
+ const parse = v => {
9
+ const m = String(v).match(/^(\d+)\.(\d+)\.(\d+)(-.+)?$/);
10
+ if (!m) return null;
11
+ return { major: +m[1], minor: +m[2], patch: +m[3], pre: m[4] || null };
12
+ };
13
+ const pa = parse(a), pb = parse(b);
14
+ if (!pa || !pb) return 0;
15
+ if (pa.major !== pb.major) return pa.major - pb.major;
16
+ if (pa.minor !== pb.minor) return pa.minor - pb.minor;
17
+ if (pa.patch !== pb.patch) return pa.patch - pb.patch;
18
+ if (pa.pre && !pb.pre) return -1;
19
+ if (!pa.pre && pb.pre) return 1;
20
+ return 0;
21
+ }
22
+
23
+ async function checkUpdate(httpGetJson, installedVersion) {
24
+ const data = await httpGetJson(NPM_LATEST_URL);
25
+ const latest = data && data.version;
26
+ if (typeof latest !== 'string' || !/^\d+\.\d+\.\d+/.test(latest)) {
27
+ throw new Error('npm registry response missing version');
28
+ }
29
+ return {
30
+ installed_version: installedVersion,
31
+ latest_version: latest,
32
+ update_available: compareSemver(latest, installedVersion) > 0,
33
+ update_check_at: new Date().toISOString(),
34
+ update_check_error: null,
35
+ };
36
+ }
37
+
38
+ module.exports = { checkUpdate, compareSemver, NPM_LATEST_URL };
@@ -19,6 +19,8 @@ const flexPlannedDispatchesCategory = require('./lib/categories/flex-planned-dis
19
19
  const applicableRatesCategory = require('./lib/categories/applicable-rates');
20
20
  const savingSessionsCategory = require('./lib/categories/saving-sessions');
21
21
  const freeElectricityCategory = require('./lib/categories/free-electricity');
22
+ const httpGetJson = require('./lib/http-get-json');
23
+ const { checkUpdate } = require('./lib/update-check');
22
24
 
23
25
  module.exports = function (RED) {
24
26
  function OctopusIntelligentNode(config) {
@@ -107,6 +109,10 @@ module.exports = function (RED) {
107
109
  { id: 'electricity_consumption_kwh', name: 'Electricity Consumption', unit: 'kWh', class: 'energy', val: 'electricity_consumption_kwh' },
108
110
  // --- Electricity (config) ---
109
111
  { id: 'electricity_unit_rate', name: 'Electricity Unit Rate', unit: 'p/kWh', icon: 'mdi:flash', val: 'electricity_unit_rate' },
112
+ { id: 'electricity_day_rate', name: 'Electricity Day Rate', unit: 'p/kWh', icon: 'mdi:weather-sunny', val: 'electricity_day_rate', category: 'config' },
113
+ { id: 'electricity_night_rate', name: 'Electricity Night Rate', unit: 'p/kWh', icon: 'mdi:weather-night', val: 'electricity_night_rate', category: 'config' },
114
+ { id: 'electricity_ev_peak_rate', name: 'Electricity EV Peak Rate', unit: 'p/kWh', icon: 'mdi:car-electric-outline', val: 'electricity_ev_peak_rate', category: 'config' },
115
+ { id: 'electricity_ev_off_peak_rate', name: 'Electricity EV Off-Peak Rate', unit: 'p/kWh', icon: 'mdi:car-electric', val: 'electricity_ev_off_peak_rate', category: 'config' },
110
116
  { id: 'electricity_tariff_code', name: 'Electricity Tariff Code', icon: 'mdi:tag', val: 'electricity_tariff_code' },
111
117
  { id: 'electricity_valid_from', name: 'Electricity Tariff Valid From', class: 'timestamp', icon: 'mdi:calendar-start', val: 'electricity_valid_from' },
112
118
  { id: 'electricity_valid_to', name: 'Electricity Tariff Valid To', class: 'timestamp', icon: 'mdi:calendar-end', val: 'electricity_valid_to' },
@@ -137,6 +143,8 @@ module.exports = function (RED) {
137
143
  { id: 'electricity_export_rate_prev_to', name: 'Electricity Export Rate Prev Ends', class: 'timestamp', icon: 'mdi:calendar-clock', val: 'electricity_export_rate_prev_to', category: 'diagnostic' },
138
144
  { id: 'electricity_export_rate_next_pence', name: 'Electricity Export Rate Next', unit: 'p/kWh', icon: 'mdi:cash-clock', val: 'electricity_export_rate_next_pence', category: 'diagnostic' },
139
145
  { id: 'electricity_export_rate_next_from', name: 'Electricity Export Rate Next From', class: 'timestamp', icon: 'mdi:calendar-clock', val: 'electricity_export_rate_next_from', category: 'diagnostic' },
146
+ { id: 'electricity_export_rate_prev_gbp', name: 'Electricity Export Rate Prev (GBP)', unit: 'GBP/kWh', icon: 'mdi:cash-clock', val: 'electricity_export_rate_prev_gbp', stateClass: 'measurement', category: 'diagnostic' },
147
+ { id: 'electricity_export_rate_next_gbp', name: 'Electricity Export Rate Next (GBP)', unit: 'GBP/kWh', icon: 'mdi:cash-clock', val: 'electricity_export_rate_next_gbp', stateClass: 'measurement', category: 'diagnostic' },
140
148
  { id: 'electricity_export_rate_error', name: 'Electricity Export Rate Error', icon: 'mdi:alert-circle', val: 'electricity_export_rate_error', category: 'diagnostic' },
141
149
 
142
150
  // --- Gas (main) ---
@@ -159,6 +167,21 @@ module.exports = function (RED) {
159
167
  // --- Applicable Rates (diagnostic) ---
160
168
  { id: 'applicable_rates_count', name: 'Applicable Rates Slots', icon: 'mdi:format-list-numbered', val: 'applicable_rates_count', category: 'diagnostic' },
161
169
  { id: 'applicable_rates_error', name: 'Applicable Rates Error', icon: 'mdi:alert-circle', val: 'applicable_rates_error', category: 'diagnostic' },
170
+ { id: 'applicable_rates_prev_pence', name: 'Applicable Rate Prev', unit: 'p/kWh', icon: 'mdi:cash-clock', val: 'applicable_rates_prev_pence', category: 'diagnostic' },
171
+ { id: 'applicable_rates_prev_gbp', name: 'Applicable Rate Prev (GBP)', unit: 'GBP/kWh', icon: 'mdi:cash-clock', val: 'applicable_rates_prev_gbp', stateClass: 'measurement', category: 'diagnostic' },
172
+ { id: 'applicable_rates_prev_to', name: 'Applicable Rate Prev Ends', class: 'timestamp', icon: 'mdi:calendar-clock', val: 'applicable_rates_prev_to', category: 'diagnostic' },
173
+ { id: 'applicable_rates_next_pence', name: 'Applicable Rate Next', unit: 'p/kWh', icon: 'mdi:cash-clock', val: 'applicable_rates_next_pence', category: 'diagnostic' },
174
+ { id: 'applicable_rates_next_gbp', name: 'Applicable Rate Next (GBP)', unit: 'GBP/kWh', icon: 'mdi:cash-clock', val: 'applicable_rates_next_gbp', stateClass: 'measurement', category: 'diagnostic' },
175
+ { id: 'applicable_rates_next_from', name: 'Applicable Rate Next From', class: 'timestamp', icon: 'mdi:calendar-clock', val: 'applicable_rates_next_from', category: 'diagnostic' },
176
+ // Applicable rates — 24h schedule stats (v1.5)
177
+ { id: 'applicable_rates_min_pence', name: 'Applicable Rates Min', unit: 'p/kWh', icon: 'mdi:arrow-down', val: 'applicable_rates_min_pence', category: 'diagnostic' },
178
+ { id: 'applicable_rates_max_pence', name: 'Applicable Rates Max', unit: 'p/kWh', icon: 'mdi:arrow-up', val: 'applicable_rates_max_pence', category: 'diagnostic' },
179
+ { id: 'applicable_rates_median_pence', name: 'Applicable Rates Median', unit: 'p/kWh', icon: 'mdi:approximately-equal', val: 'applicable_rates_median_pence', category: 'diagnostic' },
180
+ { id: 'applicable_rates_avg_pence', name: 'Applicable Rates Avg', unit: 'p/kWh', icon: 'mdi:chart-line', val: 'applicable_rates_avg_pence', category: 'diagnostic' },
181
+ { id: 'electricity_export_rate_min_pence', name: 'Electricity Export Rate Min', unit: 'p/kWh', icon: 'mdi:arrow-down', val: 'electricity_export_rate_min_pence', category: 'diagnostic' },
182
+ { id: 'electricity_export_rate_max_pence', name: 'Electricity Export Rate Max', unit: 'p/kWh', icon: 'mdi:arrow-up', val: 'electricity_export_rate_max_pence', category: 'diagnostic' },
183
+ { id: 'electricity_export_rate_median_pence', name: 'Electricity Export Rate Median', unit: 'p/kWh', icon: 'mdi:approximately-equal', val: 'electricity_export_rate_median_pence', category: 'diagnostic' },
184
+ { id: 'electricity_export_rate_avg_pence', name: 'Electricity Export Rate Avg', unit: 'p/kWh', icon: 'mdi:chart-line', val: 'electricity_export_rate_avg_pence', category: 'diagnostic' },
162
185
 
163
186
  // --- Account (main) ---
164
187
  { id: 'account_balance_pounds', name: 'Account Balance', unit: '£', icon: 'mdi:cash-multiple', val: 'account_balance_pounds' },
@@ -211,6 +234,33 @@ module.exports = function (RED) {
211
234
  { id: 'completed_dispatches_error', name: 'Completed Dispatches Error', icon: 'mdi:alert-circle', val: 'completed_dispatches_error', category: 'diagnostic' },
212
235
  { id: 'flex_planned_dispatches_error', name: 'Flex Planned Dispatches Error', icon: 'mdi:alert-circle', val: 'flex_planned_dispatches_error', category: 'diagnostic' },
213
236
  { id: 'intelligent_error', name: 'Intelligent Error', icon: 'mdi:alert-circle', val: 'intelligent_error', category: 'diagnostic' },
237
+
238
+ // v1.5 — update check (MQTT Update entity itself lands in Task 21)
239
+ { id: 'installed_version', name: 'Installed Version', icon: 'mdi:package-variant', val: 'installed_version', category: 'diagnostic' },
240
+ { id: 'latest_version', name: 'Latest Version', icon: 'mdi:package-variant-closed-plus', val: 'latest_version', category: 'diagnostic' },
241
+ { id: 'update_check_at', name: 'Update Check At', class: 'timestamp', icon: 'mdi:clock-check-outline', val: 'update_check_at', category: 'diagnostic' },
242
+ { id: 'update_check_error', name: 'Update Check Error', icon: 'mdi:alert-circle', val: 'update_check_error', category: 'diagnostic' },
243
+ ];
244
+
245
+ // v1.5 — derived binary_sensors for the unified <category>_error fields.
246
+ // OFF when the underlying field is null/empty (healthy), ON when populated.
247
+ // The string sensors stay; these are additional indicators.
248
+ const errorSensors = [
249
+ { id: 'intelligent_error_state', name: 'Intelligent Error State', val: 'intelligent_error' },
250
+ { id: 'electricity_rates_error_state', name: 'Electricity Rates Error State', val: 'electricity_rates_error' },
251
+ { id: 'electricity_consumption_error_state', name: 'Electricity Consumption Error State', val: 'electricity_consumption_error' },
252
+ { id: 'electricity_export_rate_error_state', name: 'Electricity Export Rate Error State', val: 'electricity_export_rate_error' },
253
+ { id: 'gas_rates_error_state', name: 'Gas Rates Error State', val: 'gas_rates_error' },
254
+ { id: 'gas_consumption_error_state', name: 'Gas Consumption Error State', val: 'gas_consumption_error' },
255
+ { id: 'applicable_rates_error_state', name: 'Applicable Rates Error State', val: 'applicable_rates_error' },
256
+ { id: 'account_error_state', name: 'Account Error State', val: 'account_error' },
257
+ { id: 'octoplus_error_state', name: 'Octoplus Error State', val: 'octoplus_error' },
258
+ { id: 'wheel_of_fortune_error_state', name: 'Wheel of Fortune Error State', val: 'wheel_of_fortune_error' },
259
+ { id: 'home_mini_error_state', name: 'Home Mini Error State', val: 'home_mini_error' },
260
+ { id: 'saving_sessions_error_state', name: 'Saving Sessions Error State', val: 'saving_sessions_error' },
261
+ { id: 'free_electricity_error_state', name: 'Free Electricity Error State', val: 'free_electricity_error' },
262
+ { id: 'completed_dispatches_error_state', name: 'Completed Dispatches Error State', val: 'completed_dispatches_error' },
263
+ { id: 'flex_planned_dispatches_error_state', name: 'Flex Planned Dispatches Error State', val: 'flex_planned_dispatches_error' },
214
264
  ];
215
265
 
216
266
  // 4. Helper: Announce Controls (Write-Enabled)
@@ -448,9 +498,33 @@ module.exports = function (RED) {
448
498
  payload.entity_category = sensor.category;
449
499
  }
450
500
 
501
+ if (sensor.id === 'applicable_rates_count') {
502
+ payload.json_attributes_topic = stateTopic;
503
+ payload.json_attributes_template = '{ "rates": {{ value_json.applicable_rates | tojson }} }';
504
+ } else if (sensor.id === 'electricity_export_rate_count') {
505
+ payload.json_attributes_topic = stateTopic;
506
+ payload.json_attributes_template = '{ "rates": {{ value_json.electricity_export_rate | tojson }} }';
507
+ }
508
+
451
509
  node.broker.client.publish(`${mqttPrefix}/sensor/${uniqueIdPrefix}_${sensor.id}/config`, JSON.stringify(payload), { retain: true });
452
510
  });
453
511
 
512
+ // H2. v1.5 — error binary_sensors (device_class: problem)
513
+ errorSensors.forEach(e => {
514
+ const payload = {
515
+ name: `Octopus ${e.name}`,
516
+ unique_id: `${uniqueIdPrefix}_${e.id}`,
517
+ state_topic: stateTopic,
518
+ value_template: `{{ 'OFF' if value_json.${e.val} in [None, ''] else 'ON' }}`,
519
+ payload_on: 'ON',
520
+ payload_off: 'OFF',
521
+ device_class: 'problem',
522
+ entity_category: 'diagnostic',
523
+ device: device,
524
+ };
525
+ node.broker.client.publish(`${mqttPrefix}/binary_sensor/${uniqueIdPrefix}_${e.id}/config`, JSON.stringify(payload), { retain: true });
526
+ });
527
+
454
528
  // I. Timezone Select Entity
455
529
  const TIMEZONE_OPTIONS = [
456
530
  "Europe/London", "Europe/Berlin", "Europe/Madrid",
@@ -491,6 +565,20 @@ module.exports = function (RED) {
491
565
  { retain: true }
492
566
  );
493
567
 
568
+ // K. v1.5 — Update entity (notify-only; Palette Manager is the install path)
569
+ node.broker.client.publish(
570
+ `${mqttPrefix}/update/${uniqueIdPrefix}_node_update/config`,
571
+ JSON.stringify({
572
+ name: 'Octopus Intelligent Node',
573
+ unique_id: `${uniqueIdPrefix}_node_update`,
574
+ state_topic: `${stateTopic}/node_update`,
575
+ title: 'node-red-contrib-octopus-intelligent',
576
+ release_url: 'https://github.com/ASomerN/node-red-contrib-octopus-intelligent/releases',
577
+ device: device,
578
+ }),
579
+ { retain: true }
580
+ );
581
+
494
582
  // Subscribe to Commands
495
583
  node.broker.client.subscribe(cmdTopicLimit);
496
584
  node.broker.client.subscribe(cmdTopicTime);
@@ -544,6 +632,7 @@ module.exports = function (RED) {
544
632
 
545
633
  // Last known full state - prevents sensors going unknown when controls change
546
634
  let lastKnownState = buildDefaultPayload({ confirmedLimit, confirmedTime, pendingLimit, pendingTime, chargingNow, smartChargingSuspended });
635
+ lastKnownState.installed_version = require('./package.json').version;
547
636
 
548
637
  // V2 category registry — populated after discovery
549
638
  let categories = [];
@@ -553,6 +642,9 @@ module.exports = function (RED) {
553
642
  // otherwise a redeploy during the 2s warmup leaves an orphan V2 scheduler.
554
643
  let initTimeoutHandles = [];
555
644
  let nodeClosed = false;
645
+ let lastWarnedVersion = null;
646
+ let prevPublishedInstalled = null;
647
+ let prevPublishedLatest = null;
556
648
 
557
649
  async function initCategories(discovered) {
558
650
  categories = [];
@@ -767,6 +859,14 @@ module.exports = function (RED) {
767
859
  graphqlPost
768
860
  ),
769
861
  });
862
+
863
+ categories.push({
864
+ id: 'update_check',
865
+ enabled: true,
866
+ intervalMs: 24 * 60 * 60 * 1000, // 24h — once daily npm registry check
867
+ lastPolled: 0, // fires on first scheduler tick
868
+ poll: intelligentUpdateCheck, // token ignored
869
+ });
770
870
  }
771
871
 
772
872
  async function pollDueCategories() {
@@ -779,8 +879,25 @@ module.exports = function (RED) {
779
879
  lastKnownState = result.state;
780
880
  if (enableMqtt && node.broker && node.broker.client) {
781
881
  node.broker.client.publish(stateTopic, JSON.stringify(lastKnownState), { retain: true });
882
+ // v1.5 — publish update entity state only when version values change
883
+ if (prevPublishedInstalled !== lastKnownState.installed_version
884
+ || prevPublishedLatest !== lastKnownState.latest_version) {
885
+ const updateState = {
886
+ installed_version: lastKnownState.installed_version,
887
+ latest_version: lastKnownState.latest_version || lastKnownState.installed_version,
888
+ };
889
+ node.broker.client.publish(`${stateTopic}/node_update`, JSON.stringify(updateState), { retain: true });
890
+ prevPublishedInstalled = lastKnownState.installed_version;
891
+ prevPublishedLatest = lastKnownState.latest_version;
892
+ }
782
893
  }
783
894
  node.send({ payload: lastKnownState });
895
+ if (lastKnownState.update_available === true
896
+ && lastKnownState.latest_version
897
+ && lastKnownState.latest_version !== lastWarnedVersion) {
898
+ node.warn(`Update available: v${lastKnownState.latest_version} (running v${lastKnownState.installed_version}). Install via Node-RED → Manage Palette → Upgrade.`);
899
+ lastWarnedVersion = lastKnownState.latest_version;
900
+ }
784
901
  if (!validationMode) {
785
902
  if (lastKnownState.intelligent_error) {
786
903
  node.status({ fill: "red", shape: "ring", text: `Error: ${String(lastKnownState.intelligent_error).slice(0, 40)}` });
@@ -1088,6 +1205,11 @@ module.exports = function (RED) {
1088
1205
  smartChargingRetryTimeouts.push(timeout);
1089
1206
  }
1090
1207
 
1208
+ // 6. Logic: Update Check (bespoke poll that ignores token)
1209
+ async function intelligentUpdateCheck(/* token ignored */) {
1210
+ return checkUpdate(httpGetJson, require('./package.json').version);
1211
+ }
1212
+
1091
1213
  // 6. Logic: Intelligent Poll (single-scheduler bespoke poll)
1092
1214
  async function intelligentPoll(token) {
1093
1215
  // 1. devices query
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-red-contrib-octopus-intelligent",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "description": "Node-RED integration for Octopus Energy Intelligent Octopus Go with Home Assistant MQTT discovery",
5
5
  "main": "octopus-intelligent.js",
6
6
  "scripts": {