I’m looking to be able to integrate data from Manchester’s Metrolink REST API into Home Assistant. Looking at the REST documentation though, I have 2 stumbling blocks:
- The endpoint I am querying has a space in the URI, so do I need to wrap it in quotes or will it work with the spaces left in?
GET https://api.tfgm.com/odata/Metrolinks?$filter=StationLocation eq 'Chorlton'
- The JSON I get back from the API is not hierarchical, so is there a way for me to group the results I get back into a set of sensors and attributes. As an example I’d like to produce something like this:
Sensor 1: sensor.metrolink_outgoing
Attributes: dest0, status0, wait0, dest1, status2, wait1, etc…
Sensor 2: sensor.metrolink_incoming
Attributes: dest0, status0, wait0, dest1, status2, wait1, etc…
From this JSON:
{
"@odata.context": "https://opendataclientapi.azurewebsites.net/odata/$metadata#Metrolinks",
"value": [{
"Id": 897,
"Line": "South Manchester",
"TLAREF": "CHO",
"PIDREF": "CHO-TPID01",
"StationLocation": "Chorlton",
"AtcoCode": "9400ZZMACHO1",
"Direction": "Outgoing",
"Dest0": "East Didsbury",
"Carriages0": "Double",
"Status0": "Departing",
"Wait0": "0",
"Dest1": "Manchester Airport",
"Carriages1": "Single",
"Status1": "Due",
"Wait1": "9",
"Dest2": "Manchester Airport",
"Carriages2": "Single",
"Status2": "Due",
"Wait2": "19",
"Dest3": "",
"Carriages3": "",
"Status3": "",
"MessageBoard": "ENGINEERING WORKS - Services are operating East Didsbury - Deansgate Castlefield ONLY. Replacement Buses are in operation between Deansgate and Central Park. Visit www.tfgm.com for info",
"Wait3": "",
"LastUpdated": "2021-08-04T17:10:21Z"
}, {
"Id": 898,
"Line": "South Manchester",
"TLAREF": "CHO",
"PIDREF": "CHO-TPID02",
"StationLocation": "Chorlton",
"AtcoCode": "9400ZZMACHO2",
"Direction": "Incoming",
"Dest0": "Cornbrook",
"Carriages0": "Single",
"Status0": "Due",
"Wait0": "1",
"Dest1": "Deansgate Castlefield",
"Carriages1": "Double",
"Status1": "Due",
"Wait1": "5",
"Dest2": "Cornbrook",
"Carriages2": "Single",
"Status2": "Due",
"Wait2": "12",
"Dest3": "",
"Carriages3": "",
"Status3": "",
"MessageBoard": "ENGINEERING WORKS - Services are operating East Didsbury - Deansgate Castlefield ONLY. Replacement Buses are in operation between Deansgate and Central Park. Visit www.tfgm.com for info",
"Wait3": "",
"LastUpdated": "2021-08-04T17:10:21Z"
}]
}
Is this even possible given the flat layout of the JSON I get from the API?