How to dynamically set location in Home Assistant for boats and RVs
Home Assistant has a concept of "location" for your home in the form for GPS coordinates. Various features use this setting to drive functionality. For example: What's the weather like? Or, activate particular automations when you leave or arrive home.
But what if your home moves, like a boat or RV? In that case your home location needs updating with some regularity to keep those features working.
Fortunately, Home Assistant has a programmatic location update service for exactly this use-case, and hooking everything up is relatively straightforward when you know how.
First, you need a device to get the correct GPS coordinates of your home at any given time. Most boats already have GPS devices built-in, but typically that data is only available when a chart plotter is turned on, and getting the data out of the marine network can be challenging.
An easier option is to pick up a cheap USB dongle which is broadly compatible with any device. I grabbed this one on Amazon:
The next challenge is getting the data into Home Assistant. Initially I tried using Node-RED to read the serial port and ingest the values that way, but they were in a format that I wasn't able to make any sense of - no matter what I tried.
Eventually I remembered that the Victron Cerbo GX I use for energy management has a "GPS" feature and I wondered to myself it could possibly be as simple as just plugging the GPS dongle into the Cerbo?
Answer: Yes. Yes, it was that simple. As soon as I plugged the USB stick into the Cerbo and mounted it as high up the wall as I could go, it took about 30 seconds to acquire a satellite fix and then started working flawlessly right away.
As I covered in my setup guide for using Victron systems with Home Assistant via Modbus, it's then extremely easy to get values from the Cerbo into HA. You just look up the IDs needed in the TCP register and then translate them to Modbus.
Getting GPS values into Home Assistant
So in this case, to get the GPS values out of the Cerbo and into HA, I added the following to my configuration, and restarted Home Assistant.
And right away that created new HA entities and populated their values correctly.
Formatting GPS coordinates
What you'll notice above, though, is that the "degree" units are imported as positive integers rather than decimals, which not a valid GPS coordinate. Initially I tried adjusting the Modbus sensor with a different scale, so the values would be automatically converted - but I found that I was unable to have fine-grained control over the number of (critical) decimal places.
So, instead, I ended up creating a separate template sensor in Home Asssistant which ingests the raw GPS values coming from the stick and converts them to a precise number of decimal places, giving me clean output in an expected format.
Automating Home Assistant Location
Finally, I created an automation in Home Assistant that runs every 15 minutes and updates the Home location based on the current GPS coordinates.
You can create this automation using the visual editor, or by switching to yaml
mode and just pasting in the code above.
And now Home Assistant knows where it is in the world at all times, which you can easily check by visiting the Map page. It should show a 🏠 icon for where the boat/RV is, and initials for any mobile devices and their proximity to the home.
On thing that's still missing from this implementation is that Home Assistant does not automatically update its timezone based on GPS location. So any automations based on time will go out of sync as you change location, until you manually update your HA configuration to the correct timezone.
There's an open feature request to add support for this on the Home Assistant community below, please add your vote to help get it prioritised!
Next Steps
From here you can do other interesting things, like generate a custom wind forecast for your current location which is always up to date and always relevant, then add that to your dashboard. Super handy for sailboats like ours:
Member discussion