I have two cars that I track with an entity in “known_devices.yaml”. Both are updated through the service “device_tracker.see” where the first car sends its position by mqtt and for the second car I update the position based on when someone is in the car (companion app is connected to car by Bluetooth and I then copy the state of this phones location).
Upon HA restart both of these entities become “away” with no known gps location. Is there a way to get this state to survive a restart?
It can take forever for the status to update. For car nr1 the mqtt updates come once per hour when parked and for car nr2 obviously the position won’t be updated until someone is back in the car.
For the mqtt sensor you can set the retained flag on the sent messages.
That way as soon as home assistant starts and subscribes to the topic the broker sends the last received message.
I’d need more detail on how you are actually doing the other one. If it is a triggered template sensor it should be restored after a restart.
Thanks for your reply. I thought of mentioning the retention as a nogo as I don’t have control over the cars mqtt client to set the topic QoS. Otherwise this would have been a sound solution. I could of course create an mqtt bridge republishing the messages with retention but that feels like running over the river for water.
My config looks like this.
# known_devices.yaml
kia:
name: Kia
picture: https://kiaonline.dk/wp-content/uploads/2021/03/eniro_gravityblue_my20_kia_privatleasing-800x420.png
track: true
honda:
name: Honda
picture: https://4.bp.blogspot.com/-uuIkAZJmZPM/WJGRL3-q9VI/AAAAAAAChX8/F-6uoi2J4ZwwJVfMa3Kj6J9ltphCDvYGgCLcB/s640/Honda-Civic-Tourer%2B%25283%2529.jpg
track: true
Then there is one automation for the Kia using mqtt that looks like this. There are individual topics from the car for longitude and latitude and they are broadcasted at the same time. The delay is there to make sure both topics have been received before we update.
- id: '1688248453827'
alias: Update Kia Device Tracker MQTT
description: ''
trigger:
- platform: state
entity_id:
- sensor.kia_mqtt_gps_longitude
condition:
- condition: state
entity_id: binary_sensor.kia_mqtt_gps_lock
state: 'on'
action:
- delay:
hours: 0
minutes: 0
seconds: 0
milliseconds: 500
- service: device_tracker.see
data:
dev_id: kia
gps:
- '{{ states(''sensor.kia_mqtt_gps_latitude'') }}'
- '{{ states(''sensor.kia_mqtt_gps_longitude'') }}'
mode: single
The Honda is way messier. I’ve not refactored the code after playing around with all the conditions so bare with me. Any ideas of how to optimize the duplication of code is of course welcome.
Main idea here is the following, Me and my wife have a binary_sensor for when we are in the car. If this sensor is active then the car should follow our position. When one leaves the car I want to request a GPS update for the affected device before setting the cars position so that we get an accurate one as possible. I am not sure if the notify request_location_update works without specifying an entity to update though.
- id: '1687815514032'
alias: Update Honda Device Tracker
description: Depending on who is in the car
trigger:
- platform: state
entity_id:
- person.philip
- device_tracker.galaxy_s22
id: philip
- platform: state
entity_id:
- person.mariam
- device_tracker.mariam_nord2
id: mariam
- platform: state
entity_id:
- binary_sensor.in_car_honda_presence_philip
from: 'on'
to: 'off'
id: philip_left
- platform: state
entity_id:
- binary_sensor.in_car_honda_presence_mariam
from: 'on'
to: 'off'
id: mariam_left
condition: []
action:
- if:
- condition: or
conditions:
- condition: trigger
id: philip_left
- condition: trigger
id: mariam_left
then:
- service: notify.notify
data:
message: request_location_update
- delay:
hours: 0
minutes: 0
seconds: 10
milliseconds: 0
- if:
- condition: or
conditions:
- condition: template
value_template: '{{ states(who_in_car_sensor) == ''on'' }}'
- condition: trigger
id: philip_left
- condition: trigger
id: mariam_left
then:
- service: device_tracker.see
data:
dev_id: honda
location_name: '{{ states(who_geo_entity) }}'
gps:
- '{{ state_attr(who_geo_entity, ''latitude'') }}'
- '{{ state_attr(who_geo_entity, ''longitude'') }}'
variables:
p_geo_entity: device_tracker.galaxy_s22
m_geo_entity: device_tracker.mariam_nord2
who_name: "{% if (trigger.id == 'philip' or trigger.id == 'philip_left') %}\n
\ Philip\n{% elif (trigger.id == 'mariam' or trigger.id == 'mariam_left') %}\n
\ Mariam\n{% else %}\n unknown\n{% endif %}\n"
who_geo_entity: "{% if (trigger.id == 'philip' or trigger.id == 'philip_left')
%}\n {{ p_geo_entity }}\n{% elif (trigger.id == 'mariam' or trigger.id == 'mariam_left')
%}\n {{ m_geo_entity }}\n{% else %}\n unknown\n{% endif %}\n"
who_in_car_sensor: "{% if (trigger.id == 'philip' or trigger.id == 'philip_left')
%}\n binary_sensor.in_car_honda_presence_philip\n{% elif (trigger.id == 'mariam'
or trigger.id == 'mariam_left') %}\n binary_sensor.in_car_honda_presence_mariam\n{%
else %}\n unknown\n{% endif %}\n"
mode: single