android icon indicating copy to clipboard operation
android copied to clipboard

high accuracy mode shouldn't turn on while inside zone

Open yshui opened this issue 11 months ago • 26 comments

Steps to reproduce

  1. enable background location
  2. enable high accuracy when entering zone
  3. set high accuracy mode trigger range for zone to something > 0 (say, 300m)
  4. enter selected zone

Expected behavior

When I am far away from the selected zone, high accuracy should be off. When I get within 300m of the zone, high accuracy should turn on. After I have entered the zone, it should turn off.

Actual behavior

High accuracy stayed on after I have entered. Sometimes, it even turns itself on when I am inside the zone.

yshui avatar Jan 20 '25 14:01 yshui

Your expected behavior is correct (see example), so this is most likely bug.

In Settings > Companion app > Troubleshooting > Location history, you can check all received locations by the app - look for events saying geofence enter/exit, as that is used to toggle the mode on and off. Maybe some expected events are missing.

If there are no obvious issues there, could you reproduce the issue and provide the app logs, and more details about the device? You'll want to redact the coordinates in the logs. Then we can maybe find out what the issue is.

jpelgrom avatar Jan 20 '25 19:01 jpelgrom

I saw a "zone entered" event in the log when i should already be in the zone, it's marked with a "very old location". don't know what that means

yshui avatar Jan 20 '25 23:01 yshui

That means that when the app received the zone event, the location attached to it was >5 min old. The fact that it happened while you were already in the zone shouldn't be a major issue, geofences are expected to have up to a few minutes delay and the location attached to it isn't used for high accuracy mode. Make sure the app is allowed to run in the background/battery usage is unrestricted to reduce those delays as much as possible.

Any other zone events? With one there's not much to go on.

jpelgrom avatar Jan 21 '25 20:01 jpelgrom

geofences are expected to have up to a few minutes delay

I wasn't being clear enough. There was already a previous zone enter event for that zone. About a couple of hours later, without me going anywhere, there was another zone enter for the same zone, with "very old location". I think high accuracy mode was enabled around that time too (not 100% sure).

yshui avatar Jan 21 '25 23:01 yshui

also, is there a way to export the location history? because high accuracy was enabled for hours (hence the bug report), it was sending location every 5 seconds. so there are a huge number of events I have to scroll through every time.

edit: oops, too late. the events are already gone :/

yshui avatar Jan 22 '25 00:01 yshui

There is no way to export the location history list.

Double checked the code for receiving a zone again but I don't see how that would cause your issue.

Unfortunately there isn't really any useful information yet. It would be best if you could open the app, reproduce the issue (you can turn the screen off), and when done get the debug logs from Settings > Companion app > Troubleshooting > Show and share logs. This will be very long but should include precise details for why the app is enabling/disabling high accuracy mode.

jpelgrom avatar Jan 24 '25 18:01 jpelgrom

the logs don't go far back enough. i noticed that high accuracy mode turned on in the background and open the app. the logs only started after i opened the app and nothing before that was there. is there another way or do i need to keep the app open at all times?

yshui avatar Jan 24 '25 19:01 yshui

also i hit this again and that "Zone entered (very old location)" event correlate exactly with when the high accuracy mode was enabled.

again the same thing, "zone entered" first, then about 2 hours later, "zone entered (very old location)" and high accuracy turned on at the same time.

yshui avatar Jan 24 '25 19:01 yshui

does this hypothesis make sense:

  1. me enter zone, phone sends zone entered, and thinks i am in the zone.
  2. because of, e.g. gps inaccuracies, my coordinates go out of the zone, phone thinks i am out of the zone, turns on high accuracy, but skips sending zone exit because location is old.
  3. my coordinates return to the zone, but because of the skipped zone enter event, phone thinks the zone didn't change and doesn't turn off high accuracy mode.

yshui avatar Jan 24 '25 20:01 yshui

again the same thing, "zone entered" first, then about 2 hours later, "zone entered (very old location)" and high accuracy turned on at the same time.

can you check the locations of several reports in the history both before and after that very old event? do they all look correct or do you see any of them jumping around unexpectedly?

dshokouhi avatar Jan 24 '25 20:01 dshokouhi

i can see the skipped very old event has gpsAccuracy=100 so it's not accurate.

i can probably hack around this with a notification command...

yshui avatar Jan 24 '25 23:01 yshui

To be clear, best practise is that the high accuracy goes off after configureable time (1min) in home zone. This way homeassistant knows when to open the gate.

florian-asche avatar Feb 16 '25 15:02 florian-asche

Steps to reproduce

