aboutsummaryrefslogtreecommitdiffhomepage
path: root/kauf-plug.yaml
blob: bab9eacebe86cbae4c7921e7cd3e7a720e6e4d9f (plain)
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
# https://esphome.io/guides/configuration-types.html#substitutions
substitutions:

  # substitutions can be changed here if you are using this file directly in the ESPHome dashboard.  The better approach is
  # to incorporate this file as a package using the following packages: configuration, and then overwrite these substitutions
  # in your local yaml file by redefining them.
  #
  # packages:
  #   kauf.plf10: github://bkaufx/PLF10/kauf-plug.yaml
  #

  name: kauf-plug              # **** CHANGE DEVICE NAME TO SOMETHING UNIQUE PER DEVICE.  RENAME YAML FILE TO SAME NAME.    ****
                               # **** USE DASHES (-) INSTEAD OF SPACES OR UNDERSCORE (_).  USE ONLY LOWER CASE LETTERS.     ****
  friendly_name: Kauf Plug     # **** CHANGE FRIENDLY NAME TO SOMETHING UNIQUE PER DEVICE ****

  # https://esphome.io/components/esphome.html#esphome-creators-project
  project_name: Kauf.PLF10
  project_ver_num: "1.92"
  project_ver_let: y

  # https://esphome.io/components/switch/gpio.html?highlight=restore_mode
  sub_restore_mode: RESTORE_DEFAULT_OFF # overwrite to change boot up behavior of relay

  disable_entities: "true"     # set to "false" to have all entities show up in Home Assistant automatically


  # substitutions for button actions.  yaml dashboard import uses press, but update bin file uses hold_short to allow hold_long
  # to turn on wifi ap.  These substitutions can be overwritten in the user's yaml file to do anything.
  sub_on_press:         script_start_timer # executes right when button is initially pressed
  sub_on_release:       script_stop_timer  # executes right when button is released
  sub_on_release_quick: script_do_nothing  # executes when button is released after being held under 750ms
  sub_on_release_short: script_do_nothing  # executes when button is released after being held between 750ms and 5s
  sub_on_release_long:  script_do_nothing  # executes when button is released after being held between 5s and 60s
  # For a longer hold, see kauf-plug-update.yaml in the config-update directory.
  # You have to start a timer in the on_press script and cancel the timer in the on_release script.
  # or if you wanted to require release, then start a counter on press and check the value on release.
  # If you want to have a hold action, you probably want to toggle on release_quick instead of on_press so that
  # you can perform the hold action without toggling the plug's relay.


  # substitutions for power monitoring calibration.  Allows end users to change calibration in their yaml and still
  # incorporate this file as a package to get all the updates we release.
  current_resistor_val: "0.001"
  voltage_divider_val:  "2401"
  power_cal_val1_in:    "0.0"
  power_cal_val1_out:   "0.0"
  power_cal_val2_in:    "333.8"
  power_cal_val2_out:   "60"
  current_cal_val1_in:  "0.0"
  current_cal_val1_out: "0.0"
  current_cal_val2_in:  "0.6"
  current_cal_val2_out: "0.515"
  voltage_cal_val1_in:  "0.0"
  voltage_cal_val1_out: "0.0"
  voltage_cal_val2_in:  "302.1"
  voltage_cal_val2_out: "117.1"

  # set power monitoring mode in yaml, selecting the "yaml_configured" option in the select entity will cause these
  # values to be used after next reboot.
  # https://esphome.io/components/sensor/hlw8012.html#configuration-variables
  sub_change_mode_every: "1"
  sub_update_interval: 10s
  sub_initial_mode: CURRENT

  # an extra script that, if running, will stop the relay from toggling on button release.
  # used to not toggle when WiFi AP is re-enabled on update bin file.
  sub_toggle_check: script_start_timer


# https://esphome.io/components/esp8266.html
esp8266:
  board: esp01_1m
  restore_from_flash: true
  start_free: 74
  global_addr: global_forced_addr


# https://esphome.io/guides/automations.html#global-variables
globals:
  - id: global_forced_addr
    type: int
    restore_value: no
    initial_value: '12345'


