Python Library
I put together a simple Python library for the API in case anyone wants to give it a try. It focuses mainly on usage summary and away mode, but I'd be happy to add other parts of the API if anyone would find it useful.
-
Official comment
Thank you for creating a Python Library for our API!
We are actually in the process of creating a Python Library for the StreamLabs API, but it has not been published yet. Once we have published, we will let you know. In the meantime, thank you for sharing!
Comment actions -
Hi Chris,
Thank you very much for your work on this (!)... I'm still climbing the learning curve on how to build around APIs, and your Python code has far and away been the most helpful thing I've stumbled upon.
The Summary data I can pull with your code is working great for me. I'm now trying to add to it so that I can also get hourly usage data for my monitor. Here's the very simple code I've added:
def get_hourly(self, location_id: str) -> dict:
start_time="2019-03-24T12:03:08-07:00"
url = self.__STREAMLABS_API_URL + "/locations/" + location_id + "/readings/water-usage/?startTime=" + start_time
headers = self.__get_base_headers()
r=requests.get(url, headers=headers)
print(r)
return r.json()The Streamlabs API documents say you can also specify an end_time, a groupBy, and a page # to retrieve, but that all of these will have default values if not entered, so just giving start_time should be enough (I figured I could add the other strings to my url request once I figured "simplest case" out!).
However so far, my request is getting:
<Response [200]>
None
I definitely know from my Streamlabs App and my Summary data returned from your code that I should be seeing a table populated with hourly information, so I'm not quite sure where I"m going wrong. Could you or anyone else that might be working on this share their knowledge?
Thank you,
Jason -
Well that's peculiar... just tried the code again and it worked this time! Maybe there's a bit of lag on the Streamlabs Cloud that was giving me the "None" error before... now I'm able to get the first 50 of my around 1243 hours of usage data.
In any case the hourly usage data might be a neat feature to have, especially in e.g. a Home Assistant-type dashboard panel. I'm happy to share any code that I put together here or on github, but it's definitely not hard to figure out given the examples Chris provided.
-
Chris, thanks for putting this together. I noticed you don't use the water-usage request anywhere, just water-usage/summary. How come?
In my experiments, all of the API requests work, except water-usage. No matter what I do, I keep getting an "invalid ISO 8601 date time format" reponse...
-
Leonid, the reason I didn't personally use the water-usage request was because it only allowed retrieving the water usage grouped by hour and I wanted higher precision. In my use I periodically query the current usage returned by water-usage/summary and subtract the previous reading to get usage per minute and then store that in a local database.
I just tried the water-usage API and it seems to work for me as long as I supply the startTime parameter...e.g.:
curl https://api.streamlabswater.com/v1/locations/{location-id}/readings/water-usage?startTime=2018-10-13T17:03:52Z -H "Authorization: Bearer {api-key}"
Making sure to replace {location-id} and {api-key} with yours. That will return the hourly readings since the suppled startTime. Note that it returns 50 readings by default with a max of 100...so unless you bump the startTime so a limited number of readings are returned you'll have to use the page parameter to grab the remaining results.
-
Thanks Chris! I actually got it to work before I saw your post and my problem was that I was missing that Z at the end of the date-time string. It would be helpful if the API page actually had some examples because ISO 8601 can take different forms. I'm going to add my example to yours. This is the exact request that works for me:
plus the header:
Authorization: Bearer {apiKey}
Please sign in to leave a comment.
Comments
6 comments