iobroker.device-watcher 0.1.2 → 0.2.2

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/main.js CHANGED
@@ -43,11 +43,6 @@ class DeviceWatcher extends utils.Adapter {
43
43
 
44
44
  // arrays of supported adapters
45
45
  this.arrApart = {
46
- //**** This Datapoints are only for the dev ****//
47
- test: {'Selektor':'0_userdata.*.UNREACH', 'adapter':'homematic', 'rssiState':'.RSSI_DEVICE', 'battery':'.OPERATING_VOLTAGE', 'reach':'.UNREACH'},
48
- test2: {'Selektor':'0_userdata.*.alive', 'adapter':'esphome', 'rssiState': '.Wifi_RSSI', 'battery':'none', 'reach':'.alive', 'isLowBat':'none', 'id':'.name'},
49
- test3: {'Selektor':'0_userdata.*.link_quality', 'adapter':'zigbee', 'battery':'.battery', 'reach':'available', 'isLowBat':'none'},
50
- //**** End of Dev Datapoints ****//
51
46
  alexa2: {
52
47
  'Selektor':'alexa2.*.online',
53
48
  'adapter':'alexa2',
@@ -131,7 +126,7 @@ class DeviceWatcher extends utils.Adapter {
131
126
  },
132
127
  hueExt: {
133
128
  'Selektor':'hue-extended.*.reachable',
134
- 'adapter':'hue extended',
129
+ 'adapter':'hue-extended',
135
130
  'battery':'.config.battery',
136
131
  'reach':'.reachable',
137
132
  'isLowBat':'none'
@@ -145,7 +140,7 @@ class DeviceWatcher extends utils.Adapter {
145
140
  },
146
141
  switchbotBle: {
147
142
  'Selektor':'switchbot-ble.*.rssi',
148
- 'adapter':'switchbot ble',
143
+ 'adapter':'switchbotBle',
149
144
  'battery':'.battery',
150
145
  'reach':'none',
151
146
  'isLowBat':'none',
@@ -174,8 +169,8 @@ class DeviceWatcher extends utils.Adapter {
174
169
  },
175
170
  nukiExt: {
176
171
  'Selektor':'nuki-extended.*.batteryCritical',
177
- 'adapter':'nuki_extended',
178
- 'battery':'none',
172
+ 'adapter':'nuki-extended',
173
+ 'battery':'.batteryCharge',
179
174
  'reach':'none',
180
175
  'isLowBat':'.batteryCritical'
181
176
  }
@@ -187,7 +182,8 @@ class DeviceWatcher extends utils.Adapter {
187
182
 
188
183
  try {
189
184
  await this.main();
190
- await this.sendNotifications();
185
+ if (this.config.checkSendOfflineMsg) await this.sendOfflineNotifications();
186
+ if (this.config.checkSendBatteryMsg) await this.sendBatteryNotifications();
191
187
  await this.writeDatapoints();
192
188
  this.log.debug('all done, exiting');
193
189
  this.terminate ? this.terminate('Everything done. Going to terminate till next schedule', 11) : process.exit(0);
@@ -216,864 +212,1112 @@ class DeviceWatcher extends utils.Adapter {
216
212
  if (stateVal) return stateVal.val;
217
213
  }
218
214
 
215
+ //Notification services
216
+ async sendPushover(text) {
217
+ await this.sendToAsync(this.config.instancePushover, 'send', {
218
+ message: text,
219
+ title: this.config.titlePushover,
220
+ device: this.config.devicePushover,
221
+ priority: this.config.prioPushover
222
+ });
223
+ }
224
+
225
+ async sendTelegram(text) {
226
+ await this.sendToAsync(this.config.instanceTelegram, 'send', {
227
+ text: text,
228
+ user: this.config.deviceTelegram,
229
+ chatId: this.config.chatIdTelegram
230
+ });
231
+ }
232
+
233
+ async sendWhatsapp(text) {
234
+ await this.sendToAsync(this.config.instanceWhatsapp, 'send', {
235
+ text: text,
236
+ phone: this.config.phoneWhatsapp
237
+ });
238
+ }
239
+
240
+ async sendEmail(text) {
241
+ await this.sendToAsync(this.config.instanceEmail, 'send', {
242
+ sendTo: this.config.sendToEmail,
243
+ text: text,
244
+ subject: this.config.subjectEmail
245
+ });
246
+ }
247
+
248
+ async sendJarvis(text) {
249
+ await this.setForeignStateAsync(`${this.config.instanceJarvis}.addNotification`, text);
250
+
251
+ }
252
+
253
+ async sendLovelace(text) {
254
+ await this.setForeignStateAsync(`${this.config.instanceLovelace}.notifications.add`, text);
255
+
256
+ }
257
+
219
258
  //create datapoints for each adapter
220
259
  async createDPsForEachAdapter(adptName) {
260
+
261
+ await this.setObjectNotExistsAsync(`${adptName}`, {
262
+ type: 'channel',
263
+ common: {
264
+ name: adptName,
265
+ },
266
+ native: {},
267
+ });
268
+
221
269
  await this.setObjectNotExistsAsync(`${adptName}.offlineCount`, {
222
270
  'type': 'state',
223
271
  'common': {
224
- 'name': 'Quantity devices offline',
272
+ 'name': {
273
+ 'en': 'Number of devices offline',
274
+ 'de': 'Anzahl der Geräte offline',
275
+ 'ru': 'Количество устройств offline',
276
+ 'pt': 'Número de dispositivos offline',
277
+ 'nl': 'Nummer van apparatuur offline',
278
+ 'fr': 'Nombre de dispositifs hors ligne',
279
+ 'it': 'Numero di dispositivi offline',
280
+ 'es': 'Número de dispositivos sin conexión',
281
+ 'pl': 'Ilość urządzeń offline',
282
+ 'zh-cn': '线内装置数量'
283
+ },
225
284
  'type': 'number',
226
285
  'role': 'value',
227
286
  'read': true,
228
287
  'write': false,
229
- 'def': 0
230
288
  },
231
289
  'native': {}
232
290
  });
233
291
  await this.setObjectNotExistsAsync(`${adptName}.offlineList`, {
234
292
  'type': 'state',
235
293
  'common': {
236
- 'name': 'List devices offline',
294
+ 'name': {
295
+ 'en': 'List of offline devices',
296
+ 'de': 'Liste der Offline-Geräte',
297
+ 'ru': 'Список оффлайн устройств',
298
+ 'pt': 'Lista de dispositivos off-line',
299
+ 'nl': 'List van offline apparatuur',
300
+ 'fr': 'Liste des dispositifs hors ligne',
301
+ 'it': 'Elenco dei dispositivi offline',
302
+ 'es': 'Lista de dispositivos sin conexión',
303
+ 'pl': 'Lista urządzeń offline',
304
+ 'zh-cn': '线装置清单'
305
+ },
237
306
  'type': 'array',
238
307
  'role': 'json',
239
308
  'read': true,
240
309
  'write': false,
241
- 'def': [{Device: '--keine--', Adapter: '', Last_contact: ''}]
242
310
  },
243
311
  'native': {}
244
312
  });
245
313
  await this.setObjectNotExistsAsync(`${adptName}.listAll`, {
246
314
  'type': 'state',
247
315
  'common': {
248
- 'name': 'List all devices',
316
+ 'name': {
317
+ 'en': 'List of all devices',
318
+ 'de': 'Liste aller Geräte',
319
+ 'ru': 'Список всех устройств',
320
+ 'pt': 'Lista de todos os dispositivos',
321
+ 'nl': 'List van alle apparaten',
322
+ 'fr': 'Liste de tous les dispositifs',
323
+ 'it': 'Elenco di tutti i dispositivi',
324
+ 'es': 'Lista de todos los dispositivos',
325
+ 'pl': 'Lista wszystkich urządzeń',
326
+ 'zh-cn': '所有装置清单'
327
+ },
249
328
  'type': 'array',
250
329
  'role': 'json',
251
330
  'read': true,
252
331
  'write': false,
253
- 'def': [{Device: '--keine--', Adapter: '', Battery: '', Last_contact: '', Link_quality: ''}]
254
332
  },
255
333
  'native': {}
256
334
  });
257
335
  await this.setObjectNotExistsAsync(`${adptName}.linkQualityList`, {
258
336
  'type': 'state',
259
337
  'common': {
260
- 'name': 'List devices with qualitiy strength',
338
+ 'name': {
339
+ 'en': 'List of devices with signal strength',
340
+ 'de': 'Liste der Geräte mit Signalstärke',
341
+ 'ru': 'Список устройств с силой сигнала',
342
+ 'pt': 'Lista de dispositivos com força de sinal',
343
+ 'nl': 'List van apparaten met signaalkracht',
344
+ 'fr': 'Liste des dispositifs avec force de signal',
345
+ 'it': 'Elenco dei dispositivi con forza del segnale',
346
+ 'es': 'Lista de dispositivos con fuerza de señal',
347
+ 'pl': 'Lista urządzeń z siłą sygnałową',
348
+ 'zh-cn': '具有信号实力的装置清单'
349
+ },
261
350
  'type': 'array',
262
351
  'role': 'json',
263
352
  'read': true,
264
353
  'write': false,
265
- 'def': [{Device: '--keine--', Adapter: '', Link_quality: ''}]
266
354
  },
267
355
  'native': {}
268
356
  });
269
357
  await this.setObjectNotExistsAsync(`${adptName}.countAll`, {
270
358
  'type': 'state',
271
359
  'common': {
272
- 'name': 'Quantity devices all',
360
+ 'name': {
361
+ 'en': 'Number of all devices',
362
+ 'de': 'Anzahl aller Geräte',
363
+ 'ru': 'Количество всех устройств',
364
+ 'pt': 'Número de todos os dispositivos',
365
+ 'nl': 'Nummer van alle apparaten',
366
+ 'fr': 'Nombre de tous les appareils',
367
+ 'it': 'Numero di tutti i dispositivi',
368
+ 'es': 'Número de todos los dispositivos',
369
+ 'pl': 'Ilość wszystkich urządzeń',
370
+ 'zh-cn': '所有装置的数目'
371
+ },
273
372
  'type': 'number',
274
373
  'role': 'value',
275
374
  'read': true,
276
375
  'write': false,
277
- 'def': 0
278
376
  },
279
377
  'native': {}
280
378
  });
281
379
  await this.setObjectNotExistsAsync(`${adptName}.batteryList`, {
282
380
  'type': 'state',
283
381
  'common': {
284
- 'name': 'List devices with battery state',
382
+ 'name': {
383
+ 'en': 'List of devices with battery state',
384
+ 'de': 'Liste der Geräte mit Batteriezustand',
385
+ 'ru': 'Список устройств с состоянием батареи',
386
+ 'pt': 'Lista de dispositivos com estado da bateria',
387
+ 'nl': 'List van apparaten met batterij staat',
388
+ 'fr': 'Liste des appareils avec état de batterie',
389
+ 'it': 'Elenco dei dispositivi con stato della batteria',
390
+ 'es': 'Lista de dispositivos con estado de batería',
391
+ 'pl': 'Lista urządzeń z baterią stanową',
392
+ 'zh-cn': '电池国装置清单'
393
+ },
285
394
  'type': 'array',
286
395
  'role': 'json',
287
396
  'read': true,
288
397
  'write': false,
289
- 'def': [{Device: '--keine--', Adapter: '', Battery: ''}]
290
398
  },
291
399
  'native': {}
292
400
  });
293
401
  await this.setObjectNotExistsAsync(`${adptName}.lowBatteryList`, {
294
402
  'type': 'state',
295
403
  'common': {
296
- 'name': 'List devices with low battery state',
404
+ 'name': {
405
+ 'en': 'List of devices with low battery state',
406
+ 'de': 'Liste der Geräte mit niedrigem Batteriezustand',
407
+ 'ru': 'Список устройств с низким состоянием батареи',
408
+ 'pt': 'Lista de dispositivos com baixo estado da bateria',
409
+ 'nl': 'List van apparaten met lage batterij staat',
410
+ 'fr': 'Liste des appareils à faible état de batterie',
411
+ 'it': 'Elenco di dispositivi con stato di batteria basso',
412
+ 'es': 'Lista de dispositivos con estado de batería bajo',
413
+ 'pl': 'Lista urządzeń o niskim stanie baterii',
414
+ 'zh-cn': '低电池国家装置清单'
415
+ },
297
416
  'type': 'array',
298
417
  'role': 'json',
299
418
  'read': true,
300
419
  'write': false,
301
- 'def': [{Device: '--keine--', Adapter: '', Battery: ''}]
302
420
  },
303
421
  'native': {}
304
422
  });
305
423
  await this.setObjectNotExistsAsync(`${adptName}.lowBatteryCount`, {
306
424
  'type': 'state',
307
425
  'common': {
308
- 'name': 'Quantity devices with low battery',
426
+ 'name': {
427
+ 'en': 'Number of devices with low battery',
428
+ 'de': 'Anzahl der Geräte mit niedriger Batterie',
429
+ 'ru': 'Количество устройств c низкой батареей',
430
+ 'pt': 'Número de dispositivos com bateria baixa',
431
+ 'nl': 'Nummer van apparaten met lage batterij',
432
+ 'fr': 'Nombre de dispositifs avec batterie basse',
433
+ 'it': 'Numero di dispositivi con batteria bassa',
434
+ 'es': 'Número de dispositivos con batería baja',
435
+ 'pl': 'Liczba urządzeń z niską baterią',
436
+ 'zh-cn': '低电池的装置数量'
437
+ },
309
438
  'type': 'number',
310
439
  'role': 'value',
311
440
  'read': true,
312
441
  'write': false,
313
- 'def': 0
314
442
  },
315
443
  'native': {}
316
444
  });
317
445
  await this.setObjectNotExistsAsync(`${adptName}.batteryCount`, {
318
446
  'type': 'state',
319
447
  'common': {
320
- 'name': 'Quantity devices with battery',
448
+ 'name': {
449
+ 'en': 'Number of devices with battery',
450
+ 'de': 'Anzahl der Geräte mit Batterie',
451
+ 'ru': 'Количество устройств c батареей',
452
+ 'pt': 'Número de dispositivos com bateria',
453
+ 'nl': 'Nummer van apparaten met batterij',
454
+ 'fr': 'Nombre de dispositifs avec batterie',
455
+ 'it': 'Numero di dispositivi con batteria',
456
+ 'es': 'Número de dispositivos con batería',
457
+ 'pl': 'Liczba urządzeń z baterią',
458
+ 'zh-cn': '电池的装置数量'
459
+ },
321
460
  'type': 'number',
322
461
  'role': 'value',
323
462
  'read': true,
324
463
  'write': false,
325
- 'def': 0
326
464
  },
327
465
  'native': {}
328
466
  });
329
467
  }
330
468
 
331
- async main() {
332
- this.log.debug(`Function started: ${this.main.name}`);
333
-
334
- this.supAdapter = {
335
- alexa2: this.config.alexa2Devices,
336
- esphome: this.config.esphomeDevices,
337
- zigbee: this.config.zigbeeDevices,
338
- ble: this.config.bleDevices,
339
- sonoff: this.config.sonoffDevices,
340
- shelly: this.config.shellyDevices,
341
- homematic: this.config.homematicDevices,
342
- deconz: this.config.deconzDevices,
343
- zwave: this.config.zwaveDevices,
344
- dect: this.config.dectDevices,
345
- hue: this.config.hueDevices,
346
- hueExt: this.config.hueExtDevices,
347
- nukiExt: this.config.nukiExtDevices,
348
- ping: this.config.pingDevices,
349
- switchbotBle: this.config.switchbotBleDevices,
350
- sonos: this.config.sonosDevices,
351
- mihome: this.config.mihomeDevices,
352
- mihomeGW: this.config.mihomeDevices,
353
- test: false, // Only for Developer
354
- test2: false, // Only for Developer
355
- test3: false // Only for Developer
356
- };
469
+ async createData(i) {
470
+ const devices = await this.getForeignStatesAsync(this.arrDev[i].Selektor);
471
+ const deviceAdapterName = await this.capitalize(this.arrDev[i].adapter);
472
+ const myBlacklist = this.config.tableBlacklist;
357
473
 
358
- for(const [id] of Object.entries(this.arrApart)) {
359
- const idAdapter = this.supAdapter[id];
360
- if (idAdapter) {
361
- this.arrDev.push(this.arrApart[id]);
362
- this.adapterSelected.push(await this.capitalize(id));
363
- /*try {
364
- await this.createDPsForEachAdapter(id);
365
- this.log.debug(`Created datapoints for ${await this.capitalize(id)}`);
366
- } catch (e) {
367
- this.log.warn(`Error at creating datapoints for each adapter: ${e}`);
368
- }*/
369
- }
474
+ /*---------- Loop for blacklist ----------*/
475
+ for(const i in myBlacklist){
476
+ this.blacklistArr.push(myBlacklist[i].device);
477
+ this.log.debug(`Found items on the blacklist: ${this.blacklistArr}`);
370
478
  }
371
479
 
372
- //Check if one Adapter is selected.
373
- if (this.adapterSelected.length >= 1) {
374
- this.log.info(`Number of selected adapters: ${this.adapterSelected.length}. Loading data from: ${(this.adapterSelected).join(', ')} ...`);
375
- } else {
376
- this.log.warn(`No adapter selected. Please check the instance configuration!`);
377
- }
378
-
379
- this.log.debug(JSON.stringify(this.arrDev));
380
-
381
- /*=============================================
382
- = Start of main loop =
383
- =============================================*/
384
- for (let i = 0; i < this.arrDev.length; i++) {
385
- const devices = await this.getForeignStatesAsync(this.arrDev[i].Selektor);
386
- const deviceAdapterName = await this.capitalize(this.arrDev[i].adapter);
387
- const myBlacklist = this.config.tableBlacklist;
388
-
389
- /*---------- Loop for blacklist ----------*/
390
- for(const i in myBlacklist){
391
- this.blacklistArr.push(myBlacklist[i].device);
392
- this.log.debug(`Found items on the blacklist: ${this.blacklistArr}`);
393
- }
480
+ /*---------- Start of second main loop ----------*/
481
+ for(const [id] of Object.entries(devices)) {
482
+ if (!this.blacklistArr.includes(id)) {
394
483
 
395
- /*---------- Start of second main loop ----------*/
396
- for(const [id] of Object.entries(devices)) {
397
- if (!this.blacklistArr.includes(id)) {
484
+ const currDeviceString = id.slice(0, (id.lastIndexOf('.') + 1) - 1);
485
+ const shortCurrDeviceString = currDeviceString.slice(0, (currDeviceString.lastIndexOf('.') + 1) - 1);
398
486
 
399
- const currDeviceString = id.slice(0, (id.lastIndexOf('.') + 1) - 1);
400
- const shortCurrDeviceString = currDeviceString.slice(0, (currDeviceString.lastIndexOf('.') + 1) - 1);
487
+ //Get device name
488
+ const deviceObject = await this.getForeignObjectAsync(currDeviceString);
489
+ const shortDeviceObject = await this.getForeignObjectAsync(shortCurrDeviceString);
490
+ let deviceName;
401
491
 
402
- //Get device name
403
- const deviceObject = await this.getForeignObjectAsync(currDeviceString);
404
- const shortDeviceObject = await this.getForeignObjectAsync(shortCurrDeviceString);
405
- let deviceName;
492
+ switch (this.arrDev[i].adapter) {
493
+ case 'switchbotBle': //Get ID for Switchbot and ESPHome Devices
494
+ case 'esphome':
495
+ deviceName = await this.getInitValue(currDeviceString + this.arrDev[i].id);
496
+ break;
406
497
 
407
- if (deviceObject && typeof deviceObject === 'object') {
408
- deviceName = deviceObject.common.name;
409
- }
410
-
411
- if (shortDeviceObject && typeof shortDeviceObject === 'object') {
412
- if (this.arrDev[i].adapter === 'hue extended') {
498
+ case 'hue-extended':
499
+ if (shortDeviceObject && typeof shortDeviceObject === 'object') {
413
500
  deviceName = shortDeviceObject.common.name;
414
501
  }
415
- }
502
+ break;
416
503
 
417
- //Get ID for Switchbot and ESPHome Devices
418
- switch (this.arrDev[i].adapter) {
419
- case 'switchbot ble':
420
- case 'esphome':
421
- deviceName = await this.getInitValue(currDeviceString + this.arrDev[i].id);
422
- break;
423
- }
424
-
425
- // 1. Get link quality
426
- let deviceQualityState;
427
- let linkQuality;
504
+ default:
505
+ if (deviceObject && typeof deviceObject === 'object') {
506
+ deviceName = deviceObject.common.name;
507
+ }
508
+ break;
509
+ }
428
510
 
429
- switch (this.arrDev[i].adapter) {
430
- case 'homematic':
431
- case 'sonoff':
432
- deviceQualityState = await this.getForeignStateAsync(currDeviceString + this.arrDev[i].rssiState);
433
- break;
434
- default:
435
- deviceQualityState = await this.getForeignStateAsync(id);
436
- }
511
+ // 1. Get link quality
512
+ let deviceQualityState;
513
+ let linkQuality;
514
+
515
+ switch (this.arrDev[i].adapter) {
516
+ case 'homematic':
517
+ case 'sonoff':
518
+ deviceQualityState = await this.getForeignStateAsync(currDeviceString + this.arrDev[i].rssiState);
519
+ break;
520
+ default:
521
+ deviceQualityState = await this.getForeignStateAsync(id);
522
+ }
437
523
 
438
- if ((deviceQualityState) && (typeof deviceQualityState.val === 'number')){
439
- if (this.config.trueState) {
440
- linkQuality = deviceQualityState.val;
441
- } else {
442
- if (deviceQualityState.val < 0) {
443
- linkQuality = Math.min(Math.max(2 * (deviceQualityState.val + 100), 0), 100) + '%';
444
- } else if ((deviceQualityState.val) >= 0) {
445
- linkQuality = parseFloat((100/255 * deviceQualityState.val).toFixed(0)) + '%';
446
- }
447
- }
448
- this.linkQualityDevices.push(
449
- {
450
- Device: deviceName,
451
- Adapter: deviceAdapterName,
452
- Link_quality: linkQuality
453
- }
454
- );
524
+ if ((deviceQualityState) && (typeof deviceQualityState.val === 'number')){
525
+ if (this.config.trueState) {
526
+ linkQuality = deviceQualityState.val;
455
527
  } else {
456
- // no linkQuality available for powered devices
457
- linkQuality = ' - ';
528
+ if (deviceQualityState.val < 0) {
529
+ linkQuality = Math.min(Math.max(2 * (deviceQualityState.val + 100), 0), 100) + '%';
530
+ } else if ((deviceQualityState.val) >= 0) {
531
+ linkQuality = parseFloat((100/255 * deviceQualityState.val).toFixed(0)) + '%';
532
+ }
458
533
  }
534
+ this.linkQualityDevices.push(
535
+ {
536
+ 'Device': deviceName,
537
+ 'Adapter': deviceAdapterName,
538
+ 'Signal strength': linkQuality
539
+ }
540
+ );
541
+ } else {
542
+ // no linkQuality available for powered devices
543
+ linkQuality = ' - ';
544
+ }
459
545
 
460
- // 1b. Count how many devices with link Quality
461
- this.linkQualityCount = this.linkQualityDevices.length;
546
+ // 1b. Count how many devices with link Quality
547
+ this.linkQualityCount = this.linkQualityDevices.length;
462
548
 
463
- // 2. When was the last contact to the device?
464
- let lastContactString;
549
+ // 2. When was the last contact to the device?
550
+ let lastContactString;
465
551
 
466
- const deviceMainSelector = await this.getForeignStateAsync(id);
467
- let deviceState = 'Online';
468
- if (deviceMainSelector) {
469
- try {
470
- const time = new Date();
471
- const lastContact = Math.round((time.getTime() - deviceMainSelector.ts) / 1000 / 60);
472
- const lastStateChange = Math.round((time.getTime() - deviceMainSelector.lc) / 1000 / 60);
473
- const deviceUnreachState = await this.getInitValue(currDeviceString + this.arrDev[i].reach);
552
+ const deviceMainSelector = await this.getForeignStateAsync(id);
553
+ let deviceState = 'Online';
554
+ if (deviceMainSelector) {
555
+ try {
556
+ const time = new Date();
557
+ const lastContact = Math.round((time.getTime() - deviceMainSelector.ts) / 1000 / 60);
558
+ const lastStateChange = Math.round((time.getTime() - deviceMainSelector.lc) / 1000 / 60);
559
+ const deviceUnreachState = await this.getInitValue(currDeviceString + this.arrDev[i].reach);
474
560
 
475
561
 
476
- const getLastContact = async () => {
477
- lastContactString = this.formatDate(new Date((deviceMainSelector.ts)), 'hh:mm') + ' Uhr';
478
- if (Math.round(lastContact) > 100) {
479
- lastContactString = Math.round(lastContact/60) + ' Stunden';
480
- }
481
- if (Math.round(lastContact/60) > 48) {
482
- lastContactString = Math.round(lastContact/60/24) + ' Tagen';
483
- }
484
- return lastContactString;
485
- };
562
+ const getLastContact = async () => {
563
+ lastContactString = this.formatDate(new Date((deviceMainSelector.ts)), 'hh:mm') + ' Uhr';
564
+ if (Math.round(lastContact) > 100) {
565
+ lastContactString = Math.round(lastContact/60) + ' Stunden';
566
+ }
567
+ if (Math.round(lastContact/60) > 48) {
568
+ lastContactString = Math.round(lastContact/60/24) + ' Tagen';
569
+ }
570
+ return lastContactString;
571
+ };
486
572
 
487
- const getLastStateChange = async () => {
488
- lastContactString = this.formatDate(new Date((deviceMainSelector.lc)), 'hh:mm') + ' Uhr';
489
- if (Math.round(lastStateChange) > 100) {
490
- lastContactString = Math.round(lastStateChange/60) + ' Stunden';
491
- }
492
- if (Math.round(lastStateChange/60) > 48) {
493
- lastContactString = Math.round(lastStateChange/60/24) + ' Tagen';
494
- }
495
- return lastContactString;
496
- };
497
-
498
- // 2b. wenn seit X Minuten kein Kontakt mehr besteht, nimm Gerät in Liste auf
499
- //Rechne auf Tage um, wenn mehr als 48 Stunden seit letztem Kontakt vergangen sind
500
- //lastContactString = Math.round(lastContact) + ' Minuten';
501
- switch (this.arrDev[i].adapter) {
502
- case 'ping':
503
- //State changed
504
- if (!deviceUnreachState) {
505
- await getLastStateChange();
506
- } else {
507
- await getLastContact();
508
- }
509
- break;
573
+ const getLastStateChange = async () => {
574
+ lastContactString = this.formatDate(new Date((deviceMainSelector.lc)), 'hh:mm') + ' Uhr';
575
+ if (Math.round(lastStateChange) > 100) {
576
+ lastContactString = Math.round(lastStateChange/60) + ' Stunden';
577
+ }
578
+ if (Math.round(lastStateChange/60) > 48) {
579
+ lastContactString = Math.round(lastStateChange/60/24) + ' Tagen';
580
+ }
581
+ return lastContactString;
582
+ };
510
583
 
511
- default:
584
+ // 2b. wenn seit X Minuten kein Kontakt mehr besteht, nimm Gerät in Liste auf
585
+ //Rechne auf Tage um, wenn mehr als 48 Stunden seit letztem Kontakt vergangen sind
586
+ //lastContactString = Math.round(lastContact) + ' Minuten';
587
+ switch (this.arrDev[i].adapter) {
588
+ case 'ping':
589
+ //State changed
590
+ if (!deviceUnreachState) {
591
+ await getLastStateChange();
592
+ } else {
512
593
  await getLastContact();
513
- break;
514
- }
594
+ }
595
+ break;
515
596
 
516
- switch (this.arrDev[i].adapter) {
517
- case 'alexa2':
518
- if (this.config.alexa2MaxMinutes === -1) {
519
- if (!deviceUnreachState) {
520
- deviceState = 'Offline'; //set online state to offline
521
- this.offlineDevices.push(
522
- {
523
- Device: deviceName,
524
- Adapter: deviceAdapterName,
525
- Last_contact: lastContactString
526
- }
527
- );
528
- }
529
- } else if (lastContact > this.config.alexa2MaxMinutes) {
597
+ default:
598
+ await getLastContact();
599
+ break;
600
+ }
601
+
602
+ switch (this.arrDev[i].adapter) {
603
+ case 'alexa2':
604
+ if (this.config.alexa2MaxMinutes === -1) {
605
+ if (!deviceUnreachState) {
530
606
  deviceState = 'Offline'; //set online state to offline
531
607
  this.offlineDevices.push(
532
608
  {
533
- Device: deviceName,
534
- Adapter: deviceAdapterName,
535
- Last_contact: lastContactString
609
+ 'Device': deviceName,
610
+ 'Adapter': deviceAdapterName,
611
+ 'Last contact': lastContactString
536
612
  }
537
613
  );
538
614
  }
539
- break;
540
- case 'ble':
541
- if (this.config.bleMaxMinutes === -1) {
542
- if (!deviceUnreachState) {
543
- deviceState = 'Offline'; //set online state to offline
544
- this.offlineDevices.push(
545
- {
546
- Device: deviceName,
547
- Adapter: deviceAdapterName,
548
- Last_contact: lastContactString
549
- }
550
- );
615
+ } else if (lastContact > this.config.alexa2MaxMinutes) {
616
+ deviceState = 'Offline'; //set online state to offline
617
+ this.offlineDevices.push(
618
+ {
619
+ 'Device': deviceName,
620
+ 'Adapter': deviceAdapterName,
621
+ 'Last contact': lastContactString
551
622
  }
552
- } else if (lastContact > this.config.bleMaxMinutes) {
623
+ );
624
+ }
625
+ break;
626
+ case 'ble':
627
+ if (this.config.bleMaxMinutes === -1) {
628
+ if (!deviceUnreachState) {
553
629
  deviceState = 'Offline'; //set online state to offline
554
630
  this.offlineDevices.push(
555
631
  {
556
- Device: deviceName,
557
- Adapter: deviceAdapterName,
558
- Last_contact: lastContactString
632
+ 'Device': deviceName,
633
+ 'Adapter': deviceAdapterName,
634
+ 'Last contact': lastContactString
559
635
  }
560
636
  );
561
637
  }
562
- break;
563
- case 'deconz':
564
- if (this.config.deconzMaxMinutes === -1) {
565
- if (!deviceUnreachState) {
566
- deviceState = 'Offline'; //set online state to offline
567
- this.offlineDevices.push(
568
- {
569
- Device: deviceName,
570
- Adapter: deviceAdapterName,
571
- Last_contact: lastContactString
572
- }
573
- );
638
+ } else if (lastContact > this.config.bleMaxMinutes) {
639
+ deviceState = 'Offline'; //set online state to offline
640
+ this.offlineDevices.push(
641
+ {
642
+ 'Device': deviceName,
643
+ 'Adapter': deviceAdapterName,
644
+ 'Last contact': lastContactString
574
645
  }
575
- } else if (lastContact > this.config.deconzMaxMinutes) {
646
+ );
647
+ }
648
+ break;
649
+ case 'deconz':
650
+ if (this.config.deconzMaxMinutes === -1) {
651
+ if (!deviceUnreachState) {
576
652
  deviceState = 'Offline'; //set online state to offline
577
653
  this.offlineDevices.push(
578
654
  {
579
- Device: deviceName,
580
- Adapter: deviceAdapterName,
581
- Last_contact: lastContactString
655
+ 'Device': deviceName,
656
+ 'Adapter': deviceAdapterName,
657
+ 'Last contact': lastContactString
582
658
  }
583
659
  );
584
660
  }
585
- break;
586
- case 'esphome':
587
- if (this.config.esphomeMaxMinutes === -1) {
588
- if (!deviceUnreachState) {
589
- deviceState = 'Offline'; //set online state to offline
590
- this.offlineDevices.push(
591
- {
592
- Device: deviceName,
593
- Adapter: deviceAdapterName,
594
- Last_contact: lastContactString
595
- }
596
- );
661
+ } else if (lastContact > this.config.deconzMaxMinutes) {
662
+ deviceState = 'Offline'; //set online state to offline
663
+ this.offlineDevices.push(
664
+ {
665
+ 'Device': deviceName,
666
+ 'Adapter': deviceAdapterName,
667
+ 'Last contact': lastContactString
597
668
  }
598
- } else if (lastContact > this.config.esphomeMaxMinutes) {
669
+ );
670
+ }
671
+ break;
672
+ case 'esphome':
673
+ if (this.config.esphomeMaxMinutes === -1) {
674
+ if (!deviceUnreachState) {
599
675
  deviceState = 'Offline'; //set online state to offline
600
676
  this.offlineDevices.push(
601
677
  {
602
- Device: deviceName,
603
- Adapter: deviceAdapterName,
604
- Last_contact: lastContactString
678
+ 'Device': deviceName,
679
+ 'Adapter': deviceAdapterName,
680
+ 'Last contact': lastContactString
605
681
  }
606
682
  );
607
683
  }
608
- break;
609
- case 'fritzDect':
610
- if (this.config.fritzdectMaxMinutes === -1) {
611
- if (!deviceUnreachState) {
612
- deviceState = 'Offline'; //set online state to offline
613
- this.offlineDevices.push(
614
- {
615
- Device: deviceName,
616
- Adapter: deviceAdapterName,
617
- Last_contact: lastContactString
618
- }
619
- );
684
+ } else if (lastContact > this.config.esphomeMaxMinutes) {
685
+ deviceState = 'Offline'; //set online state to offline
686
+ this.offlineDevices.push(
687
+ {
688
+ 'Device': deviceName,
689
+ 'Adapter': deviceAdapterName,
690
+ 'Last contact': lastContactString
620
691
  }
621
- } else if (lastContact > this.config.fritzdectMaxMinutes) {
692
+ );
693
+ }
694
+ break;
695
+ case 'fritzDect':
696
+ if (this.config.fritzdectMaxMinutes === -1) {
697
+ if (!deviceUnreachState) {
622
698
  deviceState = 'Offline'; //set online state to offline
623
699
  this.offlineDevices.push(
624
700
  {
625
- Device: deviceName,
626
- Adapter: deviceAdapterName,
627
- Last_contact: lastContactString
701
+ 'Device': deviceName,
702
+ 'Adapter': deviceAdapterName,
703
+ 'Last contact': lastContactString
628
704
  }
629
705
  );
630
706
  }
631
- break;
632
- case 'homematic':
633
- if (this.config.homematicMaxMinutes === -1) {
634
- if (deviceUnreachState) {
635
- deviceState = 'Offline'; //set online state to offline
636
- this.offlineDevices.push(
637
- {
638
- Device: deviceName,
639
- Adapter: deviceAdapterName,
640
- Last_contact: lastContactString
641
- }
642
- );
707
+ } else if (lastContact > this.config.fritzdectMaxMinutes) {
708
+ deviceState = 'Offline'; //set online state to offline
709
+ this.offlineDevices.push(
710
+ {
711
+ 'Device': deviceName,
712
+ 'Adapter': deviceAdapterName,
713
+ 'Last contact': lastContactString
643
714
  }
644
- } else if (lastContact > this.config.homematicMaxMinutes) {
715
+ );
716
+ }
717
+ break;
718
+ case 'homematic':
719
+ if (this.config.homematicMaxMinutes === -1) {
720
+ if (deviceUnreachState) {
645
721
  deviceState = 'Offline'; //set online state to offline
646
722
  this.offlineDevices.push(
647
723
  {
648
- Device: deviceName,
649
- Adapter: deviceAdapterName,
650
- Last_contact: lastContactString
724
+ 'Device': deviceName,
725
+ 'Adapter': deviceAdapterName,
726
+ 'Last contact': lastContactString
651
727
  }
652
728
  );
653
729
  }
654
- break;
655
- case 'hue':
656
- if (this.config.hueMaxMinutes === -1) {
657
- if (!deviceUnreachState) {
658
- deviceState = 'Offline'; //set online state to offline
659
- this.offlineDevices.push(
660
- {
661
- Device: deviceName,
662
- Adapter: deviceAdapterName,
663
- Last_contact: lastContactString
664
- }
665
- );
730
+ } else if (lastContact > this.config.homematicMaxMinutes) {
731
+ deviceState = 'Offline'; //set online state to offline
732
+ this.offlineDevices.push(
733
+ {
734
+ 'Device': deviceName,
735
+ 'Adapter': deviceAdapterName,
736
+ 'Last contact': lastContactString
666
737
  }
667
- } else if (lastContact > this.config.hueMaxMinutes) {
738
+ );
739
+ }
740
+ break;
741
+ case 'hue':
742
+ if (this.config.hueMaxMinutes === -1) {
743
+ if (!deviceUnreachState) {
668
744
  deviceState = 'Offline'; //set online state to offline
669
745
  this.offlineDevices.push(
670
746
  {
671
- Device: deviceName,
672
- Adapter: deviceAdapterName,
673
- Last_contact: lastContactString
747
+ 'Device': deviceName,
748
+ 'Adapter': deviceAdapterName,
749
+ 'Last contact': lastContactString
674
750
  }
675
751
  );
676
752
  }
677
- break;
678
- case 'hue extended':
679
- if (this.config.hueextMaxMinutes === -1) {
680
- if (!deviceUnreachState) {
681
- deviceState = 'Offline'; //set online state to offline
682
- this.offlineDevices.push(
683
- {
684
- Device: deviceName,
685
- Adapter: deviceAdapterName,
686
- Last_contact: lastContactString
687
- }
688
- );
753
+ } else if (lastContact > this.config.hueMaxMinutes) {
754
+ deviceState = 'Offline'; //set online state to offline
755
+ this.offlineDevices.push(
756
+ {
757
+ 'Device': deviceName,
758
+ 'Adapter': deviceAdapterName,
759
+ 'Last contact': lastContactString
689
760
  }
690
- } else if (lastContact > this.config.hueextMaxMinutes) {
761
+ );
762
+ }
763
+ break;
764
+ case 'hue-extended':
765
+ if (this.config.hueextMaxMinutes === -1) {
766
+ if (!deviceUnreachState) {
691
767
  deviceState = 'Offline'; //set online state to offline
692
768
  this.offlineDevices.push(
693
769
  {
694
- Device: deviceName,
695
- Adapter: deviceAdapterName,
696
- Last_contact: lastContactString
770
+ 'Device': deviceName,
771
+ 'Adapter': deviceAdapterName,
772
+ 'Last contact': lastContactString
697
773
  }
698
774
  );
699
775
  }
700
- break;
701
- case 'miHome':
702
- if (this.config.mihomeMaxMinutes === -1) {
703
- if (!deviceUnreachState) {
704
- deviceState = 'Offline'; //set online state to offline
705
- this.offlineDevices.push(
706
- {
707
- Device: deviceName,
708
- Adapter: deviceAdapterName,
709
- Last_contact: lastContactString
710
- }
711
- );
776
+ } else if (lastContact > this.config.hueextMaxMinutes) {
777
+ deviceState = 'Offline'; //set online state to offline
778
+ this.offlineDevices.push(
779
+ {
780
+ 'Device': deviceName,
781
+ 'Adapter': deviceAdapterName,
782
+ 'Last contact': lastContactString
712
783
  }
713
- } else if (lastContact > this.config.mihomeMaxMinutes) {
784
+ );
785
+ }
786
+ break;
787
+ case 'miHome':
788
+ if (this.config.mihomeMaxMinutes === -1) {
789
+ if (!deviceUnreachState) {
714
790
  deviceState = 'Offline'; //set online state to offline
715
791
  this.offlineDevices.push(
716
792
  {
717
- Device: deviceName,
718
- Adapter: deviceAdapterName,
719
- Last_contact: lastContactString
793
+ 'Device': deviceName,
794
+ 'Adapter': deviceAdapterName,
795
+ 'Last contact': lastContactString
720
796
  }
721
797
  );
722
798
  }
723
- break;
724
- case 'nuki_extended':
725
- if (this.config.nukiextendMaxMinutes === -1) {
726
- if (!deviceUnreachState) {
727
- deviceState = 'Offline'; //set online state to offline
728
- this.offlineDevices.push(
729
- {
730
- Device: deviceName,
731
- Adapter: deviceAdapterName,
732
- Last_contact: lastContactString
733
- }
734
- );
799
+ } else if (lastContact > this.config.mihomeMaxMinutes) {
800
+ deviceState = 'Offline'; //set online state to offline
801
+ this.offlineDevices.push(
802
+ {
803
+ 'Device': deviceName,
804
+ 'Adapter': deviceAdapterName,
805
+ 'Last contact': lastContactString
735
806
  }
736
- } else if (lastContact > this.config.nukiextendMaxMinutes) {
807
+ );
808
+ }
809
+ break;
810
+ case 'nuki-extended':
811
+ if (this.config.nukiextendMaxMinutes === -1) {
812
+ if (!deviceUnreachState) {
737
813
  deviceState = 'Offline'; //set online state to offline
738
814
  this.offlineDevices.push(
739
815
  {
740
- Device: deviceName,
741
- Adapter: deviceAdapterName,
742
- Last_contact: lastContactString
816
+ 'Device': deviceName,
817
+ 'Adapter': deviceAdapterName,
818
+ 'Last contact': lastContactString
743
819
  }
744
820
  );
745
821
  }
746
- break;
747
- case 'ping':
748
- if (this.config.pingMaxMinutes === -1) {
749
- if (!deviceUnreachState) {
750
- deviceState = 'Offline'; //set online state to offline
751
- this.offlineDevices.push(
752
- {
753
- Device: deviceName,
754
- Adapter: deviceAdapterName,
755
- Last_contact: lastContactString
756
- }
757
- );
822
+ } else if (lastContact > this.config.nukiextendMaxMinutes) {
823
+ deviceState = 'Offline'; //set online state to offline
824
+ this.offlineDevices.push(
825
+ {
826
+ 'Device': deviceName,
827
+ 'Adapter': deviceAdapterName,
828
+ 'Last contact': lastContactString
758
829
  }
759
- } else if ((lastStateChange > this.config.pingMaxMinutes) && (!deviceUnreachState)) {
830
+ );
831
+ }
832
+ break;
833
+ case 'ping':
834
+ if (this.config.pingMaxMinutes === -1) {
835
+ if (!deviceUnreachState) {
760
836
  deviceState = 'Offline'; //set online state to offline
761
837
  this.offlineDevices.push(
762
838
  {
763
- Device: deviceName,
764
- Adapter: deviceAdapterName,
765
- Last_contact: lastContactString
839
+ 'Device': deviceName,
840
+ 'Adapter': deviceAdapterName,
841
+ 'Last contact': lastContactString
766
842
  }
767
843
  );
768
844
  }
769
- break;
770
- case 'shelly':
771
- if (this.config.shellyMaxMinutes === -1) {
772
- if (!deviceUnreachState) {
773
- deviceState = 'Offline'; //set online state to offline
774
- this.offlineDevices.push(
775
- {
776
- Device: deviceName,
777
- Adapter: deviceAdapterName,
778
- Last_contact: lastContactString
779
- }
780
- );
845
+ } else if ((lastStateChange > this.config.pingMaxMinutes) && (!deviceUnreachState)) {
846
+ deviceState = 'Offline'; //set online state to offline
847
+ this.offlineDevices.push(
848
+ {
849
+ 'Device': deviceName,
850
+ 'Adapter': deviceAdapterName,
851
+ 'Last contact': lastContactString
781
852
  }
782
- } else if (lastContact > this.config.shellyMaxMinutes) {
853
+ );
854
+ }
855
+ break;
856
+ case 'shelly':
857
+ if (this.config.shellyMaxMinutes === -1) {
858
+ if (!deviceUnreachState) {
783
859
  deviceState = 'Offline'; //set online state to offline
784
860
  this.offlineDevices.push(
785
861
  {
786
- Device: deviceName,
787
- Adapter: deviceAdapterName,
788
- Last_contact: lastContactString
862
+ 'Device': deviceName,
863
+ 'Adapter': deviceAdapterName,
864
+ 'Last contact': lastContactString
789
865
  }
790
866
  );
791
867
  }
792
- break;
793
- case 'sonoff':
794
- if (this.config.sonoffMaxMinutes === -1) {
795
- if (!deviceUnreachState) {
796
- deviceState = 'Offline'; //set online state to offline
797
- this.offlineDevices.push(
798
- {
799
- Device: deviceName,
800
- Adapter: deviceAdapterName,
801
- Last_contact: lastContactString
802
- }
803
- );
868
+ } else if (lastContact > this.config.shellyMaxMinutes) {
869
+ deviceState = 'Offline'; //set online state to offline
870
+ this.offlineDevices.push(
871
+ {
872
+ 'Device': deviceName,
873
+ 'Adapter': deviceAdapterName,
874
+ 'Last contact': lastContactString
804
875
  }
805
- } else if (lastContact > this.config.sonoffMaxMinutes) {
876
+ );
877
+ }
878
+ break;
879
+ case 'sonoff':
880
+ if (this.config.sonoffMaxMinutes === -1) {
881
+ if (!deviceUnreachState) {
806
882
  deviceState = 'Offline'; //set online state to offline
807
883
  this.offlineDevices.push(
808
884
  {
809
- Device: deviceName,
810
- Adapter: deviceAdapterName,
811
- Last_contact: lastContactString
885
+ 'Device': deviceName,
886
+ 'Adapter': deviceAdapterName,
887
+ 'Last contact': lastContactString
812
888
  }
813
889
  );
814
890
  }
815
- break;
816
- case 'sonos':
817
- if (this.config.sonosMaxMinutes === -1) {
818
- if (!deviceUnreachState) {
819
- deviceState = 'Offline'; //set online state to offline
820
- this.offlineDevices.push(
821
- {
822
- Device: deviceName,
823
- Adapter: deviceAdapterName,
824
- Last_contact: lastContactString
825
- }
826
- );
891
+ } else if (lastContact > this.config.sonoffMaxMinutes) {
892
+ deviceState = 'Offline'; //set online state to offline
893
+ this.offlineDevices.push(
894
+ {
895
+ 'Device': deviceName,
896
+ 'Adapter': deviceAdapterName,
897
+ 'Last contact': lastContactString
827
898
  }
828
- } else if (lastContact > this.config.sonosMaxMinutes) {
899
+ );
900
+ }
901
+ break;
902
+ case 'sonos':
903
+ if (this.config.sonosMaxMinutes === -1) {
904
+ if (!deviceUnreachState) {
829
905
  deviceState = 'Offline'; //set online state to offline
830
906
  this.offlineDevices.push(
831
907
  {
832
- Device: deviceName,
833
- Adapter: deviceAdapterName,
834
- Last_contact: lastContactString
908
+ 'Device': deviceName,
909
+ 'Adapter': deviceAdapterName,
910
+ 'Last contact': lastContactString
835
911
  }
836
912
  );
837
913
  }
838
- break;
839
- case 'switchbot ble':
840
- if (this.config.switchbotMaxMinutes === -1) {
841
- if (!deviceUnreachState) {
842
- deviceState = 'Offline'; //set online state to offline
843
- this.offlineDevices.push(
844
- {
845
- Device: deviceName,
846
- Adapter: deviceAdapterName,
847
- Last_contact: lastContactString
848
- }
849
- );
914
+ } else if (lastContact > this.config.sonosMaxMinutes) {
915
+ deviceState = 'Offline'; //set online state to offline
916
+ this.offlineDevices.push(
917
+ {
918
+ 'Device': deviceName,
919
+ 'Adapter': deviceAdapterName,
920
+ 'Last contact': lastContactString
850
921
  }
851
- } else if (lastContact > this.config.switchbotMaxMinutes) {
922
+ );
923
+ }
924
+ break;
925
+ case 'switchbotBle':
926
+ if (this.config.switchbotMaxMinutes === -1) {
927
+ if (!deviceUnreachState) {
852
928
  deviceState = 'Offline'; //set online state to offline
853
929
  this.offlineDevices.push(
854
930
  {
855
- Device: deviceName,
856
- Adapter: deviceAdapterName,
857
- Last_contact: lastContactString
931
+ 'Device': deviceName,
932
+ 'Adapter': deviceAdapterName,
933
+ 'Last contact': lastContactString
858
934
  }
859
935
  );
860
936
  }
861
- break;
862
- case 'zigbee':
863
- if (this.config.zigbeeMaxMinutes === -1) {
864
- if (!deviceUnreachState) {
865
- deviceState = 'Offline'; //set online state to offline
866
- this.offlineDevices.push(
867
- {
868
- Device: deviceName,
869
- Adapter: deviceAdapterName,
870
- Last_contact: lastContactString
871
- }
872
- );
937
+ } else if (lastContact > this.config.switchbotMaxMinutes) {
938
+ deviceState = 'Offline'; //set online state to offline
939
+ this.offlineDevices.push(
940
+ {
941
+ 'Device': deviceName,
942
+ 'Adapter': deviceAdapterName,
943
+ 'Last contact': lastContactString
873
944
  }
874
- } else if (lastContact > this.config.zigbeeMaxMinutes) {
945
+ );
946
+ }
947
+ break;
948
+ case 'zigbee':
949
+ if (this.config.zigbeeMaxMinutes === -1) {
950
+ if (!deviceUnreachState) {
875
951
  deviceState = 'Offline'; //set online state to offline
876
952
  this.offlineDevices.push(
877
953
  {
878
- Device: deviceName,
879
- Adapter: deviceAdapterName,
880
- Last_contact: lastContactString
954
+ 'Device': deviceName,
955
+ 'Adapter': deviceAdapterName,
956
+ 'Last contact': lastContactString
881
957
  }
882
958
  );
883
959
  }
884
- break;
885
- case 'zwave':
886
- if (this.config.zwaveMaxMinutes === -1) {
887
- if (!deviceUnreachState) {
888
- deviceState = 'Offline'; //set online state to offline
889
- this.offlineDevices.push(
890
- {
891
- Device: deviceName,
892
- Adapter: deviceAdapterName,
893
- Last_contact: lastContactString
894
- }
895
- );
960
+ } else if (lastContact > this.config.zigbeeMaxMinutes) {
961
+ deviceState = 'Offline'; //set online state to offline
962
+ this.offlineDevices.push(
963
+ {
964
+ 'Device': deviceName,
965
+ 'Adapter': deviceAdapterName,
966
+ 'Last contact': lastContactString
896
967
  }
897
- } else if (lastContact > this.config.zwaveMaxMinutes) {
968
+ );
969
+ }
970
+ break;
971
+ case 'zwave':
972
+ if (this.config.zwaveMaxMinutes === -1) {
973
+ if (!deviceUnreachState) {
898
974
  deviceState = 'Offline'; //set online state to offline
899
975
  this.offlineDevices.push(
900
976
  {
901
- Device: deviceName,
902
- Adapter: deviceAdapterName,
903
- Last_contact: lastContactString
977
+ 'Device': deviceName,
978
+ 'Adapter': deviceAdapterName,
979
+ 'Last contact': lastContactString
904
980
  }
905
981
  );
906
982
  }
907
- break;
908
- }
909
- } catch (e) {
910
- this.log.error(`(03) Error while getting timestate ${e}`);
983
+ } else if (lastContact > this.config.zwaveMaxMinutes) {
984
+ deviceState = 'Offline'; //set online state to offline
985
+ this.offlineDevices.push(
986
+ {
987
+ 'Device': deviceName,
988
+ 'Adapter': deviceAdapterName,
989
+ 'Last contact': lastContactString
990
+ }
991
+ );
992
+ }
993
+ break;
911
994
  }
995
+ } catch (e) {
996
+ this.log.error(`(03) Error while getting timestate ${e}`);
912
997
  }
998
+ }
913
999
 
914
1000
 
915
1001
 
916
- // 2c. Count how many devcies are offline
917
- this.offlineDevicesCount = this.offlineDevices.length;
1002
+ // 2c. Count how many devcies are offline
1003
+ this.offlineDevicesCount = this.offlineDevices.length;
918
1004
 
919
- // 3. Get battery states
920
- const deviceBatteryState = await this.getInitValue(currDeviceString + this.arrDev[i].battery);
921
- const shortDeviceBatteryState = await this.getInitValue(shortCurrDeviceString + this.arrDev[i].battery);
922
- let batteryHealth;
1005
+ // 3. Get battery states
1006
+ const deviceBatteryState = await this.getInitValue(currDeviceString + this.arrDev[i].battery);
1007
+ const shortDeviceBatteryState = await this.getInitValue(shortCurrDeviceString + this.arrDev[i].battery);
1008
+ let batteryHealth;
923
1009
 
924
- if ((!deviceBatteryState) && (!shortDeviceBatteryState)) {
925
- batteryHealth = ' - ';
926
- } else {
1010
+ if ((!deviceBatteryState) && (!shortDeviceBatteryState)) {
1011
+ batteryHealth = ' - ';
1012
+ } else {
927
1013
 
928
- switch (this.arrDev[i].adapter) {
929
- case 'homematic':
930
- if (deviceBatteryState === 0) {
931
- batteryHealth = ' - ';
932
- } else {
933
- batteryHealth = deviceBatteryState + 'V';
934
- }
1014
+ switch (this.arrDev[i].adapter) {
1015
+ case 'homematic':
1016
+ if (deviceBatteryState === 0) {
1017
+ batteryHealth = ' - ';
1018
+ } else {
1019
+ batteryHealth = deviceBatteryState + 'V';
1020
+ }
935
1021
 
936
- this.batteryPowered.push(
937
- {
938
- Device: deviceName,
939
- Adapter: deviceAdapterName,
940
- Battery: batteryHealth
941
- }
942
- );
943
- break;
944
- case 'hue extended':
945
- if (shortDeviceBatteryState) {
946
- batteryHealth = shortDeviceBatteryState + '%';
947
- this.batteryPowered.push(
948
- {
949
- Device: deviceName,
950
- Adapter: deviceAdapterName,
951
- Battery: batteryHealth
952
- }
953
- );
1022
+ this.batteryPowered.push(
1023
+ {
1024
+ 'Device': deviceName,
1025
+ 'Adapter': deviceAdapterName,
1026
+ 'Battery': batteryHealth
954
1027
  }
955
- break;
956
- default:
957
- batteryHealth = (deviceBatteryState) + '%';
1028
+ );
1029
+ break;
1030
+ case 'hue-extended':
1031
+ if (shortDeviceBatteryState) {
1032
+ batteryHealth = shortDeviceBatteryState + '%';
958
1033
  this.batteryPowered.push(
959
1034
  {
960
- Device: deviceName,
961
- Adapter: deviceAdapterName,
962
- Battery: batteryHealth
1035
+ 'Device': deviceName,
1036
+ 'Adapter': deviceAdapterName,
1037
+ 'Battery': batteryHealth
963
1038
  }
964
1039
  );
965
- }
1040
+ }
1041
+ break;
1042
+ default:
1043
+ batteryHealth = (deviceBatteryState) + '%';
1044
+ this.batteryPowered.push(
1045
+ {
1046
+ 'Device': deviceName,
1047
+ 'Adapter': deviceAdapterName,
1048
+ 'Battery': batteryHealth
1049
+ }
1050
+ );
966
1051
  }
1052
+ }
967
1053
 
968
- // 3b. Count how many devices are with battery
969
- this.batteryPoweredCount = this.batteryPowered.length;
1054
+ // 3b. Count how many devices are with battery
1055
+ this.batteryPoweredCount = this.batteryPowered.length;
970
1056
 
971
- // 3c. Count how many devices are with low battery
972
- const batteryWarningMin = this.config.minWarnBatterie;
973
- const deviceLowBatState = await this.getInitValue(currDeviceString + this.arrDev[i].isLowBat);
974
- const deviceLowBatStateHM = await this.getInitValue(currDeviceString + this.arrDev[i].isLowBat2);
1057
+ // 3c. Count how many devices are with low battery
1058
+ const batteryWarningMin = this.config.minWarnBatterie;
1059
+ const deviceLowBatState = await this.getInitValue(currDeviceString + this.arrDev[i].isLowBat);
1060
+ const deviceLowBatStateHM = await this.getInitValue(currDeviceString + this.arrDev[i].isLowBat2);
975
1061
 
976
1062
 
977
- if (this.arrDev[i].isLowBat === 'none') {
978
- if (deviceBatteryState && (deviceBatteryState < batteryWarningMin)) {
979
- this.batteryLowPowered.push(
980
- {
981
- Device: deviceName,
982
- Adapter: deviceAdapterName,
983
- Battery: batteryHealth
984
- }
985
- );
986
- }
987
- } else {
988
- if (deviceLowBatState || deviceLowBatStateHM) {
989
- this.batteryLowPowered.push(
990
- {
991
- Device: deviceName,
992
- Adapter: deviceAdapterName,
993
- Battery: batteryHealth
994
- }
995
- );
996
- }
1063
+ if (this.arrDev[i].isLowBat === 'none') {
1064
+ if (deviceBatteryState && (deviceBatteryState < batteryWarningMin)) {
1065
+ this.batteryLowPowered.push(
1066
+ {
1067
+ 'Device': deviceName,
1068
+ 'Adapter': deviceAdapterName,
1069
+ 'Battery': batteryHealth
1070
+ }
1071
+ );
997
1072
  }
1073
+ } else {
1074
+ if (deviceLowBatState || deviceLowBatStateHM) {
1075
+ this.batteryLowPowered.push(
1076
+ {
1077
+ 'Device': deviceName,
1078
+ 'Adapter': deviceAdapterName,
1079
+ 'Battery': batteryHealth
1080
+ }
1081
+ );
1082
+ }
1083
+ }
998
1084
 
999
- // 3d. Count how many devices are with low battery
1000
- this.lowBatteryPoweredCount = this.batteryLowPowered.length;
1085
+ // 3d. Count how many devices are with low battery
1086
+ this.lowBatteryPoweredCount = this.batteryLowPowered.length;
1001
1087
 
1002
- // 4. Add all devices in the list
1003
- if (this.config.listOnlyBattery) {
1004
- if (deviceBatteryState !== null || shortDeviceBatteryState !== null) {
1005
- this.listAllDevices.push(
1006
- {
1007
- Device: deviceName,
1008
- Adapter: deviceAdapterName,
1009
- Battery: batteryHealth,
1010
- Link_quality: linkQuality,
1011
- Last_contact: lastContactString,
1012
- Status: deviceState
1013
- }
1014
- );
1015
- }
1016
- } else if (!this.config.listOnlyBattery) {
1088
+ // 4. Add all devices in the list
1089
+ if (this.config.listOnlyBattery) {
1090
+ if (deviceBatteryState !== null || shortDeviceBatteryState !== null) {
1017
1091
  this.listAllDevices.push(
1018
1092
  {
1019
- Device: deviceName,
1020
- Adapter: deviceAdapterName,
1021
- Battery: batteryHealth,
1022
- Link_quality: linkQuality,
1023
- Last_contact: lastContactString,
1024
- Status: deviceState
1093
+ 'Device': deviceName,
1094
+ 'Adapter': deviceAdapterName,
1095
+ 'Battery': batteryHealth,
1096
+ 'Signal strength': linkQuality,
1097
+ 'Last contact': lastContactString,
1098
+ 'Status': deviceState
1025
1099
  }
1026
1100
  );
1027
1101
  }
1102
+ } else if (!this.config.listOnlyBattery) {
1103
+ this.listAllDevices.push(
1104
+ {
1105
+ 'Device': deviceName,
1106
+ 'Adapter': deviceAdapterName,
1107
+ 'Battery': batteryHealth,
1108
+ 'Signal strength': linkQuality,
1109
+ 'Last contact': lastContactString,
1110
+ 'Status': deviceState
1111
+ }
1112
+ );
1113
+ }
1114
+
1115
+
1116
+ // 4a. Count how many devices are exists
1117
+ this.deviceCounter = this.listAllDevices.length;
1118
+ }
1119
+ } //<--End of second loop
1120
+ }
1121
+
1122
+ async createDataForEachAdpt(adptName) {
1123
+ this.log.debug(`Function started: ${this.createDataForEachAdpt.name}`);
1124
+ await this.resetVars();
1125
+
1126
+ for (let i = 0; i < this.arrDev.length; i++) {
1127
+
1128
+ if (this.arrDev[i].adapter.includes(adptName)) {
1129
+ await this.createData(i);
1130
+ }
1131
+
1132
+ }
1133
+ await this.writeDatapoints(adptName);
1134
+
1135
+ this.log.debug(`Function finished: ${this.createDataForEachAdpt.name}`);
1136
+ }
1137
+
1138
+ async createDataOfAll() {
1139
+ this.log.debug(`Function started: ${this.createDataOfAll.name}`);
1140
+ await this.resetVars();
1141
+
1142
+ for (let i = 0; i < this.arrDev.length; i++) {
1143
+
1144
+ await this.createData(i);
1028
1145
 
1146
+ }
1147
+ await this.writeDatapoints();
1148
+
1149
+ this.log.debug(`Function finished: ${this.createDataOfAll.name}`);
1150
+ }
1029
1151
 
1030
- // 4a. Count how many devices are exists
1031
- this.deviceCounter = this.listAllDevices.length;
1152
+ async resetVars() {
1153
+ // arrays
1154
+ this.offlineDevices = [],
1155
+ this.linkQualityDevices = [];
1156
+ this.batteryPowered = [];
1157
+ this.batteryLowPowered = [];
1158
+ this.listAllDevices = [];
1159
+
1160
+ // counts
1161
+ this.offlineDevicesCount = 0;
1162
+ this.deviceCounter = 0;
1163
+ this.linkQualityCount = 0;
1164
+ this.batteryPoweredCount = 0;
1165
+ this.lowBatteryPoweredCount = 0;
1166
+
1167
+ this.deviceReachable = '';
1168
+ }
1169
+
1170
+ async main() {
1171
+ this.log.debug(`Function started: ${this.main.name}`);
1172
+
1173
+ this.supAdapter = {
1174
+ alexa2: this.config.alexa2Devices,
1175
+ esphome: this.config.esphomeDevices,
1176
+ zigbee: this.config.zigbeeDevices,
1177
+ ble: this.config.bleDevices,
1178
+ sonoff: this.config.sonoffDevices,
1179
+ shelly: this.config.shellyDevices,
1180
+ homematic: this.config.homematicDevices,
1181
+ deconz: this.config.deconzDevices,
1182
+ zwave: this.config.zwaveDevices,
1183
+ dect: this.config.dectDevices,
1184
+ hue: this.config.hueDevices,
1185
+ hueExt: this.config.hueExtDevices,
1186
+ nukiExt: this.config.nukiExtDevices,
1187
+ ping: this.config.pingDevices,
1188
+ switchbotBle: this.config.switchbotBleDevices,
1189
+ sonos: this.config.sonosDevices,
1190
+ mihome: this.config.mihomeDevices,
1191
+ mihomeGW: this.config.mihomeDevices,
1192
+ };
1193
+
1194
+ for(const [id] of Object.entries(this.arrApart)) {
1195
+ if (this.supAdapter[id]) {
1196
+ this.arrDev.push(this.arrApart[id]);
1197
+ this.adapterSelected.push(await this.capitalize(id));
1198
+ this.log.debug(JSON.stringify(this.arrDev));
1199
+
1200
+ //create and fill datapoints for each adapter if selected
1201
+ if (this.config.createOwnFolder) {
1202
+ try {
1203
+ await this.createDPsForEachAdapter(id);
1204
+ this.log.debug(`Created datapoints for ${await this.capitalize(id)}`);
1205
+ await this.createDataForEachAdpt(id);
1206
+ this.log.debug(`Created and filled data for each adapter`);
1207
+ } catch (e) {
1208
+ this.log.warn(`Error at creating/filling datapoints for each adapter: ${e}`);
1209
+ return;
1210
+ }
1032
1211
  }
1033
- } //<--End of second loop
1034
- } //<---End of main loop
1212
+ }
1213
+ }
1214
+
1215
+ //Check if an Adapter is selected.
1216
+ if (this.adapterSelected.length >= 1) {
1217
+ this.log.info(`Number of selected adapters: ${this.adapterSelected.length}. Loading data from: ${(this.adapterSelected).join(', ')} ...`);
1218
+ } else {
1219
+ this.log.warn(`No adapter selected. Please check the instance configuration!`);
1220
+ return;
1221
+ }
1222
+
1223
+ /*=============================================
1224
+ = Start of main loop =
1225
+ =============================================*/
1226
+ try {
1227
+ await this.createDataOfAll();
1228
+ this.log.debug(`Created and filled data for all adapters`);
1229
+ } catch (e) {
1230
+ this.log.warn(`Error at creating/filling datapoints for all adapters: ${e}`);
1231
+ return;
1232
+ }
1035
1233
 
1036
1234
  this.log.debug(`Function finished: ${this.main.name}`);
1037
1235
  } //<--End of main function
1038
1236
 
1039
- async sendNotifications() {
1237
+
1238
+ async sendOfflineNotifications() {
1040
1239
  /*=============================================
1041
- = Notifications =
1240
+ = send offline notification =
1042
1241
  =============================================*/
1043
- this.log.debug(`Start the function: ${this.sendNotifications.name}`);
1044
1242
 
1045
- const pushover = {
1046
- instance: this.config.instancePushover,
1047
- title: this.config.titlePushover,
1048
- device: this.config.devicePushover,
1049
- prio: this.config.prioPushover
1243
+ this.log.debug(`Start the function: ${this.sendOfflineNotifications.name}`);
1050
1244
 
1051
- };
1052
- const telegram = {
1053
- instance: this.config.instanceTelegram,
1054
- user: this.config.deviceTelegram,
1055
- chatId: this.config.chatIdTelegram
1056
- };
1057
- const whatsapp = {
1058
- instance: this.config.instanceWhatsapp,
1059
- phone: this.config.phoneWhatsapp
1060
- };
1061
- const email = {
1062
- instance: this.config.instanceEmail,
1063
- subject: this.config.subjectEmail,
1064
- sendTo: this.config.sendToEmail
1245
+ try {
1246
+ let msg = '';
1247
+ const offlineDevicesCountOld = await this.getOwnInitValue('offlineCount');
1248
+
1249
+ if ((this.offlineDevicesCount != offlineDevicesCountOld)) {
1250
+ if (this.offlineDevicesCount == 1) { // make singular if it is only one device
1251
+ msg = 'Folgendes Gerät ist seit einiger Zeit nicht erreichbar: \n';
1252
+ } else if (this.offlineDevicesCount >= 2) { //make plural if it is more than one device
1253
+ msg = `Folgende ${this.offlineDevicesCount} Geräte sind seit einiger Zeit nicht erreichbar: \n`;
1254
+ }
1065
1255
 
1066
- };
1067
- const jarvis = {
1068
- instance: this.config.instanceJarvis,
1069
- title: this.config.titleJarvis
1256
+ for (const id of this.offlineDevices) {
1257
+ msg = `${msg} \n ${id['Device']} (${id['Last contact']})`;
1258
+ }
1259
+ this.log.info(msg);
1260
+ await this.setStateAsync('lastNotification', msg, true);
1261
+ if (this.config.instancePushover) {
1262
+ try {
1263
+ await this.sendPushover(msg);
1264
+ } catch (e) {
1265
+ this.log.warn (`Getting error at sending pushover notification ${e}`);
1266
+ }
1267
+ }
1268
+ if (this.config.instanceTelegram) {
1269
+ try {
1270
+ await this.sendTelegram(msg);
1271
+ } catch (e) {
1272
+ this.log.warn (`Getting error at sending telegram notification ${e}`);
1273
+ }
1274
+ }
1275
+ if (this.config.instanceWhatsapp) {
1276
+ try {
1277
+ await this.sendWhatsapp(msg);
1278
+ } catch (e) {
1279
+ this.log.warn (`Getting error at sending whatsapp notification ${e}`);
1280
+ }
1281
+ }
1282
+ if (this.config.instanceEmail) {
1283
+ try {
1284
+ await this.sendEmail(msg);
1285
+ } catch (e) {
1286
+ this.log.warn (`Getting error at sending email notification ${e}`);
1287
+ }
1288
+ }
1289
+ if (this.config.instanceJarvis) {
1290
+ try {
1291
+ await this.sendJarvis('{"title":"'+ this.config.titleJarvis +' (' + this.formatDate(new Date(), 'DD.MM.YYYY - hh:mm:ss') + ')","message":" ' + this.offlineDevicesCount + ' Geräte sind nicht erreichbar","display": "drawer"}');
1292
+ } catch (e) {
1293
+ this.log.warn (`Getting error at sending jarvis notification ${e}`);
1294
+ }
1295
+ }
1296
+ if (this.config.instanceLovelace) {
1297
+ try {
1298
+ await this.sendLovelace('{"message":" ' + this.offlineDevicesCount + ' Geräte sind nicht erreichbar", "title":"'+ this.config.titleLovelace +' (' + this.formatDate(new Date(), 'DD.MM.YYYY - hh:mm:ss') + ')"}');
1299
+ } catch (e) {
1300
+ this.log.warn (`Getting error at sending lovelace notification ${e}`);
1301
+ }
1302
+ }
1303
+ }
1304
+ } catch (e) {
1305
+ this.log.debug(`Getting error at sending offline notification ${e}`);
1306
+ }
1307
+ this.log.debug(`Finished the function: ${this.sendOfflineNotifications.name}`);
1308
+ }//<--End of offline notification
1070
1309
 
1071
- };
1072
- const lovelace = {
1073
- instance: this.config.instanceLovelace,
1074
- title: this.config.titleLovelace
1075
1310
 
1076
- };
1311
+ async sendBatteryNotifications() {
1312
+ /*=============================================
1313
+ = send low battery notification =
1314
+ =============================================*/
1315
+
1316
+ this.log.debug(`Start the function: ${this.sendBatteryNotifications.name}`);
1317
+ const now = new Date();
1318
+ const today = now.getDay();
1319
+ const checkDays = [];
1320
+ let checkToday;
1077
1321
 
1078
1322
  const choosedDays = {
1079
1323
  monday: this.config.checkMonday,
@@ -1085,262 +1329,156 @@ class DeviceWatcher extends utils.Adapter {
1085
1329
  sunday: this.config.checkSunday,
1086
1330
  };
1087
1331
 
1088
- const sendPushover = async (text) => {
1089
- await this.sendToAsync(pushover.instance, 'send', {
1090
- message: text,
1091
- title: pushover.title,
1092
- device: pushover.device,
1093
- priority: pushover.prio
1094
- });
1095
- };
1332
+ if (choosedDays.monday) checkDays.push(1);
1333
+ if (choosedDays.tuesday) checkDays.push(2);
1334
+ if (choosedDays.wednesday) checkDays.push(3);
1335
+ if (choosedDays.thursday) checkDays.push(4);
1336
+ if (choosedDays.friday) checkDays.push(5);
1337
+ if (choosedDays.saturday) checkDays.push(6);
1338
+ if (choosedDays.sunday) checkDays.push(0);
1096
1339
 
1097
- const sendTelegram = async (text) => {
1098
- await this.sendToAsync(telegram.instance, 'send', {
1099
- text: text,
1100
- user: telegram.user,
1101
- chatId: telegram.chatId
1102
- });
1103
- };
1340
+ //Check if the message should be send today
1341
+ checkDays.forEach(object => {
1342
+ if((object >= 0) && today == object){
1343
+ checkToday = true;
1344
+ }
1345
+ });
1104
1346
 
1105
- const sendWhatsapp = async (text) => {
1106
- await this.sendToAsync(whatsapp.instance, 'send', {
1107
- text: text,
1108
- phone: whatsapp.phone
1109
- });
1110
- };
1347
+ //Check first if a day is selected
1348
+ if (checkDays.length >= 1) {
1349
+ this.log.debug(`Number of selected days: ${checkDays.length}. Send Message on: ${(checkDays).join(', ')} ...`);
1350
+ } else {
1351
+ this.log.warn(`No days selected. Please check the instance configuration!`);
1352
+ return;
1353
+ }
1111
1354
 
1112
- const sendEmail = async (text) => {
1113
- await this.sendToAsync(email.instance, 'send', {
1114
- sendTo: email.sendTo,
1115
- text: text,
1116
- subject: email.subject
1117
- });
1118
- };
1355
+ try {
1356
+ //Check if the message for low battery was already sent today
1357
+ const lastBatteryNotifyIndicator = await this.getOwnInitValue('info.lastBatteryNotification');
1119
1358
 
1120
- const sendJarvis = async (text) => {
1121
- await this.setForeignStateAsync(`${jarvis.instance}.addNotification`, text);
1122
- };
1359
+ if (now.getHours() < 11) {await this.setStateAsync('info.lastBatteryNotification', false, true);} //set indicator for send message first to 'false' later after sending to 'true'
1360
+ if ((now.getHours() > 11) && (!lastBatteryNotifyIndicator) && (checkToday != undefined)){
1361
+ let infotext = '';
1123
1362
 
1124
- const sendLovelace = async (text) => {
1125
- await this.setForeignStateAsync(`${lovelace.instance}.notifications.add`, text);
1126
- };
1363
+ for (const id of this.batteryLowPowered) {
1364
+ infotext = '\n' + id['Device'] + ' (' + id['Battery'] + ')'.split(', ');
1365
+ }
1127
1366
 
1128
- /*---------- oflline notification ----------*/
1129
- if(this.config.checkSendOfflineMsg) {
1130
- try {
1131
- let msg = '';
1132
- const offlineDevicesCountOld = await this.getOwnInitValue('offlineCount');
1133
-
1134
- if ((this.offlineDevicesCount != offlineDevicesCountOld)) {
1135
- if (this.offlineDevicesCount == 1) { // make singular if it is only one device
1136
- msg = 'Folgendes Gerät ist seit einiger Zeit nicht erreichbar: \n';
1137
- } else if (this.offlineDevicesCount >= 2) { //make plural if it is more than one device
1138
- msg = `Folgende ${this.offlineDevicesCount} Geräte sind seit einiger Zeit nicht erreichbar: \n`;
1139
- }
1367
+ if (this.lowBatteryPoweredCount > 0) {
1368
+ this.log.info(`Niedrige Batteriezustände: ${infotext}`);
1369
+ await this.setStateAsync('lastNotification', `Niedrige Batteriezustände: ${infotext}`, true);
1140
1370
 
1141
- for (const id of this.offlineDevices) {
1142
- msg = `${msg} \n ${id['Device']} (${id['Last_contact']})`;
1143
- }
1144
- this.log.info(msg);
1145
- await this.setStateAsync('lastNotification', msg, true);
1146
- if (pushover.instance) {
1371
+ if (this.config.instancePushover) {
1147
1372
  try {
1148
- await sendPushover(msg);
1373
+ await this.sendPushover(`Niedrige Batteriezustände: ${infotext}`);
1149
1374
  } catch (e) {
1150
- this.log.warn (`Getting error at sending notification ${e}`);
1375
+ this.log.warn (`Getting error at sending pushover notification ${e}`);
1151
1376
  }
1152
1377
  }
1153
- if (telegram.instance) {
1378
+ if (this.config.instanceTelegram) {
1154
1379
  try {
1155
- await sendTelegram(msg);
1380
+ await this.sendTelegram(`Niedrige Batteriezustände: ${infotext}`);
1156
1381
  } catch (e) {
1157
- this.log.warn (`Getting error at sending notification ${e}`);
1382
+ this.log.warn (`Getting error at sending telegram notification ${e}`);
1158
1383
  }
1159
1384
  }
1160
- if (whatsapp.instance) {
1385
+ if (this.config.instanceWhatsapp) {
1161
1386
  try {
1162
- await sendWhatsapp(msg);
1387
+ await this.sendWhatsapp(`Niedrige Batteriezustände: ${infotext}`);
1163
1388
  } catch (e) {
1164
- this.log.warn (`Getting error at sending notification ${e}`);
1389
+ this.log.warn (`Getting error at sending whatsapp notification ${e}`);
1165
1390
  }
1166
1391
  }
1167
- if (email.instance) {
1392
+ if (this.config.instanceEmail) {
1168
1393
  try {
1169
- await sendEmail(msg);
1394
+ await this.sendEmail(`Niedrige Batteriezustände: ${infotext}`);
1170
1395
  } catch (e) {
1171
- this.log.warn (`Getting error at sending notification ${e}`);
1396
+ this.log.warn (`Getting error at sending email notification ${e}`);
1172
1397
  }
1173
1398
  }
1174
- if (jarvis.instance) {
1399
+ if (this.config.instanceJarvis) {
1175
1400
  try {
1176
- await sendJarvis('{"title":"'+ jarvis.title +' (' + this.formatDate(new Date(), 'DD.MM.YYYY - hh:mm:ss') + ')","message":" ' + this.offlineDevicesCount + ' Geräte sind nicht erreichbar","display": "drawer"}');
1401
+ await this.sendJarvis('{"title":"'+ this.config.titleJarvis +' (' + this.formatDate(new Date(), 'DD.MM.YYYY - hh:mm:ss') + ')","message":" ' + this.lowBatteryPoweredCount + ' Geräte mit schwacher Batterie","display": "drawer"}');
1177
1402
  } catch (e) {
1178
- this.log.warn (`Getting error at sending notification ${e}`);
1403
+ this.log.warn (`Getting error at sending jarvis notification ${e}`);
1179
1404
  }
1180
1405
  }
1181
- if (lovelace.instance) {
1406
+ if (this.config.instanceLovelace) {
1182
1407
  try {
1183
- await sendLovelace('{"message":" ' + this.offlineDevicesCount + ' Geräte sind nicht erreichbar", "title":"'+ lovelace.title +' (' + this.formatDate(new Date(), 'DD.MM.YYYY - hh:mm:ss') + ')"}');
1408
+ await this.sendLovelace('{"message":" ' + this.lowBatteryPoweredCount + ' Geräte mit schwacher Batterie", "title":"'+ this.config.titleLovelace +' (' + this.formatDate(new Date(), 'DD.MM.YYYY - hh:mm:ss') + ')"}');
1184
1409
  } catch (e) {
1185
- this.log.warn (`Getting error at sending notification ${e}`);
1410
+ this.log.warn (`Getting error at sending lovelace notification ${e}`);
1186
1411
  }
1187
1412
  }
1188
- }
1189
- } catch (e) {
1190
- this.log.debug(`Getting error at sending offline notification ${e}`);
1191
- }
1192
- }//<--End of offline notification
1193
-
1194
- /*---------- Low battery Notification ----------*/
1195
- const now = new Date();
1196
- const today = now.getDay();
1197
- const checkDays = [];
1198
- let checkToday;
1199
1413
 
1200
- if (choosedDays.monday) checkDays.push(1);
1201
- if (choosedDays.tuesday) checkDays.push(2);
1202
- if (choosedDays.wednesday) checkDays.push(3);
1203
- if (choosedDays.thursday) checkDays.push(4);
1204
- if (choosedDays.friday) checkDays.push(5);
1205
- if (choosedDays.saturday) checkDays.push(6);
1206
- if (choosedDays.sunday) checkDays.push(0);
1207
-
1208
- if (this.config.checkSendBatteryMsg) this.log.debug(JSON.stringify(checkDays));
1209
-
1210
- checkDays.forEach(object => {
1211
- if((object >= 0) && today == object){
1212
- checkToday = true;
1213
- }
1214
- });
1215
-
1216
- if (this.config.checkSendBatteryMsg) {
1217
- try {
1218
- const lastBatteryNotifyIndicator = await this.getOwnInitValue('info.lastBatteryNotification');
1219
- const batteryWarningMin = this.config.minWarnBatterie;
1220
-
1221
- if (now.getHours() < 11) {await this.setStateAsync('info.lastBatteryNotification', false, true);} //Nur einmal abfragen
1222
- if ((now.getHours() > 11) && (!lastBatteryNotifyIndicator) && (checkToday != undefined)){
1223
- let batteryMinCount = 0;
1224
- let infotext = '';
1225
-
1226
- for (const id of this.batteryPowered) {
1227
- if (id['Battery']) {
1228
- const batteryValue = parseFloat(id['Battery'].replace('%', ''));
1229
- if ((batteryValue < batteryWarningMin) && (id['Adapter'] != 'Homematic')) {
1230
- infotext = infotext + '\n' + id['Device'] + ' ' + ' (' + id['Battery'] + ')'.split(', ');
1231
- ++batteryMinCount;
1232
- }
1233
- }
1234
- }
1235
- if (batteryMinCount > 0) {
1236
- this.log.info(`Batteriezustände: ${infotext}`);
1237
- await this.setStateAsync('lastNotification', infotext, true);
1238
-
1239
- if (pushover.instance) {
1240
- try {
1241
- await sendPushover(`Batteriezustände: ${infotext}`);
1242
- } catch (e) {
1243
- this.log.warn (`Getting error at sending notification ${e}`);
1244
- }
1245
- }
1246
- if (telegram.instance) {
1247
- try {
1248
- await sendTelegram(`Batteriezustände: ${infotext}`);
1249
- } catch (e) {
1250
- this.log.warn (`Getting error at sending notification ${e}`);
1251
- }
1252
- }
1253
- if (whatsapp.instance) {
1254
- try {
1255
- await sendWhatsapp(`Batteriezustände: ${infotext}`);
1256
- } catch (e) {
1257
- this.log.warn (`Getting error at sending notification ${e}`);
1258
- }
1259
- }
1260
- if (email.instance) {
1261
- try {
1262
- await sendEmail(`Batteriezustände: ${infotext}`);
1263
- } catch (e) {
1264
- this.log.warn (`Getting error at sending notification ${e}`);
1265
- }
1266
- }
1267
- if (jarvis.instance) {
1268
- try {
1269
- await sendJarvis('{"title":"'+ jarvis.title +' (' + this.formatDate(new Date(), 'DD.MM.YYYY - hh:mm:ss') + ')","message":" ' + batteryMinCount + ' Geräte mit schwacher Batterie","display": "drawer"}');
1270
- } catch (e) {
1271
- this.log.warn (`Getting error at sending notification ${e}`);
1272
- }
1273
- }
1274
- if (lovelace.instance) {
1275
- try {
1276
- await sendLovelace('{"message":" ' + batteryMinCount + ' Geräte mit schwacher Batterie", "title":"'+ lovelace.title +' (' + this.formatDate(new Date(), 'DD.MM.YYYY - hh:mm:ss') + ')"}');
1277
- } catch (e) {
1278
- this.log.warn (`Getting error at sending notification ${e}`);
1279
- }
1280
- }
1281
-
1282
- await this.setStateAsync('info.lastBatteryNotification', true, true);
1283
- }
1414
+ await this.setStateAsync('info.lastBatteryNotification', true, true);
1284
1415
  }
1285
- } catch (e) {
1286
- this.log.debug(`Getting error at sending battery notification ${e}`);
1287
1416
  }
1288
- }//<--End of battery notification
1289
- this.log.debug(`Function finished: ${this.sendNotifications.name}`);
1290
- }
1291
- /*===== End of Section notifications ======*/
1417
+ } catch (e) {
1418
+ this.log.debug(`Getting error at sending battery notification ${e}`);
1419
+ }
1420
+ this.log.debug(`Finished the function: ${this.sendBatteryNotifications.name}`);
1421
+ }//<--End of battery notification
1292
1422
 
1293
- async writeDatapoints() {
1423
+ async writeDatapoints(adptName) {
1294
1424
  /*=============================================
1295
1425
  = Write Datapoints =
1296
1426
  =============================================*/
1427
+
1297
1428
  this.log.debug(`Start the function: ${this.writeDatapoints.name}`);
1298
1429
 
1299
1430
  try {
1300
- await this.setStateAsync('offlineCount', {val: this.offlineDevicesCount, ack: true});
1301
- await this.setStateAsync('countAll', {val: this.deviceCounter, ack: true});
1302
- await this.setStateAsync('batteryCount', {val: this.batteryPoweredCount, ack: true});
1303
- await this.setStateAsync('lowBatteryCount', {val: this.lowBatteryPoweredCount, ack: true});
1431
+
1432
+ let dpSubFolder;
1433
+ if (adptName) { //write the datapoints in subfolders with the adaptername otherwise write the dP's in the root folder
1434
+ dpSubFolder = adptName + '.';
1435
+ } else {
1436
+ dpSubFolder = '';}
1437
+
1438
+ await this.setStateAsync(`${dpSubFolder}offlineCount`, {val: this.offlineDevicesCount, ack: true});
1439
+ await this.setStateAsync(`${dpSubFolder}countAll`, {val: this.deviceCounter, ack: true});
1440
+ await this.setStateAsync(`${dpSubFolder}batteryCount`, {val: this.batteryPoweredCount, ack: true});
1441
+ await this.setStateAsync(`${dpSubFolder}lowBatteryCount`, {val: this.lowBatteryPoweredCount, ack: true});
1304
1442
 
1305
1443
  if (this.deviceCounter == 0) {
1306
- this.listAllDevices = [{Device: '--keine--', Adapter: '', Battery: '', Last_contact: '', Link_quality: ''}]; //JSON-Info Gesamtliste mit Info je Gerät
1444
+ this.listAllDevices = [{'Device': '--none--', 'Adapter': '', 'Battery': '', 'Last contact': '', 'Signal strength': ''}]; //JSON-Info Gesamtliste mit Info je Gerät
1307
1445
 
1308
- await this.setStateAsync('listAll', {val: JSON.stringify(this.listAllDevices), ack: true});
1446
+ await this.setStateAsync(`${dpSubFolder}listAll`, {val: JSON.stringify(this.listAllDevices), ack: true});
1309
1447
  } else {
1310
- await this.setStateAsync('listAll', {val: JSON.stringify(this.listAllDevices), ack: true});
1448
+ await this.setStateAsync(`${dpSubFolder}listAll`, {val: JSON.stringify(this.listAllDevices), ack: true});
1311
1449
  }
1312
1450
 
1313
1451
  if (this.linkQualityCount == 0) {
1314
- this.linkQualityDevices = [{Device: '--keine--', Adapter: '', Link_quality: ''}]; //JSON-Info alle mit LinkQuality
1452
+ this.linkQualityDevices = [{'Device': '--none--', 'Adapter': '', 'Signal strength': ''}]; //JSON-Info alle mit LinkQuality
1315
1453
 
1316
- await this.setStateAsync('linkQualityList', {val: JSON.stringify(this.linkQualityDevices), ack: true});
1454
+ await this.setStateAsync(`${dpSubFolder}linkQualityList`, {val: JSON.stringify(this.linkQualityDevices), ack: true});
1317
1455
  } else {
1318
- await this.setStateAsync('linkQualityList', {val: JSON.stringify(this.linkQualityDevices), ack: true});
1456
+ await this.setStateAsync(`${dpSubFolder}linkQualityList`, {val: JSON.stringify(this.linkQualityDevices), ack: true});
1319
1457
  }
1320
1458
 
1321
1459
 
1322
1460
  if (this.offlineDevicesCount == 0) {
1323
- this.offlineDevices = [{Device: '--keine--', Adapter: '', Last_contact: ''}]; //JSON-Info alle offline-Geräte = 0
1461
+ this.offlineDevices = [{'Device': '--none--', 'Adapter': '', 'Last contact': ''}]; //JSON-Info alle offline-Geräte = 0
1324
1462
 
1325
- await this.setStateAsync('offlineList', {val: JSON.stringify(this.offlineDevices), ack: true});
1463
+ await this.setStateAsync(`${dpSubFolder}offlineList`, {val: JSON.stringify(this.offlineDevices), ack: true});
1326
1464
  } else {
1327
- await this.setStateAsync('offlineList', {val: JSON.stringify(this.offlineDevices), ack: true});
1465
+ await this.setStateAsync(`${dpSubFolder}offlineList`, {val: JSON.stringify(this.offlineDevices), ack: true});
1328
1466
  }
1329
1467
 
1330
1468
  if (this.batteryPoweredCount == 0) {
1331
- this.batteryPowered = [{Device: '--keine--', Adapter: '', Battery: ''}]; //JSON-Info alle batteriebetriebenen Geräte
1469
+ this.batteryPowered = [{'Device': '--none--', 'Adapter': '', 'Battery': ''}]; //JSON-Info alle batteriebetriebenen Geräte
1332
1470
 
1333
- await this.setStateAsync('batteryList', {val: JSON.stringify(this.batteryPowered), ack: true});
1471
+ await this.setStateAsync(`${dpSubFolder}batteryList`, {val: JSON.stringify(this.batteryPowered), ack: true});
1334
1472
  } else {
1335
- await this.setStateAsync('batteryList', {val: JSON.stringify(this.batteryPowered), ack: true});
1473
+ await this.setStateAsync(`${dpSubFolder}batteryList`, {val: JSON.stringify(this.batteryPowered), ack: true});
1336
1474
  }
1337
1475
 
1338
1476
  if (this.lowBatteryPoweredCount == 0) {
1339
- this.batteryLowPowered = [{Device: '--keine--', Adapter: '', Battery: ''}]; //JSON-Info alle batteriebetriebenen Geräte
1477
+ this.batteryLowPowered = [{'Device': '--none--', 'Adapter': '', 'Battery': ''}]; //JSON-Info alle batteriebetriebenen Geräte
1340
1478
 
1341
- await this.setStateAsync('lowBatteryList', {val: JSON.stringify(this.batteryLowPowered), ack: true});
1479
+ await this.setStateAsync(`${dpSubFolder}lowBatteryList`, {val: JSON.stringify(this.batteryLowPowered), ack: true});
1342
1480
  } else {
1343
- await this.setStateAsync('lowBatteryList', {val: JSON.stringify(this.batteryLowPowered), ack: true});
1481
+ await this.setStateAsync(`${dpSubFolder}lowBatteryList`, {val: JSON.stringify(this.batteryLowPowered), ack: true});
1344
1482
  }
1345
1483
 
1346
1484
  //Zeitstempel wann die Datenpunkte zuletzt gecheckt wurden
@@ -1351,8 +1489,8 @@ class DeviceWatcher extends utils.Adapter {
1351
1489
  this.log.error(`(05) Error while writing the states ${e}`);
1352
1490
  }
1353
1491
  this.log.debug(`Function finished: ${this.writeDatapoints.name}`);
1354
- }
1355
- /*===== End of writing Datapoints ======*/
1492
+ }//<--End of writing Datapoints
1493
+
1356
1494
 
1357
1495
 
1358
1496
  onUnload(callback) {