magico13 / PyEmVue

Python Library for the Emporia Vue Energy Monitor
MIT License
185 stars 36 forks source link

Need to run twice to get output #14

Closed ifuchs closed 1 year ago

ifuchs commented 3 years ago

I am running this script: `from pyemvue.enums import Scale, Unit vue = pyemvue.PyEmVue() vue.login(username='', password='', token_storage_file='keys.json')

devices = vue.get_devices() deviceGids = [] for device in devices: deviceGids.append(device.device_gid)

channel_usage_list = vue.get_devices_usage(deviceGids, None, scale=Scale.SECOND.value,unit=Unit.KWH.value) for channel in channel_usage_list: print("%.3f" % (channel.usage*3600), 'kwh')`

Most of the time, the first time I run it (after not running it for say an hour) I get no output. The second, and subsequent executions result in the expected output. Is this a problem with the server?

magico13 commented 3 years ago

If you're not getting an exception then it's not the server returning an error, though the server could be returning no data. Since you say it's after an hour or so then it's possible that it's related to the authentication token expiring, though you should be getting a new one each time you run it if that's the exact script.

I'll have to do some testing. This might be related to an issue someone is seeing with the home assistant plugin where they are randomly not getting any data back from the same call.

ifuchs commented 3 years ago

Other than the password and userid , that is the precise script. I just tried it again and got the same result (no data followed by data).

On Apr 18, 2021, at 1:02 PM, Michael Marvin @.***> wrote:

If you're not getting an exception then it's not the server returning an error, though the server could be returning no data. Since you say it's after an hour or so then it's possible that it's related to the authentication token expiring, though you should be getting a new one each time you run it if that's the exact script.

I'll have to do some testing. This might be related to an issue someone is seeing with the home assistant plugin where they are randomly not getting any data back from the same call.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/magico13/PyEmVue/issues/14#issuecomment-822024269, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABLDPSDMEHQQ7F7ALFRG6ZLTJMGD7ANCNFSM43EHE4LA.

magico13 commented 3 years ago

I'm trying to see if I can replicate this but not having luck, unfortunately it's working fine for me. A few things to try:

  1. It looks like you want the usage over an hour but are getting the usage for the last second, so change the scale to Scale.HOUR.value and leave the rest of the script the same and it should report for usage over the last hour rather than the last second. Sorry, this is wrong, you were correct if you're trying to get the instant power usage, though note that it'll be kw and not kwh after the multiplication. I forgot that the API always returns kwh used within the scale period.
  2. Pass an explicit time rather than None. None should result in it doing datetime.datetime.utcnow() but you could specify a time yourself. Note that right now it's gotta be a timezone unaware, UTC based time. You might want to just try requesting a time a second or two before now.
  3. Add a wait for a second or two after the login before requesting the data. Or call it once and if you get nothing back call it again within the same script.

What timezone are you in? I know there's an issue open for BST in the home assistant plugin which I suspect is just due to them going from lining up with UTC to being off by one.

ifuchs commented 3 years ago

I am asking for the Sec value and multiplying to get the current utilization in KWH. The hr value you suggest is actually a quarter hour value (it is computed 4 times/ hr.) The wait after login or multiple calls may work.

On Apr 22, 2021, at 9:20 PM, Michael Marvin @.***> wrote:

I'm trying to see if I can replicate this but not having luck, unfortunately it's working fine for me. A few things to try:

It looks like you want the usage over an hour but are getting the usage for the last second, so change the scale to Scale.HOUR.value and leave the rest of the script the same and it should report for usage over the last hour rather than the last second. Pass an explicit time rather than None. None should result in it doing datetime.datetime.utcnow() but you could specify a time yourself. Note that right now it's gotta be a timezone unaware, UTC based time. Add a wait for a second or two after the login before requesting the data. Or call it once and if you get nothing back call it again within the same script. What timezone are you in? I know there's an issue open for BST in the home assistant plugin which I suspect is just due to them going from lining up with UTC to being off by one.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/magico13/PyEmVue/issues/14#issuecomment-825316249, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABLDPSGKGZGUXJGFUG3PGGLTKDDOHANCNFSM43EHE4LA.

magico13 commented 1 year ago

Cleaning up old issues so I'm closing this one out.