Skip to content

Instantly share code, notes, and snippets.

@linuxkidd
Created July 5, 2020 23:41
Show Gist options
  • Save linuxkidd/a3018f08addcf6b8715f5b169ccd9413 to your computer and use it in GitHub Desktop.
Save linuxkidd/a3018f08addcf6b8715f5b169ccd9413 to your computer and use it in GitHub Desktop.
Pipe GPSd into Home-Assistant, and update position automatically

I used 'gpsd' running on linux, along with node-red (with the node-red-contrib-gpsd node to make a GPS to MQTT bridge.

Node Flow:

[{"id":"b4379a23.82767","type":"gpsd","z":"8b68a913.550af","name":"","hostname":"localhost","port":"2947","tpv":true,"sky":false,"info":false,"device":false,"gst":false,"att":false,"x":170,"y":140,"wires":[["c728e491.f15918"]]},{"id":"c381f35a.3cd8d","type":"debug","z":"8b68a913.550af","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":470,"y":140,"wires":[]},{"id":"a41acb7e.7d2798","type":"mqtt out","z":"8b68a913.550af","name":"","topic":"gps/tpv","qos":"2","retain":"true","broker":"1767f3ec.d1e33c","x":480,"y":180,"wires":[]},{"id":"c728e491.f15918","type":"function","z":"8b68a913.550af","name":"","func":"var lock = [ \"n/a\", \"No Fix\", \"2D Fix\", \"3D Fix\"];\n\nmsg.payload.speed *= 2.23694;\nmsg.payload.lock = lock[msg.payload.mode];\nmsg.payload.latitude = msg.payload.lat;\nmsg.payload.longitude = msg.payload.lon;\ndelete msg.payload.lon;\ndelete msg.payload.lat;\nreturn msg;","outputs":1,"noerr":0,"x":310,"y":140,"wires":[["a41acb7e.7d2798","c381f35a.3cd8d"]]},{"id":"1767f3ec.d1e33c","type":"mqtt-broker","z":"","name":"prime.rv","broker":"172.16.22.2","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"gateway/availability","birthQos":"2","birthRetain":"true","birthPayload":"online","closeTopic":"gateway/availability","closeQos":"2","closeRetain":"true","closePayload":"offline","willTopic":"gateway/availability","willQos":"2","willRetain":"true","willPayload":"offline"}]

Then created an HA sensor:

sensor:
  - platform: mqtt
    name: GPS
    expire_after: 10
    state_topic: "gps/tpv"
    value_template: "{{ value_json.lock }}"
    json_attributes_topic: "gps/tpv"
    icon: "mdi:crosshairs-gps"
    availability_topic: "gateway/availability"
    payload_available: online
    payload_not_available: offline

Then an automation:

  alias: 'Helper: Move Home'
  description: ''
  trigger:
  - platform: template
    value_template: '{{ is_state("sensor.gps", "3D Fix") and distance( "sensor.gps",
      "zone.home" ) | float > 1 }}'
  condition:
  - condition: template
    value_template: '{{ state_attr("sensor.gps","speed") | float < 5 }}'
  action:
  - data_template:
      message: RV to Home distance of {{ distance( "sensor.gps", "zone.home" ) | round(2)
        }} miles, changing Home location.
      notification_id: movedhome
      title: Moved Home
    service: persistent_notification.create
  - data_template:
      latitude: '{{  state_attr("sensor.gps","latitude") }}'
      longitude: '{{ state_attr("sensor.gps","longitude") }}'
    service: homeassistant.set_location
  - data: {}
    service: homeassistant.reload_core_config

Hope this helps!

@flyboy013
Copy link

After you call the homeassistant.set_location and the homeassistant.reload_core_config services, does your home assistant timezone {{ now().tzinfo }} update based on the new location?

My location gets updated and verified on the HA -> Configuration -> General page which shows a map of my updated location, but the HA timezone (as shown on the same page) remains unchanged. The timezone if not specified in the configuration.yaml, so I would expect it to be updated based on the new location. Restarting HA has not effect. Thanks

@linuxkidd
Copy link
Author

I've had the exact same experience. Further, I'm unable to find a service call I can make which will update the timezone. So, I'm afraid that for now.. timezone has to be manually updated. :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment