Brussels Public Transportation (STIB-MIVB) integration (API v2)

Hi,
Just wanted to share my new script that publishes waiting times as json data to a mqtt topic which then can be used in Home-Assistant : GitHub - danito/stib2mqtt: Send realtime data from the Stib api v2 to a mqtt broker

The integrations made by @Helldog136 and @Emilv2 are still working, but I wanted to check out their new API v2.
Update:
I rewrote the script https://github.com/danito/stib2mqtt/tree/main/stib2mqtt to use the new API v2.1 and creating mqtt autodiscover topics.
Add a list of stop names, no need to look for stop_ids anymore.

Update 2:
new script available on GitHub - danito/stibgtfs2mqtt: get real time data from STIB-MIVB api and offline data from GTFS that uses or GTFS or API data to create the sensor.
Stib API:


GTFS + API:

Daniel

1 Like

Thanks Daniel, I will try this.

I just updated the script. It corrects some bugs and tries to catch connexion errors.

Hi Daniel,

Everthing is installed but when the script runs, I get the following error:
Error loading script stib2mqtt.py: Line 314: ā€œnameā€ is an invalid variable name because it starts with ā€œ_ā€

Strange. Do you run it with python3 stib2mqtt.py ?
Can you copy the output?

I think I made a mistake, I just copy the files into the python_scripts directory and used the service python_scripts.stib2mqtt.
How to install this in ha ?

Iā€™m afraid it doesnā€™t work in HA, itā€™s meant to run in its own session. I run it in a tmux session so I can run it forever. It pushes the data to my mqtt server and HA gets the data from there.
Maybe @helldog136 can have a look and make it a custom integrationā€¦

Update: new version https://github.com/danito/stib2mqtt/tree/main/stib2mqtt

@nxd4n I try the latest version of your python script but the command ā€˜python3 stib2mqtt.pyā€™ give me the following error:

Traceback (most recent call last):
  File "/root/stib2mqtt/stib2mqtt.py", line 8, in <module>
    import async_timeout
ModuleNotFoundError: No module named 'async_timeout'

Hi,
did you try in install it ?

pip3 install async_timeout

You might need to do this for other modules.
And please re-download the script, I just found out that it didnā€™t update the second sensor. I just updated the git.

And if you want to delete the entities, please uncomment the line 405

#j_config  = "" 

so it sends an empty config string.

Now I get this error message:

  File "/root/stib2mqtt/stib2mqtt.py", line 430
    continue
    ^
SyntaxError: invalid syntax

@nxd4n Hello, do you have any idea about the syntax error in my preview post ?

yes, there was a missing ] on line 429. I have uploaded a new version.
Please try again and open an issue on Github if you have any other issues.
Thanks
Daniel

@nxd4n
The module paho canā€™t be installed on my system:

root@odroidhc4:~/stib2mqtt# python3 stib2mqtt.py
Traceback (most recent call last):
  File "/root/stib2mqtt/stib2mqtt.py", line 16, in <module>
    from paho.mqtt import client as mqtt_client
ModuleNotFoundError: No module named 'paho'
root@odroidhc4:~/stib2mqtt# pip3 install paho
ERROR: Could not find a version that satisfies the requirement paho
ERROR: No matching distribution found for paho
pip3 install paho-mqtt

it would be too easy if the pip package has the same name than the module name.

It works now. Thanks
Could you explain how to start this python in background at startup. Iā€™m on linux Debian

There are several ways, but I have installed tmux (sudo apt install tmux).
This permits to launch a script in a sort virtual terminal that stays running even if you close your initial terminal.

pi@raspberrypi:/samba/shares $ tmux
pi@raspberrypi:/samba/shares $ python3 stib2mqtt.py
[ctrl + B D to exit the instance]

With

pi@raspberrypi:/samba/shares $ tmux a

you can return to this instance from any other terminal window.
Screen is also an alternative.

hello, thank you for your work
I test it, and it working good ! i used tmux to maintain the session running but the probleme is the script stop after like 2 hours ! the stop behouvior is similair to clt - c

as workaround, i used cron to be sure that the script run, but i got another probleme with aiohttp connection, i got this error ā€œUnclosed client sessionā€

do you have any solution for this?

is there any project t intgrate this to custum componant of ha, to run it on hassio?

thank you very much

Hi,
Iā€™m afraid thereā€™s a bug in the published version. As soon as a line is out of service, the script stops working (key errror). Iā€™m working on a new version which works with a different approach and corrects different bugs.
Iā€™m testing it now, and will soon upload it to Github.
Next step I will try to get a custom integration working.

2 Likes