I try to develop a measure of heterogeneity of beliefs for the Branch&McGough (2009) model. A New Keynesian model with heterogeneous expectations. Journal of Economic Dynamics & Control 33 (2009) pp. 1036-1051. William A. Branch, Bruce McGough

Motivated by the Di Bartolomeo et al. (2016) paper, more precisely their “consumption inequality” dimension which is driven by diverging expectations, my idea is to augment an ad hoc period loss function by some measure of heterogeneity of beliefs.

The later is given by \begin{align} \mathrm{var}_i(E_t^i(x_{t+1}))=\alpha(1-\alpha)[E_t(x_{t+1})-\theta^2x_{t-1}]^2\end{align}
which is a cross-sectional variance of one-step ahead forecasts and x is a generic variable.

My question:
Can I implement the measure above into a period loss like \begin{align} L_t &= \pi_t^2 + \omega_y y_t^2 + \omega_{E\pi} \mathrm{var}_i(E_t^i(\pi_{t+1}))
\\&=\pi_t^2 + \omega_y y_t^2 + \omega_{E\pi}\left[ \alpha(1-\alpha)E_t(\pi_{t+1})^2 - 2\alpha(1-\alpha) \theta^2\pi_{t-1}E_t(\pi_{t+1})+ \alpha(1-\alpha)\theta^4\pi_{t-1}^2\right] \end{align}

My problem is the square of the rational expectation.
One may argue in favor of an auxiliary like: E_ppi = ppi(+1); (as additional state variable).
But I am worried about the intertemporal loss J_0=E_0\sum_{k=0}^\infty\beta^k L_{t+k}
since terms like E_0(E_{t+k}(\pi_{t+k+1})^2) show up.

You can easily deal with nonlinear transformations of expected values using auxiliary variables. See Expected value of a power
That makes your objective feasible.

Can someone tell me how to compute a “targeting rule” under the loss above by hand?

For the sake of simplicity let L_t = \omega_yy_t + \omega_{E\pi} [E_t(\pi_{t+1})-\theta^2\pi_{t-1}]^2 and assume optimal monetary policy under commitment.

The Lagrangian obtains as L= E_t\sum_{k=0}^\infty \beta^k \{ L_{t+k} + \psi_{t+k}[\pi_{t+k}-\beta\alpha\pi_{t+k+1}-\beta(1-\alpha)\theta^2\pi_{t+k-1} -\kappa y_{t+k}]\} + t.i.p.

I am worried about the expectation inside the period loss.

If I simply do the job (following your three steps) I obtain the FOC w.r.t \pi_{t+k} \begin{align}\frac{\partial L}{\partial \pi_{t+k}} =E_t\Big( \beta^k\psi_{t+k} + 2\beta^{k-1}\omega_{E\pi}[E_{t+k-1}(\pi_{t+k})-\theta^2\pi_{t+k-2}] - \beta^{k-1}\beta\alpha\psi_{t+k-1} \\ + 2\beta^{k+1}\omega_{E\pi}[E_{t+k+1}(\pi_{t+k+2})-\theta^2\pi_{t+k}] (-\theta^2) -\beta^{k+1}\beta (1-\alpha)\theta^2 \psi_{t+k+1} \Big) = 0
\end{align}