I am trying to extract specific attributes from a JSON api to pull real-time train arrivals for the DC metro system (WMATA). So far I have been able to pull the first train listed (value_template: '{{ value_json.Trains[0].Min }}), but I am struggling with specifying I want to the first train in a certain direction (specified by the ‘Destination’). Can this be done via a template or is the JSON too complex?
My current configuration is:
[code]
- platform: rest
resource: https://api.wmata.com/StationPrediction.svc/json/GetPrediction/A01
method: GET
headers:
api_key: REDACTED
value_template: ‘{{ value_json.Trains.Min }}’[/code]
Which produces:
[
{
"Car": "6",
"Destination": "Shady Gr",
"DestinationCode": "A15",
"DestinationName": "Shady Grove",
"Group": "2",
"Line": "RD",
"LocationCode": "A01",
"LocationName": "Metro Center",
"Min": "BRD"
},
{
"Car": "8",
"Destination": "SilvrSpg",
"DestinationCode": "B08",
"DestinationName": "Silver Spring",
"Group": "1",
"Line": "RD",
"LocationCode": "A01",
"LocationName": "Metro Center",
"Min": "ARR"
},
{
"Car": "6",
"Destination": "Shady Gr",
"DestinationCode": "A15",
"DestinationName": "Shady Grove",
"Group": "2",
"Line": "RD",
"LocationCode": "A01",
"LocationName": "Metro Center",
"Min": "5"
},
{
"Car": "6",
"Destination": "Glenmont",
"DestinationCode": "B11",
"DestinationName": "Glenmont",
"Group": "1",
"Line": "RD",
"LocationCode": "A01",
"LocationName": "Metro Center",
"Min": "7"
},
{
"Car": "8",
"Destination": "Shady Gr",
"DestinationCode": "A15",
"DestinationName": "Shady Grove",
"Group": "2",
"Line": "RD",
"LocationCode": "A01",
"LocationName": "Metro Center",
"Min": "9"
},
{
"Car": "8",
"Destination": "SilvrSpg",
"DestinationCode": "B08",
"DestinationName": "Silver Spring",
"Group": "1",
"Line": "RD",
"LocationCode": "A01",
"LocationName": "Metro Center",
"Min": "13"
}
]
Adding [2] for example to value_template: ‘{{ value_json.Trains[2].Min }}’ returns ‘5’. How can I use the value_template to only show, say the first result, with a ‘DestinationCode’ of B08 or B11?
Bonus question: is it also possible to ignore results when the ‘Min’ attribute returns ARR or BRD? Since I don’t live on top of a station, its too late to catch these trains.