One way to reduce the negative impact of farrowing crates on sow welfare is to limit confinement of sows from the onset of farrowing until the end of the critical period of piglets" life a few days after farrowing. In order to provide an indication of the time when sows should be confined in crates, ear tag-based acceleration data was modeled to provide the following two types of alarms: A "first-stage" alarm that indicates the beginning of nest-building behaviour, and a "second-stage" alarm that indicates the ending of the nest-building behaviour. In total, 53 sows were included in the experiment. Each sow had an ear tag with an accelerometer sensor mounted on the ear. Acceleration data were modeled with the Kalman filtering and fixed interval smoothing (KALMSMO) algorithm. It was possible to predict farrowing on the basis of increased activity in the validation dataset with a median of 8 h 51 min before the onset of farrowing. Alarms that indicated the need for confinement of the sow in a crate were generated with a median of 2 h 3 min before the onset of farrowing. These results suggest that the developed model should be sufficient to provide early warning of approaching farrowing and secondary alarm indicating the need to confine a sow in a crate.