Here's how to do it automatically on iOS

How to Make an iOS Shortcut to Turn Off Wi-Fi Automatically (2023)
Keeping your iPhone and iPad Wi-Fi-connected while at home reduces cellular data usage and ensures a fast internet connection. It's best to turn Wi-Fi off when you leave home, though, to avoid connecting to unsafe networks and to reduce the battery drain that happens when your iPhone searches...
