-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.js
1896 lines (1735 loc) · 64.2 KB
/
main.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
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"use strict";
/*
* Created with @iobroker/create-adapter v2.1.1
*/
// The adapter-core module gives you access to the core ioBroker functions
// you need to create an adapter
const utils = require("@iobroker/adapter-core");
// Load your modules here, e.g.:
// const fs = require("fs");
/* Variables for runtime */
let unit;
let unit_swap;
let production;
let production0;
let production1;
let consumption;
let grid_feed;
let grid_consuming;
let grid_different;
let grid_reverse;
let grid_all_positive;
let battery_percent;
let battery_charge;
let battery_discharge;
let battery_different;
let battery_reverse;
let battery_dod;
let calculate_consumption;
let consumption_reverse;
let recalculate;
let custom0, custom1, custom2, custom3, custom4, custom5, custom6, custom7, custom8, custom9, custom10;
let swap_consumption, swap_production, swap_production0, swap_production1, swap_grid;
let house_netto = {}
let fraction;
let fraction_battery;
let threshold;
let battery_info;
let battery_capacity;
let custom0_percent;
let custom10_percent;
let car_custom_plugged;
let car_custom10_plugged;
let custom_type, custom4_type, custom9_type, custom10_type;
let automatic_animation = false;
let no_feed_in;
let neg_feed_in;
/* Data Objects */
let kwCalc = {};
let valuesObj = {};
let dataObj = {
values: {},
animations: {},
battery_animation: {},
color: {},
swap_values: {},
automatic_animation: {}
};
let configObj = {};
let subscribeArray = new Array();
let parameterObj = {
lines: {
lines: {},
style: {},
color: {},
animation_colors: {}
},
elements: {
elements: {},
style: {},
fill: {},
color: {}
},
fonts: {},
general: {},
icons: {
icons: {},
color: {},
style: {}
},
texts: {
texts: {},
labels: {},
color: {},
style: {}
},
values: {
values: {},
style: {}
},
percent: {
style: {}
},
custom_symbol: {},
swap_texts: {
labels: {}
}
};
class Energiefluss extends utils.Adapter {
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
constructor(options) {
super({
...options,
name: "energiefluss",
});
this.on("ready", this.onReady.bind(this));
this.on("stateChange", this.onStateChange.bind(this));
// this.on("objectChange", this.onObjectChange.bind(this));
// this.on("message", this.onMessage.bind(this));
this.on("unload", this.onUnload.bind(this));
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
// Upgrade older config
if (await this.migrateConfig()) {
return;
}
// Initialize your adapter here
unit = this.config.unit;
neg_feed_in = this.config.neg_feed_in ? this.config.neg_feed_in : false;
unit_swap = this.config.unit_swap;
production = this.config.production;
production0 = this.config.production0;
production1 = this.config.production1;
consumption = this.config.consumption;
consumption_reverse = this.config.consumption_reverse ? this.config.consumption_reverse : false;
grid_feed = this.config.grid_feed;
grid_consuming = this.config.grid_consuming;
grid_different = this.config.grid_different;
grid_reverse = this.config.grid_reverse ? true : false;
grid_all_positive = this.config.grid_all_positive ? true : false;
battery_percent = this.config.battery_percent;
battery_charge = this.config.battery_charge;
battery_discharge = this.config.battery_discharge;
battery_different = this.config.battery_different;
battery_reverse = this.config.battery_reverse ? this.config.battery_reverse : false;
calculate_consumption = this.config.calculate_consumption ? this.config.calculate_consumption : false;
automatic_animation = this.config.automatic_animation ? this.config.automatic_animation : false;
no_feed_in = this.config.no_feed_in ? this.config.no_feed_in : false;
custom0 = this.config.custom0;
custom1 = this.config.custom1;
custom2 = this.config.custom2;
custom3 = this.config.custom3;
custom4 = this.config.custom4;
custom5 = this.config.custom5;
custom6 = this.config.custom6;
custom7 = this.config.custom7;
custom8 = this.config.custom8;
custom9 = this.config.custom9;
custom10 = this.config.custom10;
house_netto = {
custom0: this.config.house_netto_custom0,
custom1: this.config.house_netto_custom1,
custom2: this.config.house_netto_custom2,
custom3: this.config.house_netto_custom3,
custom4: this.config.house_netto_custom4,
custom5: this.config.house_netto_custom5,
custom6: this.config.house_netto_custom6,
custom7: this.config.house_netto_custom7,
custom8: this.config.house_netto_custom8,
custom9: this.config.house_netto_custom9,
custom10: this.config.house_netto_custom10,
}
fraction = this.config.fraction;
fraction_battery = this.config.fraction_battery;
threshold = this.config.threshold ? this.config.threshold : 0;
battery_info = this.config.battery_capacity_info ? this.config.battery_capacity_info : false;
battery_capacity = this.config.battery_capacity ? this.config.battery_capacity : 0;
battery_dod = this.config.battery_dod ? this.config.battery_dod : 0;
custom0_percent = this.config.car_custom_percent;
car_custom_plugged = this.config.car_custom_plugged;
custom10_percent = this.config.car_custom10_percent;
car_custom10_plugged = this.config.car_custom10_plugged;
custom_type = this.config.custom_type;
custom10_type = this.config.custom10_type;
custom4_type = this.config.custom4_type;
custom9_type = this.config.custom9_type;
recalculate = this.config.recalculate ? this.config.recalculate : false;
swap_consumption = this.config.swap_consumption;
swap_grid = this.config.swap_grid;
swap_production = this.config.swap_production;
swap_production0 = this.config.swap_production0;
swap_production1 = this.config.swap_production1;
this.log.info("Starting Energiefluss Adapter");
// Put all kW Sources into an Object
kwCalc = {
production: this.config.kw_production ? this.config.kw_production : false,
production0: this.config.kw_production0 ? this.config.kw_production0 : false,
production1: this.config.kw_production1 ? this.config.kw_production1 : false,
consumption: this.config.kw_consumption ? this.config.kw_consumption : false,
grid_feed: this.config.kw_grid_feed ? this.config.kw_grid_feed : false,
grid_consuming: this.config.kw_grid_consuming ? this.config.kw_grid_consuming : false,
battery_charge: this.config.kw_battery_charge ? this.config.kw_battery_charge : false,
battery_discharge: this.config.kw_battery_discharge ? this.config.kw_battery_discharge : false,
custom0: this.config.kw_custom0 ? this.config.kw_custom0 : false,
custom1: this.config.kw_custom1 ? this.config.kw_custom1 : false,
custom2: this.config.kw_custom2 ? this.config.kw_custom2 : false,
custom3: this.config.kw_custom3 ? this.config.kw_custom3 : false,
custom4: this.config.kw_custom4 ? this.config.kw_custom4 : false,
custom5: this.config.kw_custom5 ? this.config.kw_custom5 : false,
custom6: this.config.kw_custom6 ? this.config.kw_custom6 : false,
custom7: this.config.kw_custom7 ? this.config.kw_custom7 : false,
custom8: this.config.kw_custom8 ? this.config.kw_custom8 : false,
custom9: this.config.kw_custom9 ? this.config.kw_custom9 : false,
custom10: this.config.kw_custom10 ? this.config.kw_custom10 : false
}
// Put all possible variables into an Object and after that, filter empty ones
configObj = {
production: production,
production0: production0,
production1: production1,
consumption: consumption,
grid_feed: grid_feed,
grid_consuming: grid_consuming,
battery_charge: battery_charge,
battery_discharge: battery_discharge,
battery_percent: battery_percent,
custom0: custom0,
custom1: custom1,
custom2: custom2,
custom3: custom3,
custom4: custom4,
custom5: custom5,
custom6: custom6,
custom7: custom7,
custom8: custom8,
custom9: custom9,
custom10: custom10,
custom0_percent: custom0_percent,
car_custom_plugged: car_custom_plugged,
custom10_percent: custom10_percent,
car_custom10_plugged: car_custom10_plugged,
swap_consumption: swap_consumption,
swap_production: swap_production,
swap_production0: swap_production0,
swap_production1: swap_production1,
swap_grid: swap_grid
};
// Delete empty ones
configObj = Object.entries(configObj).reduce((a, [k, v]) => (v ? (a[k] = v, a) : a), {})
this.log.debug("Added States for subscribing: " + JSON.stringify(configObj));
// Load all Data once before subscribing
valuesObj = await this.getInitialValues(configObj);
// Build array for subscribing to states
subscribeArray = Object.values(configObj);
this.log.debug("Requesting the following states: " + subscribeArray.toString());
/*
For every state in the system there has to be also an object of type state
Here a simple template for a boolean variable named "testVariable"
Because every adapter instance uses its own unique namespace variable names can't collide with other adapters variables
*/
await this.setObjectNotExistsAsync('configuration', {
type: 'state',
common: {
name: 'Parameters for HTML Output',
type: 'json',
role: 'state',
read: true,
write: false,
},
native: {},
});
await this.setObjectNotExistsAsync('data', {
type: 'state',
common: {
name: 'Data for HTML Output',
type: 'json',
role: 'state',
read: true,
write: false,
},
native: {},
});
await this.setObjectNotExistsAsync('battery_remaining', {
type: 'state',
common: {
name: 'Remaining Time of the battery',
type: 'string',
role: 'text',
read: true,
write: false,
},
native: {},
});
// Delete old State HTML
await this.delObject('HTML');
/* Build Parameter */
// Colors of the Elements
parameterObj.elements.color = {
house: this.config.color_house,
grid: this.config.color_grid,
solar: this.config.color_production,
solar0: this.config.color_production0,
solar1: this.config.color_production1,
battery: this.config.color_battery,
custom0: this.config.color_custom0,
custom1: this.config.color_custom1,
custom2: this.config.color_custom2,
custom3: this.config.color_custom3,
custom4: this.config.color_custom4,
custom5: this.config.color_custom5,
custom6: this.config.color_custom6,
custom7: this.config.color_custom7,
custom8: this.config.color_custom8,
custom9: this.config.color_custom9,
custom10: this.config.color_custom10
}
// Fills of the Elements
parameterObj.elements.fill = {
house: this.config.fill_color_house,
grid: this.config.fill_color_grid,
solar: this.config.fill_color_production,
solar0: this.config.fill_color_production0,
solar1: this.config.fill_color_production1,
battery: this.config.fill_color_battery,
custom0: this.config.fill_color_custom0,
custom1: this.config.fill_color_custom1,
custom2: this.config.fill_color_custom2,
custom3: this.config.fill_color_custom3,
custom4: this.config.fill_color_custom4,
custom5: this.config.fill_color_custom5,
custom6: this.config.fill_color_custom6,
custom7: this.config.fill_color_custom7,
custom8: this.config.fill_color_custom8,
custom9: this.config.fill_color_custom9,
custom10: this.config.fill_color_custom10
}
// Colors of the lines
parameterObj.lines.color = {
solar_to_battery: this.config.color_solar_to_battery,
solar_to_grid: this.config.color_solar_to_grid,
solar_to_house: this.config.color_solar_to_house,
solar0_to_solar: this.config.color_solar0_to_solar,
solar1_to_solar: this.config.color_solar1_to_solar,
grid_to_house: this.config.color_grid_to_house,
grid_to_battery: this.config.color_grid_to_battery,
battery_to_house: this.config.color_battery_to_house,
default: this.config.color_lines,
house_to_custom0: this.config.color_house_to_custom0,
house_to_custom1: this.config.color_house_to_custom1,
house_to_custom2: this.config.color_house_to_custom2,
house_to_custom3: this.config.color_house_to_custom3,
house_to_custom4: this.config.color_house_to_custom4,
house_to_custom5: this.config.color_house_to_custom5,
house_to_custom6: this.config.color_house_to_custom6,
house_to_custom7: this.config.color_house_to_custom7,
house_to_custom8: this.config.color_house_to_custom8,
house_to_custom9: this.config.color_house_to_custom9,
house_to_custom10: this.config.color_house_to_custom10,
}
// Animation Colors of the lines
parameterObj.lines.animation_colors = {
solar_to_battery: this.config.animation_color_solar_to_battery,
solar_to_grid: this.config.animation_color_solar_to_grid,
solar_to_house: this.config.animation_color_solar_to_house,
solar0_to_solar: this.config.animation_color_solar0_to_solar,
solar1_to_solar: this.config.animation_color_solar1_to_solar,
grid_to_house: this.config.animation_color_grid_to_house,
grid_to_battery: this.config.animation_color_grid_to_battery,
battery_to_house: this.config.animation_color_battery_to_house,
default: this.config.color_animation,
house_to_custom0: this.config.animation_color_house_to_custom0,
house_to_custom1: this.config.animation_color_house_to_custom1,
house_to_custom2: this.config.animation_color_house_to_custom2,
house_to_custom3: this.config.animation_color_house_to_custom3,
house_to_custom4: this.config.animation_color_house_to_custom4,
house_to_custom5: this.config.animation_color_house_to_custom5,
house_to_custom6: this.config.animation_color_house_to_custom6,
house_to_custom7: this.config.animation_color_house_to_custom7,
house_to_custom8: this.config.animation_color_house_to_custom8,
house_to_custom9: this.config.animation_color_house_to_custom9,
house_to_custom10: this.config.animation_color_house_to_custom10
}
// Style of the Lines
parameterObj.lines.style = {
line_size: this.config.line_size,
animation_width: this.config.animation_width,
animation: this.config.animation,
animation_duration: this.config.animation_duration,
animation_linecap: this.config.animation_linecap,
animation_type: this.config.animation_type
}
// Fonts
parameterObj.fonts = {
font_src: this.config.font_src,
font: this.config.font,
font_size_label: this.config.font_size_label,
font_size_value: this.config.font_size_value,
font_size_percent: this.config.font_size_percent,
font_size_unit: this.config.font_size_unit,
font_size_unit_percent: this.config.font_size_unit_percent,
font_size_remaining: this.config.font_size_remaining
}
// Labels
parameterObj.texts.labels = {
house: this.config.label_house,
solar: this.config.label_production,
solar0: this.config.label_production0,
solar1: this.config.label_production1,
grid: this.config.label_grid,
battery: this.config.label_battery,
battery_remaining: this.config.label_battery_remaining,
custom0: this.config.label_custom0,
custom1: this.config.label_custom1,
custom2: this.config.label_custom2,
custom3: this.config.label_custom3,
custom4: this.config.label_custom4,
custom5: this.config.label_custom5,
custom6: this.config.label_custom6,
custom7: this.config.label_custom7,
custom8: this.config.label_custom8,
custom9: this.config.label_custom9,
custom10: this.config.label_custom10,
}
// Label Color
parameterObj.texts.color = {
default: this.config.color_label,
// Label Battery Remaining Time Color
battery_remaining: this.config.color_battery_remain
}
parameterObj.texts.style = {
shadow: this.config.text_shadow,
shadow_color: this.config.text_shadow_color,
}
// Icon Color
parameterObj.icons.color.default = this.config.color_icon;
parameterObj.icons.style = {
shadow: this.config.icons_shadow,
shadow_color: this.config.icons_shadow_color,
}
// Custom's
parameterObj.custom_symbol = {
icon_custom0: this.config.custom_icon0,
icon_custom1: this.config.custom_icon1,
icon_custom2: this.config.custom_icon2,
icon_custom3: this.config.custom_icon3,
icon_custom4: this.config.custom_icon4,
icon_custom5: this.config.custom_icon5,
icon_custom6: this.config.custom_icon6,
icon_custom7: this.config.custom_icon7,
icon_custom8: this.config.custom_icon8,
icon_custom9: this.config.custom_icon9,
icon_custom10: this.config.custom_icon10,
icon_solar: this.config.icon_production,
icon_solar0: this.config.icon_production0,
icon_solar1: this.config.icon_production1,
icon_grid: this.config.icon_grid,
icon_house: this.config.icon_house
}
// General
parameterObj.general = {
no_battery: false,
unit: unit,
unit_swap: unit_swap,
battery_animation: this.config.battery_animation,
fill_elements: this.config.fill_elements,
slim_design: this.config.slim_design,
type: this.config.element_type,
custom_type: this.config.custom_type,
offset_icon: this.config.offset_icon || 0,
offset_text: this.config.offset_text || 0,
offset_value: this.config.offset_value || 0,
offset_percent: this.config.offset_percent || 0,
offset_remaining: this.config.offset_remaining || 0,
opacity_icon: this.config.opacity_icon || 70,
opacity_text: this.config.opacity_text || 70,
opacity_value: this.config.opacity_value || 100,
opacity_percent: this.config.opacity_percent || 100,
opacity_remaining: this.config.opacity_remaining || 70,
opacity_line: this.config.opacity_line || 70,
element_distance: this.config.element_distance || 15,
element_animation: this.config.element_animation,
element_animation_time: this.config.element_animation_time || 5000,
line_visible: this.config.line_visible,
automatic_animation: automatic_animation,
disable_icons: this.config.disable_icons,
debounce_percent: this.config.debounce_percent,
background_color: this.config.background_color || "transparent"
}
// Element - Style
parameterObj.elements.style = {
size: this.config.element_size || 2,
circle_radius: this.config.circle_radius || 50,
shadow: this.config.element_shadow,
shadow_color: this.config.element_shadow_color,
rect_height: this.config.rect_height,
rect_width: this.config.rect_width,
rect_corner: this.config.rect_corner
}
// Percent Shadow
parameterObj.percent.style = {
shadow: this.config.percent_shadow,
shadow_color: this.config.percent_shadow_color,
}
// Values Shadow
parameterObj.values.style = {
shadow: this.config.values_shadow,
shadow_color: this.config.values_shadow_color,
}
// Swap Texts
parameterObj.swap_texts.labels = {
solar: this.config.label_swap_production,
solar0: this.config.label_swap_production0,
solar1: this.config.label_swap_production1,
grid: this.config.label_swap_grid,
house: this.config.label_swap_consumption
}
// In order to get state updates, you need to subscribe to them. The following line adds a subscription for our variable we have created above.
this.subscribeForeignStates(subscribeArray);
// You can also add a subscription for multiple states. The following line watches all states starting with "lights."
// this.subscribeStates("lights.*");
// Or, if you really must, you can also watch all states. Don't do this if you don't need to. Otherwise this will cause a lot of unnecessary load on the system:
// this.subscribeStates("*");
this.log.info("Adapter started and listening to " + subscribeArray.length + " States");
this.log.debug("Initial Values: " + JSON.stringify(valuesObj));
// Build the Configuration JSON
this.buildConfigJSON();
// Build the Data JSON
this.buildDataJSON();
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
* @param {() => void} callback
*/
onUnload(callback) {
try {
// Here you must clear all timeouts or intervals that may still be active
// clearTimeout(timeout1);
callback();
} catch (e) {
callback();
}
}
// If you need to react to object changes, uncomment the following block and the corresponding line in the constructor.
// You also need to subscribe to the objects with `this.subscribeObjects`, similar to `this.subscribeStates`.
// /**
// * Is called if a subscribed object changes
// * @param {string} id
// * @param {ioBroker.Object | null | undefined} obj
// */
// onObjectChange(id, obj) {
// if (obj) {
// // The object was changed
// this.log.info(`object ${id} changed: ${JSON.stringify(obj)}`);
// } else {
// // The object was deleted
// this.log.info(`object ${id} deleted`);
// }
// }
/**
* Is called if a subscribed state changes
* @param {string} id
* @param {ioBroker.State | null | undefined} state
*/
onStateChange(id, state) {
// Check the corresponding state for changes
// Production
if (state) {
let clearValue;
if (typeof (state.val) === 'string') {
clearValue = Number(state.val.replace(/[^\d.-]/g, ''));
} else {
clearValue = state.val;
}
if (id == custom0) {
valuesObj['custom0'] = kwCalc["custom0"] === true ? this.kwValue(clearValue) : clearValue;
}
if (id == custom1) {
valuesObj['custom1'] = kwCalc["custom1"] === true ? this.kwValue(clearValue) : clearValue;
}
if (id == custom2) {
valuesObj['custom2'] = kwCalc["custom2"] === true ? this.kwValue(clearValue) : clearValue;
}
if (id == custom3) {
valuesObj['custom3'] = kwCalc["custom3"] === true ? this.kwValue(clearValue) : clearValue;
}
if (id == custom4) {
valuesObj['custom4'] = kwCalc["custom4"] === true ? this.kwValue(clearValue) : clearValue;
}
if (id == custom5) {
valuesObj['custom5'] = kwCalc["custom5"] === true ? this.kwValue(clearValue) : clearValue;
}
if (id == custom6) {
valuesObj['custom6'] = kwCalc["custom6"] === true ? this.kwValue(clearValue) : clearValue;
}
if (id == custom7) {
valuesObj['custom7'] = kwCalc["custom7"] === true ? this.kwValue(clearValue) : clearValue;
}
if (id == custom8) {
valuesObj['custom8'] = kwCalc["custom8"] === true ? this.kwValue(clearValue) : clearValue;
}
if (id == custom9) {
valuesObj['custom9'] = kwCalc["custom9"] === true ? this.kwValue(clearValue) : clearValue;
}
if (id == custom10) {
valuesObj['custom10'] = kwCalc["custom10"] === true ? this.kwValue(clearValue) : clearValue;
}
if (id == production) {
valuesObj['production'] = kwCalc["production"] === true ? this.kwValue(clearValue) : clearValue;
}
if (id == production0) {
valuesObj['production0'] = kwCalc["production0"] === true ? this.kwValue(clearValue) : clearValue;
}
if (id == production1) {
valuesObj['production1'] = kwCalc["production1"] === true ? this.kwValue(clearValue) : clearValue;
}
if (id == consumption) {
let consumption = kwCalc["consumption"] === true ? this.kwValue(clearValue) : clearValue;
if (consumption_reverse) {
consumption = consumption * (-1);
}
valuesObj['consumption'] = consumption;
}
if (id == grid_feed) {
valuesObj['grid_feed'] = kwCalc["grid_feed"] === true ? this.kwValue(clearValue) : clearValue;
}
if (id == grid_consuming) {
valuesObj['grid_consuming'] = kwCalc["grid_consuming"] === true ? this.kwValue(clearValue) : clearValue;
}
if (id == battery_percent) {
valuesObj['battery_percent'] = (Math.round(clearValue * 100) / 100).toFixed(fraction_battery);
}
if (id == battery_charge) {
valuesObj['battery_charge'] = kwCalc["battery_charge"] === true ? this.kwValue(clearValue) : clearValue;
}
if (id == battery_discharge) {
valuesObj['battery_discharge'] = kwCalc["battery_discharge"] === true ? this.kwValue(clearValue) : clearValue;
}
if (id == custom10_percent) {
valuesObj['custom10_percent'] = clearValue;
}
if (id == car_custom10_plugged) {
valuesObj['car_custom10_plugged'] = clearValue;
}
if (id == custom0_percent) {
valuesObj['custom0_percent'] = clearValue;
}
if (id == car_custom_plugged) {
valuesObj['car_custom_plugged'] = clearValue;
}
if (id == swap_consumption) {
valuesObj['swap_consumption'] = clearValue;
}
if (id == swap_production) {
valuesObj['swap_production'] = clearValue;
}
if (id == swap_production0) {
valuesObj['swap_production0'] = clearValue;
}
if (id == swap_production1) {
valuesObj['swap_production1'] = clearValue;
}
if (id == swap_grid) {
valuesObj['swap_grid'] = clearValue;
}
if (calculate_consumption) {
let prodValue = Number(valuesObj['production']) > 0 ? Number(valuesObj['production']) : 0;
if (valuesObj['production0'] != undefined) {
prodValue += Number(valuesObj['production0']) > 0 ? Number(valuesObj['production0']) : 0;
}
if (valuesObj['production1'] != undefined) {
prodValue += Number(valuesObj['production1']) > 0 ? Number(valuesObj['production1']) : 0;
}
let consumptionValue = 0;
if (grid_all_positive) {
consumptionValue = Number(valuesObj['grid_consuming']) + (prodValue - Number(valuesObj['grid_feed']));
} else {
if (grid_reverse) {
consumptionValue = Number(valuesObj['grid_feed']) + prodValue;
} else {
consumptionValue = (Number(valuesObj['grid_feed']) * -1) + prodValue;
}
}
valuesObj['consumption'] = consumptionValue;
}
}
this.log.debug("States changed: " + JSON.stringify(valuesObj));
/* Build the JSON Arrays to store in states */
this.buildDataJSON();
}
// If you need to accept messages in your adapter, uncomment the following block and the corresponding line in the constructor.
// /**
// * Some message was sent to this instance over message box. Used by email, pushover, text2speech, ...
// * Using this method requires "common.messagebox" property to be set to true in io-package.json
// * @param {ioBroker.Message} obj
// */
// onMessage(obj) {
// if (typeof obj === "object" && obj.message) {
// if (obj.command === "send") {
// // e.g. send email or pushover or whatever
// this.log.info("send command");
// // Send response in callback if required
// if (obj.callback) this.sendTo(obj.from, obj.command, "Message received", obj.callback);
// }
// }
// }
/**
* @param {number} value
*/
recalculateValue(value) {
return (Math.round((value / 1000) * 100) / 100).toFixed(fraction);
}
/**
* @param {number} value
*/
kwValue(value) {
return (value * 1000);
}
/**
* @param {number} value
*/
floorNumber(value) {
return (Math.round(value * 100) / 100).toFixed(fraction);
}
/**
* @param {number} minutes
*/
getMinHours(minutes) {
let mins = minutes;
let m = mins % 60;
let h = (mins - m) / 60;
let HHMM = (h < 10 ? "0" : "") + h.toString() + ":" + (m < 10 ? "0" : "") + m.toString();
return HHMM;
}
async migrateConfig() {
const native = {};
if (this.config?.car_charge) {
native.custom10 = this.config.car_charge;
native.car_charge = '';
native.custom_icon10 = 'M18.92 2C18.72 1.42 18.16 1 17.5 1H6.5C5.84 1 5.29 1.42 5.08 2L3 8V16C3 16.55 3.45 17 4 17H5C5.55 17 6 16.55 6 16V15H18V16C18 16.55 18.45 17 19 17H20C20.55 17 21 16.55 21 16V8L18.92 2M6.85 3H17.14L18.22 6.11H5.77L6.85 3M19 13H5V8H19V13M7.5 9C8.33 9 9 9.67 9 10.5S8.33 12 7.5 12 6 11.33 6 10.5 6.67 9 7.5 9M16.5 9C17.33 9 18 9.67 18 10.5S17.33 12 16.5 12C15.67 12 15 11.33 15 10.5S15.67 9 16.5 9M7 20H11V18L17 21H13V23L7 20Z';
}
if (this.config?.car_percent) {
native.car_custom10_percent = this.config.car_percent;
native.car_percent = '';
}
if (this.config?.car_plugged) {
native.car_custom10_plugged = this.config.car_plugged;
native.car_plugged = '';
}
if (this.config?.color_car) {
native.color_custom10 = this.config.color_car;
native.color_car = '';
}
if (this.config?.color_car_text) {
native.color_custom10_text = this.config.color_car_text;
native.color_car_text = '';
}
if (this.config?.color_car_plugged) {
native.color_car_custom10_plugged = this.config.color_car_plugged;
native.color_car_plugged = '';
}
if (this.config?.fill_color_car) {
native.fill_color_custom10 = this.config.fill_color_car;
native.fill_color_car = '';
}
if (this.config?.color_house_to_car) {
native.color_house_to_custom10 = this.config.color_house_to_car;
native.color_house_to_car = '';
}
if (this.config?.color_car_percent) {
native.color_car_custom10_percent = this.config.color_car_percent;
native.color_car_percent = '';
}
if (this.config?.label_car) {
native.label_custom10 = this.config.label_car;
native.label_car = '';
}
if (this.config?.animation_color_house_to_car) {
native.animation_color_house_to_custom10 = this.config.animation_color_house_to_car;
native.animation_color_house_to_car = '';
}
if (Object.keys(native).length) {
this.log.info('Migrate ' + Object.keys(native).length + ' value(s) from old Energiefluss Adapter version ...');
await this.extendForeignObjectAsync('system.adapter.' + this.namespace, { native: native });
return true;
}
return false;
}
/**
* @param {number} percent
* @param {string} direction
* @param {number} energy
*/
calculateBatteryRemaining(percent, direction, energy) {
let rest = 0;
let mins = 0;
let result = "";
let watts = recalculate ? energy * 1000 : energy;
let string = this.config.label_battery_remaining;
if (percent > 0 && energy > 0) {
if (direction == "charge") {
// Get the Rest to Full Charge
rest = battery_capacity - ((battery_capacity * percent) / 100);
}
if (direction == "discharge") {
// Get the Rest to Full Discharge
rest = (battery_capacity * (percent - battery_dod)) / 100;
}
mins = Math.round((rest / watts) * 60);
if (mins > 0) {
result = this.getMinHours(mins);
} else {
result = "--:--";
}
string += ": " + result + "h";
} else {
string += ": --:--h";
}
return string;
}
/**
* @param {Object} obj
*/
async getInitialValues(obj) {
let tmpObj = {};
for (var key of Object.keys(obj)) {
const value = obj[key];
const stateValue = await this.getForeignStateAsync(value);
if (stateValue) {
let tmpVal;
if (typeof (stateValue.val) === 'number') {
if (!key.includes("percent")) {
tmpVal = kwCalc[key] === true ? this.kwValue(stateValue.val) : Number(stateValue.val);
tmpObj[key] = recalculate ? this.recalculateValue(tmpVal) : this.floorNumber(tmpVal);
} else {
tmpObj[key] = Number(stateValue.val);
}
}
if (typeof (stateValue.val) === 'string') {
if (!key.includes("percent")) {
let clearValue = stateValue.val.replace(/[^\d.-]/g, '');
tmpVal = kwCalc[key] === true ? this.kwValue(Number(clearValue)) : Number(clearValue);
tmpObj[key] = recalculate ? this.recalculateValue(tmpVal) : this.floorNumber(tmpVal);
} else {
tmpObj[key] = Number(stateValue.val);
}
}
} else {
this.log.warn("The adapter could not find the state " + value + "! Please review your configuration of the adapter!");
}
}
if (calculate_consumption) {
tmpObj.consumption = 0;
}
return tmpObj;
}
async buildConfigJSON() {
// Elements - init as false
let elementsObj = {
house: false,
solar: false,
solar0: false,
solar1: false,
grid: false,
battery: false,
custom0: false,
custom1: false,
custom2: false,
custom3: false,
custom4: false,
custom5: false,
custom6: false,
custom7: false,
custom8: false,
custom9: false,
custom10: false
};
// Texts
let textObj = {
consumption_text: false,
production_text: false,
production0_text: false,
production1_text: false,
grid_text: false,
battery_text: false,
battery_remaining_text: false,
custom0_text: false,
custom1_text: false,
custom2_text: false,
custom3_text: false,
custom4_text: false,
custom5_text: false,
custom6_text: false,
custom7_text: false,
custom8_text: false,
custom9_text: false,
custom10_text: false
};
// Values
let valueObj = {
consumption_value: false,
production_value: false,
production0_value: false,
production1_value: false,
grid_value: false,
custom0_percent: false,
custom10_percent: false,
battery_value: false,
battery_percent: false,
custom0_value: false,
custom1_value: false,
custom2_value: false,
custom3_value: false,
custom4_value: false,
custom5_value: false,
custom6_value: false,
custom7_value: false,
custom8_value: false,
custom9_value: false,
custom10_value: false,
};
// Icons
let iconObj = {
house: false,
production: false,
production0: false,
production1: false,
grid: false,
battery: false,
custom0: false,
custom1: false,
custom2: false,
custom3: false,
custom4: false,
custom5: false,