-
Notifications
You must be signed in to change notification settings - Fork 0
/
pwrmon-1.26.sh
2212 lines (1983 loc) · 87.3 KB
/
pwrmon-1.26.sh
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
#!/bin/sh
#
# PWRMON v1.26 - Asus-Merlin Tesla Powerwall Monitor by Viktor Jaep, 2022
#
# PWRMON is a shell script that provides near-realtime stats about your Tesla Powerwall/Solar environment. This utility
# will show all the current electrical loads being generated or consumed by your solar system, the grid, your home and
# your Powerwall(s). Electrical transmission flows are accurately being depicted using >> and << types of arrows, as
# electricity moves between your solar, to/from your batteries, to/from the grid and to your home. In the event of a
# electrical grid outage, PWRMON will calculate your estimated remaining runtime left on your batteries based on the
# amount of kW being consumed by your home. Weather has now been incorporated to give you a 3 day outlook on what kind
# of weather to expect for generation or off-grid consumption due to regional disasters or weather events.
#
# Instead of having to find this information on various different web pages or apps, this tool was built to bring all this
# info together in one stat dashboard. Having a 'system' dashboard showing current solar, grid, home and powerwall stats
# would compliment other dashboard-like scripts greatly (like RTRMON or VPNMON-R2), sitting side-by-side in their own SSH
# windows to give you everything you need to know with a glance at your screen.
#
# Please use the 'pwrmon.sh -setup' to configure the necessary parameters that match your environment the best!
#
# -------------------------------------------------------------------------------------------------------------------------
# Shellcheck exclusions
# -------------------------------------------------------------------------------------------------------------------------
# shellcheck disable=SC2034
# shellcheck disable=SC3037
# shellcheck disable=SC2162
# shellcheck disable=SC3045
# shellcheck disable=SC2183
# shellcheck disable=SC2086
# shellcheck disable=SC3014
# shellcheck disable=SC2059
# shellcheck disable=SC2002
# shellcheck disable=SC2004
# shellcheck disable=SC3028
# shellcheck disable=SC2140
# shellcheck disable=SC3046
# shellcheck disable=SC1090
#
# -------------------------------------------------------------------------------------------------------------------------
# System Variables (Do not change beyond this point or this may change the programs ability to function correctly)
# -------------------------------------------------------------------------------------------------------------------------
Version=1.26
Beta=0
LOGFILE="/jffs/addons/pwrmon.d/pwrmon.log" # Logfile path/name that captures important date/time events - change
APPPATH="/jffs/scripts/pwrmon.sh" # Path to the location of pwrmon.sh
CFGPATH="/jffs/addons/pwrmon.d/pwrmon.cfg" # Path to the location of pwrmon.cfg
DLVERPATH="/jffs/addons/pwrmon.d/version.txt" # Path to downloaded version from the source repository
cookie_file="/jffs/addons/pwrmon.d/pwrmon.cookies" # Path to cookies file needed to log into the Powerwall Gateway
smtpbody="/jffs/addons/pwrmon.d/smtpbody.txt" # Path to email message txt file used to send Powerwall notifications
WANwxforecast="/jffs/addons/pwrmon.d/WANwx.txt" # Path to weather forecast JSON extract used for weather displays
NextPage=1
UpdateNotify=0
FromUI=0
griddown=0
#Default Values
Interval=10
email="[email protected]"
password="ABCDE"
gatewayip="192.168.1.150"
numpowerwalls=2
maxsolargen=7
maxhomeelecload=24
emailalerts="Disabled"
smtpusername="[email protected]"
smtprecipient="[email protected]"
smsrecipient="Disabled"
smtppassword="U2FsdGVkX19IDBTBl5wMrbyLkj4VspYb"
smtpserver="smtp.gmail.com"
smtpport="465"
alertsentdown="False"
alertsentsync="False"
smsalertsentdown="False"
smsalertsentsync="False"
ProgPref=0
autorotate=0
autorotateindicator="OFF"
# Color variables
CBlack="\e[1;30m"
InvBlack="\e[1;40m"
CRed="\e[1;31m"
InvRed="\e[1;41m"
CGreen="\e[1;32m"
InvGreen="\e[1;42m"
CDkGray="\e[1;90m"
InvDkGray="\e[1;100m"
InvLtGray="\e[1;47m"
CYellow="\e[1;33m"
InvYellow="\e[1;43m"
CBlue="\e[1;34m"
InvBlue="\e[1;44m"
CMagenta="\e[1;35m"
CCyan="\e[1;36m"
InvCyan="\e[1;46m"
CWhite="\e[1;37m"
InvWhite="\e[1;107m"
CClear="\e[0m"
# -------------------------------------------------------------------------------------------------------------------------
# Functions
# -------------------------------------------------------------------------------------------------------------------------
# Logo is a function that displays the PWRMON script name in a cool ASCII font
logo () {
echo -e "${CYellow} ____ _ ______ __ _______ _ __"
echo -e " / __ \ | / / __ \/ |/ / __ \/ | / / ${CGreen}v$Version${CYellow}"
echo -e " / /_/ / | /| / / /_/ / /|_/ / / / / |/ / ${CRed}(S)${CGreen}etup ${CRed}(W)${CGreen}eather${CYellow}"
echo -e " / ____/| |/ |/ / _, _/ / / / /_/ / /| / ${CRed}(N)${CGreen}xt/${CRed}(P)${CGreen}rv Pg ($NextPage/3)${CYellow}"
echo -e " /_/ |__/|__/_/ |_/_/ /_/\____/_/ |_/ ${CRed}(R)${CGreen}otate Pgs:${CCyan}$autorotateindicator ${CClear}${CRed}(E)${CGreen}xit${CClear}"
}
# -------------------------------------------------------------------------------------------------------------------------
# LogoNM is a function that displays the PWRMON script name in a cool ASCII font sans menu
logoNM () {
echo -e "${CYellow} ____ _ ______ __ _______ _ __"
echo -e " / __ \ | / / __ \/ |/ / __ \/ | / / ${CGreen}v$Version${CYellow}"
echo -e " / /_/ / | /| / / /_/ / /|_/ / / / / |/ /"
echo -e " / ____/| |/ |/ / _, _/ / / / /_/ / /| /"
echo -e " /_/ |__/|__/_/ |_/_/ /_/\____/_/ |_/"
}
# -------------------------------------------------------------------------------------------------------------------------
# promptyn takes input for Y/N questions
promptyn () { # No defaults, just y or n
while true; do
read -p "[y/n]? " -n 1 -r yn
case "${yn}" in
[Yy]* ) return 0 ;;
[Nn]* ) return 1 ;;
* ) echo -e "\nPlease answer y or n.";;
esac
done
}
# -------------------------------------------------------------------------------------------------------------------------
# Spinner is a script that provides a small indicator on the screen to show script activity
spinner() {
i=0
j=$((SPIN / 4))
while [ $i -le $j ]; do
for s in / - \\ \|; do
printf "\r$s"
sleep 1
done
i=$((i+1))
done
printf "\r"
}
# -------------------------------------------------------------------------------------------------------------------------
# Preparebar and Progressbar is a script that provides a nice progressbar to show script activity
preparebar() {
# $1 - bar length
# $2 - bar char
#printf "\n"
barlen=$1
barspaces=$(printf "%*s" "$1")
barchars=$(printf "%*s" "$1" | tr ' ' "$2")
}
# Had to make some mods to the variables being passed, and created an inverse colored progress bar
progressbar() {
# $1 - number (-1 for clearing the bar)
# $2 - max number
# $3 - system name
# $4 - measurement
# $5 - standard/reverse progressbar
# $6 - alternate display values
insertspc=" "
if [ $1 -eq -1 ]; then
printf "\r $barspaces\r"
else
barch=$(($1*barlen/$2))
barsp=$((barlen-barch))
progr=$((100*$1/$2))
if [ ! -z $6 ]; then AltNum=$6; else AltNum=$1; fi
if [ "$5" == "Standard" ]; then
if [ $progr -le 60 ]; then
printf "${InvGreen}${CWhite}$insertspc${CClear}${CGreen}${3} [%.${barch}s%.${barsp}s]${CClear} ${CWhite}${InvDkGray}$AltNum ${4} / ${progr}%%\r${CClear}" "$barchars" "$barspaces"
elif [ $progr -gt 60 ] && [ $progr -le 85 ]; then
printf "${InvYellow}${CBlack}$insertspc${CClear}${CYellow}${3} [%.${barch}s%.${barsp}s]${CClear} ${CWhite}${InvDkGray}$AltNum ${4} / ${progr}%%\r${CClear}" "$barchars" "$barspaces"
else
printf "${InvRed}${CWhite}$insertspc${CClear}${CRed}${3} [%.${barch}s%.${barsp}s]${CClear} ${CWhite}${InvDkGray}$AltNum ${4} / ${progr}%%\r${CClear}" "$barchars" "$barspaces"
fi
elif [ "$5" == "Reverse" ]; then
if [ $progr -le 35 ]; then
printf "${InvRed}${CWhite}$insertspc${CClear}${CRed}${3} [%.${barch}s%.${barsp}s]${CClear} ${CWhite}${InvDkGray}$AltNum ${4} / ${progr}%%\r${CClear}" "$barchars" "$barspaces"
elif [ $progr -gt 35 ] && [ $progr -le 85 ]; then
printf "${InvYellow}${CBlack}$insertspc${CClear}${CYellow}${3} [%.${barch}s%.${barsp}s]${CClear} ${CWhite}${InvDkGray}$AltNum ${4} / ${progr}%%\r${CClear}" "$barchars" "$barspaces"
else
printf "${InvGreen}${CWhite}$insertspc${CClear}${CGreen}${3} [%.${barch}s%.${barsp}s]${CClear} ${CWhite}${InvDkGray}$AltNum ${4} / ${progr}%%\r${CClear}" "$barchars" "$barspaces"
fi
fi
fi
}
progressbaroverride() {
# $1 - number (-1 for clearing the bar)
# $2 - max number
# $3 - system name
# $4 - measurement
# $5 - standard/reverse progressbar
# $6 - alternate display values
insertspc=" "
if [ $1 -eq -1 ]; then
printf "\r $barspaces\r"
else
barch=$(($1*barlen/$2))
barsp=$((barlen-barch))
progr=$((100*$1/$2))
if [ ! -z $6 ]; then AltNum=$6; else AltNum=$1; fi
if [ "$5" == "Standard" ]; then
printf " ${CWhite}${InvDkGray}$AltNum${4} / ${progr}%%\r${CClear}" "$barchars" "$barspaces"
fi
fi
}
# -------------------------------------------------------------------------------------------------------------------------
# emailalert send a message to a predetermined email address to inform of major events
emailalert () {
# $1 is the status needing to be sent
if [ $emailalerts == "Enabled" ]; then
smtppasswordplaintxt=$(echo "${smtppassword}" | openssl enc -d -des3 -base64 -pass pass:*TB0c7ksKGWcw22P -pbkdf2)
if [ $1 = "griddown" ] && [ $alertsentdown = "False" ]; then
alertsentdown="True"
{ echo 'From: '$smtpusername '<'$smtpusername'>'
echo 'To: '$smtprecipient '<'$smtprecipient'>'
echo 'Subject: POWERWALL ALERT'
echo 'Content-Type: text/plain'
echo 'X-Priority: 1 (Highest)'
echo 'X-MSMail-Priority: High'
echo ''
echo 'Grid Power is DOWN at site: '$sitename
echo 'Event time: '$(date)
echo 'Battery Status (%): '$battcapp
echo 'Estimated Runtime (H): '$runtime
echo ''
echo ''
echo 'Sent via PWRMON v'$Version
} > $smtpbody
curl --silent --url smtps://$smtpserver:$smtpport --ssl-reqd --mail-from $smtpusername --mail-rcpt $smtprecipient --user $smtpusername:$smtppasswordplaintxt --upload-file $smtpbody
echo -e "$(date) - PWRMON ---------> GRID OUTAGE - Notification sent to $smtprecipient!" >> $LOGFILE
fi
if [ $1 = "sync" ] && [ $alertsentsync = "False" ]; then
alertsentsync="True"
{ echo 'From: '$smtpusername '<'$smtpusername'>'
echo 'To: '$smtprecipient '<'$smtprecipient'>'
echo 'Subject: POWERWALL ALERT'
echo 'Content-Type: text/plain'
echo 'X-Priority: 1 (Highest)'
echo 'X-MSMail-Priority: High'
echo ''
echo 'POWERWALL ALERT!!!'
echo ''
echo 'Grid Power is currently SYNCING at site: '$sitename
echo 'Event time: '$(date)
echo 'Battery Status (%): '$battcapp
echo 'Estimated Runtime (H): '$runtime
echo ''
echo ''
echo 'Sent via PWRMON v'$Version
} > $smtpbody
curl --silent --url smtps://$smtpserver:$smtpport --ssl-reqd --mail-from $smtpusername --mail-rcpt $smtprecipient --user $smtpusername:$smtppasswordplaintxt --upload-file $smtpbody
echo -e "$(date) - PWRMON ---------> GRID SYNCING - Notification sent to $smtprecipient!" >> $LOGFILE
fi
if [ $1 = "normal" ]; then
alertsentdown="False"
alertsentsync="False"
{ echo 'From: '$smtpusername '<'$smtpusername'>'
echo 'To: '$smtprecipient '<'$smtprecipient'>'
echo 'Subject: POWERWALL ALERT'
echo 'Content-Type: text/plain'
echo 'X-Priority: 1 (Highest)'
echo 'X-MSMail-Priority: High'
echo ''
echo 'POWERWALL ALERT!!!'
echo ''
echo 'Grid Power has returned to NORMAL at site: '$sitename
echo 'Event time: '$(date)
echo 'Battery Status (%): '$battcapp
echo 'Estimated Runtime (H): '$runtime
echo ''
echo ''
echo 'Sent via PWRMON v'$Version
} > $smtpbody
curl --silent --url smtps://$smtpserver:$smtpport --ssl-reqd --mail-from $smtpusername --mail-rcpt $smtprecipient --user $smtpusername:$smtppasswordplaintxt --upload-file $smtpbody
echo -e "$(date) - PWRMON ---------> GRID BACK ONLINE - Notification sent to $smtprecipient!" >> $LOGFILE
fi
if [ $1 = "test" ]; then
{ echo 'From: '$smtpusername '<'$smtpusername'>'
echo 'To: '$smtprecipient '<'$smtprecipient'>'
echo 'Subject: POWERWALL ALERT (TEST)'
echo 'Content-Type: text/plain'
echo 'X-Priority: 1 (Highest)'
echo 'X-MSMail-Priority: High'
echo ''
echo 'POWERWALL ALERT!!! (TEST)'
echo ''
echo 'Testing PWRMON Notifications'
echo 'Event time: '$(date)
echo ''
echo 'This is a successful test of a PWRMON notification. In the event of a true grid down situation, you will be notified along with current Battery Status (%) and Estimated Runtime (H) stats. You will receive one final notification indicating when the grid comes back online.'
echo ''
echo ''
echo 'Sent via PWRMON v'$Version
} > $smtpbody
curl --silent --url smtps://$smtpserver:$smtpport --ssl-reqd --mail-from $smtpusername --mail-rcpt $smtprecipient --user $smtpusername:$smtppasswordplaintxt --upload-file $smtpbody
echo -e "$(date) - PWRMON ---------> GRID TEST - Notification sent to $smtprecipient!" >> $LOGFILE
fi
# Clean up messagebody
rm $smtpbody >/dev/null 2>&1
fi
}
# -------------------------------------------------------------------------------------------------------------------------
# smsalert send a message to a predetermined SMS/Text email address to inform of major events
smsalert () {
# $1 is the status needing to be sent
if [ $emailalerts == "Enabled" ] && [ $smsrecipient != "Disabled" ]; then
smtppasswordplaintxt=$(echo "${smtppassword}" | openssl enc -d -des3 -base64 -pass pass:*TB0c7ksKGWcw22P -pbkdf2)
if [ $1 = "griddown" ] && [ $smsalertsentdown = "False" ]; then
smsalertsentdown="True"
{ echo 'From: '$smtpusername '<'$smtpusername'>'
echo 'To: '$smsrecipient '<'$smsrecipient'>'
echo 'Subject: POWERWALL DOWN'
echo 'Content-Type: text/plain'
echo ''
echo 'Grid Power is DOWN!'
echo ' Batt(%):'$battcapp
echo ' Runtime(H):'$runtime
} > $smtpbody
curl --silent --url smtps://$smtpserver:$smtpport --ssl-reqd --mail-from $smtpusername --mail-rcpt $smsrecipient --user $smtpusername:$smtppasswordplaintxt --upload-file $smtpbody
echo -e "$(date) - PWRMON ---------> GRID OUTAGE - Notification sent to $smsrecipient!" >> $LOGFILE
fi
if [ $1 = "sync" ] && [ $smsalertsentsync = "False" ]; then
smsalertsentsync="True"
{ echo 'From: '$smtpusername '<'$smtpusername'>'
echo 'To: '$smsrecipient '<'$smsrecipient'>'
echo 'Subject: POWERWALL SYNCING'
echo 'Content-Type: text/plain'
echo ''
echo 'Grid Power is SYNCING!'
echo ' Batt(%):'$battcapp
echo ' Runtime(H):'$runtime
} > $smtpbody
curl --silent --url smtps://$smtpserver:$smtpport --ssl-reqd --mail-from $smtpusername --mail-rcpt $smsrecipient --user $smtpusername:$smtppasswordplaintxt --upload-file $smtpbody
echo -e "$(date) - PWRMON ---------> GRID SYNCING - Notification sent to $smsrecipient!" >> $LOGFILE
fi
if [ $1 = "normal" ]; then
smsalertsentdown="False"
smsalertsentsync="False"
{ echo 'From: '$smtpusername '<'$smtpusername'>'
echo 'To: '$smsrecipient '<'$smsrecipient'>'
echo 'Subject: POWERWALL NORMAL'
echo 'Content-Type: text/plain'
echo ''
echo 'Grid Power status NORMAL'
echo ' Batt(%):'$battcapp
echo ' Runtime(H):'$runtime
} > $smtpbody
curl --silent --url smtps://$smtpserver:$smtpport --ssl-reqd --mail-from $smtpusername --mail-rcpt $smsrecipient --user $smtpusername:$smtppasswordplaintxt --upload-file $smtpbody
echo -e "$(date) - PWRMON ---------> GRID BACK ONLINE - Notification sent to $smsrecipient!" >> $LOGFILE
fi
if [ $1 = "test" ]; then
{ echo 'From: '$smtpusername '<'$smtpusername'>'
echo 'To: '$smsrecipient '<'$smsrecipient'>'
echo 'Subject: POWERWALL ALERT (TEST)'
echo 'Content-Type: text/plain'
echo ''
echo 'Testing PWRMON Notifications'
} > $smtpbody
curl --silent --url smtps://$smtpserver:$smtpport --ssl-reqd --mail-from $smtpusername --mail-rcpt $smsrecipient --user $smtpusername:$smtppasswordplaintxt --upload-file $smtpbody
echo -e "$(date) - PWRMON ---------> GRID TEST - Notification sent to $smsrecipient!" >> $LOGFILE
fi
# Clean up messagebody
rm $smtpbody >/dev/null 2>&1
fi
}
# -------------------------------------------------------------------------------------------------------------------------
# fontprinter takes current load numbers passed, and prints them in large 5x6char block fonts
fontprinter () {
# $1 is the electrical load passed for home, battery, grid and solar
# $2 is the inverse color being passed
if [ -z $battcapp3 ]; then battcapp3="???"; fi
# Fonts 5x6 blocks
Number1Line1="${CClear} $2 ${CClear} "
Number1Line2="${CClear} $2 ${CClear} "
Number1Line3="${CClear} $2 ${CClear} "
Number1Line4="${CClear} $2 ${CClear} "
Number1Line5="${CClear} $2 ${CClear} "
Number2Line1="${CClear} $2 ${CClear} "
Number2Line2="${CClear}$2 ${CClear} $2 ${CClear}"
Number2Line3="${CClear} $2 ${CClear} "
Number2Line4="${CClear} $2 ${CClear} "
Number2Line5="${CClear}$2 ${CClear}"
Number3Line1="${CClear}$2 ${CClear}"
Number3Line2="${CClear} $2 ${CClear}"
Number3Line3="${CClear} $2 ${CClear}"
Number3Line4="${CClear} $2 ${CClear}"
Number3Line5="${CClear}$2 ${CClear}"
Number4Line1="${CClear}$2 ${CClear} $2 ${CClear} "
Number4Line2="${CClear}$2 ${CClear} $2 ${CClear} "
Number4Line3="${CClear}$2 ${CClear}"
Number4Line4="${CClear} $2 ${CClear} "
Number4Line5="${CClear} $2 ${CClear} "
Number5Line1="${CClear}$2 ${CClear}"
Number5Line2="${CClear}$2 ${CClear} "
Number5Line3="${CClear}$2 ${CClear} "
Number5Line4="${CClear} $2 ${CClear}"
Number5Line5="${CClear}$2 ${CClear} "
Number6Line1="${CClear} $2 ${CClear} "
Number6Line2="${CClear} $2 ${CClear} "
Number6Line3="${CClear}$2 ${CClear} "
Number6Line4="${CClear}$2 ${CClear} $2 ${CClear}"
Number6Line5="${CClear} $2 ${CClear} "
Number7Line1="${CClear}$2 ${CClear}"
Number7Line2="${CClear} $2 ${CClear}"
Number7Line3="${CClear} $2 ${CClear} "
Number7Line4="${CClear} $2 ${CClear} "
Number7Line5="${CClear} $2 ${CClear} "
Number8Line1="${CClear} $2 ${CClear} "
Number8Line2="${CClear}$2 ${CClear} $2 ${CClear}"
Number8Line3="${CClear} $2 ${CClear} "
Number8Line4="${CClear}$2 ${CClear} $2 ${CClear}"
Number8Line5="${CClear} $2 ${CClear} "
Number9Line1="${CClear} $2 ${CClear} "
Number9Line2="${CClear}$2 ${CClear} $2 ${CClear}"
Number9Line3="${CClear} $2 ${CClear}"
Number9Line4="${CClear} $2 ${CClear} "
Number9Line5="${CClear} $2 ${CClear} "
Number0Line1="${CClear} $2 ${CClear} "
Number0Line2="${CClear}$2 ${CClear} $2 ${CClear}"
Number0Line3="${CClear}$2 ${CClear} $2 ${CClear}"
Number0Line4="${CClear}$2 ${CClear} $2 ${CClear}"
Number0Line5="${CClear} $2 ${CClear} "
PositiveLine2="${CClear} $2 ${CClear} "
PositiveLine3="${CClear} $2 ${CClear} "
PositiveLine4="${CClear} $2 ${CClear} "
NegativeLine2="${CClear} "
NegativeLine3="${CClear} $2 ${CClear} "
NegativeLine4="${CClear} "
DecimalLine5="${CClear} $2 ${CClear} "
SolarLine1="${CBlack}${InvYellow} SOLAR ${CClear}"
SolarLine2="${CBlack}${InvYellow} \|/____ ${CClear}"
SolarLine3="${CBlack}${InvYellow} --O///// ${CClear}"
SolarLine4="${CBlack}${InvYellow} ////// ${CClear}"
SolarLine5="${CBlack}${InvYellow} ////// ${CClear}"
GridLine1="${CWhite}${InvDkGray} GRID ${CClear}"
GridLine2="${CWhite}${InvDkGray} - | + ${CClear}"
GridLine3="${CWhite}${InvDkGray} ===||=== ${CClear}"
GridLine4="${CWhite}${InvDkGray} | ${CClear}"
GridLine5="${CWhite}${InvDkGray} HI VOLTS ${CClear}"
HomeLine1="${CBlack}${InvCyan} HOME ${CClear}"
HomeLine2="${CBlack}${InvCyan} /====\| ${CClear}"
HomeLine3="${CBlack}${InvCyan} /______\ ${CClear}"
HomeLine4="${CBlack}${InvCyan} | |[]| | ${CClear}"
HomeLine5="${CBlack}${InvCyan} ^^^^^^^^ ${CClear}"
BattLine1="${InvWhite}${CBlack} BATT ${InvDkGray}${CGreen}|${CClear}"
BattLine2="${InvWhite}${CBlack} $battcapp3%% ${InvDkGray}${CGreen}|${CClear}"
BattLine3="${InvWhite} ${InvDkGray}${CGreen}|${CClear}"
BattLine4="${InvWhite}${CRed} TESLA ${InvDkGray}${CGreen}|${CClear}"
BattLine5="${InvWhite} ${InvDkGray}${CGreen}|${CClear}"
GridOutLine1="${CWhite}${InvRed} GRID ${CClear}"
GridOutLine2="${CWhite}${InvRed} X || X ${CClear}"
GridOutLine3="${CWhite}${InvRed} == XX == ${CClear}"
GridOutLine4="${CWhite}${InvRed} X || X ${CClear}"
GridOutLine5="${CWhite}${InvRed} OUTAGE ${CClear}"
GridSyncLine1="${CWhite}${InvRed} GRID ${CClear}"
GridSyncLine2="${CWhite}${InvRed} > || > ${CClear}"
GridSyncLine3="${CWhite}${InvRed} == >> == ${CClear}"
GridSyncLine4="${CWhite}${InvRed} < || < ${CClear}"
GridSyncLine5="${CBlack}${InvYellow} >SYNCING ${CClear}"
posneg=$(echo $1 | cut -b 1 )
tens=$(echo $1 | cut -b 2 )
ones=$(echo $1 | cut -b 3 )
tenths=$(echo $1 | cut -b 5 )
if [ $posneg == "+" ]; then posneg="Positive"; else posneg="Negative"; fi
case $3 in
"Solar")
imageLine1=$SolarLine1
imageLine2=$SolarLine2
imageLine3=$SolarLine3
imageLine4=$SolarLine4
imageLine5=$SolarLine5
;;
"Grid")
if [ "$gridstatus" == "SystemIslandedActive" ]; then
imageLine1=$GridOutLine1
imageLine2=$GridOutLine2
imageLine3=$GridOutLine3
imageLine4=$GridOutLine4
imageLine5=$GridOutLine5
elif [ "$gridstatus" == "SystemTransitionToGrid" ]; then
imageLine1=$GridSyncLine1
imageLine2=$GridSyncLine2
imageLine3=$GridSyncLine3
imageLine4=$GridSyncLine4
imageLine5=$GridSyncLine5
else
imageLine1=$GridLine1
imageLine2=$GridLine2
imageLine3=$GridLine3
imageLine4=$GridLine4
imageLine5=$GridLine5
fi
;;
"Home")
imageLine1=$HomeLine1
imageLine2=$HomeLine2
imageLine3=$HomeLine3
imageLine4=$HomeLine4
imageLine5=$HomeLine5
;;
"Battery")
imageLine1=$BattLine1
imageLine2=$BattLine2
imageLine3=$BattLine3
imageLine4=$BattLine4
imageLine5=$BattLine5
;;
esac
case $posneg in
"Positive")
posnegLine2=$PositiveLine2
posnegLine3=$PositiveLine3
posnegLine4=$PositiveLine4
;;
"Negative")
posnegLine2=$NegativeLine2
posnegLine3=$NegativeLine3
posnegLine4=$NegativeLine4
;;
esac
case $tens in
1) #Tens Digit
tensLine1=$Number1Line1
tensLine2=$Number1Line2
tensLine3=$Number1Line3
tensLine4=$Number1Line4
tensLine5=$Number1Line5
;;
2)
tensLine1=$Number2Line1
tensLine2=$Number2Line2
tensLine3=$Number2Line3
tensLine4=$Number2Line4
tensLine5=$Number2Line5
;;
3)
tensLine1=$Number3Line1
tensLine2=$Number3Line2
tensLine3=$Number3Line3
tensLine4=$Number3Line4
tensLine5=$Number3Line5
;;
4)
tensLine1=$Number4Line1
tensLine2=$Number4Line2
tensLine3=$Number4Line3
tensLine4=$Number4Line4
tensLine5=$Number4Line5
;;
5)
tensLine1=$Number5Line1
tensLine2=$Number5Line2
tensLine3=$Number5Line3
tensLine4=$Number5Line4
tensLine5=$Number5Line5
;;
6)
tensLine1=$Number6Line1
tensLine2=$Number6Line2
tensLine3=$Number6Line3
tensLine4=$Number6Line4
tensLine5=$Number6Line5
;;
7)
tensLine1=$Number7Line1
tensLine2=$Number7Line2
tensLine3=$Number7Line3
tensLine4=$Number7Line4
tensLine5=$Number7Line5
;;
8)
tensLine1=$Number8Line1
tensLine2=$Number8Line2
tensLine3=$Number8Line3
tensLine4=$Number8Line4
tensLine5=$Number8Line5
;;
9)
tensLine1=$Number9Line1
tensLine2=$Number9Line2
tensLine3=$Number9Line3
tensLine4=$Number9Line4
tensLine5=$Number9Line5
;;
0)
tensLine1=$Number0Line1
tensLine2=$Number0Line2
tensLine3=$Number0Line3
tensLine4=$Number0Line4
tensLine5=$Number0Line5
;;
esac
case $ones in
1)
onesLine1=$Number1Line1
onesLine2=$Number1Line2
onesLine3=$Number1Line3
onesLine4=$Number1Line4
onesLine5=$Number1Line5
;;
2)
onesLine1=$Number2Line1
onesLine2=$Number2Line2
onesLine3=$Number2Line3
onesLine4=$Number2Line4
onesLine5=$Number2Line5
;;
3)
onesLine1=$Number3Line1
onesLine2=$Number3Line2
onesLine3=$Number3Line3
onesLine4=$Number3Line4
onesLine5=$Number3Line5
;;
4)
onesLine1=$Number4Line1
onesLine2=$Number4Line2
onesLine3=$Number4Line3
onesLine4=$Number4Line4
onesLine5=$Number4Line5
;;
5)
onesLine1=$Number5Line1
onesLine2=$Number5Line2
onesLine3=$Number5Line3
onesLine4=$Number5Line4
onesLine5=$Number5Line5
;;
6)
onesLine1=$Number6Line1
onesLine2=$Number6Line2
onesLine3=$Number6Line3
onesLine4=$Number6Line4
onesLine5=$Number6Line5
;;
7)
onesLine1=$Number7Line1
onesLine2=$Number7Line2
onesLine3=$Number7Line3
onesLine4=$Number7Line4
onesLine5=$Number7Line5
;;
8)
onesLine1=$Number8Line1
onesLine2=$Number8Line2
onesLine3=$Number8Line3
onesLine4=$Number8Line4
onesLine5=$Number8Line5
;;
9)
onesLine1=$Number9Line1
onesLine2=$Number9Line2
onesLine3=$Number9Line3
onesLine4=$Number9Line4
onesLine5=$Number9Line5
;;
0)
onesLine1=$Number0Line1
onesLine2=$Number0Line2
onesLine3=$Number0Line3
onesLine4=$Number0Line4
onesLine5=$Number0Line5
;;
esac
case $tenths in
1)
tenthsLine1=$Number1Line1
tenthsLine2=$Number1Line2
tenthsLine3=$Number1Line3
tenthsLine4=$Number1Line4
tenthsLine5=$Number1Line5
;;
2)
tenthsLine1=$Number2Line1
tenthsLine2=$Number2Line2
tenthsLine3=$Number2Line3
tenthsLine4=$Number2Line4
tenthsLine5=$Number2Line5
;;
3)
tenthsLine1=$Number3Line1
tenthsLine2=$Number3Line2
tenthsLine3=$Number3Line3
tenthsLine4=$Number3Line4
tenthsLine5=$Number3Line5
;;
4) #Tenths Digit
tenthsLine1=$Number4Line1
tenthsLine2=$Number4Line2
tenthsLine3=$Number4Line3
tenthsLine4=$Number4Line4
tenthsLine5=$Number4Line5
;;
5)
tenthsLine1=$Number5Line1
tenthsLine2=$Number5Line2
tenthsLine3=$Number5Line3
tenthsLine4=$Number5Line4
tenthsLine5=$Number5Line5
;;
6)
tenthsLine1=$Number6Line1
tenthsLine2=$Number6Line2
tenthsLine3=$Number6Line3
tenthsLine4=$Number6Line4
tenthsLine5=$Number6Line5
;;
7)
tenthsLine1=$Number7Line1
tenthsLine2=$Number7Line2
tenthsLine3=$Number7Line3
tenthsLine4=$Number7Line4
tenthsLine5=$Number7Line5
;;
8)
tenthsLine1=$Number8Line1
tenthsLine2=$Number8Line2
tenthsLine3=$Number8Line3
tenthsLine4=$Number8Line4
tenthsLine5=$Number8Line5
;;
9)
tenthsLine1=$Number9Line1
tenthsLine2=$Number9Line2
tenthsLine3=$Number9Line3
tenthsLine4=$Number9Line4
tenthsLine5=$Number9Line5
;;
0)
tenthsLine1=$Number0Line1
tenthsLine2=$Number0Line2
tenthsLine3=$Number0Line3
tenthsLine4=$Number0Line4
tenthsLine5=$Number0Line5
;;
esac
printf " ${imageLine1} ${tensLine1} ${onesLine1} ${tenthsLine1}\n"
printf " ${imageLine2} ${posnegLine2} ${tensLine2} ${onesLine2} ${tenthsLine2}\n"
printf " ${imageLine3} ${posnegLine3} ${tensLine3} ${onesLine3} ${tenthsLine3}\n"
printf " ${imageLine4} ${posnegLine4} ${tensLine4} ${onesLine4} ${tenthsLine4}\n"
printf " ${imageLine5} ${tensLine5} ${onesLine5}${DecimalLine5}${tenthsLine5} kW\n"
}
# -------------------------------------------------------------------------------------------------------------------------
# This function was "borrowed" graciously from @dave14305 from his FlexQoS script to determine the active WAN connection.
# Thanks much for your troubleshooting help as we tackled how to best derive the active WAN interface, Dave!
get_wan_setting() {
local varname varval
varname="${1}"
prefixes="wan0_ wan1_"
if [ "$($timeoutcmd$timeoutsec nvram get wans_mode)" = "lb" ] ; then
for prefix in $prefixes; do
state="$($timeoutcmd$timeoutsec nvram get "${prefix}"state_t)"
sbstate="$($timeoutcmd$timeoutsec nvram get "${prefix}"sbstate_t)"
auxstate="$($timeoutcmd$timeoutsec nvram get "${prefix}"auxstate_t)"
# is_wan_connect()
[ "${state}" = "2" ] || continue
[ "${sbstate}" = "0" ] || continue
[ "${auxstate}" = "0" ] || [ "${auxstate}" = "2" ] || continue
# get_wan_ifname()
proto="$($timeoutcmd$timeoutsec nvram get "${prefix}"proto)"
if [ "${proto}" = "pppoe" ] || [ "${proto}" = "pptp" ] || [ "${proto}" = "l2tp" ] ; then
varval="$($timeoutcmd$timeoutsec nvram get "${prefix}"pppoe_"${varname}")"
else
varval="$($timeoutcmd$timeoutsec nvram get "${prefix}""${varname}")"
fi
done
else
for prefix in $prefixes; do
primary="$($timeoutcmd$timeoutsec nvram get "${prefix}"primary)"
[ "${primary}" = "1" ] && break
done
proto="$($timeoutcmd$timeoutsec nvram get "${prefix}"proto)"
if [ "${proto}" = "pppoe" ] || [ "${proto}" = "pptp" ] || [ "${proto}" = "l2tp" ] ; then
varval="$($timeoutcmd$timeoutsec nvram get "${prefix}"pppoe_"${varname}")"
else
varval="$($timeoutcmd$timeoutsec nvram get "${prefix}""${varname}")"
fi
fi
printf "%s" "${varval}"
} # get_wan_setting
# -------------------------------------------------------------------------------------------------------------------------
# weathercheck is a function that downloads the latest weather for your WAN IP location
weathercheck () {
# Get the WAN interface in order to check for the public WAN IP address
WANIFNAME=$(get_wan_setting ifname)
WANIP=$(curl --silent --fail --interface $WANIFNAME --request GET --url https://ipv4.icanhazip.com)
WANCITY=$(curl --silent --retry 3 --request GET --url http://ip-api.com/json/$WANIP | jq --raw-output .city)
# Get the latitute/longitude of the public WAN IP address
WANlat=$(curl --silent --retry 3 --request GET --url http://ip-api.com/json/$WANIP | jq --raw-output .lat)
WANlon=$(curl --silent --retry 3 --request GET --url http://ip-api.com/json/$WANIP | jq --raw-output .lon)
# Get the Weather grid for the latitude/longitude
WANgridurl=$(curl --silent --retry 3 --request GET --url https://api.weather.gov/points/$WANlat,$WANlon | jq --raw-output .properties.forecast)
# Extract the weather JSON to a text file in order to query from it with JQ
curl --silent --retry 3 --request GET --url $WANgridurl | jq . --raw-output > $WANwxforecast
LINES=$(cat $WANwxforecast | wc -l) #Check to see how many lines are in this file
if [ $LINES -eq 0 ] #If there are no lines, error out
then
echo -e "\n${CRed} [Error: Unable to download weather data. Try again later...]\n${CClear}"
echo -e "$(date) - PWRMON ----------> ERROR: Unable to fetch weather data. May be a temporary issue. Try again later." >> $LOGFILE
sleep 3
exit 0
else
# Display the city, lat and long
#WANCITY="Your City"
#WANlat=32.3321
#WANlon=-64.7660
clear
logo
if [ "$UpdateNotify" != "0" ]; then
echo -e "${CRed} $UpdateNotify${CClear}"
echo -e "${CGreen} ________${CClear}"
else
echo -e "${CGreen} ________${CClear}"
fi
echo -e "${CGreen}/${CRed}Location${CClear}${CGreen}\_________________________________________________________${CClear}"
echo ""
echo -e "${InvGreen} ${CClear}${CGreen} City: ${CCyan}$WANCITY ${CGreen}-- Latitude: ${CCyan}$WANlat ${CGreen}-- Longitude: ${CCyan}$WANlon"
echo -e "${CGreen} ________${CClear}"
echo -e "${CGreen}/${CRed}Forecast${CClear}${CGreen}\_________________________________________________________${CClear}"
echo ""
# Loop through the forecasts and display them
i=0
while [ $i -ne 6 ]
do
WANwxName=$(cat $WANwxforecast | jq -r '.properties.periods['$i'].name | select( . != null )')
WANwxTemp=$(cat $WANwxforecast | jq -r '.properties.periods['$i'].temperature | select( . != null )')
WANwxTempUnit=$(cat $WANwxforecast | jq -r '.properties.periods['$i'].temperatureUnit | select( . != null )')
WANwxWind=$(cat $WANwxforecast | jq -r '.properties.periods['$i'].windSpeed | select( . != null )')
WANwxWindDir=$(cat $WANwxforecast | jq -r '.properties.periods['$i'].windDirection | select( . != null )')
WANwxShort=$(cat $WANwxforecast | jq -r '.properties.periods['$i'].shortForecast | select( . != null )')
WANwxDetail=$(cat $WANwxforecast | jq -r '.properties.periods['$i'].detailedForecast | select( . != null )')
WANwxShortTrim=$(echo $WANwxShort | sed -e 's/.\{50\} /&\n/g')
WANwxDetailTrim=$(echo $WANwxDetail | sed -e 's/.\{50\} /&\n/g')
echo -e "${InvGreen} ${CClear}${CGreen} Day: ${CCyan}$WANwxName ${CGreen}-- Temp: ${CCyan}$WANwxTemp$WANwxTempUnit ${CGreen}-- Wind: ${CCyan}$WANwxWind from $WANwxWindDir"
echo -e "${InvGreen} ${CClear}${CGreen} Conditions: ${CCyan}$WANwxShortTrim"
echo ""
i=$(($i+1))
done
echo -en "${CRed}(M)${CGreen}ore Detail, ${CRed}(C)${CGreen}lose?"
while true; do
read -p ": " -n 1 -r WxPrompt
case $WxPrompt in
[Mm]) weathercheckext; exit 0;;
[Cc]) echo -e "${CGreen}\r [Returning to the Main UI momentarily] "; exit 0;;
"" ) echo -e "\nValid Choices [Mm / Cc]";;
* ) echo -e "\nValid Choices [Mm / Cc]";;
esac
done
fi
}
# -------------------------------------------------------------------------------------------------------------------------
# weathercheckext is a function that displays the latest extended weather forecast for your WAN IP location
weathercheckext () {
clear
logo
if [ "$UpdateNotify" != "0" ]; then
echo -e "${CRed} $UpdateNotify${CClear}"
echo -e "${CGreen} _________________${CClear}"
else
echo -e "${CGreen} _________________${CClear}"
fi
echo -e "${CGreen}/${CRed}Extended Forecast${CClear}${CGreen}\________________________________________________${CClear}"
echo ""
# Loop through the forecasts and display them
i=0
while [ $i -ne 6 ]
do
WANwxName=$(cat $WANwxforecast | jq -r '.properties.periods['$i'].name | select( . != null )')
WANwxDetail=$(cat $WANwxforecast | jq -r '.properties.periods['$i'].detailedForecast | select( . != null )')
WANwxDetailTrim=$(echo $WANwxDetail | sed -e 's/.\{57\} /&\n/g')
echo -e "${InvGreen} ${CClear}${CGreen} Day: ${CCyan}$WANwxName"
echo -e "${CCyan}$WANwxDetailTrim"
echo ""
i=$(($i+1))
done
echo -en "${CRed}(C)${CGreen}lose?"
while true; do
read -p ": " -n 1 -r WxPromptExt
case $WxPromptExt in
[Cc]) weathercheck; exit 0;;
"" ) echo -e "\nValid Choices [Cc]";;
* ) echo -e "\nValid Choices [Cc]";;
esac
done
}
# -------------------------------------------------------------------------------------------------------------------------
# updatecheck is a function that downloads the latest update version file, and compares it with what's currently installed
updatecheck () {
# Download the latest version file from the source repository
curl --silent --retry 3 "https://raw.githubusercontent.com/ViktorJp/PWRMON/master/version.txt" -o "/jffs/addons/pwrmon.d/version.txt"
if [ -f $DLVERPATH ]
then
# Read in its contents for the current version file
DLVersion=$(cat $DLVERPATH)