EDIT: Oh, yeah…pauls idea is way better. Just use that lol. The solution below works for me (in python), but it wasn’t light weight at all. required a few packages installed…and uses chromium in the background to render the HTML.
This lib has a built-in BeautifulSoup scraper but also handles the javascript. You could just write a python script that can parse this and use a command_line sensor to run it every so often (default is every 30 seconds)
from requests_html import HTMLSession
session = HTMLSession()
r = session.get('http://opole.kiedyprzyjedzie.pl/#/stops/122/departures')
r.html.render() # this call executes the js in the page
output = r.html.find('.line-no', first=True)
print(output.text)
Thank you! It seems to be the easiest method to get requested data.
I will test it later on (currently I have a high cpu load problem and I am triing to figgure it out…).
BTW - how did you find to change www address from http://opole.kiedyprzyjedzie.pl/#/stops/122/departures to http://opole.kiedyprzyjedzie.pl/api/departures/122 ??
This is a key in this particular problem…
How did you know that you should add “api” and adjust the address accordingly?
I must admit that I tried to find access to api on the site (http://kiedyprzyjedzie.pl/), but with no result…
The API response currently doesn’t have anything in the rows array, so I’m guessing it’s got an error trying to create it. Can you enable logging and see if there are any errors?
Then I get error in log (Supervisor / System / Log provider -> Core):
homeassistant.exceptions.InvalidStateError: Invalid state encountered for entity id: sensor.platform_2. State max length is 255 characters.
2020-12-24 00:26:11 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up rest platform for sensor
Oh I’m sorry! Reading the restful docs again, we need to use value_template to get the value out. the json_attributes* stuff is just to set sensor attributes, but not the value!
What if will be no rows [1] and [2]? (there are no further buses)
“Code” looks dirty, sensor by sensor… I am quite sure that it has to be way to make it much cleaner (maybe some loop or something). Unfortunately, my experience regarding json, rest, etc. is almost 0… Do you think that is a better way to code it or this is a right way?
Ok, I am going back to home duties… Thank you so much for your help!
You could combine them and have them be attributes of a sensor to make it look cleaner but it makes it a little more work to pull the data back out (not much). If it works for you keep it. I would just create a duplicate sensor to play with for testing. The only thing I would do is utilize customize.yaml to give friendly names and icons like: