Companion App provides wrong next alarm time

On Xiaomi devices and also when you install some applications that you can get wrong next alarm time. Some applications use Alarm API to start application at for example midnight to sync data. On Xiaomi devices notorious example is Xiaomi Calendar (com.xiaomi.calendar). Because of that next alarm is set to midnight of local time (example for UTC+2)


After killing Calendar it started to report alarm properly.

I think it is a good idea to add some filter in the companion app that will by default remove such alarms.

Example from adb shell dumpsys alarm

  Next wake from idle: Alarm{df04dda type 0 when 1600120800000 com.xiaomi.calendar}
    tag=*walarm*:com.xiaomi.calendar.APPWIDGET_SCHEDULED_UPDATE
    type=0 expectedWhenElapsed=+5h2m46s594ms expectedMaxWhenElapsed=+5h2m46s594ms whenElapsed=+5h2m46s594ms maxWhenElapsed=+5h2m46s594ms when=2020-09-15 00:00:00.000
    window=0 repeatInterval=0 count=0 flags=0x3
    Alarm clock:
      triggerTime=2020-09-15 00:00:00.000
      showIntent=PendingIntent{c47b2b: PendingIntentRecord{f71bb88 com.xiaomi.calendar startService}}
    operation=PendingIntent{dc07921: PendingIntentRecord{61d146 com.xiaomi.calendar broadcastIntent}}

I’m not sure what the error is. Can you tell us what time you set the alarm at and what timezone?

And your image is misleading.
You have the cursor on the brown-ish area where the alarm was set to midnight local time (22:00 UTC) and the state and milliseconds show the time in the blue area to the right in the timeline.

OK, I will try to describe it other way. My timezone is UTC+2. In Clock application I have added alarm at 7AM of my time. This means that on all widgets in phone and in HA next alarm should be 7AM next day. But it is not true, next alarm has value at midnight (22:00:00 UTC on screen shot), after midnight alarm will be properly set to 7AM UTC+2 (5:00:00 UTC on screen shot). Alarm at midnight never ring, it just changes to next alarm in queue.

It is known issue in Android. Everything is caused by application that uses Alarms API to do some scheduled task. This alarm will never trigger sound or vibration, but will start application and execute some extra task. In my example it is Xiaomi Calendar app that probably is triggering sync task at midnight (maybe it is setting up everything for next day?). It would be great if Companion app would not report such applications as Next Alarm in HA

I would say this is an error on Xiaomi not on android or HA app.
The API does not specify why there is an alarm as far as I know so HA app can’t filter it out.

This is something that you need to take with Xiaomi

Yes, but it is not only Xiaomi Calendar, there are more applications that can produce false “next alarm” states. It starts to be problematic when your automations depends on next alarm status

That is how the API works in Android.
It only gives you next alarm, not all alarms.

My guess is that you need to build something homemade in the phone to get around this if it’s even possible.
Or ask Google to change it.

More like ask the app developer not to abuse an API designed for alarm clocks. Theres not much we can do if a manufacturer like to override Google’s default. If you use an app that respects the API you don’t have this issue. Samsung is another offender, if you switch to Google Calendar for example you don’t get this issue.