home-assistant / android

:iphone: Home Assistant Companion for Android
https://companion.home-assistant.io/
Apache License 2.0
2.32k stars 641 forks source link

phone state sensor stopped updating instantly after android 14 update #4040

Open ea5522 opened 11 months ago

ea5522 commented 11 months ago

Home Assistant Android app version(s): 2023.11.2-full Android version(s): 14 Device model(s): sm-g998u Home Assistant version: latest Last working Home Assistant release (if known): issue happend after update to android 14 Description of problem, include YAML if issue is related to notifications: phone state sensor stopped working after update to android 14

Companion App Logs: not needed

Screenshot or video of problem:

Additional information:

zgbee commented 6 months ago

@dshokouhi as far as I know I've granted the proper background access. In my App battery usage settings, I have Allow background usage on, which has the description of "Enable for real-time updates, disable to save battery"

I went into the app preferences and took a screenshot of the All Permissions screen, in case that's helpful for verifying my settings: https://imgur.com/a/Lnf1Uqi

siw1973 commented 6 months ago

It seems to have as I've had no lost location tracking, but I've introduced an Octopus Energy Agile tab on our Energy Dashboard that my wife is more engaged with so I'm unsure as to whether it is the interactive sensor or the introduction of an HA Dashboard that she is actually checks.

Will report back later, but ir's a start.

I've actually had no drops on my wife's phone after adding the "compelling" element to one of the HA Dashboards and also adding the Interaction Sensor. I can't really validate which is keeping the app from closing in the background, but I don't have the Interaction Sensor on my phone, but of course I access the HA CA frequently.

@zgbee what phone do you have, assuming not a Pixel ?

zgbee commented 6 months ago

@siw1973 by the "compelling" element to you mean a card for the Interactive sensor? I had previously added one for the App Importance sensor, but not interaction. I'm not home for a couple days, but I've added an Interaction card to one of my dashboards and will try to test behavior this weekend.

I'm running Android 14 (currently build AP1A.240405.002) on a Pixel 7a

siw1973 commented 6 months ago

Hi there. unfortunatley the "compelling" element was actually an Octopus Agile Energy Rate card on my Energy Dashboard. This she is actually interested in and actually opens the HA Companion App at least every couple of days when looking at the optimum time to use the household appliances. We both open this a lot now as it can save upto 90% energy dependent on when we run the washing machine / dishwasher / dryer etc..

I added the Interaction Sensor to both of our Phone "Status" cards as well to see how often that triggers.

The point I was making was that as I added both changes together I can't tell which of the two changes has lead to the stable nature of the HA CA not being closed down by Android. Mine never closed down, but I always look at my HA CA at least 5 times a day so I suspect it's the actual opening of the App in the foreground that retains it running in the background.

With respect to the Pixel 7a, I have a Pixel 7 running the same build and don't see half of the options you show on your screen shot when accessing App Permissions ? Through which route do you access them ?

zgbee commented 6 months ago

Ah, ok. Well.. it'll be interesting to see if adding the interaction sensor to my dashboard has any effect.

