Created
January 14, 2018 15:33
-
-
Save chrisbobbe/4d2f79af65efdfa31e49bf00f983c779 to your computer and use it in GitHub Desktop.
IFTTT Fitbit to Beeminder: Get Enough Sleep!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Trigger: Fitbit, New Sleep Logged | |
// Action: Log data point in Beeminder | |
function parseFitbitDate(FitbitDate: string) { | |
var momentObj = moment(FitbitDate.replace(' at ',' '), 'MMMM DD, YYYY hh:mmA'); | |
return momentObj; | |
} | |
var TimeAwoke = parseFitbitDate(Fitbit.newSleepLogged.AwokeAt); | |
var TimeAsleep = parseFitbitDate(Fitbit.newSleepLogged.FellAsleepAt); | |
var WakeUpGoal = moment(TimeAwoke).set({'hour': 10, 'minute': 0}); | |
var FirstNapAllowed = moment(TimeAsleep).set({'hour': 14, 'minute': 0}); | |
var MinSleepDuration = 6; //in hours | |
var failed = false; | |
var value = 1; | |
var comment = ''; | |
if (parseInt(Fitbit.newSleepLogged.TotalTimeSleptInSeconds) < MinSleepDuration*3600) { | |
failed = true; | |
value -= 1; | |
comment += 'Fitbit says you only slept for ' + Fitbit.newSleepLogged.TotalTimeSlept + '...'; | |
} | |
if (TimeAwoke.isBetween(WakeUpGoal, FirstNapAllowed)) { | |
failed = true; | |
value -= 1; | |
comment += 'Fitbit says you woke up at ' + TimeAwoke.format('hh:mm A') + '...'; | |
} | |
if (TimeAsleep.isBetween(WakeUpGoal, FirstNapAllowed)) { | |
failed = true; | |
value -= 1; | |
comment += 'Fitbit says you fell asleep at ' + TimeAsleep.format('hh:mm A') + '...'; | |
} | |
if (TimeAsleep.isBefore(FirstNapAllowed) && TimeAwoke.isAfter(FirstNapAllowed)) { | |
failed = true; | |
value -= 2; | |
comment += 'You fell asleep at ' + TimeAsleep.format('hh:mm A') + ' and woke up at ' + TimeAwoke.format('hh:mm A') + '. Get going!'; | |
} | |
if (failed == false) { | |
value = 1; | |
comment += 'Good job! You\'re on track for a good day today.'; | |
} | |
if (value < -1) { value = -1; } | |
Beeminder.addData.setDatapointValue(value.toString()); | |
Beeminder.addData.setDatapointComment(comment); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment