MattJeanes / TeslaMateAgile

Integration to automatically fill in prices for charge data captured by TeslaMate for smart energy providers
MIT License
82 stars 10 forks source link

Need help in linking with HA #63

Open stauffenberg2020 opened 1 week ago

stauffenberg2020 commented 1 week ago

Hi All,

I am trying to get prices through hass-monta and I only have a "Monta - Personal Wallet" sensor/entity. The output from that monta API call is like some YAML format as mentioned in https://github.com/nickknissen/hass-monta/issues/65#issuecomment-2115166644

I am trying to link these prices to TeslaMateAgile and I am wondering how it was done previously.

I also see that @tougher has been working on a fork to support the data from Monta into TeslaMateAgile - But I am not sure if this has been completed.

Can @MattJeanes or @tougher or @goncal you help me a bit here?

Thanks in advance

MattJeanes commented 1 week ago

Hey, I don't know much about Monta or hass-monta but it sounds like what you probably need to do is create a Home Assistant template entity that references the appropriate field from the big API response entity, it should be possible to do. TeslaMateAgile requires a number / input_number type entity to work from so if you can extract it out into one of those it should work.

stauffenberg2020 commented 1 week ago

TeslaMateAgile requires a number / input_number type entity to work from

Can you help me understand bit more on this?

I see in Teslamate that each of the charges are with a id, say ...,150, 151, 152, etc. Does TeslaMateAgile queries with this id and expects a price from HA?

MattJeanes commented 1 week ago

Okay so pretty much what it's looking for is an entity (with recording turned on so it can see historically too, it's on by default unless you turn it off in HA configuration.yaml btw) that captures the current price of electricity. The Home Assistant history is then queried with the times of your charging session to get the price of electricity at those times and then calculates based on the energy being put into the car how much it cost.

For example, here's what my one looks like that I have set up based off the Octopus Intelligent integration:

image

Here's some documentation on template sensors which may help: https://www.home-assistant.io/integrations/template/

stauffenberg2020 commented 1 week ago

Okay I am surprised to see how it fetches the price or calculates the consumption/price. I don't have any such time series available for price/kwh.

I want to feed in the final receipt value irrespective of how long or how many kWh charged (from Monta which includes all sorts of losses in the cable, etc) into TeslaMateAgile through HA.

Can this be done somehow?

MattJeanes commented 1 week ago

Ah I see, TeslaMateAgile isn't really designed to work like that as all the various price providers do is give point in time data for the electricity price as the purpose of it is to calculate the charge cost from that data.

If you already have the charge cost then you'll probably need something else to do that, as all that ultimately needs to happen is edit the cost column in the charging_processes table in the database with the cost number. It could potentially be implemented in a code change to TeslaMateAgile to allow a provider to supply the entire cost I suppose.

How might you correlate the charge in TeslaMate with Monta, is there a reliable method of doing that? We could try and guess using the time or something but might not be super reliable.

stauffenberg2020 commented 1 week ago

It could potentially be implemented in a code change to TeslaMateAgile to allow a provider to supply the entire cost I suppose.

I would very much appreciate this.

How might you correlate the charge in TeslaMate with Monta, is there a reliable method of doing that? We could try and guess using the time or something but might not be super reliable.

I guess this is the major issue, I dont know if there is any reliable method, May be @tougher has more info on this as he was trying to link Monta with TeslaMateAgile.

stauffenberg2020 commented 1 week ago

On the same lines, Is it not possible to fetch the Tesla Supercharging prices directly into the cost column in the charging_processes. Then we can probably use that logic for Monta as well?

MattJeanes commented 1 week ago

Yeah that's the main concern really, depends how close the times are from the Monta data to the TeslaMate tracked data for the same charge. If it's close enough we could probably correlate them automatically, and just ignore ones we cannot find a good match for. Looking at that output example you gave probably could yeah.

Supercharging would be the same, but we'd need to access the Tesla charging history which is a little more complicated now since Tesla locked down their APIs and require people to register applications. It's doable though, if you already have e.g. Tesla connected via Fleet API in Home Assistant then you could use that same thing to get charging history. More info here https://developer.tesla.com/docs/fleet-api/getting-started/what-is-fleet-api

It's certainly an interesting feature request and supercharging especially I'm sure people would love to see!

Since I don't have Monta, would you mind exporting an example response from yours and then also export a couple of records from the charging_process table to me? Ideally as a CSV but anything will do, and I'll see what I can do with it!

stauffenberg2020 commented 1 week ago

Since I don't have Monta, would you mind exporting an example response from yours and then also export a couple of records from the charging_process table to me? Ideally as a CSV but anything will do, and I'll see what I can do with it!

Ofcourse @MattJeanes. I have attached an example fetch from the HA Monta add on here monta_personal_wallet_attributes.txt

It looks like raw text with some sort of structure around it.

You can see how it looks in my Monta charging app below. For every charging, there are two entries into the personal wallet data and I think only the last few can be fetched as per Monta API (I guess this is more than enough) IMG_4907

If you need, I can even share my client id and client secret in a private message so that you can do more testing by actually installing th Monta add on in HA and test it or, Just directly code that add on into TeslaMateAgile - Its an easy 'curl' integration I guess.

I have tested it myself using retrieve a list of wallet transactions

MattJeanes commented 1 week ago

That's great thank you, if you could also export some data from the TeslaMate charging_process table (there's a guide on fixing data in the readme for how to access the DB) that matches up with that data from Monta I'll see how easy it'll be to implement.

I don't need the client id/secret for now but I'll let you know if that changes. I'm hoping they have some kind of test or demo token I can use in the mean time but I haven't really looked at the API yet.

I'll implement it directly with calls to Monta, going via Home Assistant would actually probably make it harder to do.

Also the text file you sent is written in the YAML format for your reference 🙂

stauffenberg2020 commented 1 week ago

That's great thank you, if you could also export some data from the TeslaMate charging_process table (there's a guide on fixing data in the readme for how to access the DB) that matches up with that data from Monta I'll see how easy it'll be to implement.

I used pgadm4 and extracted the last couple of charges. The 5 charge info that I attached in the previous txt/YAML format (from Monta personal wallet pull) correspond to the charges with geofence_id 2 in this teslamate data-1725723674620.csv

Hope thats what you are expecting. Please write to me should you need any additional info/details

I'll implement it directly with calls to Monta, going via Home Assistant would actually probably make it harder to do.

I would love this as it is like a one stop solution rather than routing through another(HA) interface

MattJeanes commented 1 week ago

Fantastic, that's perfect thank you. I'll let you know when I have something, but just to set expectations this is very much a spare time project - and spare time is something of a scarce resource to me 🙂

That said I do hope to look at this soon, just can't promise anything right now!