Minimal version persistent notification with 'Always' enabled, causes SEVERE battery drain
Home Assistant Android version: 2022.9.1-minimal (also happened on 2022.8.0-minimal)
Android version: 10
Phone model: Oneplus 6T
Home Assistant version: 2022.8.7
Last working Home Assistant release (if known):
Description of problem:
Whenever i set the persistent connection to Always, after hours of leaving it that way, my battery would have drained significantly. If i set it back to Never, all is back to normal again.
Ive been wanting to report this right from the beginning when this feature for notifications on the minimal app became available, however ive been trying to find the most optimal settings to be able to use this feature without it draining my battery but unfortunately its still draining.
I have other apps, such as Linphone or Syncthing, that also have a persistent notification and do the same thing to keep the connections alive and would run also for the same amount of hrs as this minimal version, but the right side (percentage) in the screenshots is what counts as draining battery, their percentage is close to 0% or up to 1% but Home-Assistant Companion would show 6% or 9% as you can see below(even though its not open or actively used), meaning something is wrong. Maybe it would help to look at the code for Linphone's or Syncthing's persistent notification? Would really appreciate a fix for this as i really want to use the notification's feature for this minimal version without it affecting battery life.
Traceback (if applicable, to get the logs you may refer to: https://companion.home-assistant.io/docs/troubleshooting/faqs/#android-crash-logs):
Screenshot of problem:

Additional information:
Please note, i have completely disabled updating sensors as part of my attempt to find the optimal settings for this. So updating sensors is not at fault. As i have had sensors update before enabling persistent notification and never had this battery drain from the minimal version before.
This is to be expected, every device will have different drain. The app needs to maintain a constant connection so notifications can be picked up immediately or within 30 seconds. Otherwise if they are super delayed there is no point.
Some users notice very small drain.
@dshokouhi, the battery drain is very significant. Did you see the screenshots i posted?
I think i might know where the problem is. Its because of the option "Background Access" - if this is enabled, it disables "Optimize Battery" under its "Manage Battery Usage" of the app. So if i force "Optimize Battery" to be enabled, its fine, however then i get an error from the app saying that it needs access to the background, otherwise it kills the app after a while. This should be optional to the user, not forced to have both Background Access AND Persistent Connection. Or at least why should it disable "Optimize Battery"?
Why for example Linphone can Optimize Battery in the background AND keep notifications of calls on throughout the whole day without battery drain? They dont force you to disable "Optimize Battery" under the app's settings.
Just trying to make this feature useful without compromising battery usage, after all im the one that requested it: https://github.com/home-assistant/android/issues/1480
Id appreciate your feedback if you have any thoughts on the above.
Did you see the screenshots i posted?
yes but battery drain is to be expected and every device (and ROM) will experience different drain.
This should be optional to the user, not forced to have both Background Access AND Persistent Connection. Or at least why should it disable Optimize Battery?
Why for example Linphone can Optimize Battery in the background AND keep notifications of calls on throughout the whole day without battery drain? They dont force you to disable "Optimize Battery" under the app's settings.
It is not just a persistent connection but it is a Foreground Service linked to a notification. Disabling battery optimization is a requirement for that to work otherwise the app will crash with an error for not having access to run in the background. This is part of Android.
Just trying to make this feature useful without compromising battery usage, after all im the one that requested it: #1480
You may want to try another ROM or deal with the poor battery life. On my Pixel I don't see that much drain. As I said before different devices will have different results. Even poor cell signal can impact battery life due to this feature. The feature needs to maintain a constant connection to the server otherwise it will not work as desired. I am sure a very much delayed alarm notification will not be desirable.
There's probably room for improvement with regards to battery usage, but unfortunately it isn't as easy as "look at the code for [other app]". They probably have very different architectures and complex backends that make it hard to compare. While they might show a persistent notification the behavior can be very different. If you can provide a more specific pointer that is always welcome ☺️
So if i force "Optimize Battery" to be enabled, its fine, however then i get an error from the app saying that it needs access to the background, otherwise it kills the app after a while. This should be optional to the user, not forced to have both Background Access AND Persistent Connection. Or at least why should it disable "Optimize Battery"?
FYI while the app strongly recommends disabling battery optimizations when using the persistent connection it won't actually block it from working, it will always try and only stops if it receives an exception while trying to open the connection as a foreground service. So if you're getting the notification saying this, the app tried to (re)start the persistent connection in the background but was unable to do so because of an exception (limitation). More technical info here.
There's probably room for improvement with regards to battery usage,
What were you thinking we could improve here? The only thing I considered that would be the biggest impact is changing the ping/pong rate which is at 30 seconds. The big downside there is that the bigger the delay the bigger potential of delayed notifications. That delay is most noticeable when you are moving between cell > wifi or even bad cell reception. FCM users do have a fallback but minimal would be stuck until the connection is re-established.
I don't have anything specific in mind, it was just a general remark as I can't imagine the app being perfect.
FYI while the app strongly recommends disabling battery optimizations when using the persistent connection it won't actually block it from working, it will always try and only stops if it receives an exception while trying to open the connection as a foreground service. So if you're getting the notification saying this, the app tried to (re)start the persistent connection in the background but was unable to do so because of an exception (limitation).
Thats the thing, while Battery Optimization is not disabled, I noticed that it seems to kill the connection when switching between WiFi and Data or vice versa
I noticed that it seems to kill the connection when switching between WiFi and Data or vice versa
Yes thats to be expected because the call fails during that switch. The app checks that it is connected every 30 seconds and that is when it reconnects, given that the app has background permissions to perform the reconnect.
One thing you can try doing is checking developer options and enabling "mobile data always active" should minimize that downtime if not already enabled.