# https://esphome.io/components/esphome.html#adjusting-flash-writes
preferences:
  flash_write_interval: 3s


# https://esphome.io/components/esphome.html
esphome:
  name: $name

  project:
    name: $project_name
    version: $project_ver_num($project_ver_let)

  on_boot:
    priority: 700
    then:

      # priority 700 ensures power monitoring mode variables are changed
      # in this script before HLW component is setup
      # hlw_main will already have the YAML configured values by default, so
      # no need to handle that here.
      - lambda: |-

          if ( id(select_monitor_mode).state == "10s P / 40s V,I") {
            id(hlw_main)->set_update_interval(10000);
            id(hlw_main)->set_change_mode_every(1); }

          else if ( id(select_monitor_mode).state == "10s P,I Only" ) {
            id(hlw_main)->set_update_interval(10000);
            id(hlw_main)->set_change_mode_every(4294967295);
            id(hlw_main)->set_initial_mode(hlw8012::HLW8012_INITIAL_MODE_CURRENT); }

          else if ( id(select_monitor_mode).state == "10s P,V Only" ) {
            id(hlw_main)->set_update_interval(10000);
            id(hlw_main)->set_change_mode_every(4294967295);
            id(hlw_main)->set_initial_mode(hlw8012::HLW8012_INITIAL_MODE_VOLTAGE); }

          else if ( id(select_monitor_mode).state == " 2s P,I Only" ) {
            id(hlw_main)->set_update_interval(2000);
            id(hlw_main)->set_change_mode_every(4294967295);
            id(hlw_main)->set_initial_mode(hlw8012::HLW8012_INITIAL_MODE_CURRENT); }

          else if ( id(select_monitor_mode).state == " 2s P,V Only" ) {
            id(hlw_main)->set_update_interval(2000);
            id(hlw_main)->set_change_mode_every(4294967295);
            id(hlw_main)->set_initial_mode(hlw8012::HLW8012_INITIAL_MODE_VOLTAGE); } 


# https://esphome.io/components/external_components.html
external_components:
  - source:
      type: git
      url: https://github.com/KaufHA/common
    refresh: 0s


# https://esphome.io/components/wifi.html
wifi:

  # **** ENTER WI-FI CREDENTIALS HERE, USING SECRETS.YAML RECOMMENDED ****
  ssid: initial_ap            # !secret wifi_ssid
  password: asdfasdfasdfasdf  # !secret wifi_password

  # default is 20, 17 is recommended.
  output_power: 17

  # using fast_connect as default since it is required for hidden networks.
  fast_connect: true

  forced_addr: 8
  global_addr: global_forced_addr


# https://esphome.io/components/logger.html
logger:


# https://esphome.io/components/api.html
api:
  id: kauf_api


# https://esphome.io/components/ota.html
ota:
  on_error:
    then:
      - button.press: restart_button


# https://esphome.io/components/web_server.html
web_server:
  local: true


# https://esphome.io/components/binary_sensor/index.html
binary_sensor:  

    # button input toggles relay and thereby power led
    # https://esphome.io/components/binary_sensor/gpio.html
  - platform: gpio
    id: button_in
    name: $friendly_name Button
    pin:
      number: GPIO13
      mode:
        input: true
        pullup: true
      inverted: true

    on_press:
        then:
          - script.execute: $sub_on_press
    on_release:
        then:
          - script.execute: $sub_on_release
    on_click:
      - min_length: 20ms
        max_length: 750ms
        then:
          - script.execute: $sub_on_release_quick
      - min_length: 750ms
        max_length: 5s
        then:
          - script.execute: $sub_on_release_short
      - min_length: 5s
        max_length: 60s
        then:
          - script.execute: $sub_on_release_long

    # indicates whether plugged-in device is running based on configurable threshold.
    # https://esphome.io/components/binary_sensor/template.html
  - platform: template
    id: in_use
    name: ${friendly_name} Device In Use


