Custom widget: Train departure times (UK Only)

Probably not hard to find an example (as there are always bus replacement services!) but, do you have an example?

I found another bug today which I need to fix - on weekends my trains have a different destination and the API returns nothing. I need to work out how to fix this.

Here’s the output I have today.

{"date":"2019-01-27","time_of_day":"16:22","request_time":"2019-01-27T16:22:41+00:00","station_name":"Swindon","station_code":"SWI","departures":{"all":[]}}

Is that all you need?

So that is the same “problem” I am seeing today.
The response is saying that there are no trains departing from your station - this is correct. There is a BRS.

What does NationalRail.co.uk say?

When I checked it said there was a bus replacement service.

I’d like to obtain the Origin station and display that information. Depending on what station the train comes from depends on how busy it will by the time it’s reached my station. So I can judge whether or not to get the next train.

My coding abilities are quite limited and have attempted to update your code but not getting the results, so clearly I’m doing something wrong.

The data I want is provided by; origin_name

I’ll take a look over the weekend. Shouldn’t not be to difficult

Brilliant widget, many thanks as this is really making HabPanel uselful! I have a small problem; my trains are on a loop, so putting in the departure station (BFD) and the destination station (WAT) shows both those that go clockwise and those which run anti-clockwise. All the ones I need depart from Platform 1 at BFD; how can I filter the results to only show this? Any help much appreciated :slight_smile:

I’ll be a bit of a change to do that as the API is different. I’ll try muck about with it over the weekend as I want to make some other changes to it as well.

C

I’m still getting the following error;

[ERROR] [ntime.internal.engine.RuleEngineImpl] - Error during the execution of startup rule 'Transport.GetTrainTimes': cannot invoke method public org.eclipse.smarthome.core.types.State org.eclipse.smarthome.core.items.GenericItem.getState() on null

Any ideas on what’s causing this and what I can change to resolve it? I’ve getting the train data but it would be nice to clean up this error from my logs.

Regards,

Garry

What’s your from and to station? Do the errors happen at a specific time?

I’m travelling from Swindon (SWI) to London Paddington (PAD), the error happens every time the rule runs.

I’ve changed the code around a bit. It also fixed a bug I was having.

Take the rule from the original post and replace what you have.

Let me know if the error goes away.

Thanks,

I’ll take a look and let you know if I still get the error.

where is your latest code ? , so were all looking at the same version

Topic post is up to date.

Nice one, using latest build with no destination so I can get all my local trains and a late alert for services

Not sure what I get with replacement buses

Hi,

Does this code still work? I have followed the instructions but I get null values in my logs and when i save the rules file i get the following errors in the logs:

Function2 is a raw type. References to generic type Function2<P1, P2, Result> should be parameterized
The method getCalendar() from the type DateTimeType is deprecated

Does it needs a new calendar type? I’m not a coder btw.

Stuart

It’s stills working for me. What’s your form and to station? Pm me me if you’d rather it private.

Thanks Crispin,

val String STA_ORIGIN = “PAD” //this is your station
val String STA_DEST = “STD” //this is the destination of the train
val String xAPI_KEY = “xxxxxxxxxxxxxxxxxxxxxxxxxx” //this is your API key
val String xAPP_ID = “xxxxxxxxxxx” //this is your appID.

However, when I use:

https://transportapi.com/v3/uk/train/station/PAD/live.json?app_id=xxxxxxxxx&app_key=xxxxxxxxxxxxxxxxx&calling_at=STD&destination=STD&train_status=passenger&darwin=true

I get nothing but if I remove destination:

https://transportapi.com/v3/uk/train/station/PAD/live.json?app_id=xxxxxxxxx&app_key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&calling_at=STD&train_status=passenger&darwin=true

I get data back!:slight_smile:

Not sure if i get Null value as log says:

GetTrainTimes - Starting
09:00:00.713 [INFO ] [smarthome.event.ItemCommandEvent ] - Item ‘TrainDeptPADName’ received command London Paddington
09:00:00.725 [INFO ] [smarthome.event.ItemCommandEvent ] - Item ‘TrainDeptPAD3Status’ received command –
09:00:00.728 [INFO ] [smarthome.event.ItemCommandEvent ] - Item ‘TrainDeptPAD2Status’ received command –

And here is my items file:

//leave these alone
Group gTrainCollection
Group gTrainCollectionItems
/// stop leaving alone

//You can have up to 4 sets below. The format must be:
//TrainDept + Your Station Code + 1/2/3/4 + Whatever is below.

String TrainDeptPADName (gTrainCollectionItems)
Switch TrainTimesRefresh “Refresh Train Times”

String TrainDeptPAD1 (gTrainCollection)
DateTime TrainDeptPAD1TimeExpected (gTrainCollectionItems)
DateTime TrainDeptPAD1TimeAimed (gTrainCollectionItems)
String TrainDeptPAD1Status (gTrainCollectionItems)
Number TrainDeptPAD1Until (gTrainCollectionItems)

String TrainDeptPAD2 (gTrainCollection)
DateTime TrainDeptPAD2TimeExpected (gTrainCollectionItems)
DateTime TrainDeptPAD2TimeAimed (gTrainCollectionItems)
String TrainDeptPAD2Status (gTrainCollectionItems)
Number TrainDeptPAD2Until (gTrainCollectionItems)

String TrainDeptPAD3 (gTrainCollection)
DateTime TrainDeptPAD3TimeExpected (gTrainCollectionItems)
DateTime TrainDeptPAD3TimeAimed (gTrainCollectionItems)
String TrainDeptPAD3Status (gTrainCollectionItems)
Number TrainDeptPAD3Until (gTrainCollectionItems)

String TrainDeptPAD4 (gTrainCollection)
DateTime TrainDeptPAD4TimeExpected (gTrainCollectionItems)
DateTime TrainDeptPAD4TimeAimed (gTrainCollectionItems)
String TrainDeptPAD4Status (gTrainCollectionItems)
Number TrainDeptPAD4Until (gTrainCollectionItems)