yo-han / Home-Assistant-Carelink

Unofficial Home Assistant Carelink Component
MIT License
19 stars 8 forks source link

Download data history? #60

Closed Trenar closed 3 months ago

Trenar commented 3 months ago

Hi, I just started using this custom component and recognized, that it doesn't fetch data "of the past". in particular, I had some time today where my phone (that uploads the pump data to the medtronic servers) wasn't connected to the internet. That resulted in gaps in this components sensor (see screenshot). Screenshot_20240130-222507

Would it be possible to check for gaps and download the glucose history from carelink?

Although I'm not familiar with code for web applications, I know some python.. Could try helping, if someone can give a starting point.

Best, trenar

sedy89 commented 3 months ago

The problem is that the "last glucose level" sensor is not a summary of values, it is just one single value. It only shows the latest measured BG provided by the pump, if you are able to receive it within 5 minutes while it actually is the latest BG and was not yet replaced by the next one. Home Assistant will store the time and the value of every sensor change for a couple of days into it's internal database. The graph card you are using to visualize the sensor is just showing the Home Assistant's internal history of the values that sensor had. To fill the gaps you would need to modify the database of Home Assistant. I guess this can't be fixed.

Trenar commented 3 months ago

Okay, I understand this limitation of the current sensor.

Is it however possible to create a new sensor (or whatever entity in Home Assistant) that stores the "last 2 hours" of glucose values? I know of other projects (e.g. mmconnect-to-nightscout ) that can retrieve more data than just the last SGV.

I'm asking bc I plan to create a Garmin Watchface that retrieves the SGV data from Home Assistant and I'd like to see all the past data, even if I was offline for some time.

Thanks for your time :)

sedy89 commented 3 months ago

I have written an nightscout uploader for this carelink HA integration that I am currently testing. It reports most of the treatments and it also back fills the data in case of lost connection.

I am running a nightscout docker instance in my home assistant and made it visible from the internet via ngnix and duckdns. Maybe that would be something for you ... So far it is working great. With the nightscout server you could use as many instances of e.g xdrip as you wish or you can use the nightscout api for sensors in HA or your own app.

Trenar commented 3 months ago

Interesting approach. However, I'm running a raw HA container, not a supervised version. Hence, I can't install your nightspot add-on. Further, I'm trying to minimize the services it needs to get the CGM data. I was hoping to only use the HA add-on to get what I need, without having another instance of nightscout running for only one purpose. Actually, I was running a nightscout instance before Medtronic introduced the captcha auth, and try getting a good solution since. Right now, I'm running a custom build of xDrip+ on my phone, but that has some downsides like battery usage and delays..

How does the additional add-on fills the data gaps? Could this code somehow be reused for this integration?

sedy89 commented 3 months ago

How does the additional add-on fills the data gaps? Could this code somehow be reused for this integration?

The nightscout uploader will take the BG readings, the Carelink integration fetched from the carelink servers, and put them into a proper format to finally send them to nightscout in one request. Similar thing is happening to bolus, basal, treatment, alarm and alert data. Nightscout server takes care to add them (or the missing ones). That's how it will fill the gaps even if you had no connection for a couple of mintues/hours.

Without adding a database to this HA integration, it don't think you will be able to get the data you want. Someone else might know it better...

I was running a nightscout instance before Medtronic introduced the captcha auth, and try getting a good solution since.

Maybe the easiest way is to start using NS again, you won't need the captcha or any nightscout plugin to get the data. You only need this carelink integration in HA.

Trenar commented 3 months ago

Can the nightscout uploader be installed in a non-supervised HA instance? Or can the carelink integration itself be extended somehow?

sedy89 commented 3 months ago

Can the nightscout uploader be installed in a non-supervised HA instance? Or can the carelink integration itself be extended somehow?

It is an extension of the current carelink integration. On the integration page, there will be two more optional parameters: the nightscout url and the api secret.

If my changes won't get merged at the end, you can still change the source files in your home assistant file directory and copy the changes over manually. But let's see, this should only be the worst case.

Trenar commented 3 months ago

Hi @sedy89 thanks for your good advice. Your ns-uploader extension works like a charm. I feared longer delays in pushing the data from one web service to another, but it all happens within a reasonable time.

Cheers