Solution (source code)

= Solution

The <centre of mass> is at height $-a\cos\theta/2$. With gravitational <potential energy> zero at the pivot level,
$$
V=-\frac{Mga}{2}\cos\theta.
$$
Hence the <Lagrangian> is
$$
\boxed{L=T-V=\frac{Ma^2}{6}(\dot\theta^2+\sin^2\theta\,\dot\phi^2)+\frac{Mga}{2}\cos\theta.}
$$
Adding a constant to the potential would change the numerical <energy> reference but would not change the motion.