Hi there.
This is my first time posting.
I need help with something, and hope anyone can help me.
I am not a programmer, so I dont know much, but I am in a steep learning curve at the moment.
I want to make Home Assistant display on the screen in our hallway how many minutes is left until the train(s) runs.
I have managed so far to get an API and key from the swedis “Trafiklab” which in their API can give me all the train departues from my station.
Now there are three things I want to do and need help with:
Number one:
Exctract “time” only from objects (is it called objects?)
that also have the “departureFlag”: “2”.
This way I will only get the departure time for trains that run in the direction I need.
Number two:
The time that is returned (will this be returned to a sensor then)
will be in HH:MM:SS format.
I need it to be displayed as “x minutes”.
So basically take that HH:MM:SS minus the “time of day”.
I would not know how to program this for Home Assistant to understand.
Number three:
How can I automate the scan times for the API so it does not send requests through the night, and more requests during rush hour? I only have 30 000 pr month.
Thus far the tutorial I have been following has given me this:
Creating a sensor.yaml and of course including it in the config file.
And in the sensors.yaml
- platform: rest
resource: Url of API call
method: GET
name: name of sensor I want
value_template: ‘{{value_json[“Departure”][0].time}}’
scan_interval: 120
This just gives me the time of whichever object from is in place 0 in the JSON.
I dont know how to write code to get all the times in one go either.
Perhaps it is possible to write some if statement, and only extract “time” if
“direactionFlag” == “2”.
I dont know, I am not even 100% sure which language I am trying write in.
Any idea for a good way to display this in Home Assistant is also very much appreciated.
Thank you!
Here is an example of returned API JSON:
{
"Departure": [
{
"JourneyDetailRef": {
"ref": "1|67393|0|1|11012023"
},
"JourneyStatus": "P",
"ProductAtStop": {
"icon": {
"res": "prod_gen"
},
"name": "Länstrafik - Tåg 44",
"internalName": "Länstrafik - Tåg 44",
"displayNumber": "44",
"num": "44",
"line": "44",
"lineId": "1275000000003",
"catOut": "JLT",
"catIn": "JLT",
"catCode": "4",
"cls": "16",
"catOutS": "JLT",
"catOutL": "Länstrafik - Tåg",
"operatorCode": "275",
"operator": "SL",
"admin": "275___",
"matchId": "T23C001 KHÄ-TU LT;T23C001 KHÄ-TU LT;2443"
},
"Product": [
{
"icon": {
"res": "prod_gen"
},
"name": "Länstrafik - Tåg 44",
"internalName": "Länstrafik - Tåg 44",
"displayNumber": "44",
"num": "44",
"line": "44",
"lineId": "1275000000003",
"catOut": "JLT",
"catIn": "JLT",
"catCode": "4",
"cls": "16",
"catOutS": "JLT",
"catOutL": "Länstrafik - Tåg",
"operatorCode": "275",
"operator": "SL",
"admin": "275___",
"routeIdxFrom": 13,
"routeIdxTo": 14,
"matchId": "T23C001 KHÄ-TU LT;T23C001 KHÄ-TU LT;2443"
}
],
"Notes": {
"Note": [
{
"value": "Endast 2 klass",
"key": "AA",
"type": "A",
"routeIdxFrom": 13,
"routeIdxTo": 14,
"txtN": "Endast 2 klass"
}
]
},
"name": "Länstrafik - Tåg 44",
"type": "ST",
"stop": "Tullinge station (Botkyrka kn)",
"stopid": "A=1@O=Tullinge station (Botkyrka kn)@X=17903078@Y=59205222@U=1@L=740000775@",
"stopExtId": "740000775",
"time": "15:20:00",
"date": "2023-01-11",
"reachable": true,
"direction": "Tumba station (Botkyrka kn)",
"directionFlag": "1"
},
{
"JourneyDetailRef": {
"ref": "1|109804|9|1|11012023"
},
"JourneyStatus": "P",
"ProductAtStop": {
"icon": {
"res": "prod_gen"
},
"name": "Länstrafik - Tåg 41",
"internalName": "Länstrafik - Tåg 41",
"displayNumber": "41",
"num": "41",
"line": "41",
"lineId": "1275000000002",
"catOut": "JLT",
"catIn": "JLT",
"catCode": "4",
"cls": "16",
"catOutS": "JLT",
"catOutL": "Länstrafik - Tåg",
"operatorCode": "275",
"operator": "SL",
"admin": "275___",
"matchId": "R23C001 SÖC-U LT;R23C001 SÖC-U LT;2746"
},
"Product": [
{
"icon": {
"res": "prod_gen"
},
"name": "Länstrafik - Tåg 41",
"internalName": "Länstrafik - Tåg 41",
"displayNumber": "41",
"num": "41",
"line": "41",
"lineId": "1275000000002",
"catOut": "JLT",
"catIn": "JLT",
"catCode": "4",
"cls": "16",
"catOutS": "JLT",
"catOutL": "Länstrafik - Tåg",
"operatorCode": "275",
"operator": "SL",
"admin": "275___",
"routeIdxFrom": 1,
"routeIdxTo": 19,
"matchId": "R23C001 SÖC-U LT;R23C001 SÖC-U LT;2746"
}
],
"Notes": {
"Note": [
{
"value": "Endast 2 klass",
"key": "AA",
"type": "A",
"routeIdxFrom": 1,
"routeIdxTo": 19,
"txtN": "Endast 2 klass"
}
]
},
"name": "Länstrafik - Tåg 41",
"type": "ST",
"stop": "Tullinge station (Botkyrka kn)",
"stopid": "A=1@O=Tullinge station (Botkyrka kn)@X=17903078@Y=59205222@U=1@L=740000775@",
"stopExtId": "740000775",
"time": "15:21:00",
"date": "2023-01-11",
"reachable": true,
"direction": "Märsta station (Sigtuna kn)",
"directionFlag": "2"
},
{
"JourneyDetailRef": {
"ref": "1|109759|7|1|11012023"
},
"JourneyStatus": "P",
"ProductAtStop": {
"icon": {
"res": "prod_gen"
},
"name": "Länstrafik - Tåg 41",
"internalName": "Länstrafik - Tåg 41",
"displayNumber": "41",
"num": "41",
"line": "41",
"lineId": "1275000000002",
"catOut": "JLT",
"catIn": "JLT",
"catCode": "4",
"cls": "16",
"catOutS": "JLT",
"catOutL": "Länstrafik - Tåg",
"operatorCode": "275",
"operator": "SL",
"admin": "275___",
"matchId": "R23C001 SÖC-U LT;R23C001 SÖC-U LT;2745"
},
"Product": [
{
"icon": {
"res": "prod_gen"
},
"name": "Länstrafik - Tåg 41",
"internalName": "Länstrafik - Tåg 41",
"displayNumber": "41",
"num": "41",
"line": "41",
"lineId": "1275000000002",
"catOut": "JLT",
"catIn": "JLT",
"catCode": "4",
"cls": "16",
"catOutS": "JLT",
"catOutL": "Länstrafik - Tåg",
"operatorCode": "275",
"operator": "SL",
"admin": "275___",
"routeIdxFrom": 18,
"routeIdxTo": 19,
"matchId": "R23C001 SÖC-U LT;R23C001 SÖC-U LT;2745"
}
],
"Notes": {
"Note": [
{
"value": "Endast 2 klass",
"key": "AA",
"type": "A",
"routeIdxFrom": 18,
"routeIdxTo": 19,
"txtN": "Endast 2 klass"
}
]
},
"name": "Länstrafik - Tåg 41",
"type": "ST",
"stop": "Tullinge station (Botkyrka kn)",
"stopid": "A=1@O=Tullinge station (Botkyrka kn)@X=17903078@Y=59205222@U=1@L=740000775@",
"stopExtId": "740000775",
"time": "15:24:00",
"date": "2023-01-11",
"reachable": true,
"direction": "Tumba station (Botkyrka kn)",
"directionFlag": "1"
},
{
"JourneyDetailRef": {
"ref": "1|67395|1|1|11012023"
},
"JourneyStatus": "P",
"ProductAtStop": {
"icon": {
"res": "prod_gen"
},
"name": "Länstrafik - Tåg 44",
"internalName": "Länstrafik - Tåg 44",
"displayNumber": "44",
"num": "44",
"line": "44",
"lineId": "1275000000003",
"catOut": "JLT",
"catIn": "JLT",
"catCode": "4",
"cls": "16",
"catOutS": "JLT",
"catOutL": "Länstrafik - Tåg",
"operatorCode": "275",
"operator": "SL",
"admin": "275___",
"matchId": "T23C001 KHÄ-TU LT;T23C001 KHÄ-TU LT;2446"
},
"Product": [
{
"icon": {
"res": "prod_gen"
},
"name": "Länstrafik - Tåg 44",
"internalName": "Länstrafik - Tåg 44",
"displayNumber": "44",
"num": "44",
"line": "44",
"lineId": "1275000000003",
"catOut": "JLT",
"catIn": "JLT",
"catCode": "4",
"cls": "16",
"catOutS": "JLT",
"catOutL": "Länstrafik - Tåg",
"operatorCode": "275",
"operator": "SL",
"admin": "275___",
"routeIdxFrom": 1,
"routeIdxTo": 14,
"matchId": "T23C001 KHÄ-TU LT;T23C001 KHÄ-TU LT;2446"
}
],
"Notes": {
"Note": [
{
"value": "Endast 2 klass",
"key": "AA",
"type": "A",
"routeIdxFrom": 1,
"routeIdxTo": 14,
"txtN": "Endast 2 klass"
}
]
},
"name": "Länstrafik - Tåg 44",
"type": "ST",
"stop": "Tullinge station (Botkyrka kn)",
"stopid": "A=1@O=Tullinge station (Botkyrka kn)@X=17903078@Y=59205222@U=1@L=740000775@",
"stopExtId": "740000775",
"time": "15:25:00",
"date": "2023-01-11",
"reachable": true,
"direction": "Kallhäll station (Järfälla kn)",
"directionFlag": "2"
},
{
"JourneyDetailRef": {
"ref": "1|109816|4|1|11012023"
},
"JourneyStatus": "P",
"ProductAtStop": {
"icon": {
"res": "prod_gen"
},
"name": "Länstrafik - Tåg 41",
"internalName": "Länstrafik - Tåg 41",
"displayNumber": "41",
"num": "41",
"line": "41",
"lineId": "1275000000002",
"catOut": "JLT",
"catIn": "JLT",
"catCode": "4",
"cls": "16",
"catOutS": "JLT",
"catOutL": "Länstrafik - Tåg",
"operatorCode": "275",
"operator": "SL",
"admin": "275___",
"matchId": "R23C001 SÖC-U LT;R23C001 SÖC-U LT;2946"
},
"Product": [
{
"icon": {
"res": "prod_gen"
},
"name": "Länstrafik - Tåg 41",
"internalName": "Länstrafik - Tåg 41",
"displayNumber": "41",
"num": "41",
"line": "41",
"lineId": "1275000000002",
"catOut": "JLT",
"catIn": "JLT",
"catCode": "4",
"cls": "16",
"catOutS": "JLT",
"catOutL": "Länstrafik - Tåg",
"operatorCode": "275",
"operator": "SL",
"admin": "275___",
"routeIdxFrom": 5,
"routeIdxTo": 21,
"matchId": "R23C001 SÖC-U LT;R23C001 SÖC-U LT;2946"
}
],
"Notes": {
"Note": [
{
"value": "Endast 2 klass",
"key": "AA",
"type": "A",
"routeIdxFrom": 5,
"routeIdxTo": 21,
"txtN": "Endast 2 klass"
}
]
},
"name": "Länstrafik - Tåg 41",
"type": "ST",
"stop": "Tullinge station (Botkyrka kn)",
"stopid": "A=1@O=Tullinge station (Botkyrka kn)@X=17903078@Y=59205222@U=1@L=740000775@",
"stopExtId": "740000775",
"time": "15:29:00",
"date": "2023-01-11",
"reachable": true,
"direction": "Upplands Väsby station",
"directionFlag": "2"
},
{
"JourneyDetailRef": {
"ref": "1|109765|3|1|11012023"
},
"JourneyStatus": "P",
"ProductAtStop": {
"icon": {
"res": "prod_gen"
},
"name": "Länstrafik - Tåg 41",
"internalName": "Länstrafik - Tåg 41",
"displayNumber": "41",
"num": "41",
"line": "41",
"lineId": "1275000000002",
"catOut": "JLT",
"catIn": "JLT",
"catCode": "4",
"cls": "16",
"catOutS": "JLT",
"catOutL": "Länstrafik - Tåg",
"operatorCode": "275",
"operator": "SL",
"admin": "275___",
"matchId": "R23C001 SÖC-U LT;R23C001 SÖC-U LT;2945"
},
"Product": [
{
"icon": {
"res": "prod_gen"
},
"name": "Länstrafik - Tåg 41",
"internalName": "Länstrafik - Tåg 41",
"displayNumber": "41",
"num": "41",
"line": "41",
"lineId": "1275000000002",
"catOut": "JLT",
"catIn": "JLT",
"catCode": "4",
"cls": "16",
"catOutS": "JLT",
"catOutL": "Länstrafik - Tåg",
"operatorCode": "275",
"operator": "SL",
"admin": "275___",
"routeIdxFrom": 16,
"routeIdxTo": 21,
"matchId": "R23C001 SÖC-U LT;R23C001 SÖC-U LT;2945"
}
],
"Notes": {
"Note": [
{
"value": "Endast 2 klass",
"key": "AA",
"type": "A",
"routeIdxFrom": 16,
"routeIdxTo": 21,
"txtN": "Endast 2 klass"
}
]
},
"name": "Länstrafik - Tåg 41",
"type": "ST",
"stop": "Tullinge station (Botkyrka kn)",
"stopid": "A=1@O=Tullinge station (Botkyrka kn)@X=17903078@Y=59205222@U=1@L=740000775@",
"stopExtId": "740000775",
"time": "15:31:00",
"date": "2023-01-11",
"reachable": true,
"direction": "Södertälje centrum station",
"directionFlag": "1"
},
{
"JourneyDetailRef": {
"ref": "1|109797|1|1|11012023"
},
"JourneyStatus": "P",
"ProductAtStop": {
"icon": {
"res": "prod_gen"
},
"name": "Länstrafik - Tåg 41",
"internalName": "Länstrafik - Tåg 41",
"displayNumber": "41",
"num": "41",
"line": "41",
"lineId": "1275000000002",
"catOut": "JLT",
"catIn": "JLT",
"catCode": "4",
"cls": "16",
"catOutS": "JLT",
"catOutL": "Länstrafik - Tåg",
"operatorCode": "275",
"operator": "SL",
"admin": "275___",
"matchId": "R23C001 SÖC-U LT;R23C001 SÖC-U LT;2648"
},
"Product": [
{
"icon": {
"res": "prod_gen"
},
"name": "Länstrafik - Tåg 41",
"internalName": "Länstrafik - Tåg 41",
"displayNumber": "41",
"num": "41",
"line": "41",
"lineId": "1275000000002",
"catOut": "JLT",
"catIn": "JLT",
"catCode": "4",
"cls": "16",
"catOutS": "JLT",
"catOutL": "Länstrafik - Tåg",
"operatorCode": "275",
"operator": "SL",
"admin": "275___",
"routeIdxFrom": 5,
"routeIdxTo": 23,
"matchId": "R23C001 SÖC-U LT;R23C001 SÖC-U LT;2648"
}
],
"Notes": {
"Note": [
{
"value": "Endast 2 klass",
"key": "AA",
"type": "A",
"routeIdxFrom": 5,
"routeIdxTo": 23,
"txtN": "Endast 2 klass"
}
]
},
"name": "Länstrafik - Tåg 41",
"type": "ST",
"stop": "Tullinge station (Botkyrka kn)",
"stopid": "A=1@O=Tullinge station (Botkyrka kn)@X=17903078@Y=59205222@U=1@L=740000775@",
"stopExtId": "740000775",
"time": "15:36:00",
"date": "2023-01-11",
"reachable": true,
"direction": "Märsta station (Sigtuna kn)",
"directionFlag": "2"
},
{
"JourneyDetailRef": {
"ref": "1|109752|1|1|11012023"
},
"JourneyStatus": "P",
"ProductAtStop": {
"icon": {
"res": "prod_gen"
},
"name": "Länstrafik - Tåg 41",
"internalName": "Länstrafik - Tåg 41",
"displayNumber": "41",
"num": "41",
"line": "41",
"lineId": "1275000000002",
"catOut": "JLT",
"catIn": "JLT",
"catCode": "4",
"cls": "16",
"catOutS": "JLT",
"catOutL": "Länstrafik - Tåg",
"operatorCode": "275",
"operator": "SL",
"admin": "275___",
"matchId": "R23C001 SÖC-U LT;R23C001 SÖC-U LT;2645"
},
"Product": [
{
"icon": {
"res": "prod_gen"
},
"name": "Länstrafik - Tåg 41",
"internalName": "Länstrafik - Tåg 41",
"displayNumber": "41",
"num": "41",
"line": "41",
"lineId": "1275000000002",
"catOut": "JLT",
"catIn": "JLT",
"catCode": "4",
"cls": "16",
"catOutS": "JLT",
"catOutL": "Länstrafik - Tåg",
"operatorCode": "275",
"operator": "SL",
"admin": "275___",
"routeIdxFrom": 18,
"routeIdxTo": 23,
"matchId": "R23C001 SÖC-U LT;R23C001 SÖC-U LT;2645"
}
],
"Notes": {
"Note": [
{
"value": "Endast 2 klass",
"key": "AA",
"type": "A",
"routeIdxFrom": 18,
"routeIdxTo": 23,
"txtN": "Endast 2 klass"
}
]
},
"name": "Länstrafik - Tåg 41",
"type": "ST",
"stop": "Tullinge station (Botkyrka kn)",
"stopid": "A=1@O=Tullinge station (Botkyrka kn)@X=17903078@Y=59205222@U=1@L=740000775@",
"stopExtId": "740000775",
"time": "15:39:00",
"date": "2023-01-11",
"reachable": true,
"direction": "Södertälje centrum station",
"directionFlag": "1"
},
{
"JourneyDetailRef": {
"ref": "1|109781|3|1|11012023"
},
"JourneyStatus": "P",
"ProductAtStop": {
"icon": {
"res": "prod_gen"
},
"name": "Länstrafik - Tåg 40",
"internalName": "Länstrafik - Tåg 40",
"displayNumber": "40",
"num": "40",
"line": "40",
"lineId": "1275000000005",
"catOut": "JLT",
"catIn": "JLT",
"catCode": "4",
"cls": "16",
"catOutS": "JLT",
"catOutL": "Länstrafik - Tåg",
"operatorCode": "275",
"operator": "SL",
"admin": "275___",
"matchId": "R23C001 SÖC-U LT;R23C001 SÖC-U LT;2248"
},
"Product": [
{
"icon": {
"res": "prod_gen"
},
"name": "Länstrafik - Tåg 40",
"internalName": "Länstrafik - Tåg 40",
"displayNumber": "40",
"num": "40",
"line": "40",
"lineId": "1275000000005",
"catOut": "JLT",
"catIn": "JLT",
"catCode": "4",
"cls": "16",
"catOutS": "JLT",
"catOutL": "Länstrafik - Tåg",
"operatorCode": "275",
"operator": "SL",
"admin": "275___",
"routeIdxFrom": 5,
"routeIdxTo": 24,
"matchId": "R23C001 SÖC-U LT;R23C001 SÖC-U LT;2248"
}
],
"Notes": {
"Note": [
{
"value": "Endast 2 klass",
"key": "AA",
"type": "A",
"routeIdxFrom": 5,
"routeIdxTo": 24,
"txtN": "Endast 2 klass"
}
]
},
"name": "Länstrafik - Tåg 40",
"type": "ST",
"stop": "Tullinge station (Botkyrka kn)",
"stopid": "A=1@O=Tullinge station (Botkyrka kn)@X=17903078@Y=59205222@U=1@L=740000775@",
"stopExtId": "740000775",
"time": "15:44:00",
"date": "2023-01-11",
"reachable": true,
"direction": "Uppsala Centralstation",
"directionFlag": "2"
},
{
"JourneyDetailRef": {
"ref": "1|109726|4|1|11012023"
},
"JourneyStatus": "P",
"ProductAtStop": {
"icon": {
"res": "prod_gen"
},
"name": "Länstrafik - Tåg 40",
"internalName": "Länstrafik - Tåg 40",
"displayNumber": "40",
"num": "40",
"line": "40",
"lineId": "1275000000005",
"catOut": "JLT",
"catIn": "JLT",
"catCode": "4",
"cls": "16",
"catOutS": "JLT",
"catOutL": "Länstrafik - Tåg",
"operatorCode": "275",
"operator": "SL",
"admin": "275___",
"matchId": "R23C001 SÖC-U LT;R23C001 SÖC-U LT;2245"
},
"Product": [
{
"icon": {
"res": "prod_gen"
},
"name": "Länstrafik - Tåg 40",
"internalName": "Länstrafik - Tåg 40",
"displayNumber": "40",
"num": "40",
"line": "40",
"lineId": "1275000000005",
"catOut": "JLT",
"catIn": "JLT",
"catCode": "4",
"cls": "16",
"catOutS": "JLT",
"catOutL": "Länstrafik - Tåg",
"operatorCode": "275",
"operator": "SL",
"admin": "275___",
"routeIdxFrom": 19,
"routeIdxTo": 24,
"matchId": "R23C001 SÖC-U LT;R23C001 SÖC-U LT;2245"
}
],
"Notes": {
"Note": [
{
"value": "Endast 2 klass",
"key": "AA",
"type": "A",
"routeIdxFrom": 19,
"routeIdxTo": 24,
"txtN": "Endast 2 klass"
}
]
},
"name": "Länstrafik - Tåg 40",
"type": "ST",
"stop": "Tullinge station (Botkyrka kn)",
"stopid": "A=1@O=Tullinge station (Botkyrka kn)@X=17903078@Y=59205222@U=1@L=740000775@",
"stopExtId": "740000775",
"time": "15:46:00",
"date": "2023-01-11",
"reachable": true,
"direction": "Södertälje centrum station",
"directionFlag": "1"
},
"TechnicalMessages": {
"TechnicalMessage": [
{
"value": "2023-01-11 15:18:37",
"key": "requestTime"
}
]
},
"serverVersion": "2.25.0",
"dialectVersion": "2.25",
"planRtTs": "1970-01-01T01:00:00.000+01:00",
"requestId": "random string"
}