mihai-dinculescu / tapo

Unofficial Tapo API Client. Works with TP-Link Tapo smart devices. Tested with light bulbs (L510, L520, L530, L610, L630), light strips (L900, L920, L930), plugs (P100, P105, P110, P115, P300), hubs (H100), switches (S200B) and sensors (KE100, T100, T110, T300, T310, T315).
MIT License
313 stars 30 forks source link

start_timestamp and export timestamp #230

Closed syco closed 1 week ago

syco commented 2 weeks ago

Hi, I'm trying to figure out how to match the start_timestamp with the timestamp in the exported excel.

Here's my code:

async def main():
  client = ApiClient(username, password)
  device = await client.p110(ip_address)

  start_time = (datetime.datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) - datetime.timedelta(days=2))
  print(start_time.timestamp())
  print(start_time.strftime('%Y-%m-%d %H:%M:%S'))

  usage = await device.get_energy_data(EnergyDataInterval.Hourly, start_time)
  print(usage.to_dict())

  _date = datetime.datetime.fromtimestamp(usage.start_timestamp)

  for i in range(len(usage.data)):
    usage_kwh = usage.data[i] / 1000
    print(_date, usage_kwh)
    _date += datetime.timedelta(hours=1)

asyncio.run(main())

the output of that script is:

1718406000.0
2024-06-15 00:00:00
{'data': [41, 47, 59, 55, 53, 62, 42, 42, 41, 42, 42, 41, 45, 42, 41, 42, 46, 50, 44, 44, 53, 54, 54, 48], 'end_timestamp': 1718495999, 'interval': 60, 'local_time': '2024-06-17T10:32:45', 'start_timestamp': 1718409600}
2024-06-15 01:00:00 0.041
2024-06-15 02:00:00 0.047
2024-06-15 03:00:00 0.059
2024-06-15 04:00:00 0.055
2024-06-15 05:00:00 0.053
2024-06-15 06:00:00 0.062
2024-06-15 07:00:00 0.042
2024-06-15 08:00:00 0.042
2024-06-15 09:00:00 0.041
2024-06-15 10:00:00 0.042
2024-06-15 11:00:00 0.042
2024-06-15 12:00:00 0.041
2024-06-15 13:00:00 0.045
2024-06-15 14:00:00 0.042
2024-06-15 15:00:00 0.041
2024-06-15 16:00:00 0.042
2024-06-15 17:00:00 0.046
2024-06-15 18:00:00 0.05
2024-06-15 19:00:00 0.044
2024-06-15 20:00:00 0.044
2024-06-15 21:00:00 0.053
2024-06-15 22:00:00 0.054
2024-06-15 23:00:00 0.054
2024-06-16 00:00:00 0.048

See that The start_time I request and the start_timestamp returned are off by 1 hour, and when I compare all this to the exported excel I get from the app, all timestamps are off by 1 hour as well:

2024/06/15 00:00:00 | 0.041
2024/06/15 01:00:00 | 0.047
2024/06/15 02:00:00 | 0.059
2024/06/15 03:00:00 | 0.055
2024/06/15 04:00:00 | 0.053
2024/06/15 05:00:00 | 0.062
2024/06/15 06:00:00 | 0.042
2024/06/15 07:00:00 | 0.042
2024/06/15 08:00:00 | 0.041
2024/06/15 09:00:00 | 0.042
2024/06/15 10:00:00 | 0.042
2024/06/15 11:00:00 | 0.041
2024/06/15 12:00:00 | 0.045
2024/06/15 13:00:00 | 0.042
2024/06/15 14:00:00 | 0.041
2024/06/15 15:00:00 | 0.042
2024/06/15 16:00:00 | 0.046
2024/06/15 17:00:00 | 0.05
2024/06/15 18:00:00 | 0.044
2024/06/15 19:00:00 | 0.044
2024/06/15 20:00:00 | 0.053
2024/06/15 21:00:00 | 0.054
2024/06/15 22:00:00 | 0.054
2024/06/15 23:00:00 | 0.048

Am I doing something wrong in my script? Thanks

mihai-dinculescu commented 2 weeks ago

What timezone are you on?

I think that you might want

_date = datetime.datetime.fromtimestamp(usage.start_timestamp, tz.gettz())
syco commented 2 weeks ago

Yes, I'm in GMT+1, but so is the excel file. So if I change usage.start_timestamp from GTM+1 to GMT to compare it with the date in the excel file I would be comparing GMT with GMT+1. So is usage.start_timestamp double moved forward?

mihai-dinculescu commented 2 weeks ago

I've done a bit more digging, and you're correct. The request is made with the date in UTC when it should be in the local timezone.

This doesn't affect the data returned because the Tapo API will always return data from the start of the day in the local time. But it does mean that the returned start_timestamp is indeed incorrect.

The problem is here. It should be local time, not UTC.

syco commented 2 weeks ago

Well, I'm happy enough that the data returned is correct. I leave it to you to decide if you want to change start_timestamp or not. Thanks for looking into it. (Should we close this now that we know what it is? Please do if you want.)

mihai-dinculescu commented 2 weeks ago

I have a fix in progress. It should land soon.