Custom widget: Train departure times (UK Only)

and when I use the http request (post 39 above) i get data but I get Null values for:

aimed arrival time and expected arrival times

Could this be the problem? Because Paddington is the origin station?

It does not work if there is a change in the journey. I don’t know why of how to work around it.
Looking at NationalRail website it says changes=1
http://ojp.nationalrail.co.uk/service/timesandfares/PAD/STD/today/2100/dep

If you use PAD -> SWI it will at least give you the departure time from Paddington.

For Example:
Leaving Paddington and going to Exeter St David’s (PAD -> EXD) will work
Paddington to EXD calling at RDG will also work as it’s a stop to EXD.

If you can find an API which returns the whole route then I can adapt the code to use that and be more dynamic for all. I struggled to find one and, for me, it does not work on weekends as there is no WMG -> MOG as it’s to Kings Cross. But I don’t use the train on weekends so don’t mind :slight_smile:

For now, use PAD to SWI and see if that works for you?

Hi Crispin,

Are you able to get this to work if the origin station is where the train starts? i.e. Padington? i can only get it to work from stations mid journey

S

I’ll rewrite the rue this evening and see if I can make it any better.
Give me a couple of days :slight_smile:

ok, I’ve rewritten it a bit and made some improvements. It makes it easier to try your issues.

For now though, the api website is down so I can’t do much.
https://developer.transportapi.com/docs?raml=https://transportapi.com/v3/raml/transportapi.raml

I’ve emailed them and hopefully they will get it sorted soon.

@surfstu This should now work with your station. See the update notes in the first thread. You must update the widget too as I made breaking-changes to that.

Let me know how you get on.

Morning Crispin,

Thanks very much for your time on this. Unfortunatley it does not work. I discovered that i needed to uncomment 3 lines from your rules file posted at begining of thread. The “// val String STA_ORIGIN” declarations. Is this correct, it seemed to throw up less errors in log after this.

The new widget would not load into habpanel for me due to a problem with first character line 1??? This is what it said:

"
Widget import error: SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
"

I could not work out why this would be…

So… I made a switch to trigger TrainTimesrefresh and a dummy item to see if I got any returned values as the logging from the old rules did not show me anything. This returned nul value with the following warning in the logs:

"
09:08:12.061 [INFO ] [smarthome.event.ItemStateChangedEvent] - TrainTimesRefresh changed from OFF to ON
09:08:12.360 [WARN ] [org.eclipse.smarthome.model.script.F ] - https://transportapi.com/v3/uk/train/station/RDG/live.json?app_id=ad391ec2&app_key=55721e6b3bc3b717e89afe4ed10347ac&calling_at=SWI&origin=PAD&train_status=passenger&darwin=true
09:08:13.208 [INFO ] [smarthome.event.ItemCommandEvent ] - Item ‘TrainTimesRefresh’ received command OFF
"

If it helps also, when I load the rule files, having uncommented the 3 val declarations i get:

"
09:05:29.275 [INFO ] [del.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model ‘trains.rules’, using it anyway:
Function2 is a raw type. References to generic type Function2<P1, P2, Result> should be parameterized
The function must return a result of type Object.
The method getCalendar() from the type DateTimeType is deprecated
The value of the local variable itemStatus is not used
09:05:29.283 [INFO ] [del.core.internal.ModelRepositoryImpl] - Refreshing model ‘trains.rules’
"

If you have any ideas about this then do let me know,

thanks

Stuart

well, that will teach me to release this while also watching TV. :slight_smile:

ok, the widget I broke by uploading only half the code. Sorry :frowning:
New updated is there so should be better. (Caveat - I have not tested it yet)

The rule:
Yes, you need to change the stations to be your stations and un-comment the code.

I tested this with PAD->SWI as I thought that is where you travelled. I see your example has reading in it so maybe the API works differently with that. I can play around with that tonight if it does not work for you.

So long as you see this and not a red error, it’s ok. I know there are warnings which I will look at some time but the rule parses ok if you see the above message.

C

It works! It did not work whilst I was overtired last night but I had a feeling that this may be because there were no trains running at 1am

Today I have data, thanks so much Crispin for your efforts.

S

Awesome to hear!

@CDriver
I’ve just updated to the latest version and seem to have an issue where none of the items appear to be receiving data. I’m not using the HABPanel widget at this stage as I’m still building out my entire system.

I can see that the URL is working and from the logged output the right information is being returned. Just the items aren’t being updated as the simple sitemap I have doesn’t show the expected data.

The previous version worked, although I received the errors you’ve already attempted to resolve in this thread. Hence the reason for trying your new version.

Any help would be most appreciated.

Thanks,
Garry

Did you change the items? The items used to have the station code in them. They no longer have that.
If the above is OK, send me the values you’re using.

Yes I changed the items, as below. My source station is SWI and I’m heading to PAD.

Thanks,

Garry

//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 TrainDeptName (gTrainCollectionItems)
Switch TrainTimesRefresh "Refresh Train Times"

String TrainDept1 (gTrainCollection)
DateTime TrainDept1TimeExpected (gTrainCollectionItems)
String TrainDept1Status (gTrainCollectionItems)
Number TrainDept1Until (gTrainCollectionItems)
Number TrainDept1Platform (gTrainCollectionItems)

String TrainDept2 (gTrainCollection)
DateTime TrainDept2TimeExpected (gTrainCollectionItems)
String TrainDept2Status (gTrainCollectionItems)
Number TrainDept2Until (gTrainCollectionItems)
Number TrainDept2Platform (gTrainCollectionItems)

String TrainDept3 (gTrainCollection)
DateTime TrainDept3TimeExpected (gTrainCollectionItems)
String TrainDept3Status (gTrainCollectionItems)
Number TrainDept3Until (gTrainCollectionItems)
Number TrainDept3Platform (gTrainCollectionItems)

String TrainDept4 (gTrainCollection)
DateTime TrainDept4TimeExpected (gTrainCollectionItems)
String TrainDept4Status (gTrainCollectionItems)
Number TrainDept4Until (gTrainCollectionItems)
Number TrainDept4Platform (gTrainCollectionItems)

I also get this error when saving the rules file.

The value of the local variable itemStatus is not used

Don’t worry about that. It’s not an error but rather a misguided warning.

Odd, I see the API is returning nothing for SWI to PAD.
I’ll play around with it over the next few days but can’t promise when as I’m on holiday at the moment :slight_smile:

Thanks for taking a look. If you blank the STA_ORIGIN then the API will return data.

I’ve manage to sort it out, problem solved. Let’s just put it down to user error… :smile:

Like a lot of people, over the past couple of years I’ve been working from home and not needed to use the Trains. I’ve now changed jobs and having to travel into London at least once a week and decided to bring this back to life on my setup.

I’ve upgraded to Version 3.3.0 and found that the code doesn’t work as it is presently. This is the error I get:

Script execution of rule with UID 'Travel-1' failed: Text '1970-01-01T12:40:00.000+0100' could not be parsed, unparsed text found at index 26 in Travel

Anyone able to offer some assistance? I’m by no means a coder so trying to debug what’s happening is above my pay grade.

Thanks