Home Assistant "MTA Service Status" python script

Overview

A python script to provide MTA Service Statuses statuses for Home Assistant.

The script pull data from the official MTA Service Status feed and create 5 json files.

MTA_BT_Status_data.json cover Bridges and Tunnels: Bronx-Whitestone, Cross Bay, Henry Hudson, Hugh L. Carey, Marine Parkway, Queens Midtown, Robert F. Kennedy, Throgs Neck, Verrazzano-Narrows

MTA_Bus_Status_data.json cover Buses: B1 - B99, B100 - B103, BM1 - BM5, BX1 - BX99, BXM1 - BXM18, M1 - M191, Q1 - Q113, QM1 - QM44, S40 - S98, SIM1 - SIM35, X27 - X68

MTA_LIRR_Status_data.json cover Long Island Rail Road: Babylon, Belmont, City Terminal Zone, Far Rockaway, Hempstead, Long Beach, Montauk, Oyster Bay, Port Jefferson, Port Washington, Ronkonkoma, West Hempstead

MTA_MetroNorth_Status_data.json cover Metro-North Railroad: Hudson, Harlem, Wassaic, New Haven, New Canaan, Danbury, Waterbury, Pascack Valley, Port Jervis

MTA_Subway_Status_data.json cover Subways and Staten island Railway: 123, 456, 7, ACE, BDFM, G, JZ, L, NQR, S, SIR

Installation

Create “data” directory/folder in your Home Assistant configuration directory.

File paths must be added to allowlist_external_dirs:.

Add to configuration.yaml:

allowlist_external_dirs:
  - /config/data

Copy all files from data folder to /config/data/

Full path: /config/data/

To install, copy the service_status.py to a directory called python_scripts in your Home Assistant configuration directory.

Full path: /config/python_scripts/

To install the MTA icons, copy the www/icons/mta directory and all files to your Home Assistant configuration directory.

Full path: /config/www/icons/mta/

Use “customize.yaml” in include directory to add “entity_picture” and “friendly_name”.

Full path: /config/include/

Add to ui-lovelace.yaml following:

  - type: entities
    title: MTA Service Status
    show_header_toggle: false
    entities:
      - entity: sensor.trains_456
      - entity: sensor.trains_nqr
      - entity: sensor.bus_b1_b84
      - entity: sensor.bridge_verrazano_narrows

Files on github: https://github.com/vlad36N/homeassistant-mta-service-status

3 Likes

Anyway to include the LIRR?

LIRR already in.
Not included it on example, because not using it.
File name is “MTA_LIRR_Status_data.json”.

Code example:

  - platform: file
    name: Train_Babylon
    file_path: /config/data/MTA_LIRR_Status_data.json
    value_template: "{{ value_json['items'][0]['status'] }}"
1 Like

Vlad, I tried to follow your instructions but my configuration.yaml file does not pass without errors. this is what I added to my file.

Configuration.yaml

  • platform: file
    name: Train_Babylon
    file_path: /config/data/MTA_LIRR_Status_data.json
    value_template: “{{ value_json[‘items’][0][‘status’] }}”

customize.yaml

sensor.trains_babylon:
custom_ui_state_card: state-card-custom-ui
entity_picture: /local/icon/mta/mta.png
friendly_name: ‘Babylon’

and everything you mentioned to my /config directory.

This is the error I get.

Invalid config for [sensor.file]: not a file for dictionary value @ data[‘file_path’]. Got ‘/config/data/MTA_LIRR_Status_data.json’. (See ?, line ?).

If you add it to configuration.yaml, it must start with sensor, try:

sensor:
  - platform: file
    name: Trains_123
    file_path: /config/data/MTA_LIRR_Status_data.json
    value_template: "{{ value_json['items'][0]['status'] }}"

If you already have sensor: section in a file, just add after any sensor:

  - platform: file
    name: Trains_123
    file_path: /config/data/MTA_LIRR_Status_data.json
    value_template: "{{ value_json['items'][0]['status'] }}"

also in customize.yaml you should have:

sensor.trains_babylon:
  custom_ui_state_card: state-card-custom-ui
  entity_picture: /local/icon/mta/mta.png
  friendly_name: 'Babylon'

You have to make sure you have proper indentation.

I do have the proper indentation, it was stripped when I posted it. I have

In my configuration.yaml file under my sensor: section

and in my customize.yaml file I have

My bad, forgot to mention file paths must be added to whitelist_external_dirs…
Add to configuration.yaml:
correction instead of whitelist_external_dirs: use allowlist_external_dirs:

allowlist_external_dirs:
  - /config/data
1 Like

Thanks Vlad. I got it working. I had to add;

homeassistant:
  allowlist_external_dirs:
    - /config/data

I appreciate the help. this will be useful information.

You welcome

Python script updated on GitHub.
Fixed minor error.
Please re-upload python script.

1 Like