The Internet of Things (IoT) based Wireless Sensor Networks (WSNs) contain interconnected autonomous sensor nodes (SN), which wirelessly communicate with each other and the wider internet structure. Intrusion detection to secure IoT-based WSNs is critical for identifying and responding to great security attacks and threats that can cooperate with the integrity, availability, and privacy of the network and its data. Machine learning (ML) algorithms are deployed for detecting difficult patterns and subtle anomalies in IoT data. Artificial intelligence (AI) driven methods are learned and adapted from novel data for improving detection accuracy over time. In this article, we introduce a Red Kite Optimization Algorithm with an Average Ensemble Model for Intrusion Detection (RKOA-AEID) technique for Secure IoT-based WSN. The purpose of the RKOA-AEID methodology is to accomplish security solutions for IoT-assisted WSNs. To accomplish this, the RKOA-AEID technique performs pre-processing to scale the input data using min-max normalization. In addition, the RKOA-AEID technique performs an RKOA-based feature selection approach to elect an optimum set of features. For intrusion detection, an average ensemble learning model is used. Finally, the Lévy-fight chaotic whale optimization Algorithm (LCWOA) can be executed for the optimum hyperparameter chosen for the ensemble models. The performance evaluation of the RKOA-AEID algorithm can be tested on the benchmark WSN-DS dataset. The extensive experimental outcomes stated the higher outcome of the RKOA-AEID algorithm with other approaches with an improved accuracy of 98.94%.