nordicopen / easee_hass

Custom component for Easee EV charger integration with Home Assistant
197 stars 33 forks source link

FR: Users and consumption per user #158

Open rasssta opened 2 years ago

rasssta commented 2 years ago

For sites with multiple users it would be nice to have consumption per user and month.

Users for each site can be fetched via https://api.easee.cloud/api/sites/{siteId}/users (https://developer.easee.cloud/reference/get_api-sites-siteid-users)

Monthly consumption for users can be fetched via https://api.easee.cloud/api/sites/{siteId}/users/{userId}/monthly (https://developer.easee.cloud/reference/get_api-sites-siteid-users-userid-monthly).

Not really sure how the rest of the implementation would be set up. Perhaps just as easy as an entity per user (like sensor.easee_username) with current monthly consumption as the state?

Don't consider this a high priority request, just want to check if it would be easy to implement :-)

olalid commented 2 years ago

Well, it seems like an interesting idea. I do not personally have access to a site with more than one user and it may be difficult to implement without being able to test though...

olalid commented 2 years ago

I tried calling the APIs you suggested, for the first one I get a user list, consisting of only myself of course. With the second API I get nothing, no error and just an empty array. I assume this is because I am not using any authentication in my setup, so there is no connection between charging session and user. So I can not really see what this API would return in an installation where it actually provides useful information...

rasssta commented 2 years ago

Example data from my installation:

Users: {"isOwner":true,"siteUsers":[{"userId":<id1>,"email":"<email>","name":"<name>","phoneNumber":"<num>","userRole":1},{"userId":<id2>,"email":"<email>","name":"<name>","phoneNumber":"<num>","userRole":2}]}

Consumption for user 1: [{"year":2021,"month":7,"totalEnergyUsage":68.873278},{"year":2021,"month":8,"totalEnergyUsage":79.796578},{"year":2021,"month":9,"totalEnergyUsage":33.639319},{"year":2021,"month":10,"totalEnergyUsage":17.445987},{"year":2022,"month":1,"totalEnergyUsage":24.233064}]

For the yearly command the result is: [{"year":2021,"totalEnergyUsage":199.755162},{"year":2022,"totalEnergyUsage":24.233064}]

olalid commented 2 years ago

So seems like it is similar to the https://api.easee.cloud/api/sessions/charger/{chargerId}/monthly API. And maybe identical to https://api.easee.cloud/api/sessions/user/{id}/monthly?

rasssta commented 2 years ago

Yeah, almost. My data for those cmds:

https://api.easee.cloud/api/sessions/charger/{chargerId}/monthly

[{"year":2020,"month":11,"totalEnergyUsage":0.210707,"totalCost":0.0,"currencyId":"NOK"},{"year":2020,"month":12,"totalEnergyUsage":0.185231,"totalCost":0.0,"currencyId":"NOK"},{"year":2021,"month":2,"totalEnergyUsage":13.810430000000002,"totalCost":15.8819945,"currencyId":"SEK"},{"year":2021,"month":5,"totalEnergyUsage":39.567758,"totalCost":49.4596975,"currencyId":"SEK"},{"year":2021,"month":6,"totalEnergyUsage":51.836552,"totalCost":64.79569,"currencyId":"SEK"},{"year":2021,"month":7,"totalEnergyUsage":125.592364,"totalCost":156.990455,"currencyId":"SEK"},{"year":2021,"month":8,"totalEnergyUsage":120.28828300000002,"totalCost":150.36035375,"currencyId":"SEK"},{"year":2021,"month":9,"totalEnergyUsage":47.52681,"totalCost":59.4085125,"currencyId":"SEK"},{"year":2021,"month":10,"totalEnergyUsage":17.445987,"totalCost":27.442537551,"currencyId":"SEK"},{"year":2022,"month":1,"totalEnergyUsage":24.233064,"totalCost":38.118609672,"currencyId":"SEK"}]

I think I, by mistake, had it set to NOK instead of SEK in the beginning hence the crap data above.

https://api.easee.cloud/api/sessions/user/{id}/monthly

