Find parking location using bluetooth

Hi,
I’ve been playing with this for a while and read the other solutions (such as Remember where I parked in home assistant and Using bluetooth to rember parking location) and come up with a solution that I think is worth sharing and requires no extra add-ons.

I’ve done it using bluetooth but you may be able to use something else like the Android Auto sensor. I’m assuming you have already paired your car to your phone via bluetooth prior to starting this. Here we go.

In Companion app turn on the ‘Bluetooth Connection’ sensor.
In HA go to Developer Tools>States and find your mobile device. Look down the list of paired bluetooth devices for the one that looks like your car. Note the name and the MAC address.

Next go to Developer Tools>Services. Use device_tracker.see to create a new device tracker. For GPS enter any coordinates you like (the below location is The Great Pyramid of Giza)

service: device_tracker.see
data:
  dev_id: car_gps
  gps:
    - 29.978617498377062
    - 31.132829327280007

This will create write a configuration for the device tracker in known_devices.yaml. Open known_devices.yaml and complete the details for the device tracker created by running the device_tracker.see service. You can add an icon and/or a photo for it. If using a photo you need to put it in “/config/www/” and use “/local/” as the path in known_devices.yaml.

This is what device_tracker.see will put in known_devices.yaml:

car_gps:
  name: car_gps
  mac:
  icon:
  picture:
  track: true

This is the modified version - I’ve deleted the mac line but you can leave it in.

car_gps:
  name: Stonic GPS
  icon: mdi:car-sports
  picture: /local/car.jpg
  track: true

Go to Settings>Devices & Services>Helpers then “+ Create Helper” then choose “{}Template” and “Template a binary sensor”. create a binary sensor - make it so that it is on when the phone connects to the car and off when it disconnects using the name and MAC address you noted down earlier.

{{state_attr('sensor.myphone_bluetooth_connection', 'connected_paired_devices') != [] 
            and (('XX:XX:XX:XX:XX:XX (My Car)' in state_attr('sensor.phone_bluetooth_connection','connected_paired_devices')))}}

This checks to make sure your phone is connected to something via bluetooth, then checks to see if it is your car. I’ve used the full MAC address and name so it requires an exact match.

Restart HA and have a look at the map to see that your device tracker appears at the location you defined and with the photo/icon you assigned to it.

Create the automation using the binary sensor as the trigger.

alias: Find the Car
description: Get position and open Maps when parked
trigger:
  - platform: state
    entity_id:
      - binary_sensor.in_car
    from: "on"
    to: "off"

The automation has several actions:

Update device tracker with the location of your phone when bluetooth is disconnected - this might be a few metres from your car but should be close enough.

action:
  - service: device_tracker.see
    data:
      dev_id: car_gps
      gps:
        - "{{state_attr('device_tracker.phone','latitude')}}"
        - "{{state_attr('device_tracker.phone','longitude')}}"

TTS notification that the car position is stored (this is optional)

  - service: notify.mobile_app_phone
    data:
      message: TTS
      title: 🚗Car position saved
      data:
        tts_text: >-
          Your car position is saved. Don't click the notification until you are
          ready to find your car.

Normal notification with button to press when you are ready to go back to your car

  - service: notify.mobile_app_phone
    data:
      message: Open this when ready to find the car
      title: 🚗Car position saved
      data:
        actions:
          - action: Find Car
            title: Find the car
        notification_icon: mdi:car-sports

Action that waits for you to click the notification

  - wait_for_trigger:
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: Find Car

Final action to open your maps app and navigate to your car. This one is for Google Maps:

  - service: notify.mobile_app_phone
    data:
      message: command_activity
      data:
        intent_package_name: com.google.android.apps.maps
        intent_action: android.intent.action.VIEW
        intent_uri: >-
          google.navigation:q={{state_attr('device_tracker.car_gps','latitude')}},{{state_attr('device_tracker.car_gps','longitude')}}&mode=w

This version is for Here Maps:

  - service: notify.mobile_app_phone
    data:
      message: command_activity
      data:
        intent_package_name: com.here.app.maps
        intent_action: android.intent.action.VIEW
        intent_uri: >-
          https://share.here.com/r/{{state_attr('device_tracker.car_gps','latitude')}},{{state_attr('device_tracker.car_gps','longitude')}}?m=w

The bits after the last brackets of the above are to give you walking directions to the car.

The mode of the automation needs to be set to restart so that it will start again if you move your car again without clicking the notification. Using the visual editor go to the 3 dot menu at the top then select ‘Change Mode’ then ‘restart’ from the drop down. If editing in YAML then change the mode line to this:

mode: restart

As an optional extra, just in case something happens and you accidentally clear the notification and the navigation app, I’ve also created a map card in HA which shows the person and the car.

type: map
entities:
  - entity: person.lostthecar
  - entity: device_tracker.car_gps
title: Location map
hours_to_show: 4
dark_mode: false
default_zoom: 18

I put this on its own on a panel view so it is full screen on the phone.

And that should hopefully be all up and running.

You can repeat this for as many vehicles as you want. Or you could use it to track where you’ve left any bluetooth device you’ve used and disconnected from.

EDIT: Stopping the car from disappearing on restart
One thing to note is that after restarting/rebooting HA the location for the car will be lost. To get around that do the following:

Create a pair of input_numbers to hold the GPS coordinates. Go to Settings>Devices & Services>Helpers>+Create Helper>Number then create

car_lat as name then -90 as minimum, 90 as maximum
car_long as name then -180 as minimum, 180 as maximum

Go back to the automation and just after the device_tracker.see action add the following two actions:

  - service: input_number.set_value
    target:
      entity_id: input_number.car_lat
    data:
      value: "{{state_attr('device_tracker.car_gps','latitude')}}"
  - service: input_number.set_value
    target:
      entity_id: input_number.car_long
    data:
      value: "{{state_attr('device_tracker.car_gps','longitude')}}"

Everytime the position of the car is updated, the two input_numbers will be updated.

Create a new automation which is triggered when HA starts to set the tracker to the most recent position as stored in the input_numbers:

alias: Set vehicle tracker on restart
description: ""
trigger:
  - platform: homeassistant
    event: start
condition: []
action:
  - service: device_tracker.see
    data:
      dev_id: car_gps
      gps:
        - "{{ states('input_number.car_lat') }}"
        - "{{ states('input_number.car_long') }}"

The input_number values survive restarts so the car will appear at the same position it was just before the restart.

6 Likes

Thanks,
This has been absolutely awesome and helpful.

I added a TTS output, when the car is parked also:

  - service: notify.mobile_app_XXX
    data:
      message: TTS
      title: 🚗Car position saved
      data:
        tts_text: >-
          Your car position is saved. you parked at "{{
          states.sensor.XXX_geocoded_location.attributes.name,
          states.sensor.XXX_geocoded_location.attributes.thoroughfare ,
          states.sensor.XXX_geocoded_location.attributes.locality  }}"

I found the updated position to lag a bit behind and only be really up to date when I used a navigation app in parallel.
Which Companion app settings in terms of location do you recommend?

Thanks for the feedback.

I’ve got the bluetooth sensor that updates immediately. Then for location:

  • Background Location: Minimum Accuracy = 200, Location Sent = Exact
  • Location Zone: Minimum Accuracy = 200
  • Single Accurate Location: Minimum Accuracy = 200, Minimum time between updates = 60000

I have sensor update frequency set at normal.

You might get a quicker update using the Android Auto connection sensor. If you are connecting by cable that should trigger as soon as you disconnect. The car bluetooth connection ends a bit after turning the car off I think.

1 Like

Thanks!
For some time I have been trying to get something like this working. I think this is what I’m looking for or comes close anyway.
There were two things I hadn’t solved yet:

  • keep the location after restarting HA
  • driving different cars (my own car and my wife’s) messes things up

When I have the time I’m going to rewrite it using your example

So I tried this solution. I think it’s a lot more flexible than the other ones. I only have a strange problem and I can’t seem to figure out what it is causing it. It works one time and then it stops the next time the car is parked somewhere else. When I look at the trace timeline I get Stopped because the maximum number of parallel executions has been reached at followed by the time of that trace.
I’ve got no clue where to search. Any help would be very much appreciated.

Oops I forgot that detail. Make sure you have the automation mode as restart. If editing in the visual editor go to the 3 dot menu at the top then select ‘Change Mode’ then ‘restart’ from the drop down.

If using YAML then change whatever mode line you have to this:

mode: restart

In the other modes if you park your car it waits for you to click the notification before finishing. In restart mode, each time you park it ends the previous run and starts a new run.

I’ve added that to the original post now.

Thanks for your reply. I changed it to restart now.

It is working great this way! Thank you for your code. It was a good starting point. I changed one thing: I moved the walking navigation maps to a script and made it an Android tile. That way it is always available and I don’t need the part that waits for a trigger anymore.

The only thing I still have to implement yet is me and my wife both driving her car.

Happy to help.

I didn’t think of creating a tile for it but that is a great idea. Can you share your script please?

I would be interested to know if it works with two people driving the same car. I think it should as it is looking for the car connecting and disconnecting with each phone. It might be that both phones can connect at the same time if you’re in the car together so you might both get notifications.

This is the script that I use for the tile:

alias: Find car
sequence:
  - service: notify.mobile_app_xxxx
    data:
      data:
        intent_package_name: com.google.android.apps.maps
        intent_action: android.intent.action.VIEW
        intent_uri: >-
          google.navigation:q={{state_attr('device_tracker.car_gps','latitude')}},{{state_attr('device_tracker.car_gps','longitude')}}&mode=w
      message: command_activity
mode: single
icon: mdi:car-search