As for the permissions, it was a hidden screen. I went into App info (via the press-hold icon and selecting App info from the menu), then Permissions, and then using the 3-dot menu icon in top-right to select All permissions (didn't realize that existed till yesterday, ha)

siw1973 commented 6 months ago

Found it thank you 🤣 Learnt something new today!

I have exactly the same permissions, good to know.

natebgamer commented 6 months ago

I added the interactive card to my dashboard. At first it was working every time even when my phone was locked but on. It would report "On" but like the issues above, after a while it seems all sensors just stop updating due to the app being put to sleep or whatever. I recently moved to a newer phone (S21>S22) so redid all permissions etc and still have the same issues with sensors stopping working after a while if the app hasn't been opened recently.

EDIT: By saying "stopped working" I don't mean stop working completely just delayed sensor updates even when on charge with "fast updates" enabled when on charge.

dshokouhi commented 6 months ago

I added the interactive card to my dashboard. At first it was working every time even when my phone was locked but on. It would report "On" but like the issues above, after a while it seems all sensors just stop updating due to the app being put to sleep or whatever. I recently moved to a newer phone (S21>S22) so redid all permissions etc and still have the same issues with sensors stopping working after a while if the app hasn't been opened recently.

no the issue is not that all sensors stop updating, thats definitely a background access issue. The issue here is that some intents do not arrive in order to update the phone state sensor quickly. Your issue is unrelated here. Please file a new issue and make sure to go through all device settings to turn off any and all battery /power/data savings.

natebgamer commented 6 months ago

I added the interactive card to my dashboard. At first it was working every time even when my phone was locked but on. It would report "On" but like the issues above, after a while it seems all sensors just stop updating due to the app being put to sleep or whatever. I recently moved to a newer phone (S21>S22) so redid all permissions etc and still have the same issues with sensors stopping working after a while if the app hasn't been opened recently.

no the issue is not that all sensors stop updating, thats definitely a background access issue. The issue here is that some intents do not arrive in order to update the phone state sensor quickly. Your issue is unrelated here. Please file a new issue and make sure to go through all device settings to turn off any and all battery /power/data savings.

Sorry yes I misspoke with saying "Stop working" instead of delayed, but my issues are definitely the same as all those listed above where sometimes it can take a while for the sensors to update.

dshokouhi commented 6 months ago

but my issues are definitely the same as all those listed above where sometimes it can take a while for the sensors to update.

That's really not the same here. One state that does not appear long is ringing. If the phone misses that intent then indeed the state will be missing. As far as I know the other states are working. Your issue described still sounds like background access if it impacts all sensors for a while.

siw1973 commented 6 months ago

I had this go down on the wife's phone last week. Looks like what was shiny, new and compelling is no longer compelling......

I know this is annoying, but is it possible to force the app into the foreground once a week from Home Assistant to ensure it does not close ? I know this is a total hack, but I'd like to have this reliably working.

dshokouhi commented 6 months ago

I know this is annoying, but is it possible to force the app into the foreground once a week from Home Assistant to ensure it does not close ? I know this is a total hack, but I'd like to have this reliably working.

https://companion.home-assistant.io/docs/notifications/notification-commands#webview

this command will open the app

hatchling commented 6 months ago

Just to add a data point:

After migrating from my affected Pixel 5 to a new Pixel 7 Pro (which is on Android 14) I no longer experience this issue. The Pixel 7 charger type state updates instantly.

My wife's Pixel 6 is still affected.

I suspect there is more to this issue than strictly the Android version but unfortunately don't have hard data to back this up.

zgbee commented 6 months ago

fwiw, adding a card for the interaction sensor to my dashboard didn't seem to have any effect. I'm travelling right now so it has to wait a few days, but I'm actually not sure if I've tried manually opening the home assistant app just before I expect the automation to trigger - i.e. if the app is in foreground and phone is unlocked, will the sensor update instantly?

siw1973 commented 6 months ago

@dshokouhi Unfortunately I don't agree with the title change. I have the Companion App stopping sending any information to HA, not instantly, but full stop.

Of course if the App is brought to the foreground that is a separate "hack" and is something that never used to have to be done when I was on Android 13.

If you want me to open up a separate issue then fine, but a number of us are having this issue which has been discussed here.

dshokouhi commented 6 months ago

Unfortunately I don't agree with the title change. I have the Companion App stopping sending any information to HA, not instantly, but full stop.

Of course if the App is brought to the foreground that is a separate "hack" and is something that never used to have to be done when I was on Android 13.

Then that is not the same issue and you should create a new one with logs. This issue is about a device with missing intents that are used to update instantly. Full stop means there is something else at play. Every 15 minutes the sensor worker runs and that does not use intents and will pull the actual data unless there is an error. So full stop is not the same issue here. I think some users are conflating the actual issue with other similar issues here.

rezadast2 commented 4 months ago

Adding my name to the list here where I have observed the exact same behavior as discuss in this thread. Pixel 8 Pro on Android 14. Background access is granted. Battery Optimization is disabled. Sensors are updated immediatley when the App is in the foreground. Once the app is in the background, App Importance goes to "cached" and my sensors are not updated immediately and can take up to 15 minutes.

I noticed this behavior as soon as I got this Pixel 8 Pro. My previous phone was a Pixel 6 Pro on A13 with no issues. I believe there was a 2 week period where my P6Pro was on A14 and did not experience this behavior, however I cannot guarantee that.

I made a reddit thread in /r/homeassistant here, not thinking that github was the better place to report my findings. I just assumed the App needed to be updated so I left it alone for a few months. But as my automations get more complex, this issue is starting to be quite annoying, so here I am.

As i read through this thread, this comment below looks to have some promise, but I dont know what this really does:

I have the same problem on my Nothing Phone (1) with Android 14. I checked that there was no battery optimization and that the HA app has background access. The HA app goes into cached mode and then the sensors don't update unless I'm launching the HA app.

I managed to solve the problem in part as follows:

  • activate the hidden developer options
  • going to "suspend execution for cached apps"
  • select Disabled (was on "device default")
  • restart

Then the sensors update as expected ONLY if I leave HA in the recent app screen. If I clear the recent apps list, I have to launch HA again to update the sensors.

When I change the following setting in Developer Options: "Suspend execution for chached apps" > Disabled, My sensors are now updated instantly even when app is in background. I've only tested this for 15 minutes or so, but when app is in background, the App Importance is still "cached" but my sensors are updated instantly when I plug/unplug my phone to the charger.

I doubt this is the real solution to the problem, but wanted to call out my findings.

zgbee commented 4 months ago

I've kinda given up on this ever working. Even if I have the Home Assistant app open, with screen unlocked, and watch my phone join my home WiFi network via the menu bar icon change.. the automation that should trigger still doesn't seem to happen for another 5-10 minutes.

tankdeer commented 3 months ago

I stumbled upon this issue trying to determine if what I've been experiencing the past few weeks is known issue or not.

I am experiencing the same issue as others. Pixel 7. Android 14. Build AP2A.240705.004

In my case it's the charging/charger type sensors that I've noticed, but it seems like it's all "updates instantly" sensors. As mentioned I've only really noticed this the past few weeks, maybe a month. I think like many I use charging status to trigger both good night & good morning routines, and this has made that very annoying.

What's more disappointing is the apparent lack of interest from the devs to research further. I think we all realize this is an open source project, and that devs are always stretched thin, and I for one certainly appreciate that. I think it's also painfully obvious that this is not a configuration issue in the sense that it's not a couple users who accidentally turned on battery saver or disabled background usage. This is dozens of people reporting the same thing in an area that has worked without issue for, in most cases, several years. There is obviously a compatibility issue with the Companion App & Android 14 - something has changed. As for what I have no idea. Nor do I know why some people have issues and others don't, but this is only going to get worse unless a real solution is found.

I think like many I am happy to debug, provide logs, whatever. But we need somebody actively looking into this on the HA side. Keeping the app open all the time is not really a viable workaround.

dshokouhi commented 3 months ago

What's more disappointing is the apparent lack of interest from the devs to research further.

I dont know where you got this from, we have been unable to reproduce the issue and have made attempts to fix the issue and stay active responding to users like yourself. Unless the developers can reproduce the issue teh next best thing is for a user who knows kotlin and is experiencing the issue can help contribute and provide a fix.

tankdeer commented 3 months ago

What's more disappointing is the apparent lack of interest from the devs to research further.

I dont know where you got this from, we have been unable to reproduce the issue and have made attempts to fix the issue and stay active responding to users like yourself. Unless the developers can reproduce the issue teh next best thing is for a user who knows kotlin and is experiencing the issue can help contribute and provide a fix.

Perhaps that came out wrong, and for that I apologize. It just seems like this is often being dismissed as a configuration/battery saver type issue, when I truly believe that is not the case. Numerous people have confirmed they everything configured correctly and still have the issue.

dshokouhi commented 3 months ago

Perhaps that came out wrong, and for that I apologize. It just seems like this is often being dismissed as a configuration/battery saver type issue, when I truly believe that is not the case. Numerous people have confirmed they everything configured correctly and still have the issue.

I get that but at the same time numerous people have it working just fine, so yea on the surface it may appear to be a configuration issue. Often times when you are troubleshooting an issue thats pretty much where you start if you are unable to replicate the issue.

tankdeer commented 3 months ago

Often times when you are troubleshooting an issue thats pretty much where you start if you are unable to replicate the issue.

Starting there sure, but unless there are some hidden settings that numerous people have overlooked, myself included, it seems like that is not the case. (Trust me, I would love nothing more than for this to be that simple)

Personally I have a suspicion this is an android bug or other internal functionality change that is only manifesting on certain devices. But of course that is just a guess and I would not know if it was something that could be coded around

tankdeer commented 3 months ago

I am back and ready to eat some crow. 😬

I am about 95% sure I have found and resolved the issue. It was a setting, however one that was not mentioned earlier. In my case it seems like a lot of my server settings on the companion app had been wiped out including importantly here - "Persistent connection". It was set to "Never". The description implies it's only used to send notifications, but it seems like underneath it's likely used for more than that. In my case I set it to "On home WiFi only", since that's all I really care about, and now everything instantly updates again, even with the app closed, phone locked, idle for hours, etc.

I'd recommend anybody having this issue check your settings as well. They are located in the companion app:

Settings -> Companion App -> Servers & Devices (Click on your HA server) -> Persistent Connection.

In my case my home WiFI SSIDs and internal connection URLs had also been lost, not sure what happened.

Hope it helps.

Ragingfire105 commented 3 months ago

I held off on reporting that mine has been working lately because I wasn't sure why. It seems we had the same issue I just hadn't put it together until I read this. I was working on a notifications issue when I discovered mine had been also reset to none for the persistent notification as well.

I do have to wonder, is this meant to be a requirement? If not I hope this helps get to the bottom of the issue.

hatchling commented 3 months ago

I do have to wonder, is this meant to be a requirement? If not I hope this helps get to the bottom of the issue.

A persistent connection is not required for this feature to work. My fairly vanilla Pixel 7 Pro is set to "Persistent connection: Never" and it works perfectly and instantly regardless of the phone state.

If changing this helps, it could be taken as a partial workaround for some, but it's not the root cause.

rezadast2 commented 3 months ago

Going to chime in here and report back from my findings from about a month ago.

First, I want to address the setting "persistent connection" being the solution. Yes, turning this on made all my sensors update immediately, but this also absolutely killed my phone battery. So I would disagree this is a solution to the problem. Read my reddit thread I linked in my comment up above here.

Second, there was a user that posted earlier about going into the devolpoer options and change the following settings.

activate the hidden developer options going to "suspend execution for cached apps" select Disabled (was on "device default") restart

I have no idea what this setting actually does, but I have had these settings on for over a month now, and this has fixed my issue. All sensors update instantly and I almost never open the Comapanion App (meaning it persists after rebooting my phone and still not opening the app my sensors are updated instantly). I also don't believe this to be the "Solution", but maybe this helps the developers in this thread on identifying the real issue.

tankdeer commented 3 months ago

In my experience "suspend execution for cached apps" made absolutely no change. 🤷

rezadast2 commented 2 months ago

Just tested this again as a sanity check for myself.

  1. First I checked that the setting was disabled "Suspend execution for cached apps > Disabled" and confirmed that my sensors were updated immediately.
  2. Made zero changes but did a reboot. Confirmed sensors were still updated immediately.
  3. Changed the setting to "Device Default" and rebooted. Sensors were delayed.
  4. Changed the setting back to "Disabled" and rebooted. Sensors were back to immediate updates.

Throughout this testing, not once did I open the HA Companion application on my phone. And how I test my sensors is I pull up HA on my desktop browser and plug my phone into and out of the charger with the screen off, and check that it shows the correct charging status immediately.

zgbee commented 2 months ago

So after all the recent chatter in this thread, I decided to revisit this and enabled developer mode so I could toggle the "Suspend execution for cached apps" to Disabled, reboot my phone, and as @rezadast2 mentioned load up a dashboard on my laptop to monitor the WiFi status of my phone (a sensor I had enabled months ago but turned off the automation for since it was essentially non-functional).

Despite not having opened the HA app on my phone recently, and the required reboot after the dev setting change, the WiFi card on my dashboard updated (essentially) immediately when toggling my phone's WiFi off and on.

(edit: and the HA Companion app's Persistent Connection setting is at Never - just confirmed after my other test)

dshokouhi commented 1 month ago

Just wanted to check in with the users who found a potential workaround/fix how has it been going now thats been over a month? are the sensors updating as you would expect now?

tankdeer commented 1 month ago

Still working perfectly here with persistent connection enabled. Yes it does seem to increase battery usage, but that is a tradeoff I was willing to make to have this functional. I may experiment further with it disabled, but for the time being I am happy

dshokouhi commented 1 month ago

Still working perfectly here with persistent connection enabled. Yes it does seem to increase battery usage, but that is a tradeoff I was willing to make to have this functional. I may experiment further with it disabled, but for the time being I am happy

have you tried enabling various sensors other than the ones mentioned? the last notification sensor might help keep things awake without requiring the persistent connection. it requires a special permission when enabled.

tankdeer commented 1 month ago

I previously went through all the options mentioned above with no effect. The only one that made a difference was the persistent connection.

I just played with a bit now, since it's been a while and there have been android updates since then. It appears at least on my Pixel 7 that it's improved, but not completely resolved. Meaning with persistent connection disabled, it does appear to update real time unless app is force stopped (makes sense), or the phone is locked. If the phone is locked I am thinking it's on the 15 minute interval. However with the phone unlocked, screen on or off, app not in "recent", it appears to be updating real time.

That is better, but since my primary use case is as part of my "in bed" sensor, I need it real time even if locked.

I did not try the last notification sensor. I can, however it states that it increases battery usage and if I am doing that, I don't see the advantage over persistent connection which also increases battery usage.

rezadast2 commented 1 month ago

Setting "suspend execution for cached apps" to disabled under developer options is still working for me as a workaround.

But I also still believe this should not be the solution to the problem. I am using this as a workaround in hopes a real solution is provided.

dshokouhi commented 1 month ago

I did not try the last notification sensor. I can, however it states that it increases battery usage and if I am doing that, I don't see the advantage over persistent connection which also increases battery usage.

by default this sensor will not send any updates to HA server without an allow list. The note is more for users who disable teh allow list because that can send updates quite frequently. The persistent connection pings the server every 30 seconds. I think your phone may get less notifications than that. I would say its worth a shot. FWIW I am personally using this sensor to update me on food delivery status :) its one of my favorite sensors in the app :)

