The thermodynamic potential for a system in thermal contact with a bath is Helmotz's free energy F:
dF=-SdT-pdV
so S=-\frac{\partial F}{\partial T}
but F=\frac{-1}{\beta}lnZ
S=klnZ+\frac{1}{\beta Z}\frac{\partial Z}{\partial T}
and the last term is the mean value of energy / T.