-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfronius-api.c
1351 lines (1106 loc) · 39.2 KB
/
fronius-api.c
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
/**
* !!! Warning !!! deprecated unmaintained and untested code, use fronius-modbus.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <ansi-color-codes.h>
#include "fronius-config.h"
#include "fronius.h"
#include "tasmota.h"
#include "frozen.h"
#include "mosmix.h"
#include "utils.h"
#include "curl.h"
#include "mcp.h"
#define URL_READABLE "http://fronius/components/readable"
#define URL_METER "http://fronius/solar_api/v1/GetMeterRealtimeData.cgi?Scope=Device&DeviceId=0"
#define URL_FLOW10 "http://fronius10/solar_api/v1/GetPowerFlowRealtimeData.fcgi"
#define URL_FLOW7 "http://fronius7/solar_api/v1/GetPowerFlowRealtimeData.fcgi"
#define COUNTER_HISTORY 30 // days
#define PSTATE_HISTORY 32 // samples
#define MIN_SOC 70
#define AKKU_CAPACITY 11059
#define AKKU_CAPACITY_SOC(soc) (AKKU_CAPACITY * soc / 1000)
#define EMERGENCY (AKKU_CAPACITY / 10)
#define WAIT_OFFLINE 900
#define WAIT_STANDBY 300
#define WAIT_STABLE 60
#define WAIT_INSTABLE 20
#define WAIT_NEXT 5
#define WAIT_AKKU_RAMP 10
#define WAIT_RAMP 3
#define WAIT_RESPONSE 3 // TODO reicht manchmal nicht?
#define DD (*dd)
#define JMC " SMARTMETER_ENERGYACTIVE_CONSUMED_SUM_F64:%f "
#define JMP " SMARTMETER_ENERGYACTIVE_PRODUCED_SUM_F64:%f "
#define JMV1 " SMARTMETER_VOLTAGE_MEAN_01_F64:%f "
#define JMV2 " SMARTMETER_VOLTAGE_MEAN_02_F64:%f "
#define JMV3 " SMARTMETER_VOLTAGE_MEAN_03_F64:%f "
#define JMF " SMARTMETER_FREQUENCY_MEAN_F64:%f "
#define JBSOC " BAT_VALUE_STATE_OF_CHARGE_RELATIVE_U16:%f "
#define JIE1 " PV_ENERGYACTIVE_ACTIVE_SUM_01_U64:%f "
#define JIE2 " PV_ENERGYACTIVE_ACTIVE_SUM_02_U64:%f "
#define JIP " PV_POWERACTIVE_SUM_F64:%f "
#define JMMPP " PowerReal_P_Sum:%f "
#define JMMC " EnergyReal_WAC_Sum_Consumed:%f "
#define JMMP " EnergyReal_WAC_Sum_Produced:%f "
#define MOSMIX3X24 "FRONIUS mosmix Rad1h/SunD1/RSunD today %d/%d/%d tomorrow %d/%d/%d tomorrow+1 %d/%d/%d"
typedef struct _raw raw_t;
struct _raw {
float akku;
float grid;
float load;
float pv10;
float pv10_total1;
float pv10_total2;
float pv7;
float pv7_total;
float soc;
float produced;
float consumed;
float p;
float v1;
float v2;
float v3;
float f;
};
// program of the day - choose by mosmix forecast data
static potd_t *potd = 0;
// counter history every hour over one day and access pointers
static counter_t counter_hours[24];
static volatile counter_t *counter = 0;
#define COUNTER_NOW (&counter_hours[now->tm_hour])
#define COUNTER_LAST (&counter_hours[now->tm_hour > 00 ? now->tm_hour - 1 : 23])
#define COUNTER_NEXT (&counter_hours[now->tm_hour < 23 ? now->tm_hour + 1 : 00])
// 24h slots over one week and access pointers
static gstate_t gstate_hours[24 * 7];
static volatile gstate_t *gstate = 0;
#define GSTATE_NOW (&gstate_hours[24 * now->tm_wday + now->tm_hour])
#define GSTATE_LAST (&gstate_hours[24 * now->tm_wday - (now->tm_hour > 0 ? now->tm_hour - 1 : 23)])
#define GSTATE_NEXT (&gstate_hours[now->tm_hour < 23 ? now->tm_hour + 1 : 00])
#define GSTATE_HOUR(h) (&gstate_hours[24 * now->tm_wday + h])
#define GSTATE_TODAY GSTATE_HOUR(0)
// round robin power power flow and state calculations
static pstate_t pstate_history[PSTATE_HISTORY], *pstate = &pstate_history[0];
static int pstate_history_ptr = 0;
static struct tm *lt, now_tm, *now = &now_tm;
static int sock = 0;
// reading Inverter API: CURL handles, response memory, raw date, error counter
static CURL *curl10, *curl7, *curl_readable;
static response_t memory = { 0 };
static raw_t raw, *r = &raw;
static int errors;
static device_t* get_by_name(const char *name) {
for (device_t **dd = DEVICES; *dd; dd++)
if (!strcmp(DD->name, name))
return DD;
return 0;
}
static int parse_fronius10(response_t *resp) {
char *c;
int ret;
ret = json_scanf(resp->buffer, resp->size, "{ Body { Data { Site { P_Akku:%f P_Grid:%f P_Load:%f P_PV:%f } } } }", &r->akku, &r->grid, &r->load, &r->pv10);
if (ret != 4)
xlog("FRONIUS parse_fronius10() warning! parsing Body->Data->Site: expected 4 values but got %d", ret);
// workaround parsing { "Inverters" : { "1" : { ... } } }
ret = json_scanf(resp->buffer, resp->size, "{ Body { Data { Inverters:%Q } } }", &c);
if (ret == 1 && c != NULL) {
char *p = c;
while (*p != '{')
p++;
p++;
while (*p != '{')
p++;
ret = json_scanf(p, strlen(p) - 1, "{ SOC:%f }", &r->soc);
if (ret != 1)
xlog("FRONIUS parse_fronius10() warning! parsing Body->Data->Inverters->SOC: no result");
free(c);
} else
xlog("FRONIUS parse_fronius10() warning! parsing Body->Data->Inverters: no result");
return 0;
}
static int parse_fronius7(response_t *resp) {
int ret = json_scanf(resp->buffer, resp->size, "{ Body { Data { Site { P_PV:%f E_Total:%f } } } }", &r->pv7, &r->pv7_total);
if (ret != 2)
return xerr("FRONIUS parse_fronius7() warning! parsing Body->Data->Site: expected 2 values but got %d", ret);
return 0;
}
//static int parse_meter(response_t *resp) {
// int ret = json_scanf(resp->buffer, resp->size, "{ Body { Data { "JMMPP JMMC JMMP" } } }", &r->p, &r->consumed, &r->produced);
// if (ret != 3)
// return xerr("FRONIUS parse_meter() warning! parsing Body->Data: expected 3 values but got %d", ret);
//
// return 0;
//}
static int parse_readable(response_t *resp) {
int ret;
char *p;
// workaround for accessing inverter number as key: "262144" : {
p = strstr(resp->buffer, "\"262144\"") + 8 + 2;
ret = json_scanf(p, strlen(p), "{ channels { "JIP" } }", &r->pv10);
if (ret != 1)
return xerr("FRONIUS parse_readable() warning! parsing 262144: expected 1 values but got %d", ret);
// workaround for accessing inverter number as key: "393216" : {
p = strstr(resp->buffer, "\"393216\"") + 8 + 2;
ret = json_scanf(p, strlen(p), "{ channels { "JIE1 JIE2" } }", &r->pv10_total1, &r->pv10_total2);
if (ret != 2)
return xerr("FRONIUS parse_readable() warning! parsing 393216: expected 2 values but got %d", ret);
// workaround for accessing akku number as key: "16580608" : {
p = strstr(resp->buffer, "\"16580608\"") + 10 + 2;
ret = json_scanf(p, strlen(p), "{ channels { "JBSOC" } }", &r->soc);
if (ret != 1)
return xerr("FRONIUS parse_readable() warning! parsing 16580608: expected 1 values but got %d", ret);
// workaround for accessing smartmeter number as key: "16252928" : {
p = strstr(resp->buffer, "\"16252928\"") + 10 + 2;
ret = json_scanf(p, strlen(p), "{ channels { "JMC JMP JMV1 JMV2 JMV3 JMF" } }", &r->consumed, &r->produced, &r->v1, &r->v2, &r->v3, &r->f);
if (ret != 6)
return xerr("FRONIUS parse_readable() warning! parsing 16252928: expected 6 values but got %d", ret);
return 0;
}
static pstate_t* get_pstate_history(int offset) {
int i = pstate_history_ptr + offset;
if (i < 0)
i += PSTATE_HISTORY;
if (i >= PSTATE_HISTORY)
i -= PSTATE_HISTORY;
return &pstate_history[i];
}
static void bump_pstate() {
// calculate new pstate pointer
if (++pstate_history_ptr == PSTATE_HISTORY)
pstate_history_ptr = 0;
pstate_t *pstate_new = &pstate_history[pstate_history_ptr];
// take over all values
memcpy(pstate_new, (void*) pstate, sizeof(pstate_t));
pstate = pstate_new; // atomic update current pstate pointer
}
// sum up load for darkness hours - take akku discharge values from yesterday
static int collect_load(int from, int hours) {
int load = 0;
char line[LINEBUF], value[25];
strcpy(line, "FRONIUS mosmix load");
for (int i = from; i < hours; i++) {
int hour = from + i;
if (hour >= 24)
hour -= 24;
int hload = gstate_hours[hour].dakku;
load += hload;
snprintf(value, 25, " %d:%d", hour, hload);
strcat(line, value);
}
xdebug(line);
// adding +10% Dissipation / Reserve
load += load / 10;
return load;
}
static int collect_heating_total() {
int total = 0;
for (device_t **dd = DEVICES; *dd; dd++)
if (!DD->adj)
total += DD->total;
return total;
}
static int check_override(device_t *d, int power) {
if (d->override) {
time_t t = time(NULL);
if (t > d->override) {
xdebug("FRONIUS Override expired for %s", d->name);
d->override = 0;
power = 0;
} else {
xdebug("FRONIUS Override active for %lu seconds on %s", d->override - t, d->name);
power = d->adj ? 100 : 1;
}
}
return power;
}
static void print_gstate(const char *message) {
char line[512]; // 256 is not enough due to color escape sequences!!!
xlogl_start(line, "FRONIUS");
xlogl_int_b(line, "PV", gstate->pv);
xlogl_int_b(line, "PV10", gstate->mppt1 + gstate->mppt2);
xlogl_int_b(line, "PV7", gstate->mppt3 + gstate->mppt4);
xlogl_int(line, 1, 0, "↑Grid", gstate->produced);
xlogl_int(line, 1, 1, "↓Grid", gstate->consumed);
xlogl_int(line, 0, 0, "Today", gstate->today);
xlogl_int(line, 0, 0, "Tomo", gstate->tomorrow);
xlogl_int(line, 0, 0, "Exp", gstate->expected);
xlogl_float(line, 0, 0, "SoC", FLOAT10(gstate->soc));
xlogl_int(line, 0, 0, "Akku", gstate->akku);
xlogl_float(line, 0, 0, "TTL", FLOAT60(gstate->ttl));
xlogl_float(line, 1, gstate->survive < 10, "Survive", FLOAT10(gstate->survive));
xlogl_float(line, 1, gstate->heating < 10, "Heating", FLOAT10(gstate->heating));
strcat(line, " potd:");
strcat(line, potd ? potd->name : "NULL");
xlogl_end(line, strlen(line), message);
}
static void print_state(device_t *d) {
char line[512], value[16]; // 256 is not enough due to color escape sequences!!!
xlogl_start(line, "FRONIUS");
for (device_t **dd = potd->devices; *dd; dd++) {
if (DD->adj)
snprintf(value, 5, " %3d", DD->power);
else
snprintf(value, 5, " %c", DD->power ? 'X' : '_');
strcat(line, value);
}
strcat(line, " st ");
for (device_t **dd = potd->devices; *dd; dd++) {
snprintf(value, 5, "%d", DD->state);
strcat(line, value);
}
strcat(line, " nr ");
for (device_t **dd = potd->devices; *dd; dd++) {
snprintf(value, 5, "%d", DD->noresponse);
strcat(line, value);
}
xlogl_bits16(line, " Flags", pstate->flags);
xlogl_int_b(line, "PV10", pstate->mppt1 + pstate->mppt2);
xlogl_int_b(line, "PV7", pstate->mppt3 + pstate->mppt4);
xlogl_int(line, 1, 1, "Grid", pstate->grid);
xlogl_int(line, 1, 1, "Akku", pstate->akku);
xlogl_int(line, 1, 0, "Ramp", pstate->ramp);
xlogl_int(line, 0, 0, "Load", pstate->load);
xlogl_float(line, 0, 0, "SoC", FLOAT10(pstate->soc));
xlogl_end(line, strlen(line), 0);
}
// call device specific ramp function
static int ramp_device(device_t *d, int power) {
if (power < 0)
xdebug("FRONIUS ramp↓ %d %s", power, d->name);
else if (power > 0)
xdebug("FRONIUS ramp↑ +%d %s", power, d->name);
else
xdebug("FRONIUS ramp 0 %s", d->name);
return (d->ramp)(d, power);
}
static int select_program(const potd_t *p) {
if (potd == p)
return 0;
// potd has changed - reset all devices and wait for new values
for (device_t **dd = DEVICES; *dd; dd++)
ramp_device(DD, DOWN);
xlog("FRONIUS selecting %s program of the day", p->name);
potd = (potd_t*) p;
return 0;
}
// choose program of the day
static int choose_program() {
if (!gstate)
return select_program(&MODEST);
// we will NOT survive - charging akku has priority
if (gstate->survive < 10)
return select_program(&MODEST);
// tomorrow not enough pv - charging akku has priority
if (gstate->tomorrow < AKKU_CAPACITY)
return select_program(&MODEST);
// tomorrow more pv than today - charge akku tommorrow
if (gstate->tomorrow > gstate->today)
return select_program(&GREEDY);
// enough pv available for survive + heating
if (gstate->heating > 10)
return select_program(&PLENTY);
return select_program(&MODEST);
}
static device_t* rampup(int power) {
for (device_t **dd = potd->devices; *dd; dd++)
if (ramp_device(DD, power))
return DD;
return 0;
}
static device_t* rampdown(int power) {
// jump to last entry
device_t **dd = potd->devices;
while (*dd)
dd++;
// now go backward - this gives reverse order
while (dd-- != potd->devices)
if (ramp_device(DD, power))
return DD;
return 0;
}
static device_t* ramp() {
device_t *d;
// prio1: ramp down in reverse order
if (pstate->ramp < 0) {
d = rampdown(pstate->ramp);
if (d)
return d;
}
// prio2: ramp up in order when stable and ramp indicated
if (pstate->ramp > 0 && PSTATE_VALID && (PSTATE_STABLE || pstate->ramp > 1000)) {
d = rampup(pstate->ramp);
if (d)
return d;
}
return 0;
}
static int steal_thief_victim(device_t *t, device_t *v) {
// thief not active or in standby
if (t->state == Disabled || t->state == Standby)
return 0;
// thief already (full) on
if (t->power == (t->adj ? 100 : 1))
return 0;
// we can steal akkus charge charge power or victims load
int steal = 0;
if (v == &a1)
steal = pstate->akku < -100 ? pstate->akku * -0.9 : 0;
else
steal = v->load;
// nothing to steal
if (!steal)
return 0;
// not enough to steal
int min = t->adj ? t->total / 100 : t->total; // adjustable: 1% of total, dumb: total
int power = pstate->ramp + steal;
if (power < min)
return 0;
xdebug("FRONIUS steal %d from %s and provide it to %s with a load of %d min=%d", steal, v->name, t->name, t->total, min);
// ramp down victim, akku ramps down itself
if (v != &a1)
ramp_device(v, v->load * -1);
ramp_device(t, power);
// give akku time to adjust
if (v == &a1)
t->timer = WAIT_AKKU_RAMP;
// no response expected as we put power from one to another device
t->xload = 0;
return 1;
}
static device_t* steal() {
// check flags
if (!PSTATE_STABLE || !PSTATE_VALID || PSTATE_DISTORTION)
return 0;
// jump to end
device_t **tail = potd->devices;
while (*tail)
tail++;
tail--;
// thief goes forward, victim backward till it reaches thief
for (device_t **tt = potd->devices; *tt != 0; tt++)
for (device_t **vv = tail; vv != tt; vv--)
if (steal_thief_victim(*tt, *vv))
return *tt;
return 0;
}
static device_t* perform_standby(device_t *d) {
int power = d->adj ? (d->power < 50 ? +500 : -500) : (d->power ? d->total * -1 : d->total);
xdebug("FRONIUS starting standby check on %s with power=%d", d->name, power);
d->state = Standby_Check;
ramp_device(d, power);
return d;
}
static int force_standby() {
if (SUMMER)
return 1; // summer mode -> off
// force heating independently from temperature
if ((now->tm_mon == 4 || now->tm_mon == 8) && now->tm_hour >= 16) // may/sept begin 16 o'clock
return 0;
else if ((now->tm_mon == 3 || now->tm_mon == 9) && now->tm_hour >= 14) // apr/oct begin 14 o'clock
return 0;
return TEMP_IN > 25; // too hot for heating
}
static device_t* standby() {
// put dumb devices into standby if summer or too hot
if (force_standby()) {
xdebug("FRONIUS month=%d out=%.1f in=%.1f --> forcing standby", now->tm_mon, TEMP_OUT, TEMP_IN);
for (device_t **dd = DEVICES; *dd; dd++) {
if (!DD->adj && DD->state == Active) {
ramp_device(DD, DOWN);
DD->state = Standby;
}
}
}
// check flags
if (!PSTATE_CHECK_STANDBY || !PSTATE_STABLE || !PSTATE_VALID || PSTATE_DISTORTION || pstate->pv < BASELOAD * 2)
return 0;
// try first active powered adjustable device with noresponse counter > 0
for (device_t **dd = DEVICES; *dd; dd++)
if (DD->state == Active && DD->power && DD->adj && DD->noresponse > 0)
return perform_standby(DD);
// try first active powered device with noresponse counter > 0
for (device_t **dd = DEVICES; *dd; dd++)
if (DD->state == Active && DD->power && DD->noresponse > 0)
return perform_standby(DD);
// try first active powered adjustable device
for (device_t **dd = DEVICES; *dd; dd++)
if (DD->state == Active && DD->power && DD->adj)
return perform_standby(DD);
// try first active powered device
for (device_t **dd = DEVICES; *dd; dd++)
if (DD->state == Active && DD->power)
return perform_standby(DD);
return 0;
}
static device_t* response(device_t *d) {
// no expected delta load - no response to check
if (!d->xload)
return 0;
int delta = pstate->load - d->aload;
int expected = d->xload;
d->xload = 0; // reset
// ignore responses below NOISE
if (abs(expected) < NOISE) {
xdebug("FRONIUS ignoring expected response below NOISE %d from %s", expected, d->name);
d->state = Active;
return 0;
}
// valid response is at least 1/3 of expected
int response = expected > 0 ? (delta > expected / 3) : (delta < expected / 3);
// response OK
if (d->state == Active && response) {
xdebug("FRONIUS response OK from %s, delta load expected %d actual %d", d->name, expected, delta);
d->noresponse = 0;
d->timer = 0;
if (pstate->soc < 1000)
sleep(WAIT_AKKU_RAMP); // delay next round to give akku time to adjust
return 0;
}
// standby check was negative - we got a response
if (d->state == Standby_Check && response) {
xdebug("FRONIUS standby check negative for %s, delta load expected %d actual %d", d->name, expected, delta);
d->noresponse = 0;
d->state = Active;
d->timer = 0;
return d;
}
// standby check was positive -> set device into standby
if (d->state == Standby_Check && !response) {
xdebug("FRONIUS standby check positive for %s, delta load expected %d actual %d --> entering standby", d->name, expected, delta);
ramp_device(d, d->total * -1);
d->noresponse = 0;
d->state = Standby;
d->xload = 0; // no response from switch off expected
return d; // recalculate in next round
}
// ignore standby check when power was released
if (expected > 0) {
if (pstate->soc < 1000)
sleep(WAIT_AKKU_RAMP); // delay next round to give akku time to adjust
return 0;
}
// perform standby check when noresponse counter reaches threshold
if (++d->noresponse >= STANDBY_NORESPONSE)
return perform_standby(d);
xdebug("FRONIUS no response from %s count %d/%d", d->name, d->noresponse, STANDBY_NORESPONSE);
return 0;
}
static void calculate_mosmix() {
// update forecasts
if (mosmix_load(MARIENBERG))
return;
// mosmix 24h raw values forecasts today, tomorrow and tomorrow+1
mosmix_csv_t m0, m1, m2;
mosmix_24h(0, &m0);
mosmix_24h(1, &m1);
mosmix_24h(2, &m2);
xdebug(MOSMIX3X24, m0.Rad1h, m0.SunD1, m0.RSunD, m1.Rad1h, m1.SunD1, m1.RSunD, m2.Rad1h, m2.SunD1, m2.RSunD);
// update produced energy this hour and recalculate mosmix factors for each mppt
mosmix_mppt(now, gstate->mppt1, gstate->mppt2, gstate->mppt3, gstate->mppt4);
// collect total expected today, tomorrow and till end of day / start of day
int today, tomorrow, sod, eod;
mosmix_collect(now, &today, &tomorrow, &sod, &eod);
gstate->today = today;
gstate->tomorrow = tomorrow;
gstate->expected = eod;
// calculate survival factor
int hours, from, to;
mosmix_survive(now, BASELOAD / 2, &hours, &from, &to);
int available = gstate->expected + gstate->akku;
int needed = collect_load(from, hours);
float survive = needed ? (float) available / (float) needed : 0.0;
gstate->survive = survive * 10; // store as x10 scaled
xdebug("FRONIUS survive needed=%d available=%d (%d expected + %d akku) --> %.2f", needed, available, gstate->expected, gstate->akku, survive);
// calculate heating factor
int heating_total = collect_heating_total();
mosmix_heating(now, heating_total, &hours, &from, &to);
int needed_heating = heating_total * hours;
int remaining = gstate->expected - survive;
float heating = needed_heating && remaining > 0 ? (float) (remaining) / (float) needed_heating : 0.0;
// float heating = needed ? (float) available / (float) needed : 0.0;
gstate->heating = heating * 10; // store as x10 scaled
xdebug("FRONIUS heating needed=%d expected=%d --> %.2f", needed_heating, gstate->expected, heating);
// actual vs. yesterdays expected ratio
int actual = 0;
for (int i = 0; i <= now->tm_hour; i++)
actual += GSTATE_HOUR(i)->pv;
int yesterdays_tomorrow = GSTATE_HOUR(23)->tomorrow;
float error = yesterdays_tomorrow ? (float) actual / (float) yesterdays_tomorrow : 0;
xdebug("FRONIUS yesterdays forecast for today %d, actual %d, error %.2f", yesterdays_tomorrow, actual, error);
// dump todays history
mosmix_dump_history_today(now);
}
static void calculate_gstate() {
// take over raw values
gstate->soc = r->soc * 10.0; // store value as promille 0/00
counter->mppt1 = (r->pv10_total1 / 3600) + (r->pv10_total2 / 3600); // counters are in Ws!
counter->produced = r->produced;
counter->consumed = r->consumed;
if (r->pv7_total > 0.0)
counter->mppt3 = r->pv7_total; // don't take over zero as Fronius7 might be in sleep mode
// get previous values to calculate deltas
gstate_t *g = GSTATE_LAST;
counter_t *c = COUNTER_LAST;
gstate->produced = counter->produced && c->produced ? counter->produced - c->produced : 0;
gstate->consumed = counter->consumed && c->consumed ? counter->consumed - c->consumed : 0;
gstate->mppt1 = counter->mppt1 && c->mppt1 ? counter->mppt1 - c->mppt1 : 0;
gstate->mppt2 = counter->mppt2 && c->mppt2 ? counter->mppt2 - c->mppt2 : 0;
gstate->mppt3 = counter->mppt3 && c->mppt3 ? counter->mppt3 - c->mppt3 : 0;
gstate->mppt4 = counter->mppt4 && c->mppt4 ? counter->mppt4 - c->mppt4 : 0;
gstate->pv = gstate->mppt1 + gstate->mppt2 + gstate->mppt3 + gstate->mppt4;
// calculate akku energy and delta (+)charge (-)discharge when soc between 10-90% and estimate time to live when discharging
int range_ok = gstate->soc > 100 && gstate->soc < 900 && g->soc > 100 && g->soc < 900;
gstate->akku = gstate->soc > MIN_SOC ? AKKU_CAPACITY_SOC(gstate->soc - MIN_SOC) : 0;
gstate->dakku = range_ok ? AKKU_CAPACITY_SOC(gstate->soc - g->soc) : 0;
if (gstate->dakku < 0)
gstate->ttl = gstate->akku * 60 / gstate->dakku * -1; // in discharge phase - use current discharge rate (minutes)
else if (gstate->soc > MIN_SOC)
gstate->ttl = gstate->akku * 60 / BASELOAD; // not yet in discharge phase - use BASELOAD (minutes)
else
gstate->ttl = 0;
}
// shape pstate values below NOISE
static void shape_pstate() {
if (abs(pstate->dgrid) < NOISE)
pstate->dgrid = 0;
if (abs(pstate->dload) < NOISE)
pstate->dload = 0;
if (abs(pstate->akku) < NOISE)
pstate->akku = 0;
// if (abs(pstate->grid) < NOISE)
// pstate->grid = 0;
}
static void calculate_pstate1() {
// clear all flags
pstate->flags = 0;
// take over raw values from Fronius10
pstate->akku = r->akku;
pstate->grid = r->grid;
pstate->load = r->load;
pstate->mppt1 = r->pv10;
pstate->soc = r->soc * 10.0;
// get 2x history back
pstate_t *h1 = get_pstate_history(-1);
pstate_t *h2 = get_pstate_history(-2);
// offline mode when 3x not enough PV production
if (pstate->mppt1 < NOISE && h1->mppt1 < NOISE && h2->mppt1 < NOISE) {
int burnout_time = !SUMMER && (now->tm_hour == 6 || now->tm_hour == 7 || now->tm_hour == 8);
int burnout_possible = TEMP_IN < 20 && pstate->soc > 150;
if (burnout_time && burnout_possible && AKKU_BURNOUT)
pstate->flags |= FLAG_BURNOUT; // akku burnout between 6 and 9 o'clock when possible
else
pstate->flags |= FLAG_OFFLINE; // offline
pstate->ramp = pstate->xload = pstate->dxload = pstate->pv = pstate->dpv = pstate->mppt1 = pstate->mppt2 = pstate->mppt3 = pstate->mppt4 = 0;
shape_pstate();
return;
}
// emergency shutdown when three times extreme akku discharge or grid download
if (pstate->akku > EMERGENCY && h1->akku > EMERGENCY && h2->akku > EMERGENCY && pstate->grid > EMERGENCY && h1->grid > EMERGENCY && h2->grid > EMERGENCY) {
pstate->flags |= FLAG_EMERGENCY;
return;
}
pstate->flags |= FLAG_VALID;
}
static void calculate_pstate2() {
// take over raw values from Fronius7
pstate->mppt3 = r->pv7;
// clear VALID flag
pstate->flags &= ~FLAG_VALID;
// get 2x history back
pstate_t *h1 = get_pstate_history(-1);
pstate_t *h2 = get_pstate_history(-2);
// total pv from both inverters
pstate->pv = pstate->mppt1 + pstate->mppt2 + pstate->mppt3 + pstate->mppt4;
pstate->dpv = pstate->pv - h1->pv;
// subtract PV produced by Fronius7
pstate->load -= (pstate->mppt3 + pstate->mppt4);
// calculate delta load
pstate->dload = pstate->load - h1->load;
if (abs(pstate->dload) < NOISE)
pstate->dload = 0;
// calculate load manually
// pstate->cload = (pstate->pv + pstate->akku + pstate->grid) * -1;
// calculate ramp up/down power
pstate->ramp = (pstate->grid + pstate->akku) * -1;
if (-RAMP_WINDOW < pstate->grid && pstate->grid < 0)
pstate->ramp = 0; // stable window between 0..25
if (0 < pstate->grid && pstate->grid < RAMP_WINDOW)
pstate->ramp = -RAMP_WINDOW; // ramp down as soon as we are consuming grid
if (pstate->akku < -NOISE && -RAMP_WINDOW < pstate->grid && pstate->grid < RAMP_WINDOW)
pstate->ramp = 0; // akku is regulating around 0 so set stable window between -25..+25
// state is stable when we have three times no grid changes
if (!pstate->dgrid && !h1->dgrid && !h2->dgrid)
pstate->flags |= FLAG_STABLE;
// distortion when delta pv is too big
int dpv_sum = 0;
for (int i = 0; i < PSTATE_HISTORY; i++)
dpv_sum += abs(pstate_history[i].dpv);
if (dpv_sum > 1000)
pstate->flags |= FLAG_DISTORTION;
if (PSTATE_DISTORTION)
xdebug("FRONIUS distortion=%d sum=%d", PSTATE_DISTORTION, dpv_sum);
// device loop:
// - expected load
// - active devices
// - all devices in standby
pstate->flags |= FLAG_ALL_STANDBY;
pstate->xload = BASELOAD;
for (device_t **dd = DEVICES; *dd; dd++) {
pstate->xload += DD->load;
if (DD->power > 0 && DD != &a1) // excl. akku; -1 when unitialized!
pstate->flags |= FLAG_ACTIVE;
if (DD->state != Standby)
pstate->flags &= ~FLAG_ALL_STANDBY;
}
pstate->xload *= -1;
// indicate standby check when deviation between actual load and calculated load is three times above 33%
pstate->dxload = pstate->load < -BASELOAD ? (pstate->xload - pstate->load) * 100 / pstate->xload : 0;
if (pstate->dxload > 33 && h1->dxload > 33 && h2->dxload > 33)
pstate->flags |= FLAG_CHECK_STANDBY;
if (PSTATE_CHECK_STANDBY)
xdebug("FRONIUS set FLAG_CHECK_STANDBY load=%d xload=%d dxload=%d", pstate->load, pstate->xload, pstate->dxload);
// clear flag when values not valid
pstate->flags |= FLAG_VALID;
int sum = pstate->grid + pstate->akku + pstate->load + pstate->mppt1 + pstate->mppt2;
if (abs(sum) > SUSPICIOUS) {
xdebug("FRONIUS suspicious values detected: sum=%d", sum); // probably inverter power dissipations (?)
pstate->flags &= ~FLAG_VALID;
}
if (pstate->load > 0) {
xdebug("FRONIUS positive load detected");
pstate->flags &= ~FLAG_VALID;
}
if (pstate->grid < -NOISE && pstate->akku > NOISE) {
int waste = abs(pstate->grid) < pstate->akku ? abs(pstate->grid) : pstate->akku;
xdebug("FRONIUS wasting %d akku -> grid power", waste);
pstate->flags &= ~FLAG_VALID;
}
if (pstate->dgrid > BASELOAD * 2) { // e.g. refrigerator starts !!!
xdebug("FRONIUS grid spike detected %d: %d -> %d", pstate->grid - h1->grid, h1->grid, pstate->grid);
pstate->flags &= ~FLAG_VALID;
}
shape_pstate();
}
static int calculate_next_round(device_t *d) {
if (d)
return d->timer;
if (PSTATE_OFFLINE || PSTATE_BURNOUT)
return WAIT_OFFLINE;
// much faster next round on
// - suspicious values detected
// - distortion
// - pv tendence up/down
// - wasting akku->grid power
// - big akku discharge or grid download
// - actual load > calculated load --> other consumers active
if (!PSTATE_VALID || PSTATE_DISTORTION || pstate->grid > 500 || pstate->akku > 500 || pstate->dxload < -5)
return WAIT_NEXT;
if (PSTATE_ALL_STANDBY)
return WAIT_STANDBY;
if (PSTATE_STABLE)
return WAIT_STABLE;
return WAIT_INSTABLE;
}
static void emergency() {
xlog("FRONIUS emergency shutdown at akku=%d grid=%d ", pstate->akku, pstate->grid);
for (device_t **dd = DEVICES; *dd; dd++)
ramp_device(DD, DOWN);
}
static void offline() {
xlog("FRONIUS offline soc=%.1f temp=%.1f", FLOAT10(pstate->soc), TEMP_IN);
}
// burn out akku between 7 and 9 o'clock if we can re-charge it completely by day
static void burnout() {
xlog("FRONIUS burnout soc=%.1f temp=%.1f", FLOAT10(pstate->soc), TEMP_IN);
fronius_override_seconds("plug5", WAIT_OFFLINE);
fronius_override_seconds("plug6", WAIT_OFFLINE);
// fronius_override_seconds("plug7", WAIT_OFFLINE); // makes no sense due to ventilate sleeping room
fronius_override_seconds("plug8", WAIT_OFFLINE);
}
static void daily(time_t now_ts) {
xlog("FRONIUS executing daily tasks...");
// aggregate 24 gstate hours into one day
gstate_t gd;
aggregate((int*) &gd, (int*) GSTATE_TODAY, GSTATE_SIZE, 24);
dump_table((int*) GSTATE_TODAY, GSTATE_SIZE, 24, -1, "FRONIUS gstate_hours", GSTATE_HEADER);
dump_struct((int*) &gd, GSTATE_SIZE, "[ØØ]", 0);
// dump high noon mosmix slots
mosmix_dump_history_noon();
}
static void hourly(time_t now_ts) {
xlog("FRONIUS executing hourly tasks...");
// resetting noresponse counters and standby states
for (device_t **dd = DEVICES; *dd; dd++) {
DD->noresponse = 0;
if (DD->state == Standby)
DD->state = Active;
}
// force all devices off when offline
if (PSTATE_OFFLINE)
for (device_t **dd = DEVICES; *dd; dd++)
ramp_device(DD, DOWN);
// update raw values
errors += curl_perform(curl_readable, &memory, &parse_readable);
// recalculate gstate, mosmix, then choose potd
calculate_gstate();
calculate_mosmix();
choose_program();
// copy counter and gstate to next slot (Fronius7 goes into sleep mode - no updates overnight)
memcpy(COUNTER_NEXT, (void*) counter, sizeof(counter_t));
memcpy(GSTATE_NEXT, (void*) gstate, sizeof(counter_t));
// print actual gstate
dump_table((int*) GSTATE_TODAY, GSTATE_SIZE, 24, now->tm_hour, "FRONIUS gstate_hours", GSTATE_HEADER);
print_gstate(NULL);
#ifndef FRONIUS_MAIN
// store to disk at 0, 6, 12, 18
if (now->tm_hour % 5 == 0) {
store_blob(GSTATE_FILE, gstate_hours, sizeof(gstate_hours));
store_blob(COUNTER_FILE, counter_hours, sizeof(counter_hours));
mosmix_store_state();
}
#endif
}
static void fronius() {
int hour, day, wait;
device_t *device = 0;
if (pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL)) {
xlog("Error setting pthread_setcancelstate");
return;
}
// get actual time and make a copy
time_t now_ts = time(NULL);
localtime(&now_ts);
memcpy(now, lt, sizeof(*lt));
day = now->tm_wday;
hour = now->tm_hour;
errors = 0;
wait = 1;
// once upon start: calculate global state + discharge rate and choose program of the day
counter = COUNTER_NOW;
gstate = GSTATE_NOW;
curl_perform(curl_readable, &memory, &parse_readable);
calculate_gstate();
calculate_mosmix();
choose_program();
// the FRONIUS main loop
while (1) {
sleep(1);
if (wait--)
continue;
// get actual time and make a copy
now_ts = time(NULL);
localtime(&now_ts);
memcpy(now, lt, sizeof(*lt));
// hourly tasks - before updating counter/gstate
if (hour != now->tm_hour) {
hour = now->tm_hour;
hourly(now_ts);
}
// daily tasks - before updating counter/gstate
if (day != now->tm_wday) {
day = now->tm_wday;
daily(now_ts);
}
// update state and counter pointers
counter = COUNTER_NOW;
gstate = GSTATE_NOW;
// check error counter
if (errors > 10)
for (device_t **dd = DEVICES; *dd; dd++)
ramp_device(DD, DOWN);
// make Fronius10 API call and calculate first pstate
errors += curl_perform(curl10, &memory, &parse_fronius10);
calculate_pstate1();
// check emergency
if (PSTATE_EMERGENCY)
emergency();
// check offline
if (PSTATE_OFFLINE)
offline();