We prove an arithmetic refinement of the Yau–Zaslow formula by replacing the classical Euler characteristic in Beauville’s argument by a motivic Euler characteristic, related to the work of Levine. Our result implies similar formulas for other related invariants, including a generalisation of a formula of Kharlamov and Răsdeaconu on counting real rational curves on real K3 surfaces, and Saito’s determinant of cohomology.