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:
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.
- Official GTFS Integration
- GitHub - libanp/ha-gtfs-rt-v2: Real-time transit information for Home Assistant
- GitHub - phardy/hass-gtfs-realtime
- GitHub - tdickman/hass-gtfs-realtime
- GitHub - zacs/ha-gtfs-rt: Real-time transit information for Home Assistant
- [most recently updated custom component] GitHub - mark1foley/ha-gtfs-rt-v2: Real-time transit information for Home Assistant
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.