Yet another public transport API / RestArray integration

I have started “working” on a new custom integration for a german public transport company called VRN (which also contains data for a bunch of other companies like RMV, MVG, ESWE, etc.

I found it because of no existing integration giving out the data i need for my home town.

Original API URL is https://www.vrn.de/mngvrn/XSLT_DM_REQUEST?outputFormat=json&language=de&stateless=1&coordOutputFormat=WGS84[DD.ddddd]&coordOutputFormatTail=7&type_dm=stop&name_dm=6032211&useRealtime=1&mode=direct&ptOptionsActive=1&deleteAssignedStops_dm=1&useProxFootSearch=0&mergeDep=1&limit=5

At first i wanted to just read out the full thing as a custom rest sensor and get the json attributes, but thats a lot of config work and i quickly reached the REST integration 255 character limit. So i started ducttaping a bunch of existing integration code snippets, documentation examples and ChatGPT-generated code together.

Here’s the project currently on github: GitHub - Bluscream/homeassistant-next_busses-addon: HomeAssistant addon

Also i made my own proxy to the API to boil the response down to the only important values and to do some basic pre-processing (Here’s the code for that: RNV Parser API for HomeAssistant, etc. · GitHub)

API Proxy URL is https://minopia.de/api/mvg/?stop=6032211&platforms=B,C,D&max=5&meta

Now i’m wondering if instead of making the integration specifically for this API why not make a RestArray integration instead that just reads any array/dict from a json url and automatically creates and updates sensors with the subkeys as attributes from it. That must be useful for other people aswell, doesn’t it?

If anyone can contribute or even fix this hell i created, i would be more than grateful and send out $50 via paypal as a thank you

It is always nice to see when people try to create an integration, I started once nd the word ‘hell’ applied too :slight_smile:
I did not look at your work as I have no interest in this town’s service but …

Depending on what you need, note that you could put the whole response in the attribute of a template sensor and then work from there on…as REST does not allow attribute templating (as you know)

I also noted that Darmstadt offers gtfs (soll-plan) and gtfs is already an integration HA offers…again not sure if this covers your needs.

Lastly, I myself wrote some rest sensors on a so-called real-time state of a few stops per busline (going to /from school), sadly the French data sucks left-to-right so I abandoned that but it you want I can share

Yes, please share your code so i and others can learn from it :3

In this case I have to search through a large json file for the area covering my busline and direction (LR530A1). Then find the stop time for my busstop. And then return the time with delay.
The data is fully available so you can download a subset and see that I mean using a json viewer

rest:          
  - authentication: basic
    scan_interval: 300
    verify_ssl: false
    headers:
      Content-Type: application/json
      User-Agent: Home Assistant
      Accept-Encoding: identity
    resource: https://proxy.transport.data.gouv.fr/resource/region-sud-gtfs-rt-trip-update
    sensor:
      - name: 530_rt_aller_Mouans_first
        icon: mdi:bus
        unique_id: 530_rt_aller_Mouans_first
        value_template: >
             {% if value_json is defined %}
             {% set y = value_json.entity | count %}
             {% set ns = namespace(val="unknown") %}
             {% for x in range(0,y) %}           
                {% set trip = value_json["entity"][x]["trip_update"]["trip"]["trip_id"] %}
                    {% if trip[:7] == "LR530A1" %}
                        {% set y2 = value_json["entity"][x]["trip_update"]["stop_time_update"] | count %}
                        {% for x2 in range(0,y2) %}
                            {% set stop = value_json["entity"][x]["trip_update"]["stop_time_update"][x2]["stop_id"] %}
                                {% if stop == "STOPPOINT:01275" %}
                                    {% set ns.val = (as_local(as_datetime(value_json["entity"][x]["trip_update"]["stop_time_update"][x2]["departure"]["time"] + value_json["entity"][x]["trip_update"]["stop_time_update"][x2]["departure"]["delay"]))).strftime('%H:%M') %}
                                {% endif %}    
                        {% endfor %}   
                    {% endif %}
             {% endfor %}{{ns.val}} 
             {% else %}
             unknown
             {% endif %}

That’s why I just use command line sensors instead. curl to get the data, pipe the response to jq. jq can do basically anything Jinja can do, massage the response as needed into so you can capture what you want as the attributes and state.

Thanks but (at present) beyond my grasp :slight_smile: … I am reasonably well known to getting data into HA and work from there. The other option is what the OP did …write own scripts.
In this case I am not sure if it could be resolved with the gtfs integration, data-quality is at the root of all.