From 1837da10be2125baabc1a15a64719cd3d3deb150 Mon Sep 17 00:00:00 2001 From: KaufHA Date: Thu, 20 Oct 2022 08:49:42 -0700 Subject: update lite and minimal --- kauf-plug-lite.yaml | 136 ++++++++++++++++++++++++++++++++++++++++--------- kauf-plug-minimal.yaml | 96 ++++++++++++++++++++++------------ kauf-plug.yaml | 2 +- 3 files changed, 176 insertions(+), 58 deletions(-) diff --git a/kauf-plug-lite.yaml b/kauf-plug-lite.yaml index 3807828..868456d 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.97" + project_ver_num: "1.98" project_ver_let: l # https://esphome.io/components/switch/gpio.html?highlight=restore_mode @@ -24,7 +24,7 @@ substitutions: disable_entities: "true" # set to "false" to have all entities show up in Home Assistant automatically # substitutions for button actions. on_press and on_release implement a timer scheme with configurable delay. - # on_hold_5s re-enables the AP and captive portal for the precompiled update binary. + # on_hold_30s re-enables the AP and captive portal for the precompiled update binary. # any length of hold can be implemented with just on_press and on_release using the following directions. This # is basically how this yaml file works. # * have a delay for the desired hold in the on_press script @@ -34,14 +34,14 @@ substitutions: # 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_hold_30s: script_force_ap # 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 + sub_toggle_check: script_force_ap # made this a substitution so that the update bin file and yaml compiled versions can have different defaults default_button_config: "Toggle on Press" @@ -50,6 +50,7 @@ substitutions: # incorporate this file as a package to get all the updates we release. current_resistor_val: "0.001" voltage_divider_val: "2401" + sub_hlw_model: HLW8012 power_cal_val1_in: "0.0" power_cal_val1_out: "0.0" power_cal_val2_in: "333.8" @@ -70,6 +71,19 @@ substitutions: sub_update_interval: 10s sub_initial_mode: CURRENT + # GPIO definitions + sub_pm_sel_pin: GPIO12 + sub_pm_cf_pin: GPIO5 + sub_pm_cf1_pin: GPIO14 + sub_button_pin: GPIO13 + sub_blue_led_pin: GPIO2 + sub_red_led_pin: GPIO0 + sub_relay_pin: GPIO4 + + # debounce specifications + sub_default_debounce: "100" + sub_min_debounce: "50" + # https://esphome.io/components/esp8266.html esp8266: @@ -96,7 +110,10 @@ globals: # https://esphome.io/components/esphome.html#adjusting-flash-writes preferences: - flash_write_interval: 3s + # setting interval to 5 minutes since this defines writing total daily energy to flash, which will occur + # every interval length 24/7/365. Created a specific save script that is executed for things that need + # to save more quickly. + flash_write_interval: 5min # https://esphome.io/components/esphome.html @@ -107,6 +124,12 @@ esphome: name: $project_name version: $project_ver_num($project_ver_let) + on_boot: + then: + # implementing on_boot automation as a script makes it run in parallel + # with any other on_boot scripts. + - script.execute: on_wifi_connect + # https://esphome.io/components/wifi.html wifi: @@ -121,6 +144,13 @@ wifi: # using fast_connect as default since it is required for hidden networks. fast_connect: true + # enable wifi ap with ridiculous timeout so it does not normally turn on. + ap: + ap_timeout: 2147483647ms # maximum 32 bit value. About 3.5 weeks in milliseconds. + + +captive_portal: # for fallback wifi hotspot + # https://esphome.io/components/logger.html logger: @@ -151,7 +181,7 @@ binary_sensor: id: button_in name: $friendly_name Button pin: - number: GPIO13 + number: $sub_button_pin mode: input: true pullup: true @@ -166,7 +196,7 @@ binary_sensor: if (id(select_button).state == "Toggle on Press") { id(relay).toggle(); } - script.execute: $sub_on_press - - script.execute: script_5s_timer + - script.execute: script_30s_timer on_release: - lambda: |- // set duration sensor @@ -177,7 +207,7 @@ binary_sensor: id(relay).toggle(); } - script.execute: $sub_on_release - - script.stop: script_5s_timer + - script.stop: script_30s_timer # indicates whether plugged-in device is running based on configurable threshold. # https://esphome.io/components/binary_sensor/template.html @@ -193,7 +223,6 @@ script: - id: script_set_power_leds then: - lambda: |- - auto call = id(blue_led).make_call(); // if blue led follows power status @@ -217,12 +246,13 @@ script: call.set_state(!id(relay).state); } call.perform(); + - script.execute: script_save_changes + - 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") || @@ -244,7 +274,6 @@ script: - 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") || @@ -280,28 +309,64 @@ script: - delay: 10s - button.press: restart_button - - id: script_5s_timer + - id: script_30s_timer + then: + - delay: !lambda return (30000-id(number_debounce).state); + - script.execute: $sub_on_hold_30s + + - id: on_wifi_connect + then: + + # wait until wifi connects + - wait_until: wifi.connected + + - lambda: |- + ESP_LOGD("KAUF on_boot","------------------->>>>>>>>>>>>>>>>> wifi connected, cranking ap timeout back up"); + wifi_wificomponent->set_ap_timeout(2147483647); + + - id: script_force_ap + then: + - logger.log: "------------------->>>>>>>>>>>>>>>>> HELD BUTTON 30 SECONDS, FORCING AP" + + # overwrite software defined credentials to force ap to turn on. + - lambda: wifi::global_wifi_component->save_wifi_sta("initial_ap","asdfasdfasdfasdf"); + + # blink LED for 10s then restart to get captive portal to turn on. + - script.execute: blink_led + - delay: 10s + - button.press: restart_button + + # blink LED forever. Used when button is held to re-enable AP. Stops blinking because plug restarts. + - id: blink_led + mode: queued then: - - delay: !lambda return (5000-id(number_debounce).state); - - script.execute: $sub_on_hold_5s + - light.toggle: blue_led + - delay: 333ms + - script.execute: blink_led - id: script_do_nothing then: - lambda: return; + - id: script_save_changes + mode: restart + then: + - delay: 3s + - lambda: global_preferences->sync(); + # 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 + frequency: 200 Hz + pin: $sub_blue_led_pin inverted: true - platform: esp8266_pwm id: red_led_pwm - frequency: 1000 Hz - pin: GPIO0 + frequency: 200 Hz + pin: $sub_red_led_pin inverted: true @@ -342,13 +407,15 @@ switch: - platform: gpio id: relay name: $friendly_name - pin: GPIO4 + pin: $sub_relay_pin entity_category: '' restore_mode: $sub_restore_mode on_turn_on: + - script.execute: script_save_changes - script.execute: script_set_power_leds - script.execute: $sub_on_turn_on on_turn_off: + - script.execute: script_save_changes - script.execute: script_set_power_leds - script.execute: $sub_on_turn_off @@ -365,8 +432,10 @@ switch: - 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 + - script.execute: script_save_changes on_turn_off: - lambda: id(kauf_api).set_reboot_timeout(900000); + - script.execute: script_save_changes # https://esphome.io/components/button/index.html @@ -393,10 +462,10 @@ sensor: # Power monitoring sensors output to Home Assistant - platform: hlw8012 id: hlw_main sel_pin: - number: GPIO12 + number: $sub_pm_sel_pin inverted: True - cf_pin: GPIO5 - cf1_pin: GPIO14 + cf_pin: $sub_pm_cf_pin + cf1_pin: $sub_pm_cf1_pin current_resistor: $current_resistor_val voltage_divider: $voltage_divider_val @@ -404,6 +473,8 @@ sensor: # Power monitoring sensors output to Home Assistant update_interval: $sub_update_interval initial_mode: $sub_initial_mode + model: $sub_hlw_model + power: name: ${friendly_name} Power unit_of_measurement: W @@ -444,7 +515,6 @@ sensor: # Power monitoring sensors output to Home Assistant filters: - multiply: 0.001 ## convert Wh to kWh unit_of_measurement: kWh - min_save_interval: 5min # https://esphome.io/components/sensor/uptime.html - platform: uptime @@ -479,6 +549,8 @@ number: # used as a threshold for whether the plugged-in devices is running unit_of_measurement: Watt(s) mode: box disabled_by_default: $disable_entities + set_action: + - script.execute: script_save_changes on_value: # set or clear in_use template binary sensor depending on whether power usage is above threshold - lambda: id(in_use).publish_state(id(wattage).state >= x); @@ -495,6 +567,8 @@ number: # used as a threshold for whether the plugged-in devices is running unit_of_measurement: "%" mode: box disabled_by_default: $disable_entities + set_action: + - script.execute: script_save_changes on_value: # republish value. Sensor automation applies new scaling factor. - lambda: id(wattage).publish_state(id(wattage).get_raw_state()); @@ -511,6 +585,8 @@ number: # used as a threshold for whether the plugged-in devices is running unit_of_measurement: "%" mode: box disabled_by_default: $disable_entities + set_action: + - script.execute: script_save_changes on_value: # republish value. Sensor automation applies new scaling factor. - lambda: id(current).publish_state(id(current).get_raw_state()); @@ -527,15 +603,17 @@ number: # used as a threshold for whether the plugged-in devices is running unit_of_measurement: "%" mode: box disabled_by_default: $disable_entities + set_action: + - script.execute: script_save_changes on_value: # republish value. Sensor automation applies new scaling factor. - lambda: id(voltage).publish_state(id(voltage).get_raw_state()); - platform: template name: ${friendly_name} Debounce Time - min_value: 50 + min_value: $sub_min_debounce max_value: 2000 step: 1 - initial_value: 75 + initial_value: $sub_default_debounce id: number_debounce entity_category: config optimistic: true @@ -544,6 +622,7 @@ number: # used as a threshold for whether the plugged-in devices is running mode: box disabled_by_default: $disable_entities set_action: # currently rebooting plug to apply new delayed_on time + - script.execute: script_save_changes - script.execute: script_setting_reboot on_value: - lambda: |- @@ -573,6 +652,8 @@ select: restore_value: true icon: mdi:circle-double entity_category: config + set_action: + - script.execute: script_save_changes - platform: template name: $friendly_name Blue LED Config @@ -589,6 +670,8 @@ select: initial_option: Power Status restore_value: true icon: mdi:led-on + set_action: + - script.execute: script_save_changes on_value: - if: condition: @@ -612,6 +695,8 @@ select: initial_option: Error Status restore_value: true icon: mdi:led-on + set_action: + - script.execute: script_save_changes on_value: - if: condition: @@ -638,6 +723,7 @@ select: entity_category: config disabled_by_default: $disable_entities set_action: + - script.execute: script_save_changes - script.execute: script_setting_reboot on_value: - lambda: |- diff --git a/kauf-plug-minimal.yaml b/kauf-plug-minimal.yaml index 94e83a4..48018d8 100644 --- a/kauf-plug-minimal.yaml +++ b/kauf-plug-minimal.yaml @@ -5,6 +5,39 @@ substitutions: friendly_name: Kauf Plug # **** CHANGE FRIENDLY NAME TO SOMETHING UNIQUE PER DEVICE **** + # 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" + sub_hlw_model: HLW8012 + 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 + + # GPIO definitions + sub_pm_sel_pin: GPIO12 + sub_pm_cf_pin: GPIO5 + sub_pm_cf1_pin: GPIO14 + sub_button_pin: GPIO13 + sub_blue_led_pin: GPIO2 + sub_red_led_pin: GPIO0 + sub_relay_pin: GPIO4 esphome: @@ -17,7 +50,7 @@ esphome: project: name: "Kauf.PLF10" - version: "1.97(m)" + version: "1.98(m)" wifi: @@ -26,17 +59,6 @@ wifi: 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 - # gateway: !secret wifi_gateway - # subnet: !secret wifi_subnet - # dns1: !secret wifi_dns1 - - # use_address allows wireless programming through the ESPHome dashboard. - # Set to the plug's IP Address. Remove after programming. - # use_address: 192.168.1.3 - # default is 20, 17 is recommended. output_power: 17 @@ -44,15 +66,12 @@ wifi: # feel free to change or override. fast_connect: true + logger: # Enable logging -# baud_rate: 0 # Disable UART logging since TX pad not easily available api: # Enable Home Assistant API - # password: !secret api_password # optional password field for Home Assistant API. ota: - # password: !secret ota_password # optional password for OTA updates. - # red led, blink when not connected to wifi or Home Assistant @@ -67,7 +86,7 @@ binary_sensor: # button input toggles relay and thereby blue led - platform: gpio id: button - name: Kauf Plug Button + name: $friendly_name Button pin: number: GPIO13 mode: @@ -111,42 +130,55 @@ time: 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 + number: $sub_pm_sel_pin 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. + cf_pin: $sub_pm_cf_pin + cf1_pin: $sub_pm_cf1_pin + 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 + + model: $sub_hlw_model + power: name: ${friendly_name} Power unit_of_measurement: W id: wattage filters: - calibrate_linear: - - 0.0 -> 0.0 - - 333.8 -> 60 # value with 60W bulb. + - $power_cal_val1_in -> $power_cal_val1_out + - $power_cal_val2_in -> $power_cal_val2_out + current: name: ${friendly_name} Current unit_of_measurement: A + id: current filters: - calibrate_linear: - - 0.0 -> 0.0 - - 0.6 -> 0.515 # value with 60W bulb. + - $current_cal_val1_in -> $current_cal_val1_out + - $current_cal_val2_in -> $current_cal_val2_out + voltage: name: ${friendly_name} Voltage unit_of_measurement: V + id: voltage filters: - calibrate_linear: - - 0.0 -> 0.0 - - 302.1 -> 117.1 # Tested using a meter - change_mode_every: 1 - update_interval: 10s # 20 second effective update rate for Power, 40 second for Current and Voltage. + - $voltage_cal_val1_in -> $voltage_cal_val1_out + - $voltage_cal_val2_in -> $voltage_cal_val2_out -# Reports the total Power so-far each day, resets at midnight -# See https://esphome.io/components/sensor/total_daily_energy.html + # 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 diff --git a/kauf-plug.yaml b/kauf-plug.yaml index 38414ca..873c9f4 100644 --- a/kauf-plug.yaml +++ b/kauf-plug.yaml @@ -369,6 +369,7 @@ script: - delay: 3s - lambda: global_preferences->sync(); + # pwm outputs for LEDs so they can be dimmed # https://esphome.io/components/output/esp8266_pwm.html output: @@ -761,7 +762,6 @@ select: set_action: - script.execute: script_save_changes - - platform: template name: $friendly_name Blue LED Config id: select_bled -- cgit v1.2.3