diff options
-rw-r--r-- | kauf-plug-lite.yaml | 233 | ||||
-rw-r--r-- | kauf-plug-minimal.yaml | 23 | ||||
-rw-r--r-- | kauf-plug.yaml | 1 |
3 files changed, 113 insertions, 144 deletions
diff --git a/kauf-plug-lite.yaml b/kauf-plug-lite.yaml index 1c3fe3d..3807828 100644 --- a/kauf-plug-lite.yaml +++ b/kauf-plug-lite.yaml @@ -15,7 +15,7 @@ substitutions: # https://esphome.io/components/esphome.html#esphome-creators-project project_name: Kauf.PLF10 - project_ver_num: "1.96" + project_ver_num: "1.97" project_ver_let: l # https://esphome.io/components/switch/gpio.html?highlight=restore_mode @@ -30,12 +30,15 @@ substitutions: # * have a delay for the desired hold in the on_press script # * stop the on_press script in the on_release script # * place actions in the on_press script after the delay to perform them while still holding the button - # * place actions in the on_release script, with the condition that the on_press script is not running, to + # * place actions in the on_release script, with the condition that the on_press script is not running, to # perform them on release of the button. sub_on_press: script_do_nothing # executes right when button is initially pressed sub_on_release: script_do_nothing # executes right when button is released sub_on_hold_5s: script_do_nothing # executes right when button has been held for 5s, while button is still being held + sub_on_turn_on: script_do_nothing + sub_on_turn_off: script_do_nothing + # 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_do_nothing @@ -81,6 +84,14 @@ globals: type: int restore_value: no initial_value: '0' + - id: global_has_debounce + type: boolean + restore_value: no + initial_value: "false" + - id: global_has_powermode + type: boolean + restore_value: no + initial_value: "false" # https://esphome.io/components/esphome.html#adjusting-flash-writes @@ -96,13 +107,6 @@ esphome: name: $project_name version: $project_ver_num($project_ver_let) - on_boot: - priority: 700 - then: - # implementing on_boot function as a script so that any other on_boot automations in other files - # will execute in parallel and not be blocked by this one. - - script.execute: on_boot_main - # https://esphome.io/components/wifi.html wifi: @@ -130,8 +134,7 @@ api: # https://esphome.io/components/ota.html ota: on_error: - then: - - button.press: restart_button + - button.press: restart_button # https://esphome.io/components/web_server.html @@ -140,7 +143,7 @@ web_server: # https://esphome.io/components/binary_sensor/index.html -binary_sensor: +binary_sensor: # button input toggles relay and thereby power led # https://esphome.io/components/binary_sensor/gpio.html @@ -154,29 +157,27 @@ binary_sensor: pullup: true inverted: true on_press: - then: - - lambda: |- - // store time of press and clear duration sensor - id(global_press_time) = millis(); - id(sensor_press_duration).publish_state(0); - - // toggle if configured for toggle on press - if (id(select_button).state == "Toggle on Press") { - id(relay).toggle(); } - - script.execute: $sub_on_press - - script.execute: script_5s_timer + - lambda: |- + // store time of press and clear duration sensor + id(global_press_time) = millis(); + id(sensor_press_duration).publish_state(0); + + // toggle if configured for toggle on press + if (id(select_button).state == "Toggle on Press") { + id(relay).toggle(); } + - script.execute: $sub_on_press + - script.execute: script_5s_timer on_release: - then: - - lambda: |- - // set duration sensor - id(sensor_press_duration).publish_state(millis() - id(global_press_time) + id(number_debounce).state); - - // toggle if configured on release and toggle check script is not running. - if ( (id(select_button).state == "Toggle on Release") && !id($sub_toggle_check).is_running() ) { - id(relay).toggle(); - } - - script.execute: $sub_on_release - - script.stop: script_5s_timer + - lambda: |- + // set duration sensor + id(sensor_press_duration).publish_state(millis() - id(global_press_time) + id(number_debounce).state); + + // toggle if configured on release and toggle check script is not running. + if ( (id(select_button).state == "Toggle on Release") && !id($sub_toggle_check).is_running() ) { + id(relay).toggle(); + } + - script.execute: $sub_on_release + - script.stop: script_5s_timer # indicates whether plugged-in device is running based on configurable threshold. # https://esphome.io/components/binary_sensor/template.html @@ -272,16 +273,12 @@ script: else: - script.execute: script_set_power_leds # done with status LED, restore light power status - - id: script_new_debounce - mode: restart # only reboot plug once number is static for 10s. changing value again restarts timer. - then: # only do it if wifi is connected, keeps initial loading of value on boot from causing restart. - - if: - condition: - - wifi.connected - then: - - lambda: ESP_LOGD("kauf-plug.yaml","New debounce value detected, rebooting in 10 seconds to effect change."); - - delay: 10s - - button.press: restart_button + - id: script_setting_reboot + mode: restart # only reboot plug settings are static for 10s. Another change restarts timer. + then: + - lambda: ESP_LOGD("kauf-plug.yaml","Setting change requiring reboot detected, rebooting in 10 seconds to effect change."); + - delay: 10s + - button.press: restart_button - id: script_5s_timer then: @@ -292,60 +289,6 @@ script: then: - lambda: return; - - id: on_boot_main - then: - - # this on_boot script has to execute after select and number entities are set up, so - # priority < 800. Adding a check here to make sure all needed entities are set up just - # in case another yaml file makes on_boot priority higher than 800 the following code - # will still work right. If on_boot priority becomes <= 600, the code for setting up - # power monitoring mode won't work because HLW will already be set up before values are - # changed in this script. - - wait_until: - lambda: return (id(select_monitor_mode).has_state() ); - - - lambda: |- - - ////////////////////////////////////////////////////////////////////// - // implement power monitoring mode per select entity - - 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); } - - ////////////////////////////////////////////////////////////////////// - // add a delayed_on filter to the button binary_sensor with time argument from number_debounce. - // this is placed after settings recovery so that it will take into account recovered setting - // on first boot. In any case, needs to be after priority 800 so that number_debounce is set up. - - binary_sensor::DelayedOnFilter *button_delayedonfilter; - button_delayedonfilter = new binary_sensor::DelayedOnFilter(id(number_debounce).state); - button_delayedonfilter->set_component_source("binary_sensor"); - App.register_component(button_delayedonfilter); - id(button_in).add_filters({button_delayedonfilter}); - - - # pwm outputs for LEDs so they can be dimmed # https://esphome.io/components/output/esp8266_pwm.html @@ -392,7 +335,7 @@ light: # https://esphome.io/components/switch/index.html -switch: +switch: # relay output # https://esphome.io/components/switch/gpio.html @@ -404,8 +347,10 @@ switch: restore_mode: $sub_restore_mode on_turn_on: - script.execute: script_set_power_leds + - script.execute: $sub_on_turn_on on_turn_off: - script.execute: script_set_power_leds + - script.execute: $sub_on_turn_off # https://esphome.io/components/switch/template.html - platform: template @@ -455,8 +400,8 @@ sensor: # Power monitoring sensors output to Home Assistant current_resistor: $current_resistor_val voltage_divider: $voltage_divider_val - change_mode_every: $sub_change_mode_every - update_interval: $sub_update_interval + change_mode_every: $sub_change_mode_every + update_interval: $sub_update_interval initial_mode: $sub_initial_mode power: @@ -598,8 +543,19 @@ number: # used as a threshold for whether the plugged-in devices is running unit_of_measurement: "ms" mode: box disabled_by_default: $disable_entities - on_value: # currently, have to reboot plug to apply new delayed_on time - - script.execute: script_new_debounce + set_action: # currently rebooting plug to apply new delayed_on time + - script.execute: script_setting_reboot + on_value: + - lambda: |- + // add a delayed_on filter to the button binary_sensor with time argument from number_debounce. + if ( !id(global_has_debounce) ) { + binary_sensor::DelayedOnFilter *button_delayedonfilter; + button_delayedonfilter = new binary_sensor::DelayedOnFilter(id(number_debounce).state); + button_delayedonfilter->set_component_source("binary_sensor"); + App.register_component(button_delayedonfilter); + id(button_in).add_filters({button_delayedonfilter}); + id(global_has_debounce) = true; + } # https://esphome.io/components/select/index.html # https://esphome.io/components/select/template.html @@ -634,13 +590,12 @@ select: restore_value: true icon: mdi:led-on on_value: - then: - - if: - condition: - - lambda: return ( x == "Error Status" ); - then: - - light.turn_off: blue_led - - script.execute: script_set_power_leds + - 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 Config @@ -658,13 +613,12 @@ select: restore_value: true icon: mdi:led-on on_value: - then: - - if: - condition: - - lambda: return ( x == "Error Status" ); - then: - - light.turn_off: red_led - - script.execute: script_set_power_leds + - 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 @@ -684,10 +638,31 @@ select: entity_category: config disabled_by_default: $disable_entities set_action: - - delay: 1s - - lambda: global_preferences->sync(); - - delay: 2s - - button.press: restart_button + - script.execute: script_setting_reboot + on_value: + - lambda: |- + if ( !id(global_has_powermode) ) { + if ( x == "10s P / 40s V,I") { + id(hlw_main).set_update_interval(10000); + id(hlw_main).set_change_mode_every(1); } + else if ( x == "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 ( x == "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 ( x == " 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 ( x == " 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); } + id(global_has_powermode) = true; + } # Send IP Address to HA @@ -704,14 +679,8 @@ text_sensor: 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 - + - lambda: |- + if ( ( ((App.get_app_state() & STATUS_LED_ERROR ) != 0u) || + ((App.get_app_state() & STATUS_LED_WARNING) != 0u) ) + && !id(blink_status_led).is_running() ) + id(blink_status_led).execute(); diff --git a/kauf-plug-minimal.yaml b/kauf-plug-minimal.yaml index ff2aaa3..9d6292e 100644 --- a/kauf-plug-minimal.yaml +++ b/kauf-plug-minimal.yaml @@ -2,7 +2,7 @@ substitutions: 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 **** @@ -17,7 +17,7 @@ esphome: project: name: "Kauf.PLF10" - version: "1.96(m)" + version: "1.97(m)" wifi: @@ -25,7 +25,7 @@ wifi: # **** ENTER WI-FI CREDENTIALS HERE, USING SECRETS.YAML RECOMMENDED **** ssid: initial_ap # !secret my_wifi_ssid password: asdfasdfasdfasdf # !secret my_wifi_pass - + # Uncomment below to set a static IP # manual_ip: # static_ip: !secret kauf_bulb_ip_address @@ -61,8 +61,8 @@ status_led: number: GPIO0 inverted: true - -binary_sensor: + +binary_sensor: # button input toggles relay and thereby blue led - platform: gpio @@ -83,8 +83,7 @@ binary_sensor: switch.toggle: relay - -switch: +switch: # blue LED follows relay power state - platform: gpio @@ -98,7 +97,7 @@ switch: id: relay name: $friendly_name pin: GPIO4 - + # automatically make blue led equal relay state on_turn_on: - switch.turn_on: blue_led @@ -119,15 +118,15 @@ sensor: # Power monitoring sensors output to Home Assistant inverted: True cf_pin: GPIO5 cf1_pin: GPIO14 - current_resistor: 0.001 # The value of the shunt resistor for current measurement. - voltage_divider: 2401 # The value of the voltage divider on the board as (R_upstream + R_downstream) / R_downstream. + current_resistor: 0.001 # The value of the shunt resistor for current measurement. + voltage_divider: 2401 # The value of the voltage divider on the board as (R_upstream + R_downstream) / R_downstream. power: name: ${friendly_name} Power unit_of_measurement: W id: wattage filters: - calibrate_linear: - - 0.0 -> 0.0 + - 0.0 -> 0.0 - 333.8 -> 60 # value with 60W bulb. current: name: ${friendly_name} Current @@ -143,7 +142,7 @@ sensor: # Power monitoring sensors output to Home Assistant - calibrate_linear: - 0.0 -> 0.0 - 302.1 -> 117.1 # Tested using a meter - change_mode_every: 1 + change_mode_every: 1 update_interval: 10s # 20 second effective update rate for Power, 40 second for Current and Voltage. # Reports the total Power so-far each day, resets at midnight diff --git a/kauf-plug.yaml b/kauf-plug.yaml index e671626..05aca50 100644 --- a/kauf-plug.yaml +++ b/kauf-plug.yaml @@ -779,6 +779,7 @@ select: id(global_has_powermode) = true; } + # Send IP Address to HA # https://esphome.io/components/text_sensor/wifi_info.html text_sensor: |