phautamaki / TheQuantifyingStack

Collection of shortcuts and scripts for self quantification purposes.
MIT License
28 stars 0 forks source link

HRV Measurement Times Incorrect #1

Closed Houston-Synex closed 2 years ago

Houston-Synex commented 2 years ago

Hi @phautamaki [phautamaki] This shortcut is really exciting, thank you for putting it together.

After following all instructions (downloading by iCloud link, getting into shortcuts, inserting Oura PAT...) The shortcut successfully imports OURA HRV readings to Apple Health (verified by visual check of health data)

However, the time on the 'Date' field is incorrect.

For example: Data imported for night of June 5, all readings are imported as 'Jun 5, 2022 at 12:00:00 PM) See screenshot attached IMG_0163

Using : Oura Ring Gen 3 Shortcut v2 (download from iCloud link this week) iOS 15.5 on iPhone 13 Pro

phautamaki commented 2 years ago

Thanks for reporting this. This was an issue also with the previous version of the script that I thought might have been fixed because of improved timezone handling. It’s not happening in Finland and it’s a bit difficult to debug but I’ll see if I find a way to fix it next week.

Houston-Synex commented 2 years ago

Full disclosure, I'm non-technical, BUT. If it helps on your debugging journey, here are a few things from this morning.

1) I added an action to the shortcut to display the 'Adjusted Date' after each Health Sample is logged (see image IMG_0171) 2) Running the shortcut today, it looks like (see image IMG_0167-0170) a) The time is being set to midnight (even though sleep started before midnight) (first reading is IMG_0167) b) it is successfully being logged with 5minute increments (see images IMG0168-0170) 3) Checking Healthkit after the running the shortcut, it is still showing them as 'logged' at midnight. (see image IMG_0172)

Therefore - 1) it looks like there is something up with the sleep start time 2) it looks like the incrementing is working 3) it looks like the time is not being correctly ingested by Apple Healthkit and is being logged as midnight 4) I don't know why my alert is show ~7 times in the Adjusted Date variable

Let me know if there is anything I can do to assist! I really appreciate the work you are putting into this and the lightening fast response.

IMG_0169 IMG_0170 IMG_0171 IMG_0172 IMG_0168

phautamaki commented 2 years ago

Thanks for all the information you have provided so far @Houston-Synex! And apologies, I was on vacation and didn't have time to look into this before. But now I'm on it.