from the docs: https://companion.home-assistant.io/docs/core/sensors#last-notification

You need to either create an allow list or enable the setting to "Disable Allow List Requirement". Keep in mind without an allow list this sensor has the potential to drain a lot of battery. We highly recommend creating an allow list over disabling this requirement.

But I also still believe this should not be the solution to the problem. I am using this as a workaround in hopes a real solution is provided.

This is not an easy issue to work around because the app is getting intents not delivered by the OS. Which is the only real way we should be updating these sensors. Have you noticed any battery life issues since enabling that dev option?

tankdeer commented 1 month ago

I will play with the last notification sensor as a possible alternative, thanks

tankdeer commented 1 month ago

Following up after a few days. Per the above recommendation I disabled persistent connection and enabled the last notification sensor. I picked a couple random apps to add to the list since I don't currently have a use-case for them.

So far this has been an improvement. My sensors are back to updating real time, and battery life is improved over the persistent connection.

I am happy using this as a long term solution/workaround. Thanks

dshokouhi commented 1 month ago

thanks for teh update!!

and battery life is improved over the persistent connection.

curious if you notice a difference from before this issue started if enabling this sensor added to the drain but i understand lots of time has passed so may not be easy to tell :)

tankdeer commented 1 month ago

thanks for teh update!!

