magico13 / PyEmVue

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

Usage / Help / Noob question about vue.get_chart_usage() #74

Open jacksnodgrass opened 3 months ago

jacksnodgrass commented 3 months ago

I don't know python.... I can add print() statements but that's about it. I've done PHP for 20 years... but python for a few hours.

I have ( soft of )

usage_over_time, start_time = vue.get_chart_usage(devices[0].channels[0], 
   datetime.datetime.now(datetime.timezone.utc)-datetime.timedelta(days=1), 
   datetime.datetime.now(datetime.timezone.utc), scale=Scale.HOUR.value, 
   unit=Unit.KWH.value);

working, but it returns: [0.7672705789926317, 1.8751684053876665, 0.2608834574564298, 0.23772852579858567, 0.13907248973713981, 0.1145523709511757, 0.30305081161790426, 0.590115064485338, 0.5058421620337169, 0.5687163838778602, 0.6837475850900014, 0.6059227556525336, 0.4320985894287957, 0.2334514087846544, 0.2070333262708452, 0.2283613010480669, 0.21064682039154903, 0.22187652250289921, 0.20606931520779925, 0.42583842778523767, 0.17964073262161678, 0.12094377203623455, 0.07348710065007209, 0.05630943610800637, 0.1442509578291575]

I don't have an EASY way to know which times those values are associated with? I can guess... or have the script guess... but if an array of kwh AND timestamp of some sort was returned, that seems like it would be easier to work with. Any way to get that done with the current code?

magico13 commented 3 months ago

You have the timestamp on the return, it's got both the array as the usage_over_time variable and the starting timestamp as the start_time variable. Index 0 in the array is at start_time and each of the others is at start_time + datetime.timedelta(hours=index)

That's how the API returns it, just an object with the start time and the array. PyEmVue just breaks that down into the separate start time and array for you.

{
    "firstUsageInstant": "2024-03-25T17:00:00Z",
    "usageList": [
        10.424385189073352,
        7.471311534423828,
        7.318404381035698,
        4.165096328756545,
        4.105301690809462,
        3.9123346340455862,
        3.976239875859063,
        0.9884760541195339,
        1.0746342893473306,
        1.4215298784891766,
        1.36026680776808,
        1.3698353627819486,
        1.0674718824132283,
        1.366136849475437,
        1.0834297193654379,
        1.3024822784042358,
        7.448987753164503,
        6.793357279518975,
        2.5673693082427977,
        2.6854740752156574,
        1.363816265258789,
        2.9800974993981253,
        1.4480037266286214
    ]
}

Doing some post processing on your code, I can get the following:

2024-03-25 17:00:00+00:00 10.424385189073352
2024-03-25 18:00:00+00:00 7.471311534423828
2024-03-25 19:00:00+00:00 7.318404381035698
2024-03-25 20:00:00+00:00 4.165096328756545
2024-03-25 21:00:00+00:00 4.105301690809462
2024-03-25 22:00:00+00:00 3.9123346340455862
2024-03-25 23:00:00+00:00 3.976239875859063
2024-03-26 00:00:00+00:00 0.9884760541195339
2024-03-26 01:00:00+00:00 1.0746342893473306
2024-03-26 02:00:00+00:00 1.4215298784891766
2024-03-26 03:00:00+00:00 1.36026680776808
2024-03-26 04:00:00+00:00 1.3698353627819486
2024-03-26 05:00:00+00:00 1.0674718824132283
2024-03-26 06:00:00+00:00 1.366136849475437
2024-03-26 07:00:00+00:00 1.0834297193654379
2024-03-26 08:00:00+00:00 1.3024822784042358
2024-03-26 09:00:00+00:00 7.448987753164503
2024-03-26 10:00:00+00:00 6.793357279518975
2024-03-26 11:00:00+00:00 2.5673693082427977
2024-03-26 12:00:00+00:00 2.6854740752156574
2024-03-26 13:00:00+00:00 1.363816265258789
2024-03-26 14:00:00+00:00 2.9800974993981253
2024-03-26 15:00:00+00:00 1.4480037266286214
2024-03-26 16:00:00+00:00 1.4620207326041328
2024-03-26 17:00:00+00:00 3.671089441184998

All I did was add the following after the call to print out the data with the timestamp. You could create an object for each with the timestamp and usage data in it, or use a dictionary, or just work off the index, whatever you prefer.

for index in range(len(usage_over_time)):
    timestamp = start_time + datetime.timedelta(hours=index)
    usage = usage_over_time[index]
    print(timestamp, usage)
DabblerIOT commented 3 months ago

I don't like this either, but the emporia api passes back only the time/data stamp of the first entry, then you have to assume that each entry is 1 interval earlier in time. Makes my skin crawl. I'm pretty sure the timestamp is passed back in the data from the function (python is weird, I'm with you there). Think there may be an example of getting the timestamp from the response somewhere in the home assistant project that uses this module.

Neither here nor there... have you tried home assistant recently? It has come a long, long way from where it was years ago. The emporiavue-ha and home assistant energy project together really give a nice solution. Read as -- alot of the work you may be doing, may already be done for you for free, and very nice. It's actually been worth a couple weeks of aggravation sorting out home assistant and how it works. (I started along a path similar to kind of things you are doing, but by hitting emporia api directly, using a no cors proxy server running local and javascript).

Home assistant project is here.. https://github.com/magico13/ha-emporia-vue

To see what data is coming back, so you can see what the input is this project is dealing with. https://github.com/magico13/PyEmVue/blob/master/api_docs.md

Cheers.

Some examples of ha built in stuff.. (selected march 1st to 25th)... you can make similar stuff for power, but you need to built that stuff on your own.. Attached one of mine I use to keep an eye on things as well.

Screenshot 2024-03-26 at 2 01 41 PM Screenshot 2024-03-26 at 2 01 22 PM Screenshot 2024-03-26 at 2 01 02 PM

User made power dashboard Screenshot 2024-03-26 at 2 02 18 PM