[{"year":2021,"month":7,"totalEnergyUsage":68.873278,"totalCost":86.0915975,"currencyId":"SEK"},{"year":2021,"month":8,"totalEnergyUsage":79.796578,"totalCost":99.7457225,"currencyId":"SEK"},{"year":2021,"month":9,"totalEnergyUsage":33.639319,"totalCost":42.04914875,"currencyId":"SEK"},{"year":2021,"month":10,"totalEnergyUsage":17.445987,"totalCost":27.442537551,"currencyId":"SEK"},{"year":2022,"month":1,"totalEnergyUsage":24.233064,"totalCost":38.118609672,"currencyId":"SEK"}]

olalid commented 2 years ago

Yes, totalEnergyUsage seems to be the same, but there are some extra fields with cost and currency.

RobinDevogele commented 1 year ago

That would interest me as well :-) do you think it would be easy to implement ?

olalid commented 1 year ago

Well, it is not rocket science, it is just more of similar things we have already implemented. Question is how to implement it so that it is useful for as many users as possible. Also there is one general problem with the whole concept: to get this data we must poll it from the servers since it is not part of the data that is pushed out. And polling puts extra stress on the cloud servers. So we would have to be careful to a) not poll to often and b) not do it at the same time from all clients. Feel free to tell us what data you would like to have available and how it should be presented. @rasssta wrote that one sensor per user showing the current month could perhaps be enough, is that what we are aiming for?

RobinDevogele commented 1 year ago

Well, actually I would need a much more granular information. Depending on the solar production, the tariff is different every 15 minutes windows. So my ideal would be an total incrementing counter per user (I have only two users, so I would not mind to configure each user sensor individually), refreshed at least every 15 minutes (xx:00 - xx:15 - xx:30 - xx:45), but a smaller refresh period would be even better. Of course, we could limit the fetch only when a car is connected (/charging)

