How Much Did I Sleep Last Night?
COMMENT
Read the HealthKit database to find out how much you slept last night.
You can modify the shortcut to customize Siri's response based on how much time you slept.
DOCUMENTS
Get text from
Shortcut Input

Text
SCRIPTING
Count
Items
in
Text

Count
NUMBER

Number
VARIABLES
Set variable
Asleep Minutes
to
Number
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': 'days', 'glyph': '', 'UUID': ''}] [{'class': 'filter-property', 'value': 'End Date'}, {'class': 'filter-operator', 'value': 'is today'}] [{'class': 'filter-property', 'value': 'Value'}, {'class': 'filter-operator', 'value': 'is'}, {'class': 'filter-benchmark', 'value': 'Asleep'}]

Health Samples
SCRIPTING
Repeat with each item in
Health Samples

Repeat Results
HEALTH
Get
End Date
from
Repeat Item

End Date
CALENDAR
Get time between
Repeat Item
and
End Date
in
Minutes

Time Between Dates
MATHS
Time Between Dates
+
Asleep Minutes
Options Under Construction

Calculation Result
VARIABLES
Set variable
Asleep Minutes
to
Calculation Result
SCRIPTING
End Repeat

Repeat Results
MATHS
Asleep Minutes
รท
60.0
Options Under Construction

Calculation Result
MATHS
Round
Calculation Result
to
Tenths

Rounded Number
SCRIPTING
If
Rounded Number
is less than
5.0
TEXT
You should try and get more sleep.

Text
VARIABLES
Set variable
Comment
to
Text
SCRIPTING
Otherwise
SCRIPTING
If
Rounded Number
is greater than
6.0
TEXT
Nicely done.

Text
VARIABLES
Set variable
Comment
to
Text
SCRIPTING
Otherwise
SCRIPTING
End If

If Result
SCRIPTING
End If

If Result
VARIABLES
Get
Count
SCRIPTING
If
Count
is
0
SCRIPTING
Show
You slept
Rounded Number
hours last night.
Comment
SCRIPTING
Otherwise
TEXT
You slept
Rounded Number
hours last night.
Comment

Text
SCRIPTING
End If

If Result
Showcuts