Exctract information from JSON

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"
}

Need a bit more data and might not be able to help everywhere

If the json provided, do you need information from all trains, some or just one
If just a few then easier to create a sensor per train and add departure time as state

If you make the device class timestamp then it will show in a card as ‘in xx minutes’ by itself

This I am less familiar with. REST will just continue along the interval but what you could do is creata a command-line sensor using CURL that will run at specfici times (automation), which puts the output in a file, the file you then use as ‘resource’ in the REST sensor
The file-approach solution you can use right now, just put a file trains.json in your config/www directory and copy the content of the json into it. The ‘resource’ then becomes http://yourip:8123/local/trains.json

And to avoid peopl searching for the attributes you want, do provide a bit more precise what you want to extract from the json, refer to line-number please as well

The JSON is not valid as it stands: I wonder if you haven’t selected all of it properly? It can be fixed by removing the technical messages at the bottom so that it ends:

      "directionFlag": "1"
    }
  ]
}

From there, we can look at your requests.

Extract “time” only from objects that also have the “departureFlag”: “2”.

departureFlag doesn’t appear in the structure. I assume you mean directionFlag?

{{ value_json['Departure']|selectattr('directionFlag','eq','2')|map(attribute='time')|list }}

# gives me ["15:21:00","15:25:00","15:29:00","15:36:00","15:44:00"]

Once you’ve given a bit more information as per @vingerha’s posts, we’ll look at the rest of the questions.

Thank you so much!
I find it amazing that people care to reply so fast.

It is very smart to make the resource a separate file.
Then I dont have to send an API call for each piece of information that I want, right?

It will be just 3-4 trains that I want the information from. But my problem is that because I dont know how to filter out only train departures with “directionFlag”: 2, I have to create a sensors for every instance in the array.
One sensor for [0], one for [1] and so on.
Then, the only way I would know how to do this would be to create a sensor:first_train
‘{{value_json[“Departure”][0].directionFlag}}’ . Then, in Home Assistant
create a script that if sensor:first_train == 2 then display ‘{{value_json[“Departure”][0].time}}’ .

I would have to create 10 different sensors and 10 corresponding “time”-states.
I think this way I would be able to do it,
but is there no way to create something like:

if (“directionFlag” of [0,9]) == 2:
print(“time”)

I dont know, I am making up my own language.
But some code that goes through every object in the array and prints the value of “time” only
if “directionFlag” is equal to 2.

I am sorry if I am explaining poorly, but I am learning.
Thank you for the tips of including line numbers for the code.

I will repost the full JSON below.
4, 78 , 152, 226 are lines where information about every new train starts.
75, 149, 223, 297 are lines where I find “directionFlag”. This is my condition.
71, 145, 219, 293 are lines where “time” is.

Hi. Thank you for replying.

You are right, I meant “directionFlag”.

I first had to delete some of the code because I could only post 32 000 characters.
I will post it again below and hope I can get all of it.

EDIT: The full JSON is 39 000 characters, so I dont know how to post all of it.

I think you suggestion gives the correct times!
If I can now somehow use these times to get a table like

Next train leaves in: x minutes
After that in: y minutes
z minutes

and so on…

Thank you

There is no option from the source to reduce this? Donot get me wrong, I have a few very long ones but I also see issues in repsonse-to-screen. With <10 a day I guess that has no impact but it would never harm to reduce the stream as at some point many small (?) streams will make one big one.

EDIT: @Troon has given the pointers and if you donot get out, maybe can add more, I am out for the day (seems I need to have a life LOL)

There is an option to reduce it to trains only going one direction, but you have to input end destination,
and there are several trains passing in the correct direction, but with different destinations.
Not possible to add several of these filters. Only one.
Thats why “directionFlag” was such a good option, but not possible to modify the JSON from the
API Call whit “directionFlag” as a condition.

The API also can maximum give the trains for the next 60 minutes.
That is about 10 trains. But I only need the information from 3-4 of these.
So its not 10 per day, but per hour.

Thank you for the help.
Have a nice day!

Urgh, can’t believe you tried to get ChatGPT to solve this.

Create a rest sensor using the line of code I gave you to extract the list of times.

Then create a series of template sensors to extract each time from the list:

