#############################################
#############################################
# OVEN POWER MONITOR
# Monitoring power of an oven (at the main 
# switchboard) using a sonoff basic (esp8266) 
# and a PZEM-004T. The relay also allows disabling
# of the oven with a contactor (eg on smoke detection)
# https://zorruno.com/2022/nodered-oven-notifications/
##############################################
#############################################

#############################################
# Variable Substitutions
#############################################
substitutions:
  devicename: "esp-mainovenmonitor"
  friendly_name: "esp-mainovenmonitor"
  description_comment: "Oven power monitoring and disable, with a sonoff basic"
  api_key: !secret esp-mainovenmonitor_api_key #unfortunately you can't use substitutions in secrets names
  ota_pass: !secret esp-mainovenmonitor_ota_pass #unfortunately you can't use substitutions in secrets names
  mqtt_topic: "esphome" #main topic for the mqtt server, call it what you like
  #update_time: 30s #update time for for temp sensors etc
  
#############################################
# ESPHome
#############################################
esphome:
  name: ${devicename}
  comment: ${description_comment} #appears on the esphome page in HA

#############################################
# ESP Platform and Framework
# https://esphome.io/components/esp8266.html
# https://esphome.io/components/esp32.html
#############################################
esp8266:
  #board: sonoff_basic
  #board: esp01_1m
  board: esp8285
  framework:
    version: latest #recommended, latest or dev
#esp32:
#  board: nodemcu-32s
#  framework:
#    type: arduino
#    #type: esp-idf  #Suggested Use ESP-IDF Framework, or Plug Out the UART Cable Might Cause ESP32 Hang. 
#    version: recommended #recommended, latest or dev

#############################################    
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
############################################# 
logger:
  level: INFO  #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)
#############################################  
wifi:
  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 (captive portal) in case wifi connection fails https://esphome.io/components/wifi.html#access-point-mode
    ssid: ${devicename} fallback AP
    password: ${fallback_ap_password}
    ap_timeout: 30min #Time until it brings up fallback AP. default is 1min

captive_portal:

#############################################    
# Web Portal for display and monitoring
#############################################   
web_server:
  port: 80
  version: 2
  include_internal: true
  ota: false
#  auth:
#    username: !secret web_server_username
#    password: !secret web_server_password

#############################################
# MQTT Monitoring
# https://esphome.io/components/mqtt.html?highlight=mqtt
# MUST also have api enabled if you enable MQTT
#############################################
mqtt:
  broker: !secret mqtt_server
  topic_prefix: ${mqtt_topic}/${devicename}
  #username: !secret mqtt_username
  #password: !secret mqtt_password


#############################################  
#############################################
# MAIN SENSORS
#############################################
#############################################
# PEZEM4 Power Monitoring
# https://esphome.io/components/sensor/pzemac.html
#############################################
uart:
  rx_pin: GPIO3
  tx_pin: GPIO1
  #tx_pin: RX
  #rx_pin: TX
  baud_rate: 9600
  stop_bits: 1
  
modbus:

sensor:
  - platform: pzemac
    current:
      name: "Main Oven Current"
    voltage:
      name: "Main Oven Voltage"
    energy:
      name: "Main Oven Energy"
    power:
      name: "Main Oven Power"
    frequency:
      name: "Main Oven Mains Frequency"
    power_factor:
      name: "Main Oven Power Factor"
    update_interval: 10s
    
########################################
########################################
    
# Relay Output (on sonoff basic)    
switch:
  - platform: gpio
    name: "Main Oven Disable"
    pin: GPIO12

button:
  - platform: restart
    name: "Main Oven ESPHome Restart"


# LED Flashes on errors or warnings
# https://esphome.io/components/status_led.html
status_led:
  pin:
    number: GPIO13
    inverted: true