-
Notifications
You must be signed in to change notification settings - Fork 9
/
cheapest_energy_hours.jinja
764 lines (759 loc) · 52.1 KB
/
cheapest_energy_hours.jinja
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
{%- set _version = 'v5.5.4'-%}
{#- sub-macro to format datetimes to selected time_format -#}
{%- macro _format_date(datetime, time_format) -%}
{%- set datetime = datetime | as_local -%}
{%- if time_format is not none -%}
{%- set format = dict(time12='%I:%M %p', time24='%H:%M') -%}
{{- datetime.strftime(format[time_format] | default(time_format)) -}}
{%- else -%}
{{- datetime.isoformat() -}}
{%- endif -%}
{%- endmacro -%}
{#- sub-macro to find the right sensor -#}
{%- macro _find_sensor(attr_today, attr_tomorrow) -%}
{%- set price_sensors = states.sensor | selectattr('attributes.'~attr_today, 'defined') | selectattr('attributes.'~attr_tomorrow, 'defined') | map(attribute='entity_id') | list -%}
{%- set price_sensor = namespace(id=none, data_count=0) -%}
{%- for s in price_sensors if price_sensors | count > 1 -%}
{%- set count_today = state_attr(s, attr_today) | count -%}
{%- set count_tomorrow = state_attr(s, attr_tomorrow) | count -%}
{%- set count_total = count_today + count_tomorrow -%}
{%- if count_total > price_sensor.data_count -%}
{%- set price_sensor.data_count = count_total -%}
{%- set price_sensor.id = s -%}
{%- endif -%}
{%- else -%}
{%- set price_sensor.id = price_sensors | first if price_sensors else none -%}
{%- endfor -%}
{{- price_sensor.id -}}
{%- endmacro-%}
{#- sub macro to find the right attributes -#}
{%- macro _find_attr(sensor, type) -%}
{%- set td = now().date() | string -%}
{%- set tm = (now() + timedelta(days=1)).date() | string -%}
{%- if sensor | has_value -%}
{%- for attr in states[sensor].attributes.items()
| rejectattr('1', 'string')
| rejectattr('1', 'mapping')
| selectattr('1', 'iterable')
-%}
{%- if attr[1][0] is mapping -%}
{%- set ns = namespace(td=0, tm=0) -%}
{%- for i in attr[1] -%}
{%- set ns.td = ns.td + 1 if i.values() | map('string') | select('contains', td) | list | count > 0 else ns.td -%}
{%- set ns.tm = ns.tm + 1 if i.values() | map('string') | select('contains', tm) | list | count > 0 else ns.tm -%}
{%- endfor -%}
{%- set results =
{
'all': ns.td > 3 and ns.tm > 3 and attr[1] | count > 24,
'today': ns.td > 3 and ns.tm < 3,
'tomorrow': ns.td < 3 and ns.tm > 3
}
-%}
{%- if results[type] -%}
{{- attr[0] -}}
{%- break -%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{%- endif -%}
{%- endmacro -%}
{#- sub-macro to caluclate hours and times based on datatpoints per hour -#}
{%- macro _div_calc(seconds, hrs, dph, wph, before=false, mode='dph') -%}
{#- check against dph provided -#}
{%- set div_list = [3600, 1800, 1200, 900, 720, 600, 300]-%}
{%- set div_list = div_list if wph is not defined or wph is none else [3600/wph] -%}
{%- set ns = namespace(div_reject=[]) -%}
{%- for div in div_list -%}
{%- set check_list = [3600/dph | default(1), div] | sort -%}
{%- set check = check_list[1] % check_list[0] == 0 -%}
{%- if not check -%}
{%- set ns.div_reject = ns.div_reject + [div] -%}
{%- endif -%}
{%- endfor -%}
{%- set div_list = div_list | reject('in', ns.div_reject) | list -%}
{#- find best dph setting -#}
{%- set ns = namespace(r=0, wp=dph | default(1), h=hrs | default(1)) -%}
{%- for div in div_list -%}
{%- if seconds % div == 0 -%}
{%- set remain = 0 -%}
{%- elif not before -%}
{%- set remain = div - seconds % div -%}
{%- else -%}
{%- set remain = seconds - (seconds // div) * div -%}
{%- endif -%}
{%- if remain < ns.r or loop.first -%}
{%- set ns.r = remain -%}
{%- set ns.wp = (3600 / div) | int -%}
{%- set ns.h = ((hrs * 3600) / (3600 / ns.wp)) | round(0, 'ceil') * div / 3600 -%}
{%- endif -%}
{%- endfor -%}
{#- output value -#}
{%- set output = dict(dph=ns.wp, hours=ns.h, remain=ns.r) -%}
{{- output[mode] -}}
{%- endmacro -%}
{#- main macro -#}
{%- macro cheapest_energy_hours(
sensor,
value_on_error,
hours,
start='00:00',
end='00:00',
attr_today='raw_today',
attr_tomorrow='raw_tomorrow',
attr_all='prices',
time_key='start',
value_key='value',
datetime_in_data=true,
data_minutes=60,
mode='start',
time_format=none,
include_today=true,
include_tomorrow=false,
lowest=true,
look_ahead=false,
latest_possible=false,
price_tolerance=0,
precision=5,
price_factor=1,
no_weight_points=none,
weight=none,
kwh=none,
program=none,
plot_sensor='sensor.energy_plots',
plot_attr='energy_plots',
split=false,
debug=false,
datapoints_per_hour='depreciated',
look_ahead_minutes='depreciated'
) -%}
{%- set modes = ['min', 'max', 'average', 'start', 'end', 'list', 'weighted_average','time_min','time_max', 'split', 'all', 'is_now', 'extreme_now', 'estimated_costs'] -%}
{#- set input for debugging -#}
{%- if debug -%}
{%- set defaults = dict(
sensor=none,
value_on_error=none,
hours=none,
start='00:00',
end='00:00',
attr_today='raw_today',
attr_tomorrow='raw_tomorrow',
attr_all='prices',
time_key='start',
value_key='value',
datetime_in_data=true,
data_minutes=60,
mode='start',
time_format=none,
include_today=true,
include_tomorrow=false,
lowest=true,
look_ahead=false,
latest_possible=false,
price_tolerance=0,
precision=5,
price_factor=1,
no_weight_points=none,
weight=none,
kwh=none,
program=none,
plot_sensor='sensor.energy_plots',
plot_attr='energy_plots',
split=false
)
-%}
{%- set user_input = dict(
sensor=sensor | default(none),
value_on_error= value_on_error | default(none),
hours=hours | default(none) | round(3, default=none),
start=start.isoformat() if start is datetime else start,
end=end.isoformat() if end is datetime else end,
attr_today=attr_today,
attr_tomorrow=attr_tomorrow,
attr_all=attr_all,
time_key=time_key,
value_key=value_key,
datetime_in_data=datetime_in_data,
data_minutes=data_minutes,
mode=mode,
time_format=time_format,
include_today=include_today,
include_tomorrow=include_tomorrow,
lowest=lowest,
look_ahead=look_ahead,
latest_possible=latest_possible,
price_tolerance=price_tolerance,
precision=precision,
price_factor=price_factor,
no_weight_points=no_weight_points,
weight=weight,
kwh=kwh,
program=program,
plot_sensor=plot_sensor,
plot_attr=plot_attr,
split=split
)
-%}
{%- set non_default = dict(user_input.items() | reject('in', defaults.items())) -%}
{%- endif -%}
{#- find sensor if needed -#}
{%- if sensor is not defined or not sensor | has_value -%}
{%- set sensor = _find_sensor(attr_today, attr_tomorrow) -%}
{%- endif -%}
{%- if sensor | has_value -%}
{#- Get data out of the selected entity, find right attributes if needed -#}
{%- set today = state_attr(sensor, attr_today) | default([], true)-%}
{%- set tomorrow = state_attr(sensor, attr_tomorrow) | default([], true) -%}
{%- set all = state_attr(sensor, attr_all) | default([], true) -%}
{%- set day = timedelta(days=1) -%}
{# check data if data_type is set to datetimes and values #}
{%- if datetime_in_data | bool(true) -%}
{# Check if there is an all attribute in the sensor #}
{%- set all_check = all is list and all | count > 0 and all[0] is mapping -%}
{%- set today_check = not include_today or today is list and today | count > 0 and today[0] is mapping -%}
{%- set tomorrow_check = not include_tomorrow or tomorrow is list and tomorrow | count > 0 and tomorrow[0] is mapping -%}
{# Check if right attributes are already provided #}
{%- if all_check or (today_check and tomorrow_check) -%}
{%- set data = all or today + tomorrow -%}
{%- else -%}
{%- set attr_all = attr_all if all_check else _find_attr(sensor, 'all') | default('attr_all_not_found', true) -%}
{%- set all = all if all_check else state_attr(sensor, attr_all) | default([], true) -%}
{%- set all_check = all_check or (all is list and all and all[0] is mapping) -%}
{# use all atrribute if available, and otherwise use tomorrow and today #}
{%- if all_check -%}
{%- set data = all -%}
{%- else -%}
{#- Check if data is available and as expected -#}
{#- try to find correct attributes if no valid data was found -#}
{%- set attr_today = (attr_today if today_check else _find_attr(sensor, 'today')) | default('attr_today_not_found', true) -%}
{%- set attr_tomorrow = (attr_tomorrow if tomorrow_check else _find_attr(sensor, 'tomorrow')) | default('attr_tomorrow_not_found', true) -%}
{#- set set dataset with all attributes -#}
{%- set today = state_attr(sensor, attr_today) | default([], true) -%}
{%- set tomorrow = state_attr(sensor, attr_tomorrow) | default([], true) -%}
{%- set data = today + tomorrow -%}
{%- endif -%}
{%- endif -%}
{#- Try to find the right time_key and value_key if provided parameters are not found -#}
{#- time key -#}
{%- if data | count > 0 and data[0] is mapping and time_key not in data[0] -%}
{%- set tk_list =
data[0].items()
| selectattr('1', 'datetime')
| sort(attribute='1')
| map(attribute='0')
| list
-%}
{%- if tk_list | count == 0 -%}
{%- set ns = namespace(tk_list=[]) -%}
{%- for k in data[0].keys() -%}
{%- if not data[0][k] | is_number and data[0][k] | as_datetime(none) is not none -%}
{%- set ns.tk_list = ns.tk_list + [k] -%}
{%- endif -%}
{%- endfor -%}
{%- set dates_check = [(now() - day).date() | string, now().date() | string, (now() + day).date() | string] | join('|') -%}
{%- set tk_list = ns.tk_list -%}
{%- endif -%}
{%- if tk_list | count > 0 -%}
{%- set time_key = tk_list | first -%}
{%- endif -%}
{%- endif -%}
{#- value_key -#}
{%- if data | count > 0 and data[0] is mapping and value_key not in data[0] -%}
{%- set vk_list =
data[0].items()
| selectattr('1', 'is_number')
| map(attribute='0')
| list
-%}
{%- set value_key = vk_list | first if vk_list else value_key -%}
{%- endif -%}
{%- else -%}
{%- set data = today + tomorrow + all -%}
{%- set ns = namespace(data=[]) -%}
{%- for i in data -%}
{%- set dt = today_at() + timedelta(minutes = data_minutes | int(60) * loop.index0) -%}
{%- set price = i.values() | first if i is mapping else i -%}
{%- set ns.data = ns.data + [{time_key: dt, value_key: price}] -%}
{%- endfor -%}
{%- set data = ns.data -%}
{%- endif -%}
{#- check if sensor has valid data -#}
{%- if data | count > 0 and time_key in data[0] and value_key in data[0] -%}
{#- Rebuild data from sensor to generic format -#}
{%- if data[0][time_key] is not datetime -%}
{%- set rebuild = namespace(data=[]) -%}
{%- for item in data -%}
{%- set time = item[time_key] -%}
{%- set time = as_local(as_datetime(time)) if time is string else time -%}
{%- set rebuild.data = rebuild.data + [{time_key: time, value_key: item[value_key] * price_factor | float(1)}] -%}
{%- endfor -%}
{%- set data = rebuild.data | sort(attribute='time') -%}
{%- endif -%}
{%- set dph = int(3600 / (data[1][time_key] - data[0][time_key]).total_seconds() | default(1, true)) if data | count > 1 else 1 -%}
{#- set weight points based on energy plot sensor -#}
{%-if program is not none-%}
{%- set plot_data = state_attr(plot_sensor, plot_attr) | default({}, true) -%}
{%- set weight = plot_data.get(program, {}).get('data') | default(none, true) -%}
{%- set no_weight_points = plot_data.get(program, {}).get('no_weight_points', 1) -%}
{%- set kwh = plot_data.get(program, {}).get('kwh_used', none) -%}
{%- endif -%}
{%- set w = weight | map('float', none) | reject('none') | list
if weight is list
else none
-%}
{#- set number of hours based on weight points in case hours setting is not provided -#}
{%- if mode == 'extreme_now' -%}
{%- set h = 1 / dph -%}
{%- elif w and hours is not defined -%}
{%- set h = (w | count / no_weight_points | default(1)) -%}
{%- else -%}
{%- set h = hours | default(1) | float(1) -%}
{%- endif -%}
{#- convert start and end input to local datetime -#}
{%- set n = now().replace(microsecond=0) -%}
{%- set before = true if mode == 'is_now' else false -%}
{#- start -#}
{%- if start is datetime -%}
{%- set start = start.replace(microsecond=0) | as_local -%}
{%- elif start | as_datetime is not none -%}
{%- set start = as_datetime(start).replace(microsecond=0) | as_local -%}
{%- else -%}
{%- set start = today_at(start) if start is string and ':' in start and start | replace(':', '') | is_number else today_at() -%}
{%- set start = start if include_today else start + day -%}
{%- endif -%}
{%- if look_ahead -%}
{%- set start = [n, start] | max -%}
{%- endif -%}
{%- set seconds = start.minute * 60 + start.second -%}
{%- set remain = _div_calc(seconds, h, dph, before=before, mode='remain') | int -%}
{%- set start_dph = _div_calc(seconds, h, dph, before=before) | int -%}
{#- end -#}
{%- if end is datetime -%}
{%- set end = end.replace(microsecond=0) | as_local -%}
{%- elif end | as_datetime is not none -%}
{%- set end = as_datetime(end).replace(microsecond=0) | as_local -%}
{%- else -%}
{%- set end = today_at(end) if end is string and ':' in end and end | replace(':', '') | is_number else today_at() -%}
{%- set end = end + day if end == today_at() else end -%}
{%- set end = end + day if include_tomorrow else end -%}
{%- endif -%}
{%- set seconds = end.minute * 60 + end.second -%}
{%- set remain = _div_calc(seconds, h, dph, mode='remain') | int -%}
{%- set end_dph = _div_calc(seconds, h, dph) | int -%}
{#- calculate no_weight_points based on hour input -#}
{%- set seconds = (h % 1 * 3600) | round(0) -%}
{%- set h_dph = _div_calc(seconds, h, dph) | int -%}
{#- set number of weight points based on all input -#}
{%- set dph_list = [start_dph, end_dph, h_dph] | unique | reject('eq', 1) | list -%}
{%- if dph_list | count > 1 and 12 not in dph_list -%}
{%-
set dph_list = [12]
if
(6 in dph_list and (5 in dph_list or 4 in dph_list))
or (5 in dph_list and (4 in dph_list or 3 in dph_list))
or 4 in dph_list and 3 in dph_list
or 3 in dph_list and 2 in dph_list
else dph_list
-%}
{%- endif -%}
{#- set number of weight points based on all input -#}
{%- set no_weight_points = no_weight_points if no_weight_points is not none else dph_list | max | default(1) | int -%}
{#- calculate start, end and h based on dph -#}
{#- h -#}
{%- set seconds = (h % 1 * 3600) | round(0) -%}
{%- set h = _div_calc(seconds, h, dph, no_weight_points, mode='hours', before=before) | float -%}
{#- start -#}
{%- set seconds = start.minute * 60 + start.second -%}
{%- set remain = _div_calc(seconds, h, dph, no_weight_points, before=before, mode='remain') | int -%}
{%- set start = start + timedelta(seconds=remain * (-1 if mode == 'is_now' else 1)) -%}
{#- end -#}
{%- set seconds = end.minute * 60 + end.second -%}
{%- set remain = _div_calc(seconds, h, dph, no_weight_points, before=before, mode='remain') | int -%}
{%- set end = end + timedelta(seconds=remain * (-1 if mode == 'is_now' else 1)) -%}
{#- set hours if in case hours='all' -#}
{%- set end_data = (data | last)[time_key] + timedelta(hours=1/dph) -%}
{%- set end_data = [end, end_data] | min -%}
{%- set h_all = (end_data.astimezone(utcnow().tzinfo)-start.astimezone(utcnow().tzinfo)).total_seconds() / 3600 -%}
{%- set h = h_all if hours is defined and hours == 'all' else h | default(hours | default(1) | float(1)) -%}
{#- set weight values based on number of hours (either add zeros, or remove unneeded part) -#}
{%- if w is not none -%}
{%- if w | count <= h * no_weight_points -%}
{%- set w = w + [0] * int(h*no_weight_points - w | count) -%}
{%- else -%}
{%- set w = w[:int(h*no_weight_points)] -%}
{%- endif -%}
{%- endif -%}
{#- check if no_weight_points matches datapoints in source -#}
{%- set dp = (h * dph) | round(0, 'ceil') | int -%}
{%- set dp_minutes, wp_minutes = 60 / dph, 60 / no_weight_points -%}
{%- set check_list = [dp_minutes, wp_minutes] | sort -%}
{%- set wp_check = check_list[1] % check_list[0] == 0 -%}
{#- debugging data -#}
{%- if debug -%}
{%- set datapoints_source = dict(datapoints=dp, datapoints_hour=dph) -%}
{%- endif -%}
{%- endif -%}
{%- endif -%}
{#- set more data for debugging -#}
{%- if debug -%}
{%- set macro_input = dict(
sensor=sensor,
hours=h | default(hours |default(1, true) | float(1)) | round(3),
value_on_error= value_on_error | default(none),
start=start.isoformat() if start is datetime else start,
end=end.isoformat() if end is datetime else end,
attr_today=attr_today,
attr_tomorrow=attr_tomorrow,
attr_all=attr_all,
time_key=time_key,
value_key=value_key,
datetime_in_data=datetime_in_data,
data_minutes=data_minutes,
mode=mode,
time_format=time_format,
include_today=include_today,
include_tomorrow=include_tomorrow,
lowest=lowest,
look_ahead=look_ahead,
latest_possible=latest_possible,
price_tolerance=price_tolerance,
precision=precision,
price_factor=price_factor,
no_weight_points=no_weight_points,
weight=w | default(none),
kwh=kwh,
program=program,
plot_sensor=plot_sensor,
plot_attr=plot_attr,
split=split
)
-%}
{%- set set_by_macro = dict(macro_input.items() | reject('in', user_input.items())) -%}
{%- set defaults_used = dict(defaults.items() | select('in', macro_input.items()) | select('in', user_input.items())) -%}
{%- set data_used = dict(today_count=today | count, tomorrow_count = tomorrow | count, all_count = all | count, data_count = data | count) -%}
{%- endif -%}
{%- set data = data | default([]) -%}
{#- Return error messages for input -#}
{%- set h = h | default(hours |default(1, true) | float(1)) | round(3) -%}
{%- set start_end = start > end -%}
{%- set end_start = ((end-start).total_seconds() / 3600) | round(3) if start is datetime and end is datetime else h -%}
{%- set valid_sensor = sensor | has_value %}
{%- set valid_pt = (price_tolerance is string and price_tolerance[-1] == '%' and price_tolerance[:-1] | is_number) or price_tolerance | is_number%}
{%- set all_keys = iif(data) and time_key in data and value_key in data -%}
{%- set errors = [
"No valid sensor found" if not valid_sensor,
"No valid data in {}".format(sensor) if data == [] and valid_sensor,
"Time key '{}' not found in data".format(time_key) if data and time_key not in data[0],
"Value key '{}' not found in data".format(value_key) if data and value_key not in data[0],
"Invalid mode '{}' selected".format(mode) if mode not in modes,
"Boolean input expected for include_today, '{}' can not be processed as a boolean".format(include_today) if include_today | bool("") is not boolean,
"Boolean input expected for include_tomorrow, '{}' can not be processed as a boolean".format(include_tomorrow) if include_tomorrow | bool("") is not boolean,
"Boolean input expected for look_ahead, '{}' can not be processed as a boolean".format(look_ahead) if look_ahead | bool("") is not boolean,
"Boolean input expected for lowest, '{}' can not be processed as a boolean".format(lowest) if lowest | bool("") is not boolean,
"Boolean input expected for latest_possible, '{}' can not be processed as a boolean".format(latest_possible) if latest_possible | bool("") is not boolean,
"Numeric input or percentage expected for price_tolerance, '{}' can not be processed as a float or percentage".format(price_tolerance) if not valid_pt,
"Numeric input expected for kwh, '{}' can not be processed as a float".format(kwh) if kwh is not none and not kwh | is_number,
"Selected program '{}' is not available or has no data".format(program) if program is not none and w is none,
"Selected start parameter is after the end parameter" if start_end and data,
"{} hours between start and end, where {} hours are required".format(end_start, h | round(3)) if not start_end and end_start < h | round(3) and data,
"Invalid combination of source data points per hour '{}' and number of weight points '{}'".format(dp, dph) if all_keys and not wp_check
] | select() | list
-%}
{%- set use_voe = value_on_error is defined -%}
{#- check if there are errors -#}
{%- if errors | count > 0 and not debug -%}
{{- value_on_error if use_voe else '{} error{}: {}'.format(errors | count, 's' if errors | count > 1, errors | join(', ')) -}}
{%- elif errors | count > 0 -%}
{{- dict(
version=_version,
output=errors | join(', '),
error=true,
non_default_user_input=non_default,
set_by_macro=set_by_macro,
defaults_used=defaults_used,
data_used=data_used
)
-}}
{#- no error - continue with macro -#}
{%- else -%}
{#- rebuild data again to match no_weight_points -#}
{# split hours if needed #}
{%- if dp_minutes > wp_minutes -%}
{%- set rebuild = namespace(data=[]) -%}
{%- for v in (data * int(dp_minutes/wp_minutes)) | sort(attribute=time_key) -%}
{%- set t = v[time_key] -%}
{%- set t = t + timedelta(minutes=(wp_minutes*(loop.index0 % int(dp_minutes/wp_minutes)))) -%}
{%- set rebuild.data = rebuild.data + [ {time_key: t, value_key: v[value_key]} ] -%}
{%- endfor -%}
{%- set data = rebuild.data -%}
{%- set dph = 3600 / (data[1][time_key] - data[0][time_key]).seconds -%}
{%- set dp = (h * dph) | round(0, 'ceil') | int -%}
{%- set dp_minutes, wp_minutes = 60 / dph, 60 / no_weight_points -%}
{# add weights for additional datapoints if needed #}
{%- elif dp_minutes < wp_minutes and w is not none -%}
{%- set weight_adjust = namespace(weight=[]) -%}
{%- for item in w -%}
{%- set weight_adjust.weight = weight_adjust.weight + [item] * int(round(wp_minutes/dp_minutes),0) -%}
{%- endfor -%}
{%- set w = weight_adjust.weight -%}
{%- endif -%}
{#- debugging data -#}
{%- if debug -%}
{%- set datapoints_used = dict(datapoints=dp, datapoints_hour=dph) -%}
{%- endif -%}
{#- Perform selection based on start and end on the data -#}
{%- set values = data
| selectattr(time_key, '>=', start)
| selectattr(time_key, '<', end)
| selectattr(value_key, 'is_number')
| list
-%}
{#- Check if there is data, and find the right hour block -#}
{%- if values | count >= dp -%}
{# apply price_tolerance if provided #}
{%- set pt_perc = '%' in price_tolerance | string -%}
{%- set pt = price_tolerance | replace('%', '') | float / 100 if pt_perc else price_tolerance | float -%}
{%- if pt != 0 %}
{%- set base_price = values | map(attribute=value_key) | min if lowest else values | map(attribute=value_key) | max -%}
{%- set pt = base_price * pt if pt_perc else pt -%}
{%- set ns = namespace(new_values=[]) -%}
{%- for v in values %}
{%- set original = v[value_key] -%}
{%- set adjusted = original - pt if lowest else original + pt -%}
{%- set adjusted = [adjusted, base_price] | max if lowest else [adjusted, base_price] | min -%}
{%- set ns.new_values = ns.new_values + [{
time_key: v[time_key],
value_key: adjusted,
'original_value': original
}]
-%}
{%- endfor -%}
{%- set values = ns.new_values -%}
{%- endif -%}
{%- set split = mode == 'split' or split -%}
{%- set pr = precision | default(5) -%}
{%- if split -%}
{#- sort values and take out hours needed -#}
{%- set prices = (values | sort(attribute=value_key, reverse=not lowest) | map(attribute=value_key) | list)[:dp] -%}
{%- set values = values | selectattr(value_key, 'in', prices) | sort(attribute=time_key) | list -%}
{%- set last_value = prices | max if lowest else prices | min -%}
{%- set values_last = values | selectattr(value_key, 'eq', last_value) | list -%}
{%- set last_needed = dp - (values | count - values_last | count) -%}
{#- make sure values are in the least possible number of time blocks -#}
{%- if last_needed != values_last | count -%}
{#- select those datetimes which are connected to datetimes with the other values -#}
{%- set ns = namespace(needed=last_needed, other=values | rejectattr(value_key, 'eq', last_value) | list, max=values_last) -%}
{%- for v in values_last -%}
{%- set above = ns.other | selectattr(time_key, 'eq', v[time_key] + timedelta(minutes=dp_minutes)) | list | count -%}
{%- set below = ns.other | selectattr(time_key, 'eq', v[time_key] - timedelta(minutes=dp_minutes)) | list | count -%}
{%- if above or below -%}
{%- set ns.other = ns.other + [v] -%}
{%- set ns.max = ns.max | reject('eq', v) | sort(attribute=time_key) | list -%}
{%- set ns.needed = ns.needed - 1 -%}
{%- endif -%}
{%- if ns.needed == 0 -%}
{%- break -%}
{%- endif -%}
{%- endfor -%}
{%- set values = (ns.other + ns.max[:ns.needed]) | sort(attribute=time_key) -%}
{%- endif -%}
{#- create the split data -#}
{%- set split = namespace(split=[], start=none, prices=[], datapoints=0) -%}
{%- for i in range(dp) -%}
{%- set dt = values[loop.index0][time_key] -%}
{%- if i > 0 and values[loop.index0 - 1][time_key] != dt - timedelta(hours=1/dph) -%}
{%- set is_now = split.start <= now() < split.start + timedelta(hours=split.datapoints/dph) -%}
{%- set prices = split.prices -%}
{%- set split.split = split.split + [
dict(
start=_format_date(split.start, time_format),
end=_format_date(split.start + timedelta(hours=split.datapoints/dph), time_format),
hours=(split.datapoints/dph)|round(2),
prices=prices,
is_now=is_now
)
]
-%}
{%- set split.start = dt -%}
{%- set split.datapoints = 1 -%}
{%- set price = values[loop.index0][value_key] if pt == 0 else values[loop.index0].original_value %}
{%- set split.prices = [price | round(pr)] -%}
{%- if loop.last -%}
{%- set is_now = split.start <= now() < dt + timedelta(hours=1/dph) -%}
{%- set prices = split.prices -%}
{%- set split.split = split.split + [
dict(
start=_format_date(split.start, time_format),
end=_format_date(dt + timedelta(hours=1/dph), time_format),
hours=(split.datapoints/dph)|round(2),
prices=prices,
is_now=is_now
)
]
-%}
{%- endif -%}
{%- else -%}
{%- set split.start = split.start | default(dt, true) -%}
{%- set split.datapoints = split.datapoints + 1 -%}
{%- set price = values[loop.index0][value_key] if pt == 0 else values[loop.index0].original_value %}
{%- set split.prices = split.prices + [price | round(pr)] -%}
{%- if loop.last -%}
{%- set is_now = split.start <= now() < dt + timedelta(hours=1/dph) -%}
{%- set prices = split.prices -%}
{%- set split.split = split.split + [
dict(
start=_format_date(split.start, time_format),
end=_format_date(dt + timedelta(hours=1/dph), time_format),
hours=(split.datapoints/dph)|round(2),
prices=prices,
is_now=is_now
)
]
-%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{#- prepare the output -#}
{%- set split_all = split.split + [ dict(total_hours=h | round(2), datapoints_per_hour=dph, datapoints=dp)] -%}
{%- set pr = precision | default(5) -%}
{%- set list = split_all | selectattr('prices', 'defined') | map(attribute='prices') | sum(start=[]) -%}
{%- set min, max = list | min, list | max -%}
{%- set time_min = split_all | selectattr('prices', 'defined') | selectattr('prices', 'contains', min) | map(attribute='start') | first -%}
{%- set time_max = split_all | selectattr('prices', 'defined') | selectattr('prices', 'contains', max) | map(attribute='start') | first -%}
{%- set is_now = split_all | selectattr('is_now', 'defined') | selectattr('is_now') | list | count > 0 -%}
{%- set output = dict(
list = list | map('round', pr) | list,
min = min | round(pr),
max = max | round(pr),
average = list | average | round(pr),
weighted_average = list | average | round(pr),
start = _format_date(as_datetime(split_all[0].start), time_format),
end = _format_date(as_datetime(split_all[-2].end), time_format),
time_min = _format_date(as_datetime(time_min), time_format | default(none)),
time_max = _format_date(as_datetime(time_max), time_format | default(none)),
is_now = is_now,
all = split_all | to_json
)
-%}
{#- output the data -#}
{%- set macro_output = output.all if mode == 'split' else output[mode] -%}
{%- else -%}
{#- create output for all modes -#}
{%- set output = namespace(average=none, start=none, min=none, max=none, weighted_average=none, compare=none, time_min=none, time_max=none, prices_list=[], list=[], is_now=false, extreme_now=none, estimated_costs="unknown") -%}
{%- set last_values = (h * no_weight_points) | round(0) | int -%}
{%- for i in range(values | count - (last_values - 1)) -%}
{%- set prices_list = values[i:i + last_values] | map(attribute=value_key) | list -%}
{%- set original_price_list =
values[i:i + last_values] | map(attribute='original_value') | list
if pt != 0
else prices_list
-%}
{#- calculate (weighted) average price -#}
{%- if w is not none -%}
{%- set weighted = namespace(sum=0, original=0) -%}
{%- for price in prices_list -%}
{%- set weighted.sum = weighted.sum + price * w[loop.index0] -%}
{%- endfor -%}
{%- set a = weighted.sum / w | count -%}
{%- if pt != 0 -%}
{%- for price in original_price_list -%}
{%- set weighted.original = weighted.original + price * w[loop.index0] -%}
{%- endfor -%}
{%- set original_w_avg = weighted.original / w | count -%}
{%- endif -%}
{%- else -%}
{%- set a = prices_list | sum / dp -%}
{%- endif -%}
{%- set b = output.compare -%}
{%- set min, max = (prices_list | min, prices_list | max) if pt == 0 else (original_price_list | min, original_price_list | max) -%}
{%- set avg = prices_list | average | round(pr) if pt == 0 else original_price_list | average | round(pr) -%}
{%- if loop.first or (((a <= b) if lowest else (a >= b)) if latest_possible | bool else ((a < b) if lowest else (a > b))) -%}
{%- set output.prices_list = original_price_list if pt != 0 else prices_list -%}
{%- set output.list = output.prices_list | map('round', pr) | list -%}
{%- set output.min = min | round(pr) -%}
{%- set output.max = max | round(pr) -%}
{%- set output.weighted_average = a | round(pr) if pt == 0 else original_w_avg | default(avg) | round(pr) -%}
{%- set output.compare = [a, b | default(a, true)] | min if lowest else [a, b | default(a, true)] | max %}
{%- set output.average = avg -%}
{%- set output.start = _format_date(values[i][time_key], time_format | default(none)) -%}
{%- set output.end = _format_date(values[i][time_key] + timedelta(hours=h), time_format | default(none)) -%}
{%- set index_min, index_max = output.prices_list.index(min), output.prices_list.index(max) -%}
{%- set output.time = values[i:i+last_values] | map(attribute=time_key) | list -%}
{%- set output.time_min = _format_date(output.time[index_min], time_format | default(none)) -%}
{%- set output.time_max = _format_date(output.time[index_max], time_format | default(none)) -%}
{%- set output.is_now = values[i][time_key] < now() < values[i][time_key] + timedelta(hours=h) -%}
{%- endif -%}
{%- endfor -%}
{#- determine estimated_costs if kwh is provided -#}
{%- if kwh is not none -%}
{%- set price_count = output.prices_list | count %}
{%- if w is none -%}
{%- set kwh_list = [kwh/price_count] * price_count -%}
{%- else -%}
{%- set ns = namespace(kwh_list=[]) -%}
{%- for i in w -%}
{%- set ns.kwh_list = ns.kwh_list + [kwh * (i/w | sum)] -%}
{%- endfor -%}
{%- set kwh_list = ns.kwh_list-%}
{%- endif -%}
{%- set output.estimated_costs = 0 -%}
{%- for p in output.prices_list -%}
{%- set output.estimated_costs = (output.estimated_costs + p * kwh_list[loop.index0]) -%}
{%- set output.estimated_costs = output.estimated_costs | round(pr) if loop.last else output.estimated_costs -%}
{%- endfor -%}
{%- endif -%}
{#- determine extreme_now -#}
{%-
set compare_price = values | map(attribute=value_key) | min + pt
if lowest
else values | map(attribute=value_key) | max - pt
-%}
{%- set current_price = data | sort(attribute=time_key, reverse=true) | selectattr(time_key, '<=', now()) | map(attribute=value_key) | list | first | default(none) -%}
{%- set output.extreme_now = current_price is not none and current_price <= compare_price if lowest else current_price >= compare_price -%}
{#- output date based on the selected mode -#}
{%- if mode == 'all' -%}
{%- set macro_output = dict(
start=output.start,
end=output.end,
min=output.min,
max=output.max,
time_min=output.time_min,
time_max=output.time_max,
average=output.average,
weighted_average=output.weighted_average,
list=output.list,
is_now=output.is_now,
extreme_now=output.extreme_now,
estimated_costs=output.estimated_costs,
datapoints_per_hour=dph,
hours=h | default(hours | default(1, true) | float(1)),
datapoints=dp
) | to_json
-%}
{%- else -%}
{%- set macro_output = output[mode] -%}
{%- endif -%}
{%- endif -%}
{#- output error message if there is not enought data -#}
{%- else -%}
{%- set v = values | count -%}
{%- set h_v = v / no_weight_points | round(3) -%}
{%- set error_msg = '1 error: {} datapoints ({} hours) in selection, where {} ({} hours) are expected'.format(v, h_v, dp, h | round(3)) -%}
{%- set macro_output = value_on_error if use_voe and not debug else error_msg -%}
{%- endif -%}
{%- if debug -%}
{%- set dict_add = dict(values_count=values|count, datapoints_source=datapoints_source, datapoints_used=datapoints_used, original_prices=output.prices_list, price_count=price_count, kwh_list=kwh_list) -%}
{{- dict(
version=_version,
output=macro_output,
error=true if error_msg is defined else false,
non_default_user_input=non_default,
set_by_macro=set_by_macro,
defaults_used=defaults_used,
data_used=dict(data_used, **dict_add)
) | to_json
-}}
{%- else -%}
{{- macro_output -}}
{%- endif -%}
{%- endif -%}
{%- endmacro -%}