We propose a definition of Vafa–Witten invariants counting semistable Higgs pairs on a polarised surface. We use virtual localisation applied to Mochizuki/Joyce–Song pairs. For KS≤0 we expect our definition coincides with an alternative definition using weighted Euler characteristics. We prove this for degKS<0 here, and it is proved for S a K3 surface in “Sheaf counting on local K3 surfaces” [D. Maulik and R. P. Thomas, arXiv:1806.02657]. For K3 surfaces we calculate the invariants in terms of modular forms which generalise and prove conjectures of Vafa and Witten.