The connection of many devices has brought new challenges with respect to the centralized architecture of cloud computing. The fog environment is suitable for many services and applications for which cloud computing does not support these well, such as: traffic light monitoring systems, healthcare monitoring systems, connected vehicles, smart cities, homes, and many others. Sending high-velocity data to the cloud leads to the congestion of the cloud infrastructure, which further leads to high latency and violations of the Quality-of-Service (QoS). Thus, delay-sensitive applications need to be processed at the edge of the network or near the end devices, rather than the cloud, in order to provide the guaranteed QoS related to the reduced latency, increased throughput, and high bandwidth. The aim of this paper was to propose a two-stage optimal task scheduling (2-ST) approach for the distribution of tasks executed within smart homes among several fog nodes. To effectively solve the task scheduling, this proposed approach uses a naïve-Bayes-based machine learning model for training in the first stage and optimization in the second stage using a hyperheuristic approach, which is a combination of both Ant Colony Optimization (ACO) and Particle Swarm Optimization (PSO). In addition, the proposed mechanism was validated against various metrics such as energy consumption, latency time, and network usage.