Skip to content

Instantly share code, notes, and snippets.

@tdhopper
Last active May 15, 2017 15:14
Show Gist options
  • Select an option

  • Save tdhopper/58539c3d9a285c0f635a to your computer and use it in GitHub Desktop.

Select an option

Save tdhopper/58539c3d9a285c0f635a to your computer and use it in GitHub Desktop.
A totally unrefined technique for sending a moving average of my weight to my phone.

Weight measurement is noisy. It can be discouraging to see your weight fluctuate even if those fluctuations are meaningless. I taped over the weight display on my Withings scale and hacked together this method of getting smoothed statistics (moving average) of my weight.

This this requires:

Create an IFTTT rule to send your weight to Dropbox.

Mine is in this format:

153.98 July 08, 2013 at 07:16AM 
150.09 July 09, 2013 at 07:50AM 
150.75 July 09, 2013 at 10:45AM 
148.37 July 10, 2013 at 07:11AM 
150.09 July 11, 2013 at 07:41AM 

Create an IFTTT rule to create a copy of Weight-To-Pushover.sh in your Dropbox whenever your weight is updated.

Create a Hazel script to run and then delete this script whenever it is created.

library(lubridate)
library(ggplot2)
library(zoo)
con <- file("~/Dropbox/Text Notes/Weight.txt", "rt")
lines <- readLines(con)
close(con)
parse.line <- function(line) {
s <- strsplit(line, split=" ")[[1]]
date.str <- paste(s[2:10][!is.na(s[2:10])], collapse=" ")
date <- mdy_hm(date.str, quiet=TRUE)
list(s[1], date)
}
list.weight.date <- lapply(lines, parse.line)
weights= lapply(list.weight.date, function(X) as.numeric(X[1]))
dates = lapply(list.weight.date, function(X) X[[2]])
df <- data.frame(weight = unlist(weights), date = do.call("c", dates) )
ts <- zoo(c(df$weight), df$date)
ts <- aggregate(ts, time(ts), tail, 1)
g <- round(seq(start(ts), end(ts), 60 * 60 * 24), "days")
ts <- na.approx(ts, xout = g)
days.ago <- function(days, smooth.n) {
date <- head(tail(index(ts),days + 1),1)
smoothed <- rollmean(ts, smooth.n, align="right")
as.numeric(smoothed[date])
}
current.weight <- days.ago(0, 10)
x <- c(current.weight, days.ago(7, 10)-current.weight, days.ago(30, 10)-current.weight, days.ago(365, 10)-current.weight,max(ts)-current.weight)
cat(paste("Weight (lbs):", round(x[1], 1),"\n"))
cat(paste("1 Week Δ:", round(x[2], 1),"\n"))
cat(paste("1 Month Δ:", round(x[3], 1),"\n"))
cat(paste("1 Year Δ:", round(x[4], 1),"\n"))
cat(paste("Total Δ:", round(x[5], 1),"\n"))
cd;weight=$(Rscript "Dropbox/Scripts/Weight Stats.R"); curl -s -F "token=MY_TOKEN" -F "message=$weight" -F "device=iphone" https://api.pushover.net/1/messages.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment