magico13 / PyEmVue

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

Issue getting instant wattage #42

Open sbates130272 opened 1 year ago

sbates130272 commented 1 year ago

Hi

I love this project. I have had some success getting kWH from the API but I am having issues getting instant watts (like I can in the App). Can anyone paste in a snippet of code I can use to obtain this?

Stephen

magico13 commented 1 year ago

This should do it, you're probably just missing the scaling step. When you make a request for the 1 second timeframe with kWh as the unit, you get the kWh used over a second, ie kWh/s. Energy over Time is a Power measurement (eg Joules per Second is Watts) so you just need to convert kWh/s to Watts, which is just multiplying by 3600 s/h to get to kW and then by 1000 to get to Watts. You should find that lines up with what the app is doing internally.

from pyemvue.pyemvue import PyEmVue, Scale, Unit
import datetime

username='user@email.com'
password='password'

vue = PyEmVue()
vue.login(username, password)
print('Logged in.')
print()

device_gids = []
devices = vue.get_devices()
for device in devices:
    if not device.device_gid in device_gids: device_gids.append(device.device_gid)

now = datetime.datetime.now(datetime.timezone(datetime.timedelta(hours=-5))) # This is my timezone, yours may vary
usageDict = vue.get_device_list_usage(device_gids, now, Scale.SECOND.value, Unit.KWH.value)
for (gid, usageDevice) in usageDict.items():
    for (channelNum, usageChannel) in usageDevice.channels.items():
        # unit is kWh/s, multiply by 3600 seconds/hour to get kW, multiply by 1000 to get W
        # if the usage is ever None this will throw an exception so you should check for that and replace it with 0
        scaledUsage = 3600*1000*usageChannel.usage
        print(f'{gid} - {channelNum} - {usageChannel.name} - {scaledUsage}W')
sbates130272 commented 1 year ago

Thanks so much @magico13! Yes this makes sense and works for me. I knew it had to be do-able because the app does it.

nrgz28 commented 4 months ago

Hi! Is there anyway to integrate this to HomeAsisstant ? I'd really love to have second by second watts use from Emporia!

nrgz28 commented 4 months ago

@magico13 In case you didnt see it :)

magico13 commented 4 months ago

@nrgz28 There is a Home Assistant integration (also by me) here https://github.com/magico13/ha-emporia-vue. Unfortunately, per #19 seconds-level data isn't something that I can officially support, it ends up making way too many calls to their API. If you want that level of data, there is a way to reflash the Vue with ESPHome to enable local access and get sub-second readings.

For the brief period where second-level data was in the Home Assistant integration it was quite helpful for me to detect things like my garage door being activated, so I definitely understand how nice it would be. Unfortunately if you want that sort of info you're better off with a device that has local access.