I did this math many many times. I have yet to see SP500 outperform owning a house in my area (I'm in NYC burbs).
I have yet to find a single place that would have been worth it long term versus renting (Bay area, which is a VHCOL) while you do the correct math.
https://www.nytimes.com/interactive/2024/upshot/buy-rent-cal...