System alarmclock updated every minute while tracking

Detailed description of the problem:
Sleep as Android is updating the system alarm clock every minute.

The internal alarmclock used by Android (the one you can also set via the normal clock app), seems to be updated 1 minute advanced to the current time, on every minute. This might be new behavior, as I only noticed this when changing phones (coming from a OnePlus 6T, android 11, to a OnePlus 9 Pro, android 12)

This behavior beaks other apps reading that alarm state, for example openHAB.

Steps to reproduce:

  • Set a alarm; Now you can see the system alarm being set at that same time.
  • Start sleep tracking; Now you will see the alarm time being updated every minute.

The openHAB logs, receiving updates from the system alarmclock, and MQTT, show this quite nicely:

# Setting my Alarm
Item 'AlarmClock' received command 2022-09-12T05:45:00.000+0000

# Starting tracking
Item 'SleepasAndroid_Event' changed from sleep_tracking_stopped to sleep_tracking_started

Item 'AlarmClock' received command 2022-09-11T21:10:17.000+0000
Item 'AlarmClock' changed from 2022-09-12T05:45:00.000+0000 to 2022-09-11T21:10:17.000+0000
Item 'AlarmClock' received command 2022-09-11T21:11:17.000+0000
Item 'AlarmClock' changed from 2022-09-11T21:10:17.000+0000 to 2022-09-11T21:11:17.000+0000
Item 'AlarmClock' received command 2022-09-11T21:12:17.000+0000
Item 'AlarmClock' changed from 2022-09-11T21:11:17.000+0000 to 2022-09-11T21:12:17.000+0000
Item 'AlarmClock' received command 2022-09-11T21:13:17.000+0000
Item 'AlarmClock' changed from 2022-09-11T21:12:17.000+0000 to 2022-09-11T21:13:17.000+0000

Version of Sleep as Android:

20220609 on Android 12

Hi, this is a consequence of Android’s doze mode, which severely complicates sensor batching. Sensor batching is a battery saving feature which enables the accelerometer to gather data in it’s own buffer and the phone can wake up just once in a while to collect the data.
Unfortunately the doze mode imposed limits on how often the phone can be woken up, so it rendered sensor batching unusable. We are using a workaround for that which uses fake alarm calls every minute to reliably wake the phone to collect data from sensor.
You can disable sensor batching in Setttings > Sleep tracking > Battery optimized , but the battery drain of sleep tracking will probably grow.

Thank you @lenka-urbandroid for the explanation.

I will try with the Battery optimized feature turned off, and see if this stops the alarm clock jumping around.

Would it be useful to add a event in the likes of ‘alarm_set’ with a value of the unix time of the next alarm, be something to consider? That way you’d be able to inform whatever is on the other end of the MQTT integration about when the next time is the alarm is going to ring.