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

Unable to get historical emeter data from P110M #746

Open
HughMacdonald opened this issue Feb 8, 2024 · 7 comments
Open

Unable to get historical emeter data from P110M #746

HughMacdonald opened this issue Feb 8, 2024 · 7 comments
Labels
bug Something isn't working

Comments

@HughMacdonald
Copy link

I'm not certain exactly where this issue is coming from, but I've got the Tapo P110M plugs.

This module seems to work pretty much as expected most of the time, but the one thing that fails is calling:

await device.get_emeter_monthly(year = 2024)

This fails with:

kasa.exceptions.SmartDeviceException: Error querying device: <IP ADDRESS>: UNKNOWN_METHOD_ERROR(-1002)

I traced through the code, and it eventually calls Usage.get_raw_monthstat() like this:

await device.modules["emeter"].call("get_monthstat", {"year": 2024})

I was wondering if the P110M has a very slightly different API for some of this to the P110, but I don't have a P110 to test that with.

I've tried figuring out what the actual method should be, but haven't managed to so far. I attempted to do this by running a proxy server on my PC, getting my phone to route through this proxy, and then running WireShark, but it appears to all be encrypted.

If anyone is able to give me some pointers as to how to decrypt this, if that is even possible, then I can dig further into this.

Thanks

@sdb9696
Copy link
Collaborator

sdb9696 commented Feb 8, 2024

What command are you using and which version of python-kasa?

AFAIK the TAPO devices don’t support the monthly stats.

@rytilahti rytilahti added the bug Something isn't working label Feb 8, 2024
@HughMacdonald
Copy link
Author

I'm doing this through the Python API - using 0.6.2.1 (downloaded this morning)

I can share the full script (it's only a handful of lines) in a bit if you'd like.

The reason I was hoping this would be available is because it's accessible through the Tapo app, so there must be some way of getting it

@sdb9696
Copy link
Collaborator

sdb9696 commented Feb 8, 2024

The TAPO app just has last month not historical months right? We will support that soon once we get a few structural things in.

@HughMacdonald
Copy link
Author

It gives the following data (which can also be requested to be emailed as Excel files). Some of the data in the email goes back further than the app shows

  • Energy usage
    • Monthly
      • Not sure how far it goes back, as I've only recently got my plugs - my data goes back to 2024/01
    • Daily
      • This currently goes back to 2023/12/01 for me
    • Hourly
      • This appears to go back a week, and then to the start of that day - mine currently goes back to the very start of 2024/02/02
  • Power
    • Hourly
      • This goes back about a week
    • 5-minutely
      • This goes back about 24 hours

@HughMacdonald
Copy link
Author

For my purposes, I'm probably just going to grab the data once a week by hand through the app, but at some point I'd like to automate this.

Thanks, and appreciate all of your work on this :)

@rytilahti
Copy link
Member

rytilahti commented Feb 8, 2024

The error is coming as it's trying to execute a command that's used by the kasa ("iot") devices so that's a bug (#718 is aimed to "fix" that, but looks like it doesn't currently raise exceptions when trying to access those methods..).

Adding support for energy meter stats is something that comes later at some point, but before that you may find some success deciphering the data using a raw command like this with different start&end timestamps and interval values:

kasa --host 192.168.xx command 'get_energy_data' '{"start_timestamp": 1706815511, "end_timestamp": 1707420311, "interval": 1440}'
{"get_energy_data": {"local_time": "2024-02-08 21:32:33", "data": [0, 5, 31, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
"start_timestamp": 1706815511, "end_timestamp": 1707420311, "interval": 1440}}

That 5 = 0,05kWh and 31 = 0,031kWh in the monthly view of the app.

Let us know about what you find. There is also another command, get_power_data, which may be interesting for you.

There is also yet another for get some quick stats, see https://github.com/python-kasa/python-kasa/blob/master/kasa/tests/fixtures/smart/P110(UK)_1.0_1.3.0.json#L331

@HughMacdonald
Copy link
Author

Ahh - that makes sense that the existing functions would be for Kasa rather than Tapo devices.

Based on your suggestion, I've changed my python code to this, which works perfectly

async def main():
  credentials = kasa.Credentials(username=username, password=password)
  dev = await kasa.Discover.discover_single(
    ip_address,
    credentials=credentials)
  await dev.update()

  end_time = datetime.datetime.now()
  start_time = end_time - datetime.timedelta(days=1)

  # print(await dev.get_emeter_monthly(year = 2024))
  print(await dev.modules["emeter"].call(
    "get_power_data",
    {
      "start_timestamp": int(start_time.timestamp()),
      "end_timestamp": int(end_time.timestamp()),
      "interval": 60,
    }))

Thanks for this - this will certainly get me what I'm after - I can get both energy and power data that way, even if it's not through a cleaner API

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants