Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
IFTTT Fitbit to Beeminder: Get Enough Sleep!
// 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
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.