project: when is the next train from the station?

Travel information can be one of the things you need easy access to. If you have Google Home or Alexa you’ll find it helpful to recall the exact way to ask for the info. For example it might be “Alexa, ask Virgin trains to plan a journey” or “Hey Google, when’s the next train from XX to YY”.

As shown on posts nearby, I use Home Assistant as a home automation hub. A recycled monitor displays all the information I care to know just now. Continue reading to learn how we create the Home Assistant travel displays you see below.

If you need train or bus information there’s probably a public source in your country, such as the transportAPI for UK Transport. This information source gives you a number of free information requests per day. Sign up for an account and get yourself an APP_ID and an APP_PASSWORD.

To display this information in Home Assistant, edit your configuration.yaml and add the following information block as text. Do check here https://www.home-assistant.io/integrations/uk_transport/ for detailed instructions and also how to add buses or multiple journey requests.

sensor:
  - platform: uk_transport 
    app_id: YOUR_APP_ID 
    app_key: YOUR_APP_KEY 
    queries: 
      - mode: train
        origin: KGX
        destination: WAT 
   scan_interval: 500

Save your configuration.yaml with this extra text, check it and then restart Home Assistant so that it starts picking up journey information from the TransportAPI. All being well, you’ll now have a few new sensor ‘entities’ to add to your Home Assistant page. You need to configure the Home Assistant overview page. And you need to find out what your entities are called. So click Configure UI and add a card called Entities. Then add whatever train sensor entities you can find e.g.:

entities:
 entity: sensor.next_train_to_kgx
 entity: sensor.next_train_to_rys
 entity: sensor.next_train_to_fxn
 entity: sensor.time
 show_header_toggle: false
 type: entities 

You can also add a gauge that will countdown how long it is before the next train. My example changes colour according to how soon that is. When the gauge is yellow I can just about catch the train. When it shows in red it’s unlikely I will get to the station in time.

 entity: sensor.next_train_to_kgx
 max: 60
 min: 0
 name: next train down to KGX
 severity:
   green: 10
   red: 6
   yellow: 7
 theme: default
 type: gauge 

Leave a Reply

Your email address will not be published. Required fields are marked *