Minimum up time for a machine in a linear program?

If we let $$x_i$$ = 1 if a machine is on during hour $$i$$, and 0 if the machine is off, how would we enforce a constraint that requires the machine to be “on” for a minimum of at least $$M$$ hours?

For example, if a machine turns on at hour 5, then that means at least $$x_5$$ through $$x_{5+M}$$ all equal 1. I was thinking about introducing an auxiliary variable such that $$y_i$$ = 1 if machine turns on at hour $$i$$, but I cannot bridge how to use this to enforce a minimum run time.

I would also like to enforce a minimum down time for the machine, but I imagine a similar logic can be applied.

I would also follow your idea of introducing an additional variable $$y_i$$ if the process of switching the machine on is done at time $$i$$. The first constraint makes sure that $$y_i$$ is 1 if the machine is being switched on at time $$i$$. Constraint 2, is just an auxillary constraint for index $$i=0$$, and the third constraint states that $$x_i$$ must be 1 if $$y_i$$ is 1, for $$M$$ hours.
\begin{align}y_i &= x_i - x_{i-1} \quad i=1,\ldots,I \\ x_0 &= 0 \\ x_{\min(i+t,I)} &\geq y_i \quad i=1,\ldots,I;\quad t=0,\ldots,M-1 \end{align}