Skip to content

high accuracy mode shouldn't turn on while inside zone #4997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
yshui opened this issue Jan 20, 2025 · 26 comments
Open

high accuracy mode shouldn't turn on while inside zone #4997

yshui opened this issue Jan 20, 2025 · 26 comments
Labels
bug Something isn't working location-tracking question Further information is requested

Comments

@yshui
Copy link

yshui commented Jan 20, 2025

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 yshui added the enhancement New feature or request label Jan 20, 2025
@jpelgrom
Copy link
Member

jpelgrom commented Jan 20, 2025

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 jpelgrom added bug Something isn't working location-tracking question Further information is requested and removed enhancement New feature or request labels Jan 20, 2025
@yshui
Copy link
Author

yshui commented Jan 20, 2025

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

@jpelgrom
Copy link
Member

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.

@yshui
Copy link
Author

yshui commented Jan 21, 2025

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
Copy link
Author

yshui commented Jan 22, 2025

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 :/

@jpelgrom
Copy link
Member

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.

@yshui
Copy link
Author

yshui commented Jan 24, 2025

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
Copy link
Author

yshui commented Jan 24, 2025

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
Copy link
Author

yshui commented Jan 24, 2025

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.

@dshokouhi
Copy link
Member

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?

@yshui
Copy link
Author

yshui commented Jan 24, 2025

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...

@florian-asche
Copy link

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
Copy link

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
Copy link

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

@yshui
Copy link
Author

yshui commented Feb 22, 2025

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

@dshokouhi
Copy link
Member

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

@yshui
Copy link
Author

yshui commented Feb 27, 2025

@dshokouhi can you tell me how may i get the log? see my comment: #4997 (comment) 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?

@dshokouhi
Copy link
Member

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

@yshui
Copy link
Author

yshui commented Feb 27, 2025

@dshokouhi looks like my hypothesis is exactly right.

so, first look at how high accuracy is enabled:

if (highAccuracyZones.isNotEmpty()) {
constraintsUsed = true
// (Expanded) Zone entered
val zoneExpEntered = lastEnteredGeoZones.isNotEmpty() && highAccuracyExpZones.containsAll(lastEnteredGeoZones)
// Exits events are only used if expended zones are used. The exit events are used to determine the enter of the expanded zone from the original zone
// Zone exited
val zoneExited = useTriggerRange && lastExitedGeoZones.isNotEmpty() && highAccuracyZones.containsAll(lastExitedGeoZones)

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:

if (zoneStatusEvent == "android.zone_entered") {
lastEnteredGeoZones.add(zone)
} else {
lastEnteredGeoZones.remove(zone)
}
if (zoneStatusEvent == "android.zone_exited") {
lastExitedGeoZones.add(zone)
} else {
lastExitedGeoZones.remove(zone)
}

sent to server later here:

serverManager(latestContext).integrationRepository(serverId).updateLocation(updateLocation)
Log.d(TAG, "Location update sent successfully for $serverId as $updateLocationAs")

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

} else {
Log.d(TAG, "Skipping location update due to old timestamp ${location.time} compared to $now")
logLocationUpdate(location, updateLocation, serverId, trigger, LocationHistoryItemResult.SKIPPED_OLD)
return
}

(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.

@dshokouhi
Copy link
Member

dshokouhi commented Feb 27, 2025

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

@jack5mikemotown
Copy link

jack5mikemotown commented Mar 18, 2025

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

@dshokouhi
Copy link
Member

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

@jack5mikemotown
Copy link

jack5mikemotown commented Mar 25, 2025

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
Copy link

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
Copy link

jack5mikemotown commented Apr 1, 2025

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
Copy link

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working location-tracking question Further information is requested
Projects
None yet
Development

No branches or pull requests

5 participants