Another possibility would be to have a kind of "state" of the user currently connected (via observations ? https://developer.easee.cloud/docs/observation-ids ) , then I could easily create a template sensor using the lifetime energy. But that leads to other potential issue (what if HA is down... we'll lose the history of who was connected and used that amount energy)

RobinDevogele commented 1 year ago

This request: GET /api/sessions/user/xxxxx/total/1970-04-22T06:00:00Z/2060-04-22T06:00:00Z return a float number with the total amount of energy for that user. Example: 50.355061

olalid commented 1 year ago

Sorry, I am not following your logic here, how are energy per user related to tariffs and solar production? What is it you want to accomplish?

RobinDevogele commented 1 year ago

Basically, my easee charger is shared amongst two users. The electricity tariff depends on the solar production (if solar production covers 100% of the consumption, then it is "free", otherwise if the electricity is taken from the grid, then we need to pay for it). The electricity provider is computing every 15 minutes if we have produced as much electricity as we have consumed. It's the reason I would like to know the amount of energy used by user per 15minutes window so I can compute the real cost.

Is it clearer ?

olalid commented 1 year ago

Well, I think I understand, but not sure if that is possible to accomplish what you want to do. Because I do not think this is real-time data, there is a certain lag on sensors from when it changes locally until they are transferred over to the cloud and then back again to HA, and this is different dependant on some priority that Easee has set for different type of data. Energy consumption is usually low priority and does not update all the time. Some energy meters only update when a session completes, or they are frozen at some incorrect value until the car is unplugged. The delay from reading to new value displaying is intentionally random, so when a new value is received it may be x amount of minutes old, we just do not know. I have not investigated specifically for this per user energy data, but look at lifetime energy and session energy and you will see that it does not have the granularity you would need to use them for billing on 15 minute intervals.

If you want data that is more real-time I think your best bet is to use the charging power meter and integrate that over time to get a simulated energy meter. The power meter updates quite frequently and does not have a huge delay.

Of course you would need to have information about which user is charging to do anything useful with it, but that I guess could be accomplished more easily. I will investigate that...

RobinDevogele commented 1 year ago

Yes you're right, there is a huge delay in the user energy data. It looks like the session energy is the one the most frequently updated (like every 20 minutes, which would still be ok, close enough). If we have the possibility know which user is currently connected, then with the session energy it could work !

olalid commented 1 year ago

In 0.9.46 there are now attributes on the status sensor that are related to user information. Please give som feedback on how that works, since I am not able to test this in my own setup.

rasssta commented 1 year ago

Hi,

sorry for the late reply, my Easee isn't located at my permanent residence so takes some "calendar time" to test stuff this time of the year :-)

Unfortunately I don't get any user information in HASS.

config_authorizationRequired: true
state_pairedUserIDToken: 
state_userIDToken: 
state_userIDTokenReversed: 

I have authorized with my RFID tag and the box LED turns green and start to supply power to the car, but HASS don't update the above attributes.

I tried to browse through https://developer.easee.cloud/reference/ to figure out which command you're using to fetch the user information but I didn't find anything of interest. Could you enlighten me, please? It would make it easier to debug.

olalid commented 1 year ago

Hmm, ok. That probably means that this information is not available then. The functions we are using for this are kind of undocumented since we are using signalR to have the servers push out changes to us rather than polling an API function. But you can try https://developer.easee.cloud/reference/get_api-chargers-id-state, it should provide similar information.

rasssta commented 1 year ago

Nah, no user info there :(

olalid commented 1 year ago

Well ok. Then that plan failed I guess. Another approach could be to set the price with the service easee.set_charging_cost every 15 minutes. The cloud should save the total cost for each charing session so it can be retrieved later. Of course I assume you will not really know until after the 15 minute period if the price was correct, so this would not be completely reliable, over time the error would probably be small though...

rasssta commented 1 year ago

Pricing (in Sweden at least) only changes by the hour (tops), so letting the user perform a set price hourly would be enough if they have hourly pricing I guess, otherwise just set a fixed price in the Easee app. The hourly stuff could be done via a nordpool sensor or similar.

For my use case it would be enough if there were an hourly fetch of the sessions for each user connected to each charger. Like a sensor with state of the daily usage, and attributes for weekly/monthly/yearly usage in kWh and [currency].

rasssta commented 1 year ago

Like...

sensor.<easee>_user_<id>     2.2
Attributes:
- userId: <num>
- userEmail <email>
- dailyKWh: 2.2 kWh
- dailyCost X SEK
- weeklyKWh Y kWh
- weeklyCost Z SEK
unit_of_measurement: kWh
olalid commented 1 year ago

Yes, sorry I was thinking about RobinDevogeles case above where it depends on solar production what the price is going to be for each 15 minute period.

olalid commented 1 year ago

As I said above setting the price is already supported, where the price information comes from is not in our scope I think.

Regarding creating a sensor we are experimenting with this a bit, but so far the results are a bit puzzling. First of all you need to undersstand that a session needs to be completed before it is at all included in the consumption numbers. But even then, I can not really grasp what controls when they are included. Or there is just some bugs in the API, because the results are incosistent with how it should be I think...

rasssta commented 1 year ago

Yes, you seem to be correct that it doesn't include the stats until the session is completed (as in, car disconnected).

So daily usage might be something to give up on. That might also be the reason why Easee only has API commands for weekly/monthly/yearly and not daily.

olalid commented 1 year ago

What I am seeing is that a session may or may not be included depending on how I specify the cut of time/dates, even though the session falls within that range. So if I ask for consumption between previous midnight and current time, it may or may not include a session that ended during that period depending on what time of day it currently is... I.e. a session that ended at 04:00 could be included if I ask for consumption between 00:00 and 14:00, but not if I ask for comsumption between 00:00 and 20:00...

nitrox-github commented 2 weeks ago

My, maybe strange, usecase would be to add my friends, parents, colleagues or even my neighbour as a user to my easee wallbox. Whenever they visit and want to charge, have a mail generated with kWh charged for that session and a paypal.me link with price (kWh * price/kWh) added. I´m living in europe, so i can´t give it away for free :-/

That would require me to have some sort of user-id or RFID for that session.

Kinda CPO for the poor :-P


Hi <user>!

You started your sesssion at <START-TIME> and charged <usage> kWh til <END-TIME>.

Please pay via https://paypal.me/<me>/<usage * price/kWh>.

Thanks for your visit!
olalid commented 2 weeks ago

Hi @nitrox-github, to me that sounds like it would a separate script that calls the Easee API that you run every week or month rather than something that would be part of HA? I am not even sure how that would fit in to the model of what HA usually does.

nitrox-github commented 2 weeks ago

You are probably right.

Pity that no user-id or RFID token info is available, start, end and usage i already have. A sensor for the last session would be so cool, the mail-stuff is ofc. none of this plugins business and needs to be figured out by myself.