What is the difference between the SGP30 and the SGP40?
The SGP40 offers a chip that runs straight from 3.3 V, a lower average current, and a part still in production; in our table the SGP30 has no advantage of its own apart from price, and it is at the end of its life.
Which is better for air quality, the SGP30 or the SGP40?
They measure different things: the SGP30 gives TVOC and eCO₂ (estimated), the SGP40 VOC index. Choose by what you want to know.
Can I replace the SGP30 with the SGP40?
Often yes: both use I²C. Change the driver or, in ESPHome, the component (sgp30 → sgp4x). The I²C address changes too (0x58 → 0x59). Check the supply too: 1.62 to 1.98 V for the SGP30, 1.7 to 3.6 V for the SGP40.