# https://esphome.io/guides/automations.html#script-component
script:

    # sets LEDs to proper state based on LED configuration and relay state
  - id: script_set_power_leds
    then:
      - lambda: |-

          auto call = id(blue_led).make_call();

          // blue power status
          if ( (id(select_bled).state == "Power Status") || (id(select_bled).state == "Error and Power") ) {
            call.set_state(id(relay).state);
          }

          // blue invert power status
          else if ( (id(select_bled).state == "Invert Power Status") || (id(select_bled).state == "Error and Invert Power") ) {
            call.set_state(!id(relay).state);
          }

          call.perform();


          call = id(red_led).make_call();

          // red power status
          if ( (id(select_rled).state == "Power Status") || (id(select_rled).state == "Error and Power") ) {
            call.set_state(id(relay).state);
          }

          // red invert power status
          else if ( (id(select_rled).state == "Invert Power Status") || (id(select_rled).state == "Error and Invert Power") ) {
            call.set_state(!id(relay).state);
          }

          call.perform();

  - id: blink_status_led
    mode: queued
    then:

      - lambda: |-

          // turn on blue LED if configured for error status
          if ( (id(select_bled).state == "Error Status")  ||
               (id(select_bled).state == "Error and Power") ||
               (id(select_bled).state == "Error and Invert Power") ) {
            auto call = id(blue_led).turn_on();
            call.set_save(false);
            call.perform();
          }

          // turn on red LED if configured for error status
          if ( (id(select_rled).state == "Error Status")  ||
               (id(select_rled).state == "Error and Power") ||
               (id(select_rled).state == "Error and Invert Power") ) {
            auto call = id(red_led).turn_on();
            call.set_save(false);
            call.perform();
          }

      - delay: 350ms

      - lambda: |-

          // turn off blue LED if configured for error status
          if ( (id(select_bled).state == "Error Status")  ||
               (id(select_bled).state == "Error and Power") ||
               (id(select_bled).state == "Error and Invert Power") ) {
            auto call = id(blue_led).turn_off();
            call.set_save(false);
            call.perform();
          }

          // turn off red LED if configured for error status
          if ( (id(select_rled).state == "Error Status")  ||
               (id(select_rled).state == "Error and Power") ||
               (id(select_rled).state == "Error and Invert Power") ) {
            auto call = id(red_led).turn_off();
            call.set_save(false);
            call.perform();
          }

      - delay: 1150ms

      - if:
          condition:
            - lambda: return ( ( (App.get_app_state() & STATUS_LED_ERROR) != 0u) || ((App.get_app_state() & STATUS_LED_WARNING) != 0u) );
          then:
            - script.execute: blink_status_led      # repeat as long as error/warning exists
          else:
            - script.execute: script_set_power_leds # done with status LED, restore light power status

    ############################################################################
    # scripts for different things the button can do:                          #
    #  * start timer (on press)                                                #
    #  * stop timer  (on release)                                              #
    #  * nothing                                                               #
    ############################################################################
  - id: script_start_timer
    then:

        # variable delay before toggling plug
      - delay: !lambda |-
          if      ( id(select_button).state == "Toggle if Pressed For 20ms" )
            return 20;
          else if ( id(select_button).state == "Toggle if Released After 20ms" )
            return 20;
          else if ( id(select_button).state == "Toggle if Pressed For 100ms" )
            return 100;
          else if ( id(select_button).state == "Toggle if Released After 100ms" )
            return 100;
          else if ( id(select_button).state == "Toggle if Pressed For 250ms" )
            return 250;
          else if ( id(select_button).state == "Toggle if Released After 250ms" )
            return 250;
          else
            return 1000;

        # toggle immediately after delay if a "pressed" option is selected
      - if:
          condition:
            lambda: |-
                std::size_t found = id(select_button).state.find("Press");
                return (found != std::string::npos);
          then:
            - switch.toggle: relay

  - id: script_stop_timer
    then:
      
      - if:
          condition:    # potentially toggle if a "released" option is selected
            lambda: |-
                std::size_t found = id(select_button).state.find("Release");
                return (found != std::string::npos);
          then:
          
            - if:   
                condition:
                  or:  # do nothing if timer is still running or toggle check script is running.
                    - script.is_running: script_start_timer
                    - script.is_running: $sub_toggle_check
                then:
                  - script.execute: script_do_nothing
                else:  # otherwise toggle relay
                  - switch.toggle: relay

        # in all cases, stop timer on button release.
      - script.stop: script_start_timer

  - id: script_do_nothing
    then:
      - lambda: return;


