The problem:
HA installed in a van (or boat, etc) which makes zone based automations very difficult or rather impassible.
There is a really good topic showing how to achieve this in great detail so why bother with another take on this, you ask. Not to forget the fact that I am new to the game and know very little about HA. Well, because it’s easier for a novice like me…
Prerequisites:
- mobile HA installation or otherwise why bother with this at all?!?
- GPS capable router (like the Teltonika RUTX11) or any other GPS device capable to communicate with Traccar
The above-mentioned router is capable of reporting the lat & lon values over MQTT as well, the reason I went with Traccar is because it can help with service intervals (after set mileage reached) and I also use it for my car as well.
Both HA (as a VM) and Traccar (as an LXC container) run in a Proxmox install on my home server in the van, right next to the Teltonika router. On the router I set up an AVL server in the Services menu which sends data to my Traccar install. On the Traccar server I made a restricted user for HA with readonly privileges to view the van location. You can use the Traccar addon but I decided I want my HA instance as light as possible with as few addons as possible.
Now let’s talk about the HA side of things…
To integrate your Traccar instance into HA you need to copy this to your configuration.yaml:
device_tracker:
- platform: traccar
host: 192.168.xxx.xxx #your_traccar_IP
port: 8082
username: your_traccar_user
password: your_traccar_password
new_device_defaults:
track_new_devices: true
You need to create a couple of sensors (latitude and longitude) but I created some more for later use like vehicle speed. This also should be included in your configuration.yaml or you can put the whole thing into a traccar.yaml file but don’t forget to reference that file in your config.
template:
- sensor:
- name: "van_latitude"
state: "{{ state_attr('device_tracker.crafter', 'latitude') | float }}"
- name: "van_longitude"
state: "{{ state_attr('device_tracker.crafter', 'longitude') | float }}"
- name: "van_altitude"
state: "{{ state_attr('device_tracker.crafter', 'altitude') | float }}"
- name: "van_speed"
state: "{{ state_attr('device_tracker.crafter', 'speed') | float }}"
At this point you can test if the Traccar integration works in HA and your sensors are reporting the correct values.
Then comes the automation. I could not imagine an easy way to do this, I had nightmares for weeks. Not really, but anyway. I was scrolling through services and there it was: “homeassistant.set_location”!!! I could not believe my eyes If this is not beginner’s luck then what is?!
I implemented a 5 minutes delay so on a long drive HA does not get mad updating its location every 20 seconds or so.
alias: Homezone Update
description: ""
trigger:
- platform: state
entity_id:
- device_tracker.crafter
from: home
to: not_home
for:
hours: 0
minutes: 5
seconds: 00
condition: []
action:
- service: homeassistant.set_location
data_template:
latitude: |
{{ state_attr('device_tracker.crafter', 'latitude') }}
longitude: |
{{ state_attr('device_tracker.crafter', 'longitude') }}
mode: single
And that’s it. No Raspberry Pi, no python script, gpsd connected rpiclient, just a simple copy paste exercise for you. For me it took a couple of days or more and some help to figure out the syntax but hey, we want to have some fun, right?