Addon GPSD to MQTT

This is a gpsd — a GPS service daemon to MQTT Home Assistant Addon.

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 MQTT addon 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.

GitHub Release
The addon supports for following hardware architectures:

Supports aarch64 Architecture Supports amd64 Architecture Supports armhf Architecture Supports armv7 Architecture Supports i386 Architecture

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.

Open your Home Assistant instance and show the add add-on repository dialog with a specific repository URL pre-filled.

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.

There are probably many other usescases, but this is mine.

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?

Thanks for your good work! Same issue here. After a while the GPS device is not seen anymore. Restarting will solve the problem for some time.

Hey! Which version of the addon are you running.

I had this problem with a USB connected device but never with the GPS Hat. Any error message in the log?

Also, are you loosing the device_tracker or is GPSD loosing connection with the GPS device? Do you know?

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. Which version of the addon are you running?

Latest: [2024.4.2]

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.

After this check the logs and copy them for me.

If you prefer you could DM to me.

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.

Thanks! Try the new 2024.4.3 version. See if that makes any difference.

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!

Thanks :tada: I struggle with reproducing the problem but I will keep digging.

Any changes in the logs? What is your setup (HA, MQTT, hardware etc).

I have now been able to reproduce the problem. I will get this fixed in the next release :slight_smile:

1 Like

Pretty sure it should work now in the 2024.4.4 release. :pray:

1 Like