-
Notifications
You must be signed in to change notification settings - Fork 0
/
Esphome培正电表谷峰计量本地计算-mqtt通信-带rtc时钟.yaml
910 lines (815 loc) · 29.6 KB
/
Esphome培正电表谷峰计量本地计算-mqtt通信-带rtc时钟.yaml
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
####################################################################################
# 项目名称: Esphome培正电表谷峰计量本地计算-mqtt通信-带rtc时钟
#
# 作者: jjcs
#
# 时间: 2024.7.25
#
# 更新:V1.3
# 对上月的数据flash清零
#
#
####################################################################################
####################################################################################
#
# 配置定义
#
####################################################################################
substitutions:
#初始配置
node_name: "需要修改" #名称 修改
node_logger_level: INFO #日志级别
node_api_key: 需要修改 #api密钥 修改
node_ota_password: "需要修改" #ota密钥 修改
node_wifi_ssid: "需要修改" #wifi名称
node_wifi_password: "需要修改" #wifi密码
node_manual_ip_static_ip: 需要修改 #静态ip
node_manual_ip_gateway: 需要修改 #网关
node_manual_ip_subnet: 255.255.255.0 #子网掩码 修改
node_ap_ssid: ${node_name}_ap #ap
node_ap_password: 需要修改
#####################################################################################
#####################################################################################
#
# #官方诊断组件
#
####################################################################################
node_version_name: ${node_name}_version #编译时间
node_sensor_wifi_signal_name: ${node_name}_wifi_signal #wifi强度
node_sensor_uptime_name: ${node_name}_uptime
node_internal_temperature_name: ${node_name}_internal_temperature
#node_esp_cpu_freq_name: ${node_name}_esp_cpu_freq
#node_esp_free_memory_name: ${node_name}_esp_free_memory
node_linkstatus: ${node_name}_linkstatus
#node_esp_memory_frag_name: ${node_name}_esp_memory_frag
node_wifi_info_ip_address_name: ${node_name}_ip_address
#node_wifi_info_bssid_name: ${node_name}_bssid
node_wifi_info_mac_address_name: ${node_name}_mac_address
#node_wifi_latest_scan_results_name: ${node_name}_latest_scan_results
#node_wifi_dns_address_name: ${node_name}_dns_address
node_switch_restart_name: ${node_name}_restart
node_switch_safe_mode_name: ${node_name}_safe_mode
node_debug_free_name: ${node_name}_debug_free
node_debug_block_name: ${node_name}_debug_block
node_debug_reset_reason_name: ${node_name}_debug_reset_reason
####################################################################################
#####################################################################################
#
# #web_server
#
####################################################################################
web_server:
port: 80
local: true
version: 3
auth:
username: 需要修改
password: 需要修改
####################################################################################
####################################################################################
#
# 头文件
#
####################################################################################
packages:
common-component: !include esp32c3.yaml
####################################################################################
####################################################################################
#
# mqtt配置
#
####################################################################################
mqtt:
broker: 需要修改
port: 需要修改
username: 需要修改
password: 需要修改
client_id: 需要修改
#discovery: true
discovery_retain: true #保留
keepalive: 15s
topic_prefix: 需要修改 #主题前缀
#log_topic:
# topic: "rural-electricity-meter/logs"
# level: INFO
#discovery_prefix: homeassistant
#discovery_object_id_generator: device_name
#discovery_unique_id_generator: mac
####################################################################################
#最后的遗嘱
birth_message:
topic: rural-electricity-meter/birth_message
payload: online
retain: true
will_message:
topic: rural-electricity-meter/will_message
payload: offline
retain: true
shutdown_message:
topic: rural-electricity-meter/shutdown_message
payload: offline
retain: true
on_connect:
- switch.turn_on: gp12
on_disconnect:
- switch.turn_off: gp12
switch:
- platform: output
name: "${node_name}_gpio12"
output: 'gpio_12'
id: gp12
- platform: output
name: "${node_name}_gpio13"
output: 'gpio_13'
output:
- platform: ledc
pin: #GPIO12 #D4
number: LED1_BUILTIN
ignore_pin_validation_error: true
id: gpio_12
frequency: 2000
# inverted: true
- platform: ledc
pin: #GPIO13 #D5
number: LED2_BUILTIN
ignore_pin_validation_error: true
id: gpio_13
frequency: 2000
####################################################################################
#
# 串口底层配置
#
####################################################################################
uart:
- id: main_uart
rx_pin:
#number: GPIO6
number: GPIO1
inverted: false
mode:
input: true
pullup: true
tx_pin:
#number: GPIO7
number: GPIO0
inverted: false
mode:
output: true
baud_rate: 9600
#debug:
# direction: BOTH
####################################################################################
####################################################################################
#
# i2c总线配置-rtc时钟
#
####################################################################################
i2c:
sda: 4
scl: 5
scan: true
id: bus_a
####################################################################################
modbus:
- id: mod_bus_pzemac
send_wait_time: 500ms
uart_id: main_uart
#清零组
button:
- platform: template
name: ${node_name}_Meter Reset
id: reset_button
icon: "mdi:emoticon-outline"
on_press:
- pzemac.reset_energy: pzemac_main
- platform: template
name: ${node_name}_Clear_Memory
id: clear_memory
entity_category: diagnostic
on_press:
- lambda: |-
id(global_today_peak_usage) = 0.000f; // # flash保存今日峰电量
id(global_today_valley_usage) = 0.000f; // # flash保存今日谷电量
id(global_today_peak_bill) = 0.000f; //flash保存今日峰电费
id(global_today_valley_bill) = 0.000f; //# flash保存今日谷电费
id(global_month_peak_usage) = 0.000f; // # flash保存本月峰电量(昨日为止)
id(global_month_valley_usage) = 0.000f; //# flash保存本月谷电量(昨日为止)
id(global_month_peak_bill) = 0.000f; //# flash保存本月峰电费(昨日为止)
id(global_month_valley_bill)= 0.000f; //# flash保存本月谷电费(昨日为止)
//2024.7.23 基础上添加修改
id(global_yesterday_usage)= 0.000f;
id(global_yesterday_peak_usage)= 0.000f;
id(global_yesterday_valley_usage)= 0.000f;
id(global_yesterday_peak_bill)= 0.000f;
id(global_yesterday_valley_bill)= 0.000f;
#对上月的数据flash清零
- platform: template
name: ${node_name}_Clear_Memory_Month
id: clear_memory_Month
entity_category: diagnostic
on_press:
- lambda: |-
//2024.7.25 基础上添加修改
id(global_last_month_peak_usage)= 0.000f;
id(global_last_month_valley_usage)= 0.000f;
id(global_last_month_peak_bill)= 0.000f;
id(global_last_month_valley_bill)= 0.000f;
globals:
# flash保存表复位(清0)时的年度用电量
- id: global_reset_annual_usage
type: float
restore_value: yes
initial_value: '0.000'
# flash保存1号00:00:00的用电度数(上月用电度数)
- id: global_last_month_usage
type: float
restore_value: yes
initial_value: '0.000'
# flash保存00:00:00的抄表数(昨日抄表数)
- id: global_yesterday_energy
type: float
restore_value: yes
initial_value: '0.000'
# flash保存00:00:00的昨日用电量
- id: global_yesterday_usage
type: float
restore_value: yes
initial_value: '0.000'
# flash保存00:00:00的昨日峰电量
- id: global_yesterday_peak_usage
type: float
restore_value: yes
initial_value: '0.000'
# flash保存00:00:00的昨日谷电量
- id: global_yesterday_valley_usage
type: float
restore_value: yes
initial_value: '0.000'
# flash保存00:00:00的昨日峰电费
- id: global_yesterday_peak_bill
type: float
restore_value: yes
initial_value: '0.000'
# flash保存00:00:00的昨日谷电费
- id: global_yesterday_valley_bill
type: float
restore_value: yes
initial_value: '0.000'
# flash保存上月峰电量
- id: global_last_month_peak_usage
type: float
restore_value: yes
initial_value: '0.000'
# flash保存上月谷电量
- id: global_last_month_valley_usage
type: float
restore_value: yes
initial_value: '0.000'
# flash保存上月峰电费
- id: global_last_month_peak_bill
type: float
restore_value: yes
initial_value: '0.000'
# flash保存上月谷电费
- id: global_last_month_valley_bill
type: float
restore_value: yes
initial_value: '0.000'
# flash保存今日峰电量
- id: global_today_peak_usage
type: float
restore_value: yes
initial_value: '0.000'
# flash保存今日谷电量
- id: global_today_valley_usage
type: float
restore_value: yes
initial_value: '0.000'
# flash保存今日峰电费
- id: global_today_peak_bill
type: float
restore_value: yes
initial_value: '0.000'
# flash保存今日谷电费
- id: global_today_valley_bill
type: float
restore_value: yes
initial_value: '0.000'
# flash保存本月峰电量(昨日为止)
- id: global_month_peak_usage
type: float
restore_value: yes
initial_value: '0.000'
# flash保存本月谷电量(昨日为止)
- id: global_month_valley_usage
type: float
restore_value: yes
initial_value: '0.000'
# flash保存本月峰电费(昨日为止)
- id: global_month_peak_bill
type: float
restore_value: yes
initial_value: '0.000'
# flash保存本月谷电费(昨日为止)
- id: global_month_valley_bill
type: float
restore_value: yes
initial_value: '0.000'
# 全局变量:当前是峰电价(true,flase)flash不保存,计算所得
- id: global_is_peak_price
type: bool
restore_value: false
initial_value: 'true'
# 全局变量:当前阶梯档位(0,1,2)flash不保存,计算所得
- id: global_step_id
type: int
restore_value: false
initial_value: '0'
# 全局变量:上1分钟抄表数,不保存
- id: global_last_energy
type: float
restore_value: false
initial_value: '-0.001'
number:
# 设置表读数为0(复位)时的年度电量,加上表上读数为实际当年使用度数, 用来计算阶梯电价
- platform: template
id: set_reset_annual_usage
name: ${node_name}_Set Reset Annual Usage
mode: box
min_value: 0
max_value: 9999
initial_value: 0
optimistic: true
step: 1
restore_value: false
unit_of_measurement: kwh
on_value_range:
above: 1
below: 9999
then:
- lambda: |-
id(global_reset_annual_usage) = x;
id(reset_annual_usage).publish_state(id(global_reset_annual_usage));
id(actual_annual_usage).publish_state( id(global_reset_annual_usage) + id(main_energy).state );
id(current_price).update();
sensor:
####################################################################################
#
# ##时间月份传感器
#
####################################################################################
- platform: template
name: "${node_name}_sntp_sensor"
id: "id_spot_price_sntp_sensor"
unit_of_measurement: s
- platform: template
name: "${node_name}_month_sntp_sensor"
id: "id_month_sntp_sensor"
unit_of_measurement: 月
####################################################################################
# 培正电表,每月重置
- platform: pzemac
id: pzemac_main
update_interval: 10s
address: 0x01
modbus_id: mod_bus_pzemac
current:
name: "${node_name}_Current"
voltage:
name: "${node_name}_Voltage"
power:
name: "${node_name}_Power"
id: main_power
frequency:
name: "${node_name}_Frequency"
power_factor:
name: "${node_name}_Factor"
energy:
# 当前读表度数,每月清0,即为当月度数
name: "${node_name}_Energy"
id: main_energy
unit_of_measurement: kWh
device_class: energy
state_class: total_increasing
accuracy_decimals: 3
filters:
- multiply: 0.001
on_value:
then:
- lambda: |-
id(actual_annual_usage).publish_state( id(global_reset_annual_usage) + id(main_energy).state );
id(today_usage).publish_state( id(main_energy).state - id(global_yesterday_energy));
id(current_price).update();
# 表读数为0(复位)时的年度电量,加上当前读表数为实际年度用量
- platform: template
name: ${node_name}_Reset Annual Usage
id: reset_annual_usage
accuracy_decimals: 3
device_class: "energy"
state_class: "total_increasing"
icon: mdi:lightning-bolt
unit_of_measurement: kWh
update_interval: 15s
lambda:
id(set_reset_annual_usage).publish_state(0);
return id(global_reset_annual_usage);
# 实际年度用量( 表复位时的年度电量+当前电量(表读数))
- platform: template
name: ${node_name}_Actual Annual Usage
id: actual_annual_usage
accuracy_decimals: 3
device_class: "energy"
state_class: "total_increasing"
icon: mdi:lightning-bolt
unit_of_measurement: kWh
update_interval: 15s
lambda:
return id(global_reset_annual_usage) + id(main_energy).state;
# 当前电价
- platform: template
name: ${node_name}_Current Price
id: current_price
accuracy_decimals: 3
icon: mdi:home-lightning-bolt
unit_of_measurement: 'CNY/kWh'
update_interval: 10s
lambda: |-
int step_id=0;
float current_price = 0.000f;
float all_prices[3][2] = { {0.617, 0.307}, {0.677, 0.337}, {0.977, 0.487} };
float total_usage = id(actual_annual_usage).state; //标准电费。5人以上用电阶梯加1200度.普通3120-4800 5人4320-6000
if(total_usage <= 4320) step_id=0;
if((total_usage>4320) && (total_usage <= 6000)) step_id=1;
if(total_usage > 6000) step_id=2;
id(global_step_id) = step_id;
int t_now = parse_number<int>(id(time_sntp).now().strftime("%H%M")).value();
int t_nowsntp1 = parse_number<int>(id(time_sntp).now().strftime("%m")).value();
id(id_spot_price_sntp_sensor).publish_state(t_now);
id(id_month_sntp_sensor).publish_state(t_nowsntp1);
if ( t_now>=800 && t_now < 2200 ){
current_price = all_prices[step_id][0];
id(global_is_peak_price) = true;
}else{
current_price = all_prices[step_id][1];
id(global_is_peak_price) = false;
}
return current_price;
# 昨日抄表数
- platform: template
name: ${node_name}_Yesterday Energy
id: yesterday_energy
accuracy_decimals: 3
device_class: "energy"
state_class: "total_increasing"
icon: mdi:lightning-bolt
unit_of_measurement: kWh
update_interval: 15s
lambda:
return id(global_yesterday_energy);
# 昨日用电量
- platform: template
name: ${node_name}_Yesterday Usage
id: yesterday_usage
accuracy_decimals: 3
device_class: "energy"
state_class: "total_increasing"
icon: mdi:lightning-bolt
unit_of_measurement: kWh
update_interval: 15s
lambda:
return id(global_yesterday_usage);
# 今日用电量
- platform: template
name: ${node_name}_Today Usage
id: today_usage
accuracy_decimals: 3
device_class: "energy"
state_class: "total_increasing"
icon: mdi:lightning-bolt
unit_of_measurement: kWh
update_interval: 10s
lambda:
return id(main_energy).state - id(global_yesterday_energy);
# 上月用电量
- platform: template
name: ${node_name}_Last Month Usage
id: last_monthly_usage
accuracy_decimals: 3
device_class: "energy"
state_class: "total_increasing"
icon: mdi:lightning-bolt
unit_of_measurement: kWh
update_interval: 20s
lambda:
return id(global_last_month_usage);
# 昨日、上月、今日、当月谷电量
- platform: template
name: ${node_name}_Yesterday Valley Usage
id: yesterday_valley_usage
accuracy_decimals: 3
device_class: "energy"
state_class: "total_increasing"
icon: mdi:lightning-bolt
unit_of_measurement: kWh
update_interval: 20s
lambda:
return id(global_yesterday_valley_usage);
- platform: template
name: ${node_name}_Last Month Valley Usage
id: last_month_valley_usage
accuracy_decimals: 3
device_class: "energy"
state_class: "total_increasing"
icon: mdi:lightning-bolt
unit_of_measurement: kWh
update_interval: 20s
lambda:
return id(global_last_month_valley_usage);
- platform: template
name: ${node_name}_Today Valley Usage
id: today_valley_usage
accuracy_decimals: 3
device_class: "energy"
state_class: "total_increasing"
icon: mdi:lightning-bolt
unit_of_measurement: kWh
update_interval: 15s
lambda:
return id(global_today_valley_usage);
- platform: template
name: ${node_name}_Month Valley Usage
id: month_valley_usage
accuracy_decimals: 3
device_class: "energy"
state_class: "total_increasing"
icon: mdi:lightning-bolt
unit_of_measurement: kWh
update_interval: 15s
lambda:
return id(global_month_valley_usage) + id(global_today_valley_usage);
# 昨日、上月、今日、当月峰电量
- platform: template
name: ${node_name}_Yesterday Peak Usage
id: yesterday_peak_usage
accuracy_decimals: 3
device_class: "energy"
state_class: "total_increasing"
icon: mdi:lightning-bolt
unit_of_measurement: kWh
update_interval: 20s
lambda:
return id(global_yesterday_peak_usage);
- platform: template
name: ${node_name}_Last Month Peak Usage
id: last_month_peak_usage
accuracy_decimals: 3
device_class: "energy"
state_class: "total_increasing"
icon: mdi:lightning-bolt
unit_of_measurement: kWh
update_interval: 20s
lambda:
return id(global_last_month_peak_usage);
- platform: template
name: ${node_name}_Today Peak Usage
id: today_peak_usage
accuracy_decimals: 3
device_class: "energy"
state_class: "total_increasing"
icon: mdi:lightning-bolt
unit_of_measurement: kWh
update_interval: 15s
lambda:
return id(global_today_peak_usage);
- platform: template
name: ${node_name}_Month Peak Usage
id: monthly_peak_usage
accuracy_decimals: 3
device_class: "energy"
state_class: "total_increasing"
icon: mdi:lightning-bolt
unit_of_measurement: kWh
update_interval: 20s
lambda:
return id(global_month_peak_usage) + id(global_today_peak_usage);
# 昨日、上月、今日、当月电费
- platform: template
name: ${node_name}_yesterday_bill
id: yesterday_bill
accuracy_decimals: 3
unit_of_measurement: CNY
update_interval: 25s
lambda:
return id(global_yesterday_valley_bill)+id(global_yesterday_peak_bill);
- platform: template
name: ${node_name}_Last Month bill
id: last_month_bill
accuracy_decimals: 3
unit_of_measurement: CNY
update_interval: 25s
lambda:
return id(global_last_month_valley_bill)+id(global_last_month_peak_bill);
- platform: template
name: ${node_name}_today_bill
id: today_bill
accuracy_decimals: 3
unit_of_measurement: CNY
update_interval: 15s
lambda:
return id(global_today_valley_bill)+id(global_today_peak_bill);
- platform: template
name: ${node_name}_monthly_bill
id: monthly_bill
accuracy_decimals: 3
unit_of_measurement: CNY
update_interval: 15s
lambda:
return id(global_month_valley_bill)+id(global_month_peak_bill) + id(today_bill).state;
# 昨日、上月、今日、当月谷电费
- platform: template
name: ${node_name}_yesterday_valley_bill
id: yesterday_valley_bill
accuracy_decimals: 3
unit_of_measurement: CNY
update_interval: 25s
lambda:
return id(global_yesterday_valley_bill);
- platform: template
name: ${node_name}_Last Month Valley Bill
id: last_month_valley_bill
accuracy_decimals: 3
unit_of_measurement: CNY
update_interval: 25s
lambda:
return id(global_last_month_valley_bill);
- platform: template
name: ${node_name}_Today Valley Bill
id: today_valley_bill
accuracy_decimals: 3
unit_of_measurement: CNY
update_interval: 15s
lambda:
return id(global_today_valley_bill);
- platform: template
name: ${node_name}_Month Valley Bill
id: month_valley_bill
accuracy_decimals: 3
unit_of_measurement: CNY
update_interval: 25s
lambda:
return id(global_month_valley_bill) + id(global_today_valley_bill);
# 昨日、上月、今日、当月峰电费
- platform: template
name: ${node_name}_Yesterday Peak Bill
id: yesterday_peak_bill
unit_of_measurement: CNY
accuracy_decimals: 3
update_interval: 15s
lambda:
return id(global_yesterday_peak_bill);
- platform: template
name: ${node_name}_Last Month Peak Bill
id: last_month_peak_bill
accuracy_decimals: 3
unit_of_measurement: CNY
update_interval: 15s
lambda:
return id(global_last_month_peak_bill);
- platform: template
name: ${node_name}_Today Peak_bill
id: today_peak_bill
accuracy_decimals: 3
unit_of_measurement: CNY
update_interval: 15s
lambda:
return id(global_today_peak_bill);
- platform: template
name: ${node_name}_Month Peak Bill
id: month_peak_bill
accuracy_decimals: 3
unit_of_measurement: CNY
update_interval: 15s
lambda:
return id(global_month_peak_bill) + id(global_today_peak_bill);
time:
# - platform: homeassistant
# timezone: UTC-8
# id: time_ha
# - platform: homeassistant
# timezone: UTC-8
# id: time_ha
# on_time_sync: #执行之前强制同步时间
# then:
# - ds1307.write_time:
# - logger.log: "rtcokha"
- platform: ds1307
id: time_sntp
update_interval: never
timezone: UTC-8
- platform: sntp
id: time_sntp1
servers: ntp.aliyun.com #NTP服务器地址
timezone: UTC-8 #中国时区
on_time_sync: #执行之前强制同步时间
then:
- ds1307.write_time:
- logger.log: "rtcoksntp"
on_time:
# 昨日总电量,昨日抄表数,昨日总电费,今日总电费重置
# 当月峰电量,当月谷电量(昨日为止)
# 当月峰电费,当月谷电费(昨日为止)
# 昨日峰电量,今日峰电量重置
# 昨日谷电量,今日谷电量重置
# 昨日峰电费,今日峰电费重置
# 昨日谷电费,今日谷电费重置
- seconds: 0
minutes: 0
hours: 0
then:
- if:
condition:
time.has_time:
then:
- lambda: |-
id(global_yesterday_usage) = id(main_energy).state - id(global_yesterday_energy);
id(global_yesterday_energy) = id(main_energy).state;
id(yesterday_energy).publish_state( id(global_yesterday_energy) );
id(yesterday_usage).publish_state( id(global_yesterday_usage) );
id(today_bill).publish_state(0.000f);
id(global_month_peak_usage) = id(global_month_peak_usage) + id(global_today_peak_usage);
id(global_month_valley_usage) = id(global_month_valley_usage) + id(global_today_valley_usage);
id(global_month_peak_bill) = id(global_month_peak_bill) + id(global_today_peak_bill);
id(global_month_valley_bill) = id(global_month_valley_bill) + id(global_today_valley_bill);
id(global_yesterday_peak_usage) = id(global_today_peak_usage);
id(global_today_peak_usage) = 0.000f;
id(global_yesterday_valley_usage) = id(global_today_valley_usage);
id(global_today_valley_usage) = 0.000f;
id(global_yesterday_peak_bill) = id(global_today_peak_bill);
id(global_today_peak_bill) = 0.000f;
id(global_yesterday_valley_bill) = id(global_today_valley_bill);
id(global_today_valley_bill) = 0.000f;
# 上月电量, 表复位年度用电量
# 上月峰电量,上月谷电量, 本月峰电量重置,本月谷电量重置
# 上月峰电费,上月谷电费, 本月峰电费重置,本月谷电费重置
- seconds: 00
minutes: 00
hours: 00
days_of_month: 1
then:
- lambda: |-
id(global_last_month_usage) = id(main_energy).state;
id(global_reset_annual_usage) = id(global_reset_annual_usage) + id(global_last_month_usage);
id(last_monthly_usage).publish_state(id(global_last_month_usage));
id(reset_annual_usage).publish_state(id(global_reset_annual_usage));
id(actual_annual_usage).publish_state(id(global_reset_annual_usage));
id(global_last_month_peak_usage) = id(global_month_peak_usage);
id(global_month_peak_usage) = 0.000f;
id(global_last_month_valley_usage) = id(global_month_valley_usage);
id(global_month_valley_usage) = 0.000f;
id(global_last_month_peak_bill) = id(global_month_peak_bill);
id(global_month_peak_bill) = 0.000f;
id(global_last_month_valley_bill) = id(global_month_valley_bill);
id(global_month_valley_bill) = 0.000f;
id(global_last_energy)=0.000f;
//bug修复
id(global_yesterday_energy) = 0.000f;
- if:
condition:
time.has_time:
then:
- logger.log: "-->publish last monthly electricity."
- pzemac.reset_energy: pzemac_main
- delay: 2s
- pzemac.reset_energy: pzemac_main
- seconds: /5
then:
- if:
condition:
time.has_time:
then:
- lambda: |-
if(id(global_last_energy)==-0.001f) id(global_last_energy) = id(main_energy).state;
# 今日峰电量、峰电费、谷电量、谷电费
- seconds: 59
then:
- if:
condition:
time.has_time:
then:
- lambda: |-
if(id(global_last_energy)==-0.001f) id(global_last_energy) = id(main_energy).state;
id(current_price).update();
float price = id(current_price).state;
if(id(global_is_peak_price)){
id(global_today_peak_usage) = id(global_today_peak_usage) + id(main_energy).state - id(global_last_energy);
id(global_today_peak_bill) = id(global_today_peak_usage) * price;
id(today_peak_usage).publish_state(id(global_today_peak_usage));
id(today_peak_bill).publish_state(id(global_today_peak_bill));
}else{
id(global_today_valley_usage) = id(global_today_valley_usage) + id(main_energy).state - id(global_last_energy);
id(global_today_valley_bill) = id(global_today_valley_usage) * price;
id(today_valley_usage).publish_state(id(global_today_valley_usage));
id(today_valley_bill).publish_state(id(global_today_valley_bill));
}
id(today_bill).publish_state(id(global_today_peak_bill) + id(global_today_valley_bill));
id(global_last_energy) = id(main_energy).state;