sensor:
  - platform: rest
    resource: YOUR_URL
    name: Train times list
    value_template: "{{ value_json['Departure']|selectattr('directionFlag','eq','2')|map(attribute='time')|list }}"
    scan_interval: 120

template:
  - sensor:
      - name: Next train time 1
        state: >-
          {% if states('sensor.train_times_list')|from_json|length > 0 %}
            {{ (states('sensor.train_times_list')|from_json)[0] }}
          {% else %}
            unavailable
          {% endif %}

      - name: Next train time 2
        state: >-
          {% if states('sensor.train_times_list')|from_json|length > 1 %}
            {{ (states('sensor.train_times_list')|from_json)[1] }}
          {% else %}
            unavailable
          {% endif %}

…and so on.

And in future, please format any code in your posts with the </> button.

As far as I know this only works if the main/rest sensor is below 255 char…which I believe to be the ‘state’ limit. There is an option to import as attributes which seems to have no (?I have no clue?) limit

My “extract the times” template needs 11 characters per time, plus one overhead - so should be fine so long as there aren’t more than 22 times in the list. See my post above.

OK, but I was thinking about multiple train-lines, not just the time
EDIT: the OP should chime in and present their view

You can create separate lists per line; and if needs be, constrain the number of items with something like (foo|list)[:10].

Clear but if stored in state, the limit is (?) 255. If the OP is ok then this is the best (!) option indeed as putting stuff in attribs and then adding new sensor just makes is ‘less nice’

You don’t need to stuff this in the attributes, you can use the rest integration (instead of using the rest platform)

quite sometimes it is not needed to create multiple sensors with rest. I use a workaround to collect data into a single sensor with substantial attrib…this allows me to run a graph on it which I would not be able to do if it was split into sensors (in the line of weather ‘forecast’)

Hi Troon,

I apologize for this. I thought that my original post had drowned in other requests and forgotten about.
Since I am a beginner I thought instead of asking again for help (which I eventually ended up doing) I would try ChatGPT which I had never tried before and only heard and read about in the news.
The code GPT generated looked cool to me, so I thought it might work, and thought perhaps there only was some slight error that someone could point out.
I was wrong.

Thank you so much for the work you put in to create this for me.
I am not indifferent to the time people have to put in to help others, so I am very grateful.
I will repay you by learning a lot more myself so that I dont have to waste any more time of others, and perhaps be able to help someone else down the road.

Perhaps my biggest problem when learning this (I have only been at Home Assistant and Raspberry Pi and coding for abouth a month) is that sometimes I dont even know where to begin looking for information.

Tutorials on YouTube only tend to tell you what to do, not why you do it, or how it works.
And for this case there was no tutorial for my exact request.

If you could point me in a direction where to start reading to create sensors, templates or integrate stuff from the web, I assure you I will read. I am not trying to just get other people to do my work, I want to learn.

As for you last comment,
HA said I needed code and that would fix the code being displayed in my post.
Thats why I probably havent done it correctly.
Ill try and figure out how to do what you told me.

Thank you again.

1 Like

Yes. With the extraction of trains only going one direction I think it will only be about maximum of 10-11 trains. 11 characters per time x 10 will be 110. So it should work then. Thank you for helping me!

Thank you so much for helping me! I will post back if I make it work, or not.
I think I have stupidly given the impression that perhaps I am lazy and only want you to fix my problems.
I want to say that I am a very beginner, and because I think this is so much fun that I have spent quite a lot of time to get to where I am now.
I was super exited when I could just actually get a JSON file from an API! Hehe
I didnt even know what that was a month ago.

I would be very grateful if someone could point me in a direction where I can read more about how to use custom code in Home Assistant. I have done some easy Python code, which I understand HA is based on, but I have to use Jinja2 when coding a template?
And I still have to learn how to use the configuration.yaml file correctly and how to reference to other files and how to create custom stuff.
When I dont know where to start it feels like it would take forever to educate oneself. But its fun!

Thank you again for your time!

Absolutely no need to apologise. Hope it works out for you, and do come back with code and specific questions if not. Just amused me that we’ve got to the point where it’s almost reasonable to ask a general purpose machine to write code…

HA has excellent documentation — I’ve linked to some of it already but the starting point is here:

It’s better to work through this than use YouTube tutorials — HA has evolved a lot over the years, so many videos will be out of date.