SF BART Sensor with the REST API Component

Home Assistant BART Sensor with the REST API Component

I’ve been trying to get a BART sensor into Home Assistant since I started using the platform in 2017. Home Assistant already has some great public transportation sensors that I use:

  • NextBus - for Buses
  • CityBikes - for BayWheels (to see how many bikes are at each dock)

GTFS aka The General Transit Feed Specification is described as “is a data specification that allows public transit agencies to publish their transit data in a format that can be consumed by a wide variety of software applications.” Which sounds awesome, because in theory you should be able to find a specific public transportation’s GTFS feed link and plug it into a GTFS app and then you have a way for the user to consume the public transportation information.

There are already quite a few GTFS integrations, but I haven’t found one that works reliably or isn’t real-time. Also, it seems like people maintain their software for a bit then a new fork takes over.

My use-case is pretty simple: I need a way to see when the next train is arriving at my station (24th Street) and heading north. All lines heading north stop at Embarcadero Station (which is the closest station to my office).

Lovelace Code:

type: entities
entities:
  - entity: sensor.bart_24th_street_station_red_line_north
  - entity: sensor.bart_24th_street_station_yellow_line_north
  - entity: sensor.bart_24th_street_station_blue_line_north
  - entity: sensor.bart_24th_street_station_green_line_north
title: BART REST Sensor
state_color: false
footer:
  type: picture
  image: >-
    https://www.bart.gov/sites/default/files/images/basic_page/system-map-everyday-until-9pm.png
  tap_action:
    action: none
  hold_action:
    action: none
theme: macOS Theme

Sensor Configuration:

############################################################################
############ SF BART REST API TEMPLATE SENSOR #############################
############################################################################

  - platform: rest
    name: "BART 24TH Street Station - Yellow Line North"
    json_attributes_path: "$.root.station[0].etd[0].estimate[0]"
    json_attributes:
      - minutes
      - platform
      - direction
      - length
      - color
      - delay
      - hexcolor
      - bikeflag
    value_template: "{{ value_json['root']['station'][0]['etd'][0]['estimate'][0]['minutes'].title() }}"
    ## "OK"
    ### "{{ value_json['root']['station'][0]['etd'][0]['estimate'][0]['minutes'].title() }}"
    unit_of_measurement: "minutes"
    icon: mdi:train
    resource: https://api.bart.gov/api/etd.aspx?orig=24TH&cmd=etd&key=MW9S-E7SL-26DU-VV8V&dir=n&json=y

  - platform: rest
    name: "BART 24TH Street Station - Green Line North"
    json_attributes_path: "$.root.station[0].etd[1].estimate[0]"
    json_attributes:
      - minutes
      - platform
      - direction
      - length
      - color
      - delay
      - hexcolor
      - bikeflag
    value_template: "{{ value_json['root']['station'][0]['etd'][1]['estimate'][0]['minutes'].title() }}"
    unit_of_measurement: "minutes"
    icon: mdi:train
    resource: https://api.bart.gov/api/etd.aspx?orig=24TH&cmd=etd&key=MW9S-E7SL-26DU-VV8V&dir=n&json=y
    
  - platform: rest
    name: "BART 24TH Street Station - Blue Line North"
    json_attributes_path: "$.root.station[0].etd[2].estimate[0]"
    json_attributes:
      - minutes
      - platform
      - direction
      - length
      - color
      - delay
      - hexcolor
      - bikeflag
    value_template: "{{ value_json['root']['station'][0]['etd'][2]['estimate'][0]['minutes'].title() }}"
    unit_of_measurement: "minutes"
    icon: mdi:train
    resource: https://api.bart.gov/api/etd.aspx?orig=24TH&cmd=etd&key=MW9S-E7SL-26DU-VV8V&dir=n&json=y
    
  - platform: rest
    name: "BART 24TH Street Station - Red Line North"
    json_attributes_path: "$.root.station[0].etd[3].estimate[0]"
    json_attributes:
      - minutes
      - platform
      - direction
      - length
      - color
      - delay
      - hexcolor
      - bikeflag
    value_template: "{{ value_json['root']['station'][0]['etd'][3]['estimate'][0]['minutes'].title() }}"
    unit_of_measurement: "minutes"
    icon: mdi:train
    resource: https://api.bart.gov/api/etd.aspx?orig=24TH&cmd=etd&key=MW9S-E7SL-26DU-VV8V&dir=n&json=y

Let me know if this is helpful for you! Maybe you can apply the same technique to make a REST Sensor with an API you find.

4 Likes

How I used the official BART API to make the sensor with the REST API Component:

The most helpful tools for doing this were:

I used Postman and found the BART API Collection by searching at the top. Inside that collection, there is a request called “Estimated time of departure”. I forked the collection so I could start running my own request. I looked at the BART API Developer page and saw the API key is listed right there out in the open for everyone to use as long as you follow their usage rules.

I copied the resulting json code and pasted it into the left and started guessing the JSONPath route to get the value I was looking for. As the JSON cascades down to the nested fields, I picked the headers until I got the value for minutes that I was looking for.

There’s a handy little toggle which shows the Output paths which are used in the REST sensor json_attributes_path and value_template.

Using the REST sensor seems like a good way for someone to build a very “light” integration if they find the right API to work with. Postman can be a great way to discover what APIs are out there and combining that with the versatility of the REST Sensor can lead to some creative solutions.

Hi, I know this is a old-ish post but I am revamping gtfs with realtime and am in need of multiple real-life cases to verify that, mainly because the gtfs data seems (!) standardized but providers differ a lot …hence he HA gtfs is often not working.

I would like to understand which train/route you are interested in from a gtfs perspective as th json one is not giving me the details (or: costs too much time)
Of course, if you are happy and not interested, fine too :slight_smile:

vingerha/gtfs2: Support GTFS in Home Assistant GUI-only (github.com)

2 Likes

thanks @vingerha! I sent you a DM with the route that I want to get into Home Assistant

Just wanted to say thank you for this!

1 Like