I didn´t add a second driver yet. Have to find the time to do that. The car has an old Parrot carkit, so it is not possible to connect 2 phones at the same time. You’ll never know upfront which phone connects first.

Thanks for sharing this. The script as a tile works really well.

Hi, guys,
I’ve been trying some similar aproach and have mixed results. I created automation below, which save the position of device tracker of my phone (phone1), or wife’s phone (phone2) to text helpers (separate for latitude and longitude). Custom sensor in configuration.yaml was created, when attributes were set from text helepers (after converting value to float ofcorse). The custom sensor can be shown on maps in HA, what is crucial for me.

alias: Last parking
description: ""
trigger:
  - platform: template
    value_template: >
      {{state_attr ( 'sensor.phone1_bluetooth_connection',
      'connected_paired_devices') == ['QQ:QQ:QQ:QQ:QQ:QQ (car AVN)'] }}
    enabled: true
    id: me
  - platform: template
    value_template: >
      {{state_attr ( 'sensor.phone2_bluetooth_connection',
      'connected_paired_devices') == ['QQ:QQ:QQ:QQ:QQ:QQ (car AVN)'] }}
    id: wife
  - platform: state
    entity_id:
      - binary_sensor.phone1_android_auto
    from: "off"
    to: "on"
    id: me android auto
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - me
              - me android auto
        sequence:
          - wait_for_trigger:
              - platform: template
                value_template: >
                  {{state_attr ( 'sensor.phone1_bluetooth_connection',
                  'connected_paired_devices') != ['QQ:QQ:QQ:QQ:QQ:QQ (car AVN)'] }}
                enabled: true
              - platform: state
                entity_id:
                  - binary_sensor.phone1_android_auto
                from: "on"
                to: "off"
          - service: device_tracker.see
            metadata: {}
            data:
              dev_id: phone1
          - service: input_text.set_value
            metadata: {}
            data:
              value: "{{ state_attr ('device_tracker.phone1', 'longitude')}}"
            target:
              entity_id: input_text.gps_longitude
            enabled: true
          - service: input_text.set_value
            metadata: {}
            data:
              value: "{{ state_attr ( 'device_tracker.phone1', 'latitude')}}"
            target:
              entity_id: input_text.gps_latitude
            enabled: true
      - conditions:
          - condition: trigger
            id:
              - wife
        sequence:
          - wait_for_trigger:
              - platform: template
                value_template: >
                  {{state_attr ( 'sensor.phone2_bluetooth_connection',
                  'connected_paired_devices') != ['QQ:QQ:QQ:QQ:QQ:QQ (car AVN)'] }}
                enabled: true
          - service: device_tracker.see
            metadata: {}
            data:
              dev_id: phone2
          - service: input_text.set_value
            metadata: {}
            data:
              value: "{{ state_attr ('device_tracker.phone2', 'longitude')}}"
            target:
              entity_id: input_text.gps_longitude
            enabled: true
          - service: input_text.set_value
            metadata: {}
            data:
              value: "{{ state_attr ( 'device_tracker.phone2', 'latitude')}}"
            target:
              entity_id: input_text.gps_latitude
            enabled: true
mode: single

custom sensor:

  - platform: template
    sensors:
      parking_gps_tracker_longitude:
        friendly_name: "parking position"
        value_template: "{{ states('input_text.gps_latitude')| float}}, {{ states('input_text.gps_longitude')| float }}"
        attribute_templates:
          latitude: "{{ states('input_text.gps_latitude')| float}}"
          longitude: "{{ states('input_text.gps_longitude')| float }} "

And now the problem. For phone1 it works pretty well (trigered by bluethoot or android auto). However, for phone2 is the saved position when the automation starts (the positon of departure) :smiley:. Honestly, I’m running out of ideas why is this happening. I tried device_tracker.see to force refresh of device trackers position, but same results as previous. Both of phones are android devices, same manufacturer, but different models.
Do you have som sugestions?

I’m a bit confused by the triggers. Try separating out the automations and do one for each phone as that will make it easier to trace the issue.

Also to make it easier change all the input_text to input_number then you don’t have to convert to float.

Glad I could do something in return

1 Like

Thanks, that sorted it for me! It works now:) .
It appears that my problem was that I needed to go to (within the android companion app) to Location Services>Background Location>and there enable BG location+under sensor settings also activate High Accuracy Mode. There I also chose “High Accuracy Mode only when connected to BT devices” and then I selected the car there.
Its running now for some weeks without issues and I really love it!

Thanks a lot for your help!

1 Like

You’re welcome!

Since reading your post I’ve changed my settings too. Now I have high accuracy mode only when connected to BT devices and within a certain distance from particular zones. It’s now much more accurate for parking at those zones and responds much more quickly for automations that I have running when we get home or to other places (e.g. switching from data to WiFi at certain places). Also doesn’t noticeably increase battery drain.

1 Like