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.