REST sensor - help with pulling JSON bus times

Hi, I have been struggling to set up a rest sensor that pulls bus times from the operators API. I’ve tried a few configs based on posts I’ve seen here but I am not very good with code, in particular JSON.

Could someone please advise how I could pull the next 3 “dueIn” times from this resource and display those on a card?

Here’s the code from one of my failed attempts below.

- platform: rest
    resource: https://www.trentbarton.co.uk/RTILiveTimings.aspx?m=GetRtiFull&service=8&stop=22326
    method: GET
    name: cotgrave_bus
    value_template: "
        {% for entry in value_json %}
          {% if loop.index <= 3 %}
            {{entry.dueIn  }}
          {% endif %}
        {% endfor %}"

Any help appreciated! Thanks.

You are alsmost there, paste this in Developter tools > Template and play around

{## Imitate available variables: ##}
{% set value_json = [
  {
    "serviceName": "the cotgrave",
    "serviceIcon": "\"the",
    "serviceIconRaw": "/resources/res.aspx?p=/DF243034569DC881C3ADBB185E7EF7869F1F67FC4B037931/cotgrave-connection.png",
    "stopName": "Ring Leas",
    "latLongPosition": "52.906537,-1.029848",
    "latitude": 52.906537,
    "longitude": -1.029848,
    "result": [
      {
        "uniqueIdentifier": "400144",
        "serviceName": "the cotgrave",
        "serviceDisplayName": "the cotgrave",
        "serviceIcon": "\"RouteListServiceIcon\"",
        "busIconRaw": "/images/icons/bus.png",
        "dueIn": "3 mins",
        "destination": "Nottingham",
        "dueTime": "2024-12-02T13:11:00Z",
        "latitude": 52.912528,
        "longitude": -1.037203,
        "preciseTime": 3.0,
        "cancelledService": false,
        "ServiceId": 8
      },
      {
        "uniqueIdentifier": "400142",
        "serviceName": "the cotgrave",
        "serviceDisplayName": "the cotgrave",
        "serviceIcon": "\"RouteListServiceIcon\"",
        "busIconRaw": "/images/icons/bus.png",
        "dueIn": "23 mins",
        "destination": "Nottingham",
        "dueTime": "2024-12-02T13:31:00Z",
        "latitude": 52.949158,
        "longitude": -1.146637,
        "preciseTime": 22.0,
        "cancelledService": false,
        "ServiceId": 8
      },
      {
        "uniqueIdentifier": "400143",
        "serviceName": "the cotgrave",
        "serviceDisplayName": "the cotgrave",
        "serviceIcon": "\"RouteListServiceIcon\"",
        "busIconRaw": "/images/icons/bus.png",
        "dueIn": "51 mins",
        "destination": "Nottingham",
        "dueTime": "2024-12-02T13:51:00Z",
        "latitude": 52.908916,
        "longitude": -1.034698,
        "preciseTime": 51.0,
        "cancelledService": false,
        "ServiceId": 8
      }
    ],
    "serviceTimetable": [
      {
        "timetableLink": "/services/cotgrave/timetable",
        "serviceName": "the cotgrave",
        "serviceIcon": "\"RouteListServiceIcon\""
      }
    ],
    "communicationError": false,
    "status": true,
    "statusMessage": " Communication Issue We are currently experiencing an issue with realtime information. Please either view the timetables below, or try again in a few minutes."
  }
] %}

{{ value_json.0.result[0:3] }}

{% for entry in value_json.0.result %}
{% if loop.index <= 3 %}
{{entry.dueIn  }}
{% endif %}
{% endfor %}
1 Like

Brilliant, thank you. I haven’t used Dev Tools before, will be a handy tool going forwards!