Last app sensor nearly always reads as Home Assistant app
See original GitHub issueHome Assistant Android version: b7e4fb7b9b4b2ada30c23eeb42c255b7f082e619
Android version: varies
Phone model: varies
Home Assistant version:
Last working Home Assistant release (if known):
Description of problem:
The last app sensor is implemented using the usage stats that Android produces. However, it simply takes the last app that was reported as being in use.
On the devices I’ve run this on, this has no relationship to the app that the user thinks they are using, and in fact nearly all the time the last used app reported by the sensor is the Home Assistant app itself. This makes perfect sense - in order to send out the sensor data, the Home Assistant app has been woken up (and hence is in use in the background), and thus has the most recent usage stats record. I guess there is some asynchronisity to the usage gathering, since just occasionally another app is given as the last used one (perhaps it depends on what other apps are running at the same time, and whether they also do lots of background processing).
Looking via Git, I can see that during development this sensor was changed from returning the app package, to returning the app label if available, only falling back to the app package if not. I can understand that that gives a better display string, but it’s fundamentally flawed; the app label is not unique - for example I have multiple apps on my phone that call themselve “Maps” or “Camera”. For this sensor to be useful, it needs to return the package name which has uniqueness that allows scripts to be written knowing that they have identified the correct app. By all means also include additional information about the app as additional attributes, such as the app version, the display name, the app publisher etc.
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:
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (6 by maintainers)
Top GitHub Comments
I’ve put some extra diags in to a local build of the code. I think the sensor should probably be using lastTimeVisible rather than lastTimeUsed. (I’m also worried that the maxByOrNull will have random behaviour if two apps have the same lastTime value - I’ve just seen an example where there were 1 apart, so tiny differences are possible, possibly zero). Got to go now, but will try and investigate a bit more later.
I found some time to test out
lastTimeVisible
and the results are not as consistent for me when switching apps. I would personally expect every time I take a screenshot of the sensor screen then only the HA app would be visible but it is not.When I switch back to using
lastTimeUsed
everything goes back to reporting as I would expect.Also going to revert back to package name for the state to make it more unique.