Last Night's Sleep - Start Of Sleep
COMMENT
Made by: mvan231
Modified by mvan231
~~~~~For~~~~~
Post: How to find sleep time and wake up time
By: WheretheEoopy
Link: https://www.reddit.com/r/shortcuts/comments/18b4xlb/how_to_find_sleep_time_and_wake_up_time/
~~~~~~~~~~
Originally created before advanced sleep tracking was available in WatchOS 9 / iOS 16. Updated in 2022 for the inclusion of these sleep stages
Updates for showing Hours and Minutes of total sleep time vs x.y hours
V4
- Updated calculation method to use seconds instead of relying on the sum method
- Handling added for when detailed sleep data isn't available
CALENDAR
Subtract
1
days
from
Current Date

Adjusted Date
DATE
Specified Date
Adjusted Date
18:00

Date
TEXT
{
"In Bed":[],
"Core":[],
"REM":[],
"Deep":[],
"Awake":[]
}

Text
VARIABLES
Set variable
Dict
to
Text
HEALTH
Find
All Health Samples
where
All the following are true [{'class': 'filter-property', 'value': 'Type'}, {'class': 'filter-operator', 'value': 'is'}, {'class': 'filter-benchmark', 'value': 'Sleep'}] [{'class': 'filter-property', 'value': 'Start Date'}, {'class': 'filter-operator', 'value': 'is in the last'}, {'class': 'filter-benchmark', 'value': '1', 'glyph': '', 'UUID': ''}, {'class': 'filter-unit', 'value': 'day', 'glyph': '', 'UUID': ''}]

Health Samples
SCRIPTING
Repeat with each item in
Health Samples

Repeat Results
NOTHING
Nothing
SCRIPTING
If
Date
is less than
Repeat Item
SCRIPTING
Get
Value
for
Repeat Item
in
Dict

Dictionary Value
CALENDAR
Get time between
Repeat Item
and
Repeat Item
in
Seconds

Time Between Dates
LIST

List
SCRIPTING
Set
Repeat Item
to
List
in
Dictionary

Dictionary
VARIABLES
Set variable
Dict
to
Dictionary
VARIABLES
Get
Repeat Item
SCRIPTING
End If

If Result
SCRIPTING
End Repeat

Repeat Results
SCRIPTING
Get
First Item
from
Repeat Results

Item from List
TEXT
Item from List

Text
DOCUMENTS
Show
Text
in Quick Look
Showcuts