In this study, hourly Ts variations at 5, 10, and 30 cm soil depth were investigated and predicted for an arid site (Sirjan) and a semi-humid site (Sanandaj) in Iran. Standalone machine learning models (adaptive neuron fuzzy interface system (ANFIS), support vector machine model (SVM), radial basis function neural network (RBFNN), and multilayer perceptron (MLP)) were hybridized with four optimization algorithms (sunflower optimization (SFO), firefly algorithm (FFA), salp swarm algorithm (SSA), particle swarm optimization (PSO)) to improve prediction accuracy and reduce uncertainty. Uncertainty analysis was performed using generalized likelihood uncertainty estimation (GLUE), while wavelet coherence was used to assess interactions between Ts and meteorological parameters. For the arid site, ANFIS-SFO (RMSE = 1.18oC, MAE = 1.05oC, NSE = 0.93, PBIAS = 7%, and R2 = 0.9998) produced the most accurate performance at 5 cm soil depth. At best, hybridization with SFO (ANFIS-SFO, MLP-SFO, RBFNN-SFO, SVM-SFO) decreased RMSE by 5.6, 18, 18.3, and 18.18 % compared with the respective standalone model. At the semi-humid site, all integrated models showed most accurate performance at 10 cm soil depth, with RMSE for the best model (ANFIS-SFO) increasing by 10.5%, and MAE by 10.1%, from 10 to 30 cm depth. GLUE analysis confirmed that integrating optimization algorithms with machine learning models decreased the uncertainty in Ts predictions. Wavelet coherence analysis demonstrated that air temperature, relative humidity, and solar radiation, but not wind speed, had high coherence with Ts at different soil depths at both sites, and meteorological parameters mostly influenced Ts in upper soil layers.