# pwm outputs for LEDs so they can be dimmed
# https://esphome.io/components/output/esp8266_pwm.html
output:
  - platform: esp8266_pwm
    id: blue_led_pwm
    frequency: 1000 Hz
    pin: GPIO2
    inverted: true
  - platform: esp8266_pwm
    id: red_led_pwm
    frequency: 1000 Hz
    pin: GPIO0
    inverted: true


# https://esphome.io/components/light/index.html
# https://esphome.io/components/light/monochromatic.html
light:
  - platform: monochromatic
    name: $friendly_name Blue LED
    id: blue_led
    output: blue_led_pwm
    entity_category: config
    default_transition_length: 0s
    forced_hash: 3831461887
    forced_addr: 44
    global_addr: global_forced_addr
    effects:
      - flicker:
          name: Flicker
          alpha: 90%
          intensity: 10%

  - platform: monochromatic
    name: $friendly_name Red LED
    id: red_led
    output: red_led_pwm
    entity_category: config
    default_transition_length: 0s
    forced_hash: 261191305
    forced_addr: 62
    global_addr: global_forced_addr
    effects:
      - flicker:
          name: Flicker
          alpha: 90%
          intensity: 10%


# https://esphome.io/components/switch/index.html
switch:    

    # relay output
    # https://esphome.io/components/switch/gpio.html
  - platform: gpio
    id: relay
    name: $friendly_name
    pin: GPIO4
    entity_category: ''
    forced_hash: 41191675
    forced_addr: 2
    global_addr: global_forced_addr
    restore_mode: $sub_restore_mode

    on_turn_on:
        - script.execute: script_set_power_leds
    on_turn_off:
        - script.execute: script_set_power_leds


    # https://esphome.io/components/switch/template.html
  - platform: template
    id: switch_no_hass
    name: $friendly_name No HASS
    optimistic: true
    restore_state: true
    entity_category: config
    disabled_by_default: $disable_entities
    icon: mdi:toggle-switch-off-outline
    on_turn_on:
      - lambda: |-
          id(kauf_api)->set_reboot_timeout(0);  // 0 disables auto rebooting and also new status led blinking.  doesn't stop current status led blinking
          id(kauf_api)->status_clear_warning(); // stops current status led blinking, timeout 0 keeps it from restarting
    on_turn_off:
      - lambda: id(kauf_api)->set_reboot_timeout(900000);
    forced_hash: 657159011
    forced_addr: 42
    global_addr: global_forced_addr


# https://esphome.io/components/button/index.html
# https://esphome.io/components/button/restart.html
button:
  - platform: restart
    id: restart_button
    name: $friendly_name Restart Firmware
    entity_category: diagnostic
    disabled_by_default: $disable_entities


# clock input from Home Assistant used to calculate total daily energy
# https://esphome.io/components/time.html#home-assistant-time-source
time:
  - platform: homeassistant
    id: homeassistant_time


