This addon will run gpsd and serve the data to MQTT and show a device tracker device (device_tracker.gpsd_location). The addon has been tested with Mosquitto MQTTaddon but can also be configued to use another broker if wanted. The idea is to update the home zone in Home Assistant with the actual position from gpsd, in order to run automations based on actual position. Perfect for installations of Home Assistant in cars, caravans, boats or other moving “homes”. This addon enables you to install a supported GPS receiver to the Home Assistant host directly and interface over a serial device.
The addon supports for following hardware architectures:
Prerequisite
Remember to install Mosquitto or another broker before setting up this addon.
Installation requires you to set up a username and password to publish to MQTT. Also you must select the serial device for GPSD in the configuration. The configuration is done via the addon GUI inside Home Assistant after installation.
Install Addon
Add my repository and add the addon there.
Repository on Github
Security
The addon uses both apparmour with profile for both GPSD and MQTT, also it is signed and verified with Codenotary Community Attestation Service (CAS). For good measure the listening port is disabled by default as it is not needed for MQTT. If you want to enable the port it can be done in the configuration.
Examples for a moving home location update
Automation Example
Some Code
alias: Dynamic update location
description: Change home location when GPS tracker gets new position
trigger:
- platform: state
entity_id: device_tracker.gps_location
attribute: latitude
- platform: state
entity_id: device_tracker.gps_location
attribute: longitude
condition: []
action:
- service: homeassistant.set_location
data_template:
latitude: "{{ state_attr('device_tracker.gps_location', 'latitude') }}"
longitude: "{{ state_attr('device_tracker.gps_location', 'longitude') }}"
mode: single
Tracked boat / car example with images
If you create a person you could also track the location of the car/boat/whatever with a nice image. Combining this with a “moving” home location will set the location to “home” like in these examples.
This addon is exactly what I was looking for. However I’m having an issue with the device tracker becoming unavailable after awhile. I’ve opened an issue on your Github for this.
Sorry to not had time to look ar this. I am speculating that it might be the GPSD is loosing connection with the device. Is a simple restart enough to wake it up again?
It’s a usb connected device. After restart of HA the service is not working anymore. When i manualy restart the GPSDtoMQTT service everything is working again. Log says: NameError: name ‘mqtt_config’ is not defined
Ok cool. Is there any message in the log that it detected the HA restart? You are using mosquitto? Do you have a custom username and password set up for MQTT or are you using the Internal authentication?
Thanks for your quick reply’s! Plug in seems to run okay after restart (green bullet). But doesn’t give location. After restarting the plugin it works again. Maybe the Mosquitto broker is not running before the plugin is ready? (after restarting the system) I’m using internal authentication.
Could you send me a more complete log where you start the plugin, verify a working positiion. Then restart HA and provoke the error. Remember to enable debug please.
2024-04-26 21:05:31,071 - MQTT Publisher - INFO - Published 11 updates to the device_tracker in last 2.0 minutes. Achieved 3D fix, position with 10 of required 0 GPS satellites.
Exception in thread Thread-1 (_thread_main):
Traceback (most recent call last):
File “/usr/lib/python3.11/threading.py”, line 1045, in _bootstrap_inner
self.run()
File “/usr/lib/python3.11/threading.py”, line 982, in run
self._target(*self._args, **self._kwargs)
File “/usr/lib/python3.11/site-packages/paho/mqtt/client.py”, line 3591, in _thread_main
self.loop_forever(retry_first_connection=True)
File “/usr/lib/python3.11/site-packages/paho/mqtt/client.py”, line 1756, in loop_forever
rc = self._loop(timeout)
^^^^^^^^^^^^^^^^^^^
File “/usr/lib/python3.11/site-packages/paho/mqtt/client.py”, line 1164, in _loop
rc = self.loop_read()
^^^^^^^^^^^^^^^^
File “/usr/lib/python3.11/site-packages/paho/mqtt/client.py”, line 1556, in loop_read
rc = self._packet_read()
^^^^^^^^^^^^^^^^^^^
File “/usr/lib/python3.11/site-packages/paho/mqtt/client.py”, line 2439, in _packet_read
rc = self._packet_handle()
^^^^^^^^^^^^^^^^^^^^^
File “/usr/lib/python3.11/site-packages/paho/mqtt/client.py”, line 3033, in _packet_handle
return self._handle_publish()
^^^^^^^^^^^^^^^^^^^^^^
File “/usr/lib/python3.11/site-packages/paho/mqtt/client.py”, line 3327, in _handle_publish
self._handle_on_message(message)
File “/usr/lib/python3.11/site-packages/paho/mqtt/client.py”, line 3570, in _handle_on_message
on_message(self, self._userdata, message)
File “/gpsd2mqtt.py”, line 123, in on_message
client.publish(mqtt_config, json_config)
^^^^^^^^^^^
NameError: name ‘mqtt_config’ is not defined
2024-04-26 21:07:31,073 - MQTT Publisher - INFO - Published 12 updates to the device_tracker in last 2.0 minutes. Achieved 3D fix, position with 10 of required 0 GPS satellites.
2024-04-26 21:09:31,075 - MQTT Publisher - INFO - Published 11 updates to the device_tracker in last 2.0 minutes. Achieved 3D fix, position with 10 of required 0 GPS satellites.
Thank you for your work and being proactive! Still the same issue after the update. It’s not a big deal, now I just have to manually restart the GPSD to MQTT add-on every time after rebooting HA and it works fine again!