Sorption-based atmospheric water harvesting (AWH) is a promising technology to produce clean potable water in arid areas with scarce freshwater resources. However, most sorbents developed for this technology can only perform one cycle of water production per day due to slow water-sorption kinetics. Moreover, the heat produced during this process in current AWH systems is discarded and ultimately wasted. Here, we design and fabricate a hygroscopic aerogel material that has high water-sorption capacity, fast sorption kinetics, and excellent photothermal properties, and thus enables highly efficient solar-thermal driven AWH over a wide range of relative humidity. Furthermore, we demonstrate with this aerogel the concept of a dual-function system that simultaneously generates electricity while extracting fresh water from the air. The dual-function system achieves this by combining AWH with thermoelectric technology and using natural sunlight as the sole energy input. The model system can produce a maximum output power density of 6.6 mW/m2 during the moisture capture process at the relative humidity of 60%, and 520 mW/m2 during the water release process under 1 kW/m2 solar irradiation. We verify the real-world application and utility of this novel concept by conducting outdoor experiments using a homemade prototype.