A Generalization of Wald's Fundamental Identity