Help with templating

Hi all, trying to set up a rest sensor for this json https://api.tfl.gov.uk/Line/district/Status
and I want the “statusSeverityDescription” field. I have the below but not working… Any tips appreciated :slight_smile:

It’s somehting like

value_json[0][‘lineStatuses’][0][‘statusSeverityDescription’]

Note that the template dev tool is really helpful for this kind of thing.

{% set JSON = [{"$type":"Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities","id":"district","name":"District","modeName":"tube","disruptions":[],"created":"2017-04-01T01:31:02.467Z","modified":"2017-04-01T01:31:02.467Z","lineStatuses":[{"$type":"Tfl.Api.Presentation.Entities.LineStatus, Tfl.Api.Presentation.Entities","id":0,"statusSeverity":10,"statusSeverityDescription":"Good Service","created":"0001-01-01T00:00:00","validityPeriods":[]}],"routeSections":[],"serviceTypes":[{"$type":"Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities","name":"Regular","uri":"/Line/Route?ids=District&serviceTypes=Regular"}],"crowding":{"$type":"Tfl.Api.Presentation.Entities.Crowding, Tfl.Api.Presentation.Entities"}}] %}

{{JSON[0]['lineStatuses'][0]['statusSeverityDescription']}}
2 Likes

Also, keep in mind that JSON is just a collection of objects, usually arrays. And the arrays are zero-based. You might need to experiment to learn when to use a number index and when to use a string (like “statusSeverityDescription”).

Thanks mate! Working great now. One last question: is it possible to turn the train icon to green or red depending on the status?

I don’t think that’s possible with normal config. I think you’d need to do some extra work that’s not supported by HA (dynamically changing some CSS in the front-end).

1 Like

A work around for changing icons on sensors is to use automation. You can create a group call District line in Good Service and customize the icon to a green bus. You can then create another group called District line is Out of Service with a red bus icon. You can then automate which one is hidden or visible based on your District line sensor.

Thanks Rob!