and battery life is improved over the persistent connection.

curious if you notice a difference from before this issue started if enabling this sensor added to the drain but i understand lots of time has passed so may not be easy to tell :)

Hard to say for sure since as you said it's been a while. I'd say it's likely comparable to what it was before this all started. Close enough at least.

rezadast2 commented 1 month ago

Have you noticed any battery life issues since enabling that dev option?

I have not noticed any negative battery life since changing the dev option.

On that note, wanted to check in with another data point. I have upgraded to a Pixel 9 Pro and experience the same behavior. Needed to change "suspend execution for cached apps" > disabled. After doing this, instant sensor updates when the app is not at the foreground.

carlomoretto27 commented 1 week ago

Same issue with S22 and S22 ultra.

Only thing that works for me is persistent connection being on. Disabled the suspend execution for cached apps but that made no difference.

What other workarounds have people found as I don't want our batteries draining just to have location updates work

dshokouhi commented 1 week ago

What other workarounds have people found as I don't want our batteries draining just to have location updates work

try to enable the last notification sensor and grant the permission, no need to set an allow list ot disable it unless you actually want to use the sensor. Granting the permission should be enough. A user reported earlier that worked for them and teh battery impact was negligible.

https://companion.home-assistant.io/docs/core/sensors#last-notification

carlomoretto27 commented 1 week ago

What other workarounds have people found as I don't want our batteries draining just to have location updates work

try to enable the last notification sensor and grant the permission, no need to set an allow list ot disable it unless you actually want to use the sensor. Granting the permission should be enough. A user reported earlier that worked for them and teh battery impact was negligible.

https://companion.home-assistant.io/docs/core/sensors#last-notification

Thank you so much. Have just tried and it seems to be working. Hopefully it carries on working