A device for detecting the state of a leaf of doors, gates or the like, including a magnetometer that can move with respect to a reference geometric plane that has a substantially fixed orientation with respect to an external magnetic field, the magnetometer being connected to a control unit that is configured to produce a movement of the magnetometer with respect to a preset position, on the basis of a measurement of the external magnetic field which is detected by the magnetometer; in addition or as an alternative to the magnetometer, the device can include an accelerometer.