############################################# ############################################# # Data Cupboard Power Monitor A # Athom Smart Plug Power Monitor ESP32-C3 # # V1.0 2025-03-13 Initial Version # # based on https://github.com/athom-tech/esp32-configs/blob/main/athom-smart-plug.yaml # # SUMMARY # Smart plug with power moniroting. It has limited ability to # accidentally turn it off as it powers equipment in a data cupboard. # It does have the ability to turn off and back on automatically # a short time later, just in case a remote system reset is needed. # Also, the button on the side can't be bumped, you have to hold # it down for a few seconds to power it off. # The yaml code also calculates various power use summaries. # ############################################# ############################################# substitutions: ############################################# # SPECIFIC DEVICE VARIABLE SUBSTITUTIONS # If NOT using a secrets file, just replace these with the passwords etc (in quotes) ############################################# devicename: "esp-datapower-a" friendly_name: "Data Cupboard Power Monitor A" description_comment: "Smart plug power Monitor A, Data Cupboard" room: "Data Cupboard" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant. api_key: !secret esp-datapower-a_api_key # unfortunately you can't use substitutions inside secrets names ota_pass: !secret esp-datapower-a_ota_pass # unfortunately you can't use substitutions inside secrets names static_ip_address: !secret esp-datapower-a_ip log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE update_interval: 10s # update time for for general sensors etc # Project Details project_name: "Athom Technology.Smart Plug V3" project_version: "v1.0.7" # Project V denotes release of yaml file, allowing checking of deployed vs latest version # Restore the relay (GPO switch) upon reboot to state: relay_restore_mode: RESTORE_DEFAULT_ON # Current Limit in Amps. AU Plug = 10. IL, BR, EU, UK, US Plug = 16. current_limit : "10" # Hide the ENERGY sensor that shows kWh consumed, but with no time period associated with it. Resets when device restarted and reflashed. hide_energy_sensor: "true" # Enable or disable the use of IPv6 networking on the device ipv6_enable: "false" # Power plug icon selection. Change to reflect the type/country of powr plug in use, this will update the power plug icon shown next to the switch power_plug_type: "power-socket-au" # Options: power-socket-au | power-socket-ch | power-socket-de | power-socket-eu | power-socket-fr | power-socket-it | power-socket-jp | power-socket-uk | power-socket-us | ############################################# # SYSTEM SPECIFIC VARIABLE SUBSTITUTIONS ############################################# timezone: "Pacific/Auckland" sntp_update_interval: 6h # Set the duration between the sntp service polling # Network time servers https://www.ntppool.org/zone/@ sntp_server_1: !secret ntp_server_1 sntp_server_2: !secret ntp_server_2 sntp_server_3: !secret ntp_server_3 wifi_ssid: !secret ha_wifi_ssid wifi_password: !secret ha_wifi_password fallback_ap_password: !secret fallback_ap_password # Enables faster network connections, with last connected SSID being connected to and no full scan for SSID being undertaken wifi_fast_connect: "false" # Define a domain for this device to use. i.e. iot.home.lan (so device will appear as athom-smart-plug-v2.iot.home.lan in DNS/DHCP logs) dns_domain: ".local" # Automatically add the mac address to the name # eg so you can use a single firmware for all devices add_mac_suffix: "false" # Add these if we are giving it a static ip, or remove them in the Wifi section static_ip_subnet: !secret ha_wifi_subnet static_ip_gateway: !secret ha_wifi_gateway mqtt_server: !secret ha_mqtt_server mqtt_username: !secret ha_mqtt_username mqtt_password: !secret ha_mqtt_password mqtt_topic: "esphome" #main topic for the mqtt server, call it what you like # Add these if we are using the internal web server (this is pretty processor intensive) #web_server_username: !secret web_server_username #web_server_password: !secret web_server_password ############################################# # Included Common Packages # https://esphome.io/components/esphome.html ############################################# packages: wifi: !include common/wifi_common.yaml ############################################# # ESPHome # https://esphome.io/components/esphome.html ############################################# esphome: name: ${devicename} friendly_name: ${friendly_name} comment: ${description_comment} #Appears on the esphome page in HA area: "${room}" name_add_mac_suffix: "${add_mac_suffix}" min_version: 2024.6.0 project: name: "${project_name}" version: "${project_version}" platformio_options: board_build.mcu: esp32c3 board_build.variant: esp32c3 board_build.flash_mode: dio ############################################# # ESP Platform and Framework # https://esphome.io/components/esp32.html ############################################# #esp8266: # board: esp01_1m # The original sonoff basic esp32: board: esp32-c3-devkitm-1 flash_size: 4MB variant: ESP32C3 framework: type: arduino version: recommended preferences: flash_write_interval: 5min ############################################# # ESPHome Logging Enable # https://esphome.io/components/logger.html ############################################# logger: level: ${log_level} #INFO Level suggested, or DEBUG for testing baud_rate: 0 #set to 0 for no logging via UART, needed if you are using it for other serial things (eg PZEM) #esp8266_store_log_strings_in_flash: false #tx_buffer_size: 64 ############################################# # Enable the Home Assistant API # https://esphome.io/components/api.html ############################################# api: encryption: key: ${api_key} ############################################# # Enable Over the Air Update Capability # https://esphome.io/components/ota.html?highlight=ota ############################################# ota: - platform: esphome password: ${ota_pass} ############################################# # Safe Mode # Safe mode will detect boot loops # https://esphome.io/components/safe_mode ############################################# safe_mode: ############################################# # Wifi Settings # https://esphome.io/components/wifi.html # # Power Save mode (can reduce wifi reliability) # NONE (least power saving, Default for ESP8266) # LIGHT (Default for ESP32) # HIGH (most power saving) ############################################# ##ifi: # ssid: ${wifi_ssid} # password: ${wifi_password} #power_save_mode: LIGHT # https://esphome.io/components/wifi.html#wifi-power-save-mode # manual_ip: # optional static IP address # static_ip: ${static_ip_address} # gateway: ${static_ip_gateway} # subnet: ${static_ip_subnet} # ap: # Details for fallback hotspot in case wifi connection fails https://esphome.io/components/wifi.html#access-point-mode # ssid: ${devicename} AP # password: ${fallback_ap_password} # ap_timeout: 30min # Time until it brings up fallback AP. default is 1min # # Allow rapid re-connection to previously connect WiFi SSID, skipping scan of all SSID # fast_connect: "${wifi_fast_connect}" # # Define dns domain / suffix to add to hostname # domain: "${dns_domain}" #captive_portal: # extra fallback mechanism for when connecting if the configured WiFi fails ############################################# # Real time clock time source for ESPHome # If it's invalid, we fall back to an internal clock # https://esphome.io/components/time/index.html # https://esphome.io/components/time/sntp ############################################# time: - platform: sntp id: sntp_time # Define the timezone of the device timezone: "${timezone}" # Change sync interval from default 5min to 6 hours (or as set in substitutions) update_interval: ${sntp_update_interval} # Set specific sntp servers to use servers: - "${sntp_server_1}" - "${sntp_server_2}" - "${sntp_server_3}" # Publish the time the device was last restarted on_time_sync: then: # Update last restart time, but only once. - if: condition: lambda: 'return id(device_last_restart).state == "";' then: - text_sensor.template.publish: id: device_last_restart state: !lambda 'return id(sntp_time).now().strftime("%a %d %b %Y - %I:%M:%S %p");' ############################################# # MQTT Monitoring # https://esphome.io/components/mqtt.html?highlight=mqtt # MUST also have api enabled if you enable MQTT ############################################# mqtt: broker: ${mqtt_server} topic_prefix: ${mqtt_topic}/${devicename} username: ${mqtt_username} password: ${mqtt_password} discovery: false # enable entity discovery (true is default, we don't want two HA Instances) ############################################# # Web Portal for display and monitoring # Turning this off is maybe a good idea to save resources, # especially on an esp8266. # https://esphome.io/components/web_server.html ############################################# #web_server: # port: 80 # auth: # username: ${web_server_username} # probably a good idea to secure it # password: ${web_server_password} network: enable_ipv6: ${ipv6_enable} esp32_improv: authorizer: none dashboard_import: package_import_url: github://athom-tech/esp32-configs/athom-smart-plug.yaml uart: rx_pin: GPIO20 baud_rate: 4800 data_bits: 8 stop_bits: 1 parity: EVEN globals: - id: total_energy type: float restore_value: yes initial_value: '0.0' # - id: restore_mode # type: int # restore_value: yes # initial_value: "2" # 0 = Always_Off. 1 = Restore_Power_Off. 2 = Always_On. binary_sensor: - platform: status name: "Status" icon: mdi:check-network-outline entity_category: diagnostic - platform: gpio pin: number: GPIO3 mode: INPUT_PULLUP inverted: true name: "Power Button" id: power_button disabled_by_default: true on_multi_click: - timing: - ON for at most 10s - OFF for at least 2s then: - switch.toggle: relay # - timing: # - ON for at least 4s # then: # - button.press: Reset - platform: template name: "Relay Status" lambda: |- return id(relay).state; sensor: - platform: uptime name: "Uptime Sensor" id: uptime_sensor entity_category: diagnostic internal: true - platform: wifi_signal name: "WiFi Signal dB" id: wifi_signal_db update_interval: 60s entity_category: "diagnostic" - platform: copy source_id: wifi_signal_db name: "WiFi Signal Percent" filters: - lambda: return min(max(2 * (x + 100.0), 0.0), 100.0); unit_of_measurement: "Signal %" entity_category: "diagnostic" device_class: "" - platform: cse7766 id: athom_cse7766 current: name: "Current" icon: mdi:current-ac filters: - throttle_average: ${update_interval} - lambda: if (x < 0.060) return 0.0; else return x; #For the chip will report less than 3w power when no load is connected on_value_range: - above: ${current_limit} then: - switch.turn_off: relay voltage: name: "Voltage" icon: mdi:sine-wave filters: - throttle_average: ${update_interval} power: name: "Power" id: power_sensor icon: mdi:power filters: - throttle_average: ${update_interval} - lambda: if (x < 3.0) return 0.0; else return x; #For the chip will report less than 3w power when no load is connected energy: name: "Energy" id: energy icon: mdi:lightning-bolt unit_of_measurement: kWh filters: - throttle: ${update_interval} # Multiplication factor from W to kW is 0.001 - multiply: 0.001 on_value: then: - lambda: |- static float previous_energy_value = 0.0; float current_energy_value = id(energy).state; id(total_energy) += current_energy_value - previous_energy_value; previous_energy_value = current_energy_value; id(total_energy_sensor).update(); apparent_power: name: "Apparent Power" icon: mdi:power filters: - throttle_average: ${update_interval} reactive_power: name: "Reactive Power" icon: mdi:flash filters: - throttle_average: ${update_interval} power_factor: name: "Power Factor" icon: mdi:percent-outline filters: - throttle_average: ${update_interval} - platform: template name: "Total Energy" id: total_energy_sensor unit_of_measurement: kWh device_class: "energy" state_class: "total_increasing" icon: mdi:lightning-bolt accuracy_decimals: 3 lambda: |- return id(total_energy); update_interval: ${update_interval} - platform: total_daily_energy name: "Total Daily Energy" restore: true power_id: power_sensor unit_of_measurement: kWh icon: mdi:hours-24 accuracy_decimals: 3 filters: - multiply: 0.001 button: # - platform: restart # name: "Restart" # entity_category: config # - platform: factory_reset # name: "Factory Reset" # id: Reset # entity_category: config # - platform: safe_mode # name: "Safe Mode" # internal: false # entity_category: config - platform: template name: "Turn Off Relay Temporarily" on_press: then: - switch.turn_off: relay - delay: 5s - switch.turn_on: relay switch: - platform: gpio name: "Switch" pin: GPIO5 id: relay restore_mode: ALWAYS_ON # Ensures the relay is on at boot internal: true # Hides the switch from Home Assistant #icon: mdi:${power_plug_type} # Don't need an icon if we can't see it... light: - platform: status_led name: "Status LED" id: blue_led icon: mdi:lightbulb-outline disabled_by_default: false pin: inverted: true number: GPIO6 text_sensor: - platform: wifi_info ip_address: name: "IP Address" icon: mdi:ip-network entity_category: diagnostic ssid: name: "Connected SSID" icon: mdi:wifi-strength-2 entity_category: diagnostic mac_address: name: "Mac Address" icon: mdi:network-pos entity_category: diagnostic # Creates a sensor showing when the device was last restarted - platform: template name: 'Last Restart' id: device_last_restart icon: mdi:clock entity_category: diagnostic # device_class: timestamp # Creates a sensor of the uptime of the device, in formatted days, hours, minutes and seconds - platform: template name: "Uptime" entity_category: diagnostic lambda: |- int seconds = (id(uptime_sensor).state); int days = seconds / (24 * 3600); seconds = seconds % (24 * 3600); int hours = seconds / 3600; seconds = seconds % 3600; int minutes = seconds / 60; seconds = seconds % 60; if ( days > 3650 ) { return { "Starting up" }; } else if ( days ) { return { (String(days) +"d " + String(hours) +"h " + String(minutes) +"m "+ String(seconds) +"s").c_str() }; } else if ( hours ) { return { (String(hours) +"h " + String(minutes) +"m "+ String(seconds) +"s").c_str() }; } else if ( minutes ) { return { (String(minutes) +"m "+ String(seconds) +"s").c_str() }; } else { return { (String(seconds) +"s").c_str() }; } icon: mdi:clock-start