Variational approach to Robertson–Walker spacetimes with homogeneous scalar fields