main.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # This script will find all devices in your installation and
  2. # output their per-minute usage for the previous DAYS_BACK days.
  3. # Per-minute data is stored for a maximum of 7 days.
  4. #
  5. # To change the timing, update the DAYS_BACK variable below.
  6. #
  7. # Reads credentials from `keys.json`
  8. import pyemvue
  9. import json
  10. from pyemvue.enums import Scale, Unit
  11. from pyemvue import PyEmVue
  12. from pprint import pprint
  13. from datetime import datetime, timezone, timedelta
  14. NOW = datetime.now(timezone.utc)
  15. DAYS_BACK = 7 # Number of days in the past to get per-minute data for.
  16. MAX_HOURS = 12 # The maxinum number of hours that can be retrieved at once.
  17. # Get & display minutely data for all devices.
  18. def allUsageOverTime(vue: PyEmVue):
  19. devices = vue.get_devices()
  20. print(f'time\tdevice_gid\tchannel_num\twH\twatts\tmultiplier\tname')
  21. for device in devices:
  22. # print('=== Device ===')
  23. # pprint(vars(device))
  24. for channel in device.channels:
  25. # pprint(vars(channel))
  26. if not channel.name is None:
  27. channelUsageOverTime(vue, channel)
  28. def channelUsageOverTime(vue: PyEmVue, c):
  29. start_time = NOW - timedelta(days=DAYS_BACK)
  30. end_time = start_time + timedelta(hours=MAX_HOURS)
  31. while start_time < NOW:
  32. usage_over_time, start = vue.get_chart_usage(
  33. c,
  34. start_time, end_time,
  35. scale=Scale.MINUTE.value,
  36. unit=Unit.KWH.value
  37. )
  38. time = start
  39. for kwh in usage_over_time:
  40. if kwh is None:
  41. continue
  42. wattHours = kwh * 1000
  43. watts = wattHours * 60
  44. print(f'{time}\t{c.device_gid}\t{c.channel_num}\t{wattHours}\t{watts}\t{c.channel_multiplier}\t{c.name}')
  45. time += timedelta(minutes=1)
  46. # Jump to the next start time.
  47. start_time = end_time
  48. end_time += timedelta(hours=MAX_HOURS)
  49. if __name__ == '__main__':
  50. with open('keys.json') as f:
  51. keys = json.load(f)
  52. vue = pyemvue.PyEmVue()
  53. if 'id_token' in keys:
  54. # Login using access tokens.
  55. vue.login(
  56. id_token=keys['id_token'],
  57. access_token=keys['access_token'],
  58. refresh_token=keys['refresh_token'],
  59. token_storage_file='keys.json'
  60. )
  61. else:
  62. # Login with username & password, and update keys.json.
  63. vue.login(
  64. username=keys['username'],
  65. password=keys['password'],
  66. token_storage_file='keys.json'
  67. )
  68. allUsageOverTime(vue)