1. enable background location

2. enable high accuracy when entering zone

3. set high accuracy mode trigger range for zone to something > 0 (say, 300m)

4. enter selected zone

Expected behavior

When I am far away from the selected zone, high accuracy should be off. When I get within 300m of the zone, high accuracy should turn on. After I have entered the zone, it should turn off.

Actual behavior

High accuracy stayed on after I have entered. Sometimes, it even turns itself on when I am inside the zone.

i can confirm this bug with at least 2 seperated installations

florian-asche avatar Feb 16 '25 15:02 florian-asche

I created this workaround:

alias: Toggle High Precision Based on Location
description: ""
triggers:
  - entity_id: device_tracker.your_phone
    zone: zone.home
    event: leave
    trigger: zone
  - entity_id: device_tracker.your_phone
    zone: zone.home
    event: enter
    trigger: zone
  - entity_id: device_tracker.your_phone
    zone: zone.gps_on
    event: enter
    trigger: zone
  - entity_id: device_tracker.your_phone
    zone: zone.gps_on
    event: leave
    trigger: zone
actions:
  - if:
      - condition: zone
        entity_id: device_tracker.your_phone
        zone: zone.home
    then:
      - delay:
          hours: 0
          minutes: 3
          seconds: 0
          milliseconds: 0
      - data:
          message: command_high_accuracy_mode
          data:
            command: force_off
        action: notify.mobile_app_your_phone
    else:
      - if:
          - condition: zone
            entity_id: device_tracker.your_phone
            zone: zone.gps_on
        then:
          - data:
              message: command_high_accuracy_mode
              data:
                command: force_on
            action: notify.mobile_app_your_phone
        else:
          - data:
              message: command_high_accuracy_mode
              data:
                command: force_off
            action: notify.mobile_app_your_phone
mode: parallel

florian-asche avatar Feb 17 '25 21:02 florian-asche

i don't think anyone has commented on my hypothesis (https://github.com/home-assistant/android/issues/4997#issuecomment-2613290694) yet

yshui avatar Feb 22 '25 01:02 yshui

i don't think anyone has commented on my hypothesis (#4997 (comment)) yet

the only way to test your theory is to look at the app logs when the issue occurs as we print all the logic there

dshokouhi avatar Feb 27 '25 16:02 dshokouhi

@dshokouhi can you tell me how may i get the log? see my comment: https://github.com/home-assistant/android/issues/4997#issuecomment-2613266221 do i have to keep home assistant in the foreground and force my phone to stay awake all day? what if it doesn't happen with phone forced awake/hass in the foreground?

if you are familiar with the logic governing this, can you at least tell me if my theory is something that could happen given the code?

yshui avatar Feb 27 '25 16:02 yshui

the logs dont go back that far, depends on the OS when it decides to clear them for our PID, try to install an app like logcat reader so you can look at historical logcat LocBroadcastReceiver is the tag we use

if you are familiar with the logic governing this, can you at least tell me if my theory is something that could happen given the code?

we log every geofence event we get for this logic so none would be skipped there, we only skip device tracker specific updates

https://github.com/home-assistant/android/blob/master/app/src/full/java/io/homeassistant/companion/android/sensors/LocationSensorManager.kt#L762-L775

we even send an event when this happens:

https://companion.home-assistant.io/docs/integrations/app-events

the logs tell the best picture here because we can see exactly what the app does, troubleshooting location issues is tricky and that is why we have extensive logging here

dshokouhi avatar Feb 27 '25 16:02 dshokouhi

@dshokouhi looks like my hypothesis is exactly right.

so, first look at how high accuracy is enabled:

https://github.com/home-assistant/android/blob/eb12d9465de4391832209f1c52a78eaaa7974de0/app/src/full/java/io/homeassistant/companion/android/sensors/LocationSensorManager.kt#L540-L548

here, it uses lastEnteredGeoZones and lastExitedGeoZones to check last entered and exited zones.

note these are updated before the zone events are sent to the server.

updated here:

https://github.com/home-assistant/android/blob/eb12d9465de4391832209f1c52a78eaaa7974de0/app/src/full/java/io/homeassistant/companion/android/sensors/LocationSensorManager.kt#L765-L775

sent to server later here:

https://github.com/home-assistant/android/blob/eb12d9465de4391832209f1c52a78eaaa7974de0/app/src/full/java/io/homeassistant/companion/android/sensors/LocationSensorManager.kt#L926-L927

however, there are conditions that could prevent this send to happen. what i hit is this one:

https://github.com/home-assistant/android/blob/eb12d9465de4391832209f1c52a78eaaa7974de0/app/src/full/java/io/homeassistant/companion/android/sensors/LocationSensorManager.kt#L910-L914

(though i believe a "android.zone_entered/zone_exited" event is always sent, but i think that's separate thing from location updates).

in summary, zone change caused lastEnteredGeoZones and lastExitedGeoZones to be updated, which enables high accuracy mode. later this zone event is not sent to the server because of the SKIPPED_OLD condition. that's how the high accuracy mode gets enabled.

and IIUC, when high accuracy is enabled, the zone entered/exited events are no longer generated (as they were generated by android's geofence service, which is disabled when high accuracy is running), and disabling high accuracy is solely the server's responsibility. and the server can't do it because it doesn't know the phone has enabled it.

yshui avatar Feb 27 '25 20:02 yshui

high accuracy logic is not sent to the server but kept in the app directly, skipped events are for the tracker ad not enabling of high accuracy mode

and disabling high accuracy is solely the server's responsibility

that is incorrect, the server has nothing to do with high accuracy mode unless you take control yourself and send notification commands

dshokouhi avatar Feb 27 '25 20:02 dshokouhi

Because this issue seems to be similar to mine. I've attached my logs. It seems like my app thinks that im in the expanded zone, despite very clearly and accurately being within my regular home zone. Here, at the attached timestamp, I had just enabled "high accuracy when in expanded home zone" from the settings and as you can see, it thinks i'm in the expanded zone. Naturally, I removed any gps coordinates from the logs. The relevant lines should be around 03-18 13:10:29.706

Let me know if there's anything I can provide to help with this issue, or if this issue is considered different enough, I can create a new issue

homeassistant_companion_log_2-18-2025_13-10-50.txt

jack5mikemotown avatar Mar 18 '25 17:03 jack5mikemotown

so the app depends on both events to determine if you are just in the expanded zone or both home and expanded to make the decision, in your logs i can see we only got told about the expanded zone and not the home zone and the app just marks that. This depends on google telling the app about each geofence event and not the coordinates specifically. Testing standing still and toggling the settings may not yield proper results as we probably only got 1 geofence event and not 2. The only way to clear that variable and do a proper check would be to configure high accuracy mode as to your requirements and then force stop and start the app so the geofences can register again and hopefully both events will come. Your logs dont show any geofence events so the app is behaving as expected as it only thinks it is in the expanded zone.

03-18 13:10:29.538 11136 21283 D LocBroadcastReceiver: High accuracy mode enabled, because in zone [2_zone.home_expanded]

to all others having this issue please let us know what app version you are on, as the original issue was an enhancement we did not get the actual version number impacted

dshokouhi avatar Mar 18 '25 17:03 dshokouhi

Unfortunately, I did not manage to capture this while it was in my logs, so all I have is this screenshot of the location tracking section of the logs (started at around 3pm, its 9pm now, so high accuracy mode has been on 6 hours) . I never left home in or near that time, and you can see here that Home assistant is reporting correctly that my phone's gps device tracker has been at home the entire duration of this happening, yet The app gets an old location, sees me entering a zone, and turns on high accuracy mode. I'm not sure how to replicate this behavior so that I can capture the logs while it's still there. Best I have for now is these 2 screenshots that show that this is an issue. I'll try to watch out for this issue so that I can get the logs in time. I'm currently on Version 2025.1.2-full, but I've been having the issue for a long time.

Image Image

jack5mikemotown avatar Mar 25 '25 01:03 jack5mikemotown

After making my last comment, I noticed the app disabled high accuracy mode on its own, so I went into the logs and grabbed near that part. I checked all of the coordinates and at all times both before and after high accuracy mode turned off, I was well within the bounds set by home assistant for my home zone. Maybe this can help. The first report of high accuracy mode being disabled is at 03-24 21:09:56.136

homeassistant_companion_log_2-24-2025_21-19-8.txt

jack5mikemotown avatar Mar 25 '25 01:03 jack5mikemotown

I finally managed to catch the issue starting in the logs. I get a zone entered entry at 18:18:10 and high accuracy mode turns on. I've been within my home zone the entire time. The app version is 2025.1.2-full

homeassistant_companion_log_3-1-2025_19-32-22.txt

Please let me know if this is of any help

jack5mikemotown avatar Apr 01 '25 23:04 jack5mikemotown

Here's one more log of the issue occurring. Hopefully this can help

homeassistant_companion_log_3-6-2025_2-48-45.txt

jack5mikemotown avatar Apr 06 '25 07:04 jack5mikemotown