# https://esphome.io/components/sensor/index.html
sensor:   # Power monitoring sensors output to Home Assistant

    # https://esphome.io/components/sensor/hlw8012.html
  - platform: hlw8012
    id: hlw_main
    sel_pin:
      number: GPIO12
      inverted: True
    cf_pin: GPIO5
    cf1_pin: GPIO14
    current_resistor: $current_resistor_val
    voltage_divider: $voltage_divider_val

    change_mode_every: $sub_change_mode_every 
    update_interval: $sub_update_interval 
    initial_mode: $sub_initial_mode

    power:
      name: ${friendly_name} Power
      unit_of_measurement: W
      id: wattage
      filters:
        - calibrate_linear:
            - $power_cal_val1_in -> $power_cal_val1_out
            - $power_cal_val2_in -> $power_cal_val2_out
        - lambda: return x * id(scale_power).state/100.0f;
      on_value:   # set or clear in_use template binary sensor depending on
        - if:     # whether power usage is over threshold
            condition:
              lambda: return (x >= id(threshold).state);
            then:
              - binary_sensor.template.publish:
                  id: in_use
                  state: ON
            else:
              - binary_sensor.template.publish:
                  id: in_use
                  state: OFF

    current:
      name: ${friendly_name} Current
      unit_of_measurement: A
      id: current
      filters:
        - calibrate_linear:
            - $current_cal_val1_in -> $current_cal_val1_out
            - $current_cal_val2_in -> $current_cal_val2_out
        - lambda: return x * id(scale_current).state/100.0f;

    voltage:
      name: ${friendly_name} Voltage
      unit_of_measurement: V
      id: voltage
      filters:
        - calibrate_linear:
            - $voltage_cal_val1_in -> $voltage_cal_val1_out
            - $voltage_cal_val2_in -> $voltage_cal_val2_out
        - lambda: return x * id(scale_voltage).state/100.0f;


    # Reports the total Power so-far each day, resets at midnight
    # https://esphome.io/components/sensor/total_daily_energy.html
  - platform: total_daily_energy
    name: ${friendly_name} Total Daily Energy
    power_id: wattage
    filters:
        - multiply: 0.001  ## convert Wh to kWh
    unit_of_measurement: kWh
    min_save_interval: 5min
    forced_hash: 1903527169
    forced_addr: 6
    global_addr: global_forced_addr

    # https://esphome.io/components/sensor/uptime.html
  - platform: uptime
    name: $friendly_name Uptime
    update_interval: 60s
    entity_category: diagnostic
    disabled_by_default: $disable_entities


# https://esphome.io/components/number/index.html
# https://esphome.io/components/number/template.html
number:      # used as a threshold for whether the plugged-in devices is running.
  - platform: template
    name: ${friendly_name} Use Threshold
    min_value: 1
    max_value: 100
    step: 1
    initial_value: 3
    id: threshold
    entity_category: config
    optimistic: true
    restore_value: true
    unit_of_measurement: Watt(s)
    mode: box
    disabled_by_default: $disable_entities
    forced_hash: 3932521563
    forced_addr: 4
    global_addr: global_forced_addr
    on_value:        # set or clear in_use template binary sensor depending on
      - if:          # whether power usage is above threshold
          condition:
            lambda: return (id(wattage).state >= x);
          then:
            - binary_sensor.template.publish:
                id: in_use
                state: ON
          else:
            - binary_sensor.template.publish:
                id: in_use
                state: OFF

  - platform: template
    name: ${friendly_name} Scale Power
    min_value: 50
    max_value: 200
    step: .1
    initial_value: 100
    id: scale_power
    entity_category: config
    optimistic: true
    restore_value: true
    unit_of_measurement: "%"
    mode: box
    disabled_by_default: $disable_entities
    forced_hash: 3565176138
    forced_addr: 56
    global_addr: global_forced_addr
    on_value:
      - lambda: id(wattage)->publish_state(id(wattage)->get_raw_state());

  - platform: template
    name: ${friendly_name} Scale Current
    min_value: 50
    max_value: 200
    step: .1
    initial_value: 100
    id: scale_current
    entity_category: config
    optimistic: true
    restore_value: true
    unit_of_measurement: "%"
    mode: box
    disabled_by_default: $disable_entities
    forced_hash: 2293595686
    forced_addr: 58
    global_addr: global_forced_addr
    on_value:
      - lambda: id(current)->publish_state(id(current)->get_raw_state());

  - platform: template
    name: ${friendly_name} Scale Voltage
    min_value: 50
    max_value: 200
    step: .1
    initial_value: 100
    id: scale_voltage
    entity_category: config
    optimistic: true
    restore_value: true
    unit_of_measurement: "%"
    mode: box
    disabled_by_default: $disable_entities
    forced_hash: 254525215
    forced_addr: 60
    global_addr: global_forced_addr
    on_value:
      - lambda: id(voltage)->publish_state(id(voltage)->get_raw_state());
  

# https://esphome.io/components/select/index.html
# https://esphome.io/components/select/template.html
select:

    # option to disable button
  - platform: template
    name: $friendly_name Button Config
    id: select_button
    optimistic: true
    options:
      - Toggle if Pressed For 20ms
      - Disable Button
      - Toggle if Pressed For 100ms
      - Toggle if Pressed For 250ms
      - Toggle if Pressed For 1s
      - Toggle if Released After 20ms
      - Toggle if Released After 100ms
      - Toggle if Released After 250ms
      - Toggle if Released After 1s
    initial_option: Toggle if Pressed For 20ms
    restore_value: true
    icon: mdi:circle-double
    entity_category: config
    forced_hash: 3616613942
    forced_addr: 34
    global_addr: global_forced_addr

  - platform: template
    name: $friendly_name Blue LED
    id: select_bled
    optimistic: true
    entity_category: config
    options:
      - Power Status
      - No Automation
      - Invert Power Status
      - Error Status
      - Error and Power
      - Error and Invert Power
    initial_option: Power Status
    restore_value: true
    icon: mdi:led-on
    forced_hash: 3104663617
    forced_addr: 36
    global_addr: global_forced_addr
    
    on_value:
      then:
        - if:
            condition:
              - lambda: return ( x == "Error Status" );
            then:
              - light.turn_off: blue_led
        - script.execute: script_set_power_leds

  - platform: template
    name: $friendly_name Red LED
    id: select_rled
    optimistic: true
    entity_category: config
    options:
      - Power Status
      - No Automation
      - Invert Power Status
      - Error Status
      - Error and Power
      - Error and Invert Power
    initial_option: Error Status
    restore_value: true
    icon: mdi:led-on
    forced_hash: 261191305
    forced_addr: 38
    global_addr: global_forced_addr
    
    on_value:
      then:
        - if:
            condition:
              - lambda: return ( x == "Error Status" );
            then:
              - light.turn_off: red_led

        - script.execute: script_set_power_leds

  # change mode of power monitoring
  - platform: template
    name: $friendly_name Monitoring Mode
    id: select_monitor_mode
    optimistic: true
    options:
      - "10s P / 40s V,I"
      - "10s P,I Only"
      - "10s P,V Only"
      - " 2s P,I Only"
      - " 2s P,V Only"
      - "YAML Configured"
    initial_option: "10s P / 40s V,I"
    restore_value: true
    icon: mdi:wrench-clock
    entity_category: config
    disabled_by_default: $disable_entities

    forced_hash: 1025003090
    forced_addr: 0
    global_addr: global_forced_addr
    
    set_action:
      - delay: 1s
      - lambda: global_preferences->sync();
      - delay: 2s
      - button.press: restart_button


# Send IP Address to HA
# https://esphome.io/components/text_sensor/wifi_info.html
text_sensor:
  - platform: wifi_info
    ip_address:
      name: $friendly_name IP Address
      disabled_by_default: $disable_entities


# emulate status_led
interval:
  - interval: 5s
    then:

    - if:
        condition:
          - lambda: return ( ( (App.get_app_state() & STATUS_LED_ERROR) != 0u) || ((App.get_app_state() & STATUS_LED_WARNING) != 0u) );
        then:
          - if:
              condition:
                not:
                  - script.is_running: blink_status_led
              then:
                - script.execute: blink_status_led



# Current reserved flash memory:
# 00-01: Power Monitoring Mode
# 02-03: Relay output
# 04-05: Use Threshold
# 06-07: Total Daily Energy
# 08-33: Wi-Fi Credentials
# 34-35: Disable Button select entity
# 36-37: Blue LED select entity
# 38-39: Red LED select entity

# 42-43: No HASS switch
# 44-55: Blue LED light entity
# 56-57: Scale Power
# 58-59: Scale Current
# 60-61: Scale Voltage
# 62-73: Red LED light entity