Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get_all_usage_data for time period #51

Open
benbogart opened this issue Oct 30, 2021 · 1 comment
Open

get_all_usage_data for time period #51

benbogart opened this issue Oct 30, 2021 · 1 comment

Comments

@benbogart
Copy link
Contributor

I need the on/off times for a specific device to compare runtime from now to a year ago. Getting the runtimes now is easy with get_all_usage_data() which returns DeviceOn and DeviceOff events.

Are there any options to get the on/off times for a device from a year ago?

The get_all_usage_data method passes a payload to the api. Currently the payload is hardcoded as:
payload = {'n_items': 30}

Is there a parameter that can be passed to the api to get usage data as of a time? I tried modifying the payload to:
payload = {'n_items': 20, 'start': '2020-10-30T16:24:35.314Z'} with no luck.

It's worth noting that the n_items parameter doesn't do anything. When modified to 20 or 1000 the api still returns 30 results.

Any ideas on how to get historical on/off data by device would be appreciated.

@benbogart
Copy link
Contributor Author

I resolved this by making two changes to the code.

  1. Fixed a typo in the api_call method where the data param of the session get call should be params.
    def api_call(self, url, payload={}):
        try:
            
            r = self.s.get(API_URL + url,
                              headers=self.headers,
                              timeout=self.api_timeout,

                              # changed data to params
                              params=payload) # data=payload)
            
            print(r.url)
            
            return r.json()
        
        except ReadTimeout:
            raise SenseAPITimeoutException("API call timed out")   
  1. move the definition of payload in the get_all_usage_data method to the definition so other params can be passed in.
    def get_all_usage_data(self, payload = {'n_items': 30} ):

Now you can pass in prior_to_item with a date to paginate the results. Unfortunately for my use case, it looks like they only make the last 30 worth of on/off data available.

I will submit a pull request with the changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant