forked from leinich/MMM-homeassistant-sensors
-
Notifications
You must be signed in to change notification settings - Fork 14
/
MMM-homeassistant-sensors.js
executable file
·765 lines (683 loc) · 22.1 KB
/
MMM-homeassistant-sensors.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
'use strict';
Module.register("MMM-homeassistant-sensors", {
result: {},
defaults: {
prettyName: true,
stripName: true,
title: 'Home Assistant',
host: 'hassio.local',
port: '8321',
https: false,
token: '',
apipassword: '',
fade: 100,
updateInterval: 300000,
id: false,
displaySymbol: true,
displaydates: false,
displaytimes: false,
notificationOnly: false,
notificationCondition: [],
dateformat: 'YYYY-MM-DD', // See moments for more format options: https://momentjs.com/docs/#/displaying/
timeformat: 'HH:mm:ss', // See moments for more format options: https://momentjs.com/docs/#/displaying/
controlsensor: 'sensor control disabled', // If you want to show this instans of HA-Sensors only when this sensor is the value below.
controlsensorvalue: 'sensor control disabled', // The value the above sensor must have to show this instans of HA-Sensors.
noaddress: 'away', // If address field is "undefined" or "null" on the sensor, this string will be displayed instead of the address.
debuglogging: false,
rowClass: 'small', // small, normal or big
values: []
},
// Load the moment script (from MM) and chart.js from the module.
getScripts: function () {
return ["moment.js"];
// Use this when Chart.js is needed (and installed via npm).
//return ["moment.js", this.file('node_modules/chart.js/dist/chart.js')];
},
// Load the css script from the module.
getStyles: function () {
return ["modules/MMM-homeassistant-sensors/node_modules/@mdi/font/css/materialdesignicons.min.css", "MMM-homeassistant-sensors.css"];
},
// Refresh the data from HA if the notification "REFRESHVALUES(x)" arrives.
notificationReceived: function (notification, payload, sender) {
var self = this;
if (self.config.id !== false ) {
if (notification === "REFRESHVALUES" + self.config.id) {
this.getStats();
}
} else {
if (notification === "REFRESHVALUES") {
this.getStats();
}
}
},
start: function () {
this.getStats();
this.scheduleUpdate();
},
isEmpty: function (obj) {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
return false;
}
}
return true;
},
getDom: function () {
var wrapper = document.createElement("ticker");
wrapper.className = "ha-"+this.config.rowClass;
var data = this.result;
// For debugging
//console.log(data);
// Hide display if notificationOnly is set
if(this.config.notificationOnly) {
wrapper.style.display = "none";
}
// Hides and shows the module if the control sensor is defined and the control sensor value is defined.
else if (data && !this.isEmpty(data)) {
// If the control sensor is set to anything else the the default continue.
if (this.config.controlsensor !== "sensor control disabled") {
var stateval = this.getState(data, this.config.controlsensor);
// If the control sensor value is anything not the default or not the defined value, hide the module.
if ((stateval !== this.config.controlsensorvalue && this.config.controlsensorvalue !== "sensor control disabled")) {
if (!this.hidden) {
this.hide();
}
} else {
if (this.hidden) {
this.show();
}
}
/*
// *****************************************************************************************
//
// This below is not working for me, I'm not using pages...
// For anyone using pages, please fix so that it works both with and without MMM-Pages... :)
//
// *****************************************************************************************
// this.hide() does not work well with MMM-Pages, so use wrapper.style.display instead
if (wrapper.style.display != "none") {
this.visibleStyle = wrapper.style.display;
wrapper.style.display = "none";
}
} else {
console.log("here 2");
if (wrapper.style.display == "none") {
wrapper.style.display = this.visibleStyle;
}
}
// *****************************************************************************************
*/
}
}
// Starting to build the elements.
var statElement = document.createElement("header");
var title = this.config.title;
statElement.innerHTML = title;
wrapper.appendChild(statElement);
if (data && !this.isEmpty(data)) {
var tableElement = document.createElement("table");
var values = this.config.values;
if (values.length > 0) {
for (var i = 0; i < values.length; i++) {
// Check if there is icons in the config.
if (values[i].icons) {
var icons = values[i].icons[0];
} else {
var icons = "none";
}
// Check if there is values to replace in the config.
if (values[i].replace) {
var replace = values[i].replace[0];
} else {
var replace = "none";
}
// Check if the unit should be replaced in the config.
if (values[i].defunit) {
var defunit = values[i].defunit;
} else {
var defunit = "none";
}
// Check if a global Show Date is set in the config.
if (this.config.displaydates === true) {
var showdate = true;
} else {
var showdate = false;
}
// Check if a local entity date is set to show (overriding the default).
if (values[i].showdate === true) {
showdate = true;
}
// Check if a local entity date is not set to show (overriding the default).
if (values[i].showdate === false) {
showdate = false;
}
// Check if a global Show Time is set in the config.
if (this.config.displaytimes === true) {
var showtime = true;
} else {
var showtime = false;
}
// Check if a local entity time is set to show (overriding the default).
if (values[i].showtime === true) {
showtime = true;
}
// Check if a local entity time is not set to show (overriding the default).
if (values[i].showtime === false) {
showtime = false;
}
// Check if the valueSeparator is defined (overriding the default).
if (typeof values[i].valueSeparator === "undefined") {
values[i].valueSeparator = "|";
}
// Check if the graph parameter is set (to create a graph of the array in an attribute).
if (typeof values[i].graph === "undefined") {
var graph = false;
} else {
var graph = true;
}
// Pulling all entity values.
// The Sensor
var sensor = values[i].sensor;
// Name of the Sensor
var name = this.getName(data, sensor);
// State of the Sensor
var stateval = this.getState(data, sensor);
// Value of the Sensor
var sensval = this.getValue(data, sensor);
// Make the data array.
var sensordata = [
stateval,
this.getUnit(data, sensor),
icons,
replace,
values[i].name,
defunit,
showdate,
showtime,
this.getLastupd(data, sensor),
this.getPicture(data, sensor),
values[i].displayvalue,
values[i].divider,
values[i].multiplier,
values[i].round,
this.getAddress(data, sensor),
values[i].displayunit,
values[i].highAlertThreshold,
values[i].lowAlertThreshold,
sensval,
values[i].useValue,
this.getAttribute(data, sensor, values[i].attribute),
values[i].valueSeparator,
values[i].highDisplayThreshold,
values[i].lowDisplayThreshold,
values[i].displayWhenEqualTo,
values[i].highDisplayClass,
values[i].lowDisplayClass,
graph,
];
// For debugging
//console.log(sensordata);
if (stateval) {
tableElement.appendChild(this.addValue(name, sensordata));
}
// Send notification
if(values[i].notificationName !== undefined) {
var notificationValue = undefined;
var origValue = values[i].notificationState;
if(this.debuglogging) {
console.log("MMM-homeassistant-sensors - stateval: " + stateval);
}
// Check conditions of sending notification
if(values[i].notificationConditions.length != 0) {
var found = false;
values[i].notificationConditions.forEach((condition) => {
var negState = false;
if(condition.negState === true) {
negState = true;
}
condition.stateVals.forEach((val) => {
if(this.debuglogging) {
console.log("MMM-homeassistant-sensors - vals: " + val);
}
// If sensor value matches the value we're looking for
if(stateval == val) {
if(this.debuglogging) {
console.log("MMM-homeassistant-sensors - " + val + " found");
}
found = true;
}
});
// If we found what we're looking for - or didn't find what we're NOT looking for
if((!found && negState) ||(found && !negState)) {
notificationValue = condition.notificationVal;
}
// Set negative value for notification if necessary
else if(!found && !negState && condition.notificationValNeg !== undefined) {
if(this.debuglogging) {
console.log("MMM-homeassistant-sensors - vals not found");
}
notificationValue = condition.notificationValNeg;
}
else if(found && negState && condition.notificationValNeg !== undefined) {
notificationValue = condition.notificationValNeg;
}
});
// Send notification
if(notificationValue !== undefined && notificationValue != values[i].notificationState) {
values[i].notificationState = notificationValue;
this.sendNotification(values[i].notificationName, notificationValue);
}
}
}
}
} else {
for (var key in data) {
if (data.hasOwnProperty(key)) {
tableElement.appendChild(this.addValue(key, data[key]));
}
}
}
wrapper.appendChild(tableElement);
} else {
console.log("MMM-homeassistant-sensors - Error! No data returned!");
var error = document.createElement("span");
error.innerHTML = "Error fetching stats.";
wrapper.appendChild(error);
}
return wrapper;
},
// Collect the state from the entity.
getState: function (data, value) {
for (var i = 0; i < data.length; i++) {
if (data[i].entity_id == value) {
return data[i].state;
}
}
return null;
},
// Collect the value from the entity.
getValue: function (data, value) {
for (var i = 0; i < data.length; i++) {
if (data[i].entity_id == value) {
if (typeof data[i].attributes.value !== "undefined") {
return data[i].attributes.value;
} else {
return null;
}
}
}
return null;
},
// Collect the unit from the entity.
getUnit: function (data, value) {
for (var i = 0; i < data.length; i++) {
if (data[i].entity_id == value) {
if (typeof data[i].attributes.unit_of_measurement !== "undefined") {
return data[i].attributes.unit_of_measurement;
}
return "";
}
}
return "";
},
// Collect the address from the entity (if available).
getAddress: function (data, value) {
for (var i = 0; i < data.length; i++) {
if (data[i].entity_id == value) {
return data[i].attributes.address;
}
}
return null;
},
// Collect the name from the entity.
getName: function (data, value) {
for (var i = 0; i < data.length; i++) {
if (data[i].entity_id == value) {
return data[i].attributes.friendly_name;
}
}
return null;
},
// Collect the last update date and time from the entity.
getLastupd: function (data, value) {
for (var i = 0; i < data.length; i++) {
if (data[i].entity_id == value) {
return data[i].last_updated;
}
}
return null;
},
// Collect the picture from the entity.
getPicture: function (data, value) {
for (var i = 0; i < data.length; i++) {
if (data[i].entity_id == value) {
return data[i].attributes.entity_picture;
}
}
return null;
},
// Collect selected attribute from the entity.
getAttribute: function (data, value, attribute) {
for (var i = 0; i < data.length; i++) {
if (data[i].entity_id == value) {
return data[i].attributes[attribute];
}
}
return null;
},
// Adding alla the sensors to the table.
addValue: function (name, sensordata) {
// The array looks like this.
//sensordata = [0]State, [1]unit, [2]icons, [3]replace, [4]displayname, [5]defunit, [6]showdate, [7]showtime, [8]lastupd, [9]picture, [10]displayvalue, [11]divider, [12]multiplier, [13]round, [14]address, [15]displayunit, [16]highAlertThreshold, [17]lowAlertThreshold, [18]Value, [19]useValue, [20]attribute (may NOT be a multi dimensional array (yet)), [21]valueSeparator, [22].highDisplayThreshold, [23].lowDisplayThreshold, [24].displayWhenEqualTo, [25]highDisplayClass, [26]lowDisplayClass, [27]graph,
var newrow,
newText,
newCell;
var newValue;
var newValueArray = "|";
var datedata;
var timedata;
var unit;
var picture;
var address;
var addblinkhigh = 0;
var addblinklow = 0;
var addsensor = 0;
var addhighsensor = 0;
var addlowsensor = 0;
var addequalsensor = 0;
// Set the value to the sensors status
newValue = sensordata[0];
// console.log(newValue);
//console.log(sensordata);
// If higher then display threshold add the sensor to the table.
if (!isNaN(sensordata[22])) {
if (newValue > sensordata[22]) {
addhighsensor = 1;
}
}
// If lower then display threshold add the sensor to the table.
if (!isNaN(sensordata[23])) {
console.log("here0");
if (newValue < sensordata[23]) {
addlowsensor = 1;
}
}
// If equal to this string/value/boolian add the sensor to the table.
if (typeof sensordata[24] !== 'undefined') {
if (newValue == sensordata[24]) {
addequalsensor = 1;
}
}
// If neither lower or higher display threshold is set add the sensor to the table.
if ((isNaN(sensordata[22])) && (isNaN(sensordata[23])) && (typeof sensordata[24] === 'undefined')){
addsensor = 1;
}
// Adds a row in the table.
newrow = document.createElement("tr");
// Only add the sensor if any of these are set.
if (addhighsensor == 1 || addlowsensor == 1 || addequalsensor == 1 || addsensor == 1) {
// Fix the time and date.
var thetime = new Date(sensordata[8]);
var momentdate = moment(thetime);
// The time formatted.
var timedata = moment(thetime).format(this.config.timeformat);
// The date formatted
var datedata = moment(thetime).format(this.config.dateformat);
// Format the time to human readable...
var rtime = momentdate.from(moment());
// Adds the address if available.
if (sensordata[14]) {
address = sensordata[14];
} else {
address = this.config.noaddress;
}
// Unit
if (sensordata[5] !== "none") {
unit = sensordata[5];
unit = unit.replace("%t%", timedata);
unit = unit.replace("%r%", rtime);
unit = unit.replace("%m%", momentdate);
unit = unit.replace("%d%", datedata);
unit = unit.replace("%a%", address);
} else {
unit = sensordata[1];
}
// Name
if (sensordata[4]) {
name = sensordata[4];
name = name.replace("%t%", timedata);
name = name.replace("%r%", rtime);
name = name.replace("%m%", momentdate);
name = name.replace("%d%", datedata);
name = name.replace("%a%", address);
name = name.replace("%u%", unit);
} else {
if (this.config.stripName) {
var split = name.split(".");
name = split[split.length - 1];
}
if (this.config.prettyName) {
name = name.replace(/([A-Z])/g, function ($1) {
return "_" + $1.toLowerCase();
});
name = name.split("_").join(" ");
name = name.replace(/\w\S*/g, function (txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
}
}
// removes the date from the output table if selected.
if (sensordata[6] === false) {
datedata = "";
}
// Removes the date from the output table if selected.
if (sensordata[7] === false) {
timedata = "";
}
// Removes the unit if set not to be displayed.
if (sensordata[15] === false) {
unit = "";
}
// Column start point.
var column = -1;
// Last Date Updated
column++;
newCell = newrow.insertCell(column);
newCell.className = "ha-date";
newText = document.createTextNode(datedata);
newCell.appendChild(newText);
// Last Time Updated
column++;
newCell = newrow.insertCell(column);
newCell.className = "ha-time";
newText = document.createTextNode(timedata);
newCell.appendChild(newText);
// icons
column++;
newCell = newrow.insertCell(column);
if (this.config.displaySymbol) {
if (typeof sensordata[2] === "object") {
var iconsinline = "none";
//Change icons based on HA status
for (var key in sensordata[2]) {
// Sets the icon defined in the config specified value will give specified icon.
if (sensordata[0] === key) {
if (!sensordata[2][key].includes("/")) {
newCell.className = "ha-icon";
iconsinline = document.createElement("i");
iconsinline.className = "mdi mdi-" + sensordata[2][key];
//break;
} else {
iconsinline = document.createElement("img");
iconsinline.src = sensordata[2][key];
iconsinline.className = "ha-img";
}
}
// If no icon is set by values, the default one will be used.
if (iconsinline === "none") {
if (!sensordata[2][key].includes("/")) {
newCell.className = "ha-icon";
iconsinline = document.createElement("i");
iconsinline.className = "mdi mdi-" + sensordata[2][key];
//break;
} else {
iconsinline = document.createElement("img");
iconsinline.src = sensordata[2][key];
iconsinline.className = "ha-img";
}
}
}
newCell.appendChild(iconsinline);
} else {
// Setting the Picture if defined in the entity.
if (sensordata[9]) {
if (!sensordata[9].includes("http")) {
if (this.config.https) {
var picturestart = "https://";
} else {
var picturestart = "http://";
}
picture = picturestart.concat(this.config.host, ":", this.config.port, sensordata[9]);
} else {
picture = sensordata[9];
}
var iconsinline = document.createElement("img");
iconsinline.src = picture;
iconsinline.className = "ha-img";
newCell.appendChild(iconsinline);
}
}
}
// Add all array values from the attribute to one value (divided by a defined separator (default=|)).
if (typeof sensordata[20] !== "undefined") {
if (sensordata[20].length > 1) {
newValue = sensordata[21];
for (var i = 0; i < sensordata[20].length; i++) {
newValue = newValue + sensordata[20][i] + sensordata[21];
}
if (sensordata[27] === true) {
// Figure out how to make a graph using the chart.js script with an attribute array...
console.log("Fix a graph here!");
}
} else {
newValue = sensordata[20];
}
}
// If higher then alert threshold add blink high class.
if (!isNaN(sensordata[16])) {
if (newValue > sensordata[16]) {
addblinkhigh = 1;
}
}
// If lower then alert threshold add blink low class.
if (!isNaN(sensordata[17])) {
if (newValue < sensordata[17]) {
addblinklow = 1;
}
}
// Replace the "state" with the "value" if set to true in config.
if (sensordata[19]) {
newValue = sensordata[18];
}
// Replace the "value" with something defined in config.
if (sensordata[3] !== "none") {
for (var key in sensordata[3]) {
if (sensordata[0] === key) {
newValue = sensordata[3][key];
}
}
}
// Calculate the divider
if (sensordata[11]) {
newValue = newValue / sensordata[11];
}
// Calculate the multiplier
if (sensordata[12]) {
newValue = newValue * sensordata[12];
}
// Round the value to two decimals.
// Todo: Add a better function for this...
if (sensordata[13]) {
newValue = Math.round(newValue * 100) / 100;
}
// If you want to add the value to the defined unit.
if (sensordata[5] !== "none") {
unit = unit.replace("%v%", newValue);
}
// Change the value to the address if %a% defined as a value replacement array.
if (typeof newValue === 'string') {
if (newValue.includes("%a%")) {
newValue = newValue.replace("%a%", address);
}
}
// If you want to add the value to the defined name.
if (sensordata[4]) {
name = name.replace("%v%", newValue);
}
// Removes the value if selected.
if (sensordata[10] === false) {
newValue = "";
}
// Name
column++;
newCell = newrow.insertCell(column);
newCell.className = "ha-name";
newText = document.createTextNode(name);
newCell.appendChild(newText);
// Value
column++;
newCell = newrow.insertCell(column);
newCell.className = "ha-value";
if (addblinkhigh > 0) {
// If high class set, add the class to the sensor.
if (typeof sensordata[25] !== 'undefined') {
newrow.className += sensordata[25];
} else {
newrow.className += "blinkhigh";
}
}
if (addblinklow > 0) {
// If low class set, add the class to the sensor.
if (typeof sensordata[26] !== 'undefined') {
newrow.className += sensordata[26];
} else {
newrow.className += "blinklow";
}
}
newText = document.createTextNode(newValue);
newCell.appendChild(newText);
// Unit
column++;
newCell = newrow.insertCell(column);
newCell.className = "ha-unit";
newText = document.createTextNode(unit);
newCell.appendChild(newText);
// Return the sensor row if sensor should be shown.
return newrow;
}
// Return an empty row if sensor should not be shown.
return newrow;
},
// Update
scheduleUpdate: function (delay) {
var nextLoad = this.config.updateInterval;
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
}
var self = this;
setInterval(function () {
self.getStats();
}, nextLoad);
},
// Added "this.identifier" to identify what instance of the module that sent update request.
getStats: function () {
this.sendSocketNotification('GET_STATS', { id: this.identifier, config: this.config });
},
// Added "this.identifier" to be able to receive updates from "this" instance of the module only.
socketNotificationReceived: function (notification, payload) {
if (notification === "STATS_RESULT" && this.identifier == payload.id) {
this.result = payload.data;
this.updateDom(this.config.fade);
}
},
});