5 min read

How to dynamically set location in Home Assistant for boats and RVs

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:

VK-172 USB GPS Stick

A simple plug-and-play USB dongle to get GPS coordinates

$12.99 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?

Wrapped the dongle in black electrical tape to mask the retina-burningly bright green status LED

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.

Boat energy management using Victron and Home Assistant
The most important systems on modern cruising boats are the ones which generate, store, and distribute energy throughout the vessel. When we took delivery of our boat we immediately added a solar arch, lithium batteries, and a 5kw inverter to create a powerful enough electrical system to live off fu…

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.

# Victron
modbus:
  - name: cerbo
    host: 192.168.13.221
    type: tcp
    port: 502
    
    sensors:
        # GPS
      - name: "GPS Latitude"
        data_type: int32
        unit_of_measurement: "deg"
        slave: 100
        address: 2800
      - name: "GPS Longitude"
        data_type: int32
        unit_of_measurement: "deg"
        slave: 100
        address: 2802
      - name: "GPS Course"
        data_type: uint
        unit_of_measurement: "deg"
        slave: 100
        address: 2804
        scale: 0.01
      - name: "GPS Speed"
        data_type: uint
        unit_of_measurement: "m/s"
        slave: 100
        address: 2805
        scale: 0.01
      - name: "GPS Fix"
        data_type: uint
        slave: 100
        address: 2806
      - name: "GPS Satellites"
        data_type: uint
        slave: 100
        address: 2807
configuration.yaml

And right away that created new HA entities and populated their values correctly.

Lovelace dashboard - GPS values via Modbus

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.

sensor:
  - platform: template
    sensors:
      # Clean formatting for GPS values
      gps_latitude_clean:
        friendly_name: "GPS Latitude"
        value_template: >
          {{ (states("sensor.gps_latitude") | float * 0.0000001) | round(3) }}
      gps_longitude_clean:
        friendly_name: "GPS Longitude"
        value_template: >
          {{ (states("sensor.gps_longitude") | float * 0.0000001) | round(3) }}
configuration.yaml
Lovelace dashboard - Raw GPS values vs formatted

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.

alias: Set Location
description: ''
trigger:
  - platform: time_pattern
    minutes: /15
condition: []
action:
  - service: homeassistant.set_location
    data_template:
      latitude: |
        {{ states("sensor.gps_latitude_clean") }}
      longitude: |
        {{ states("sensor.gps_longitude_clean") }}
mode: single
automation.yaml

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!

Ability to set timezone when location updates
Running HA on a boat and also have this issue. Ideally I would love to be able to have HA automatically update timezone based on an optional parameter called alongside homeassistant.set_location - perhaps update_timezone: true ?

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:

Generating GPS-powered wind forecasts with Windy.com
In a previous post, I covered how I use a cheap GPS USB stick to keep our boat’s GPS location updated in Home Assistant, which allows for location-based automations. How to dynamically set location in Home Assistant for boats and RVsHome Assistant has a concept of “location” for your home