Your style worked for me, except for the fact that the zigzag segment wasn't centred correctly because you shifted it by 1cm, not by 0.5cm.
I would suggest to keep as many of the lengths in your decoration as variables, instead of hard coding your values. If you set \pgfset{/pgf/decoration/segment length=8pt}, you rob yourself of the freedom to change the zigzag wavelength by setting segment length=<value> as an option to your decoration. The same goes for \pgfsetlinewidth{1pt}: If you leave out this line, you can just specify the line width as you would with any other TikZ path. If you need to draw the decoration frequently with the same values, you can just define a new style that sets these for you.
Also, you don't need to say \beforedecoration{\pgfpathmoveto{\pgfpointmetadecoratedpathfirst}} at the beginning of each segment if your segments are joined.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations,decorations.pathmorphing}
\pgfdeclaremetadecoration{middlezigzag}{straight}{
\state{straight}[switch if less than=\pgfmetadecorationsegmentlength to final,
width=\pgfmetadecoratedpathlength/2 - \pgfmetadecorationsegmentlength/2 ,
next state=zigzag] {
\decoration{curveto}
}
\state{zigzag}[width=\pgfmetadecorationsegmentlength,
next state=final] {
\decoration{zigzag}
}
\state{final}{
\decoration{curveto}
\beforedecoration{\pgfpathmoveto{\pgfpointmetadecoratedpathfirst}}
}
}
\tikzset{
middle zigzag/.style={
decorate,
decoration={
middlezigzag,
meta-segment length=#1,
segment length=0.5cm
}
},
middle zigzag/.default=1cm
}
\begin{document}
\begin{tikzpicture}
\draw [gray!50,yshift=-0.5cm] (0,0) grid (5,1);
\draw [middle zigzag, ultra thick] (0,0) -- (5,0);
\end{tikzpicture}
\end{document}