I love the ferry. What I like most is how quiet it is. While all the morning commuters angrily inch across the gridlock of the Bay Bridge above, or packed trains screech below the water, you glide quietly through the fog towards the hills of the city. And on the afternoon commutes, they serve beer. Hard to beat.

I don’t have a regular work schedule, and Google Maps does not include ferry info, so taking one in the morning requires a little planning. What would make taking the ferry easier is if Home Assistant could look up the times for me.

Most transit agencies publish many open ways of accessing their data, the most common being a simple database called GTFS. This is not real time data, but fortunately there’s no traffic on the water.

11372786534_75ff1c36d1_b

The Transit Feeds database has an extensive source of the GTFS zip files we need. Here’s the one for the San Francisco Bay Ferry. The information on the route is broken down into several simple CSV text documents inside the zip. My stops.txt looks like:

stop_id,stop_name,stop_lat,stop_lon,zone_id
12030042,Alameda Main St,37.790667,-122.294058,
12030044,Alameda Main Street,37.790667,-122.294058,
12048536,Harbor Bay Ferry Terminal,37.736528,-122.256966,
1222694,Mare Island Terminal,38.1012069,-122.2694045,
12030043,Oakland Terminal,37.795032,-122.279777,
12048538,Pier 41,37.809594,-122.412272,
890001,San Francisco Ferry Building,37.795924,-122.392066,
12048537,San Francisco Ferry Building [Arrive],37.795924,-122.392066,
12030041,South San Francisco,37.662676,-122.377245,
12149044,Vallejo Ferry Terminal,38.100149,-122.262586,

The first number is the stop_id. So to go from Oakland to San Francisco, I add in the sensors section of my configuration.yaml

- platform: gtfs
  name: "Ferry to SF"
  origin: 12030043
  destination: 890001
  # How long it takes to bike from home to terminal in minutes
  offset: 8
  data: sfbayferry.zip

Perfect! But I also need to know what time it arrives on the other side of the bay. GTFS returns that information as an attribute of the sensor I just created, so I will use a template to extract it and create a second sensor:

- platform: template
  sensors:
    ferry_to_sf_arrival:
      value_template: "{{ as_timestamp(states.sensor.ferry_to_sf.attributes['Destination Stop Arrival Time']) | timestamp_custom('%k:%M') }}"
      friendly_name: "SF Ferry Arrival"

I always use the very help http://strftime.net/ for figuring out time formatting in templates in Linux. The %k:%M at the end returns the time in 24 hour format. Note that unusual attribute syntax in the first part, I’m not sure why that works but it was necessary as noted in this helpful post on the HA forum.

Screen Shot 2017-07-12 at 4.24.43 PM.png

Troubleshooting tips for the GTFS component:

  • Your GTFS zip file needs to be owned and accessible by your HA user
  • If you get a “stoptime not found” or similar error, your stop_id numbers are wrong or the route is not possible

Resources

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s