I have been debugging the shortcut now with US as the area but haven't been able to reproduce the problem. Therefore, it would be interesting to know some additional details.

  1. Where are you located? I'm assuming United States. If you open your phone's Settings -> General -> Language & Region -section, how does the Region Format Example in the end look like? I'm expecting it to be similar to the screenshot below. Screenshot 2022-07-26 at 11 29 50
  2. If you copy/paste the Oura API request (e.g. https://api.ouraring.com/v1/sleep?access_token=YOUR_API_KEY&start=25-07-2022&end=25-07-2022) from the shortcut into the browser (with your API key), how does the response's bedtime_start value/format look like? I'm expecting it to be something along the lines of "2022-07-26T00:21:52+03:00" but with different timezone for you ofc.
Houston-Synex commented 2 years ago
  1. See attached - Canada and metric, but same same IMG_0393

  2. "bedtime_start": "2022-07-25T23:41:10-04:00"

Full copy and paste: {"sleep": [{"summary_date": "2022-07-25", "period_id": 1, "is_longest": 1, "timezone": -240, "bedtime_end": "2022-07-26T06:14:10-04:00", "bedtime_start": "2022-07-25T23:41:10-04:00", "breath_average": 10.375, "duration": 23580, "total": 20430, "awake": 3150, "rem": 1560, "deep": 5190, "light": 13680, "midpoint_time": 12150, "efficiency": 87, "restless": 42, "onset_latency": 390, "hr_5min": [0, 63, 64, 63, 62, 62, 61, 61, 59, 56, 55, 54, 55, 55, 55, 56, 55, 56, 55, 55, 58, 63, 65, 60, 58, 56, 55, 55, 55, 55, 55, 55, 55, 54, 53, 54, 53, 51, 56, 57, 58, 56, 57, 53, 53, 54, 55, 55, 56, 56, 54, 53, 54, 54, 54, 54, 54, 55, 53, 53, 53, 53, 52, 51, 52, 51, 53, 58, 59, 56, 57, 54, 54, 52, 53, 53, 52, 50, 51], "hr_average": 55.51, "hr_lowest": 50.0, "hypnogram_5min": "4211144422222221111111122222211112211122222422222222222222222222222333322223324", "rmssd": 40, "rmssd_5min": [0, 31, 29, 30, 30, 30, 34, 36, 31, 35, 42, 47, 39, 38, 36, 32, 32, 32, 32, 35, 38, 28, 27, 40, 43, 49, 35, 44, 36, 32, 38, 48, 36, 43, 28, 28, 55, 73, 46, 43, 42, 59, 46, 65, 55, 51, 41, 44, 50, 38, 37, 35, 34, 35, 30, 27, 29, 38, 32, 34, 36, 39, 52, 49, 46, 53, 56, 21, 29, 43, 32, 50, 54, 67, 53, 47, 71, 67, 54], "score": 65, "score_alignment": 97, "score_deep": 91, "score_disturbances": 59, "score_efficiency": 88, "score_latency": 78, "score_rem": 21, "score_total": 55, "temperature_deviation": 0.12, "temperature_trend_deviation": -0.06, "bedtime_start_delta": -1130, "bedtime_end_delta": 22450, "midpoint_at_delta": 11020, "temperature_delta": 0.12}]}

Thank you!

phautamaki commented 2 years ago

I think I found the problem @Houston-Synex. There seems to be some kind of bug in how shortcuts handle ISO 8601 timestamps with negative offsets containing colon (in your case, -04:00). I reported this to Apple, let's see if they fix it at some point.

However, I also noticed that it seems to work OK if there is no colon (i.e. -0400), and therefore I created a new version of the shortcut that drops the unnecessary colon from the bedtime_start value. Could you try if this works better for you: https://www.icloud.com/shortcuts/84d220835df145d590cb85aaa6c6eb31

Thanks!

Houston-Synex commented 2 years ago

Unfortunately, I am seeing no improvement with the new version. screenshot attached, but let me know if there is more helpful information I can provide.

9C08E022-4652-4E82-BEE3-560A9EFF9A67

Thank you for all of your help!!

phautamaki commented 2 years ago

Thanks for the info @Houston-Synex! That's weird though, I was pretty sure that would have fixed it. 🤔 Could you try running this new debug version and taking screenshots of all of the "quick look" prints it gives. Trying to find where the time parsing goes wrong. Thanks!

https://www.icloud.com/shortcuts/9e494a3ade024e649feec684f6aa08de

Houston-Synex commented 2 years ago

Hi @phautamaki Screenshots below! IMG_0534 IMG_0536 IMG_0537 IMG_0538 IMG_0540 IMG_0539

phautamaki commented 2 years ago

Thanks for the swift response!

I believe the last two screenshots are the other way around they originally appeared? If that’s the case then everything seems to be as expected at that point. The saved values still have timestamps of 12PM as before?

Houston-Synex commented 2 years ago

Sorry that looks like bad Github loading: Archive.zip

True order of images: IMG_0534, IMG_0536,IMG_0537,IMG_05348,IMG_0539,IMG_0540 (Numerical identifier is in order of sequence)

Yes everything is still coming in at midnight -> IMG_AB7A6E70B668-1

Houston-Synex commented 2 years ago

Hi, @phautamaki any more information I can get you?

phautamaki commented 2 years ago

Sorry @Houston-Synex, I've been busy with other stuff but will get back to this shortly!

The only explanation that comes to mind is that even though the bedtime_start is correct after parsing it in the way described above, something goes wrong either when the addition (of 5 minutes) is made or when the Health value is saved.

I added one more debug print to this, could you try this out (I'm only interested about the last print in case the values seem OK up to that point): https://www.icloud.com/shortcuts/9cdadd7278a24695917af498ba70b651

Houston-Synex commented 2 years ago

Hi @phautamaki, no problem and I appreciate your help! I just wanted to make sure you didn't need anything from me.

See the zip for the new set of images (in order by sequence number) 1Sept22-HRV.zip

Unfortunately still seeing all oura ring data as midnight.

phautamaki commented 2 years ago

I immediately tried if the timestamp handling problem had disappeared after updating to iOS 16. Unfortunately, no effect. :/

However, as it seems also the reverse conversion from ISO 8601 time to something else goes wrong, I changed the saving format to regular. Could you try if this works any better: https://www.icloud.com/shortcuts/ad0fa8d7016d40879059d1c258a7fe39

Houston-Synex commented 2 years ago

I think that did it!!!! 12Sept22-HRV.zip

Last screenshot in zip - reposting here:

IMG_1074

phautamaki commented 2 years ago

Finally! Sorry it took so long. I will clean that shortcut from debug prints and add it as an alternative to the actual readme tomorrow morning. The original should start working if Apple fixes the timestamp handling at some point but as long as that isn’t happening it’s better to use this version if the time offset is negative in the user’s region, which is the case there.

Houston-Synex commented 2 years ago

Thank you for all your help! I learned a lot more about shortcuts along the way ☺️.

phautamaki commented 2 years ago

@Houston-Synex I added the alternative version of the sync shortcut without debugs to the documentation now. Please do inform me if there is still something off. Have a nice rest of the week!