diff --git a/ecobee.R b/ecobee.R index b9251ab..1232e56 100644 --- a/ecobee.R +++ b/ecobee.R @@ -6,22 +6,10 @@ library(jsonlite) library(plotly) library(scales) library(readODS) +library(ggrepel) setwd("~/Documents/dataProjects/ecobee") -pause = function() -{ - if (interactive()) - { - invisible(readline(prompt = "Press to continue...")) - } - else - { - cat("Press to continue...") - invisible(readLines(file("stdin"), 1)) - } -} - #make a file called 'apiKey' in your working directory that has your api key for your ecobee if(file.exists('authTokens.Rda')) { #if the authToken file already exists, then refresh it @@ -57,7 +45,7 @@ if(file.exists('authTokens.Rda')) { # load data ---- -startdate <- ymd('2021-11-13') +startdate <- ymd('2021-11-01') enddate <- Sys.Date() columns <- c('zoneClimate', @@ -73,33 +61,37 @@ columns <- c('zoneClimate', 'zoneHumidity', 'zoneHvacMode') -months <- data.frame(start = seq(floor_date(startdate, unit = "months"), - floor_date(enddate, unit = "months"), - length.out = interval(floor_date(startdate, unit = "months"), enddate) %/% months(1) + 1)) +months <- data.frame(start = seq(startdate, enddate, by = "month")) months <- months %>% mutate(end = start + months(1) - 1) -ecobee_new <- list(NULL) +ecobee <- list(NULL) for(i in 1:nrow(months)){ - message(paste("Downloading", months$start[[i]], "to", months$end[[i]])) - - url <- paste0('https://api.ecobee.com/1/runtimeReport?format=json&body={"startDate":', - paste0('\"',as.Date(months$start[i]),'\",'), - '"endDate":', - paste0('\"',as.Date(months$end[i]),'\",'), - '"columns":', - paste0('\"',paste0(columns, collapse = ","),'\"'), - ',"selection":{"selectionType":"thermostats","selectionMatch":"413788899054"}}') - ecobee_month <- httr::GET( - url = url, - add_headers('Content-Type' = 'application/json;charset=UTF-8', - 'Authorization' = paste0('Bearer ', access_token) - ) - ) %>% httr::content() - + if(file.exists(paste0('data/',months$start[i], '.Rda')) && Sys.Date() > months$end[i]){ + + message(paste("Loading", months$start[[i]], "to", months$end[[i]])) + + load(file=paste0('data/',months$start[i], '.Rda')) + } else{ + message(paste("Downloading", months$start[[i]], "to", months$end[[i]])) + + url <- paste0('https://api.ecobee.com/1/runtimeReport?format=json&body={"startDate":', + paste0('\"',as.Date(months$start[i]),'\",'), + '"endDate":', + paste0('\"',as.Date(months$end[i]),'\",'), + '"columns":', + paste0('\"',paste0(columns, collapse = ","),'\"'), + ',"selection":{"selectionType":"thermostats","selectionMatch":"413788899054"}}') + ecobee_month <- httr::GET( + url = url, + add_headers('Content-Type' = 'application/json;charset=UTF-8', + 'Authorization' = paste0('Bearer ', access_token) + ) + ) %>% httr::content() + # parse data ---- - ecobee_new[[i]] <- tibble(ecobee_month[["reportList"]][[1]][["rowList"]]) %>% + ecobee_month <- tibble(ecobee_month[["reportList"]][[1]][["rowList"]]) %>% separate(sep = ",", col = 1, into = c('date', 'time', columns), @@ -108,10 +100,15 @@ for(i in 1:nrow(months)){ "dateTime" = ymd_hms(paste(date, time)), outdoortemp_bin = round(outdoorTemp)) %>% filter(ymd(date) <= Sys.Date()) + + if(Sys.Date() > months$end[i]){ + save(ecobee_month, + file = paste0('data/',months$start[i], '.Rda')) + } + } + ecobee[[i]] <- ecobee_month } -ecobee <- bind_rows(ecobee_new) - -save(ecobee, file = 'data/ecobee.Rda') +ecobee <- bind_rows(ecobee) daterange <- data.frame(start = min(ecobee$date), end = max(ecobee$date)) @@ -167,6 +164,12 @@ ggplot() + theme(legend.title = element_blank()) + labs(x = 'Date', y = 'Temperature (\u00B0F)') +ggsave(filename = "temp_time_all.png" , + path = paste(getwd(),"/figures/", sep = ""), + device = "png", + width = 11, + height = 8.5, + units = "in") ggplot() + geom_line(data = ecobee %>% filter(date > Sys.Date() - 3) %>% pivot_longer(cols = c(zoneAveTemp, outdoorTemp, zoneHeatTemp, zoneCoolTemp), names_to = "temp_type", values_to = "temp"), @@ -184,6 +187,12 @@ ggplot() + theme(legend.title = element_blank()) + labs(x = 'Date', y = 'Temperature (\u00B0F)') +ggsave(filename = "temp_time_last_3_days.png" , + path = paste(getwd(),"/figures/", sep = ""), + device = "png", + width = 11, + height = 8.5, + units = "in") ggplot(data = ecobee %>% filter(date != "2021-11-13", @@ -204,7 +213,13 @@ ggplot(data = ecobee %>% scale_y_continuous(labels = label_percent(), expand = expansion(mult = c(0,0))) + labs(x = 'Outdoor Temperature (\u00B0F)', y = 'Percent of the time the equipment runs') - +ggsave(filename = "outdoorTemp_equipmentRun_zone.png" , + path = paste(getwd(),"/figures/", sep = ""), + device = "png", + width = 11, + height = 8.5, + units = "in") + ggplot() + geom_col(data = ecobee %>% pivot_longer(cols = c(compHeat1, auxHeat1, compCool1), names_to = "equipment", values_to = "active_sec"), aes(x = as.Date(dateTime) + hours(12), @@ -219,6 +234,12 @@ ggplot() + y = 'Time equipment runs (hours)') + scale_y_continuous(sec.axis = sec_axis(trans = ~ .x*5, name = "Temperature (\u00B0F)")) +ggsave(filename = "dateDay_timeRunning.png" , + path = paste(getwd(),"/figures/", sep = ""), + device = "png", + width = 11, + height = 8.5, + units = "in") ggplot() + geom_col(data = ecobee %>% pivot_longer(cols = c(compHeat1, auxHeat1, compCool1), names_to = "equipment", values_to = "active_sec"), @@ -234,6 +255,12 @@ ggplot() + y = 'Time equipment runs (hours)') + scale_y_continuous(sec.axis = sec_axis(trans = ~ .x*5/7, name = "Temperature (\u00B0F)")) +ggsave(filename = "dateWeek_timeRunning.png" , + path = paste(getwd(),"/figures/", sep = ""), + device = "png", + width = 11, + height = 8.5, + units = "in") ggplot(data = ecobee %>% filter(date != "2021-11-13", @@ -254,4 +281,24 @@ ggplot(data = ecobee %>% scale_color_manual(labels = equipment_types$label, values = equipment_types %>% pull(color, column)) + labs(x = 'Outdoor Temperature (\u00B0F)', y = 'Time equipment runs (hours)') +ggsave(filename = "outdoorTemp_equipmentRun.png" , + path = paste(getwd(),"/figures/", sep = ""), + device = "png", + width = 11, + height = 8.5, + units = "in") +ggplot(data = utility_data %>% filter(start_date>startdate)) + + geom_point(aes(x = `Electricity (kWh)`, + y = heat_pump_hours+heat_aux_hours+cool_pump_hours)) + + geom_label_repel(aes(x = `Electricity (kWh)`, + y = heat_pump_hours+heat_aux_hours+cool_pump_hours, + label = `mid date`)) + + scale_x_continuous(limits = c(0, NA), expand = expansion(mult = c(0,0.1))) + + scale_y_continuous(limits = c(0, NA), expand = expansion(mult = c(0,0.1))) +ggsave(filename = "equipmentRun_electicCost.png" , + path = paste(getwd(),"/figures/", sep = ""), + device = "png", + width = 11, + height = 8.5, + units = "in") diff --git a/figures/dateDay_timeRunning.png b/figures/dateDay_timeRunning.png new file mode 100644 index 0000000..bbe6f1b Binary files /dev/null and b/figures/dateDay_timeRunning.png differ diff --git a/figures/dateWeek_timeRunning.png b/figures/dateWeek_timeRunning.png new file mode 100644 index 0000000..6686908 Binary files /dev/null and b/figures/dateWeek_timeRunning.png differ diff --git a/figures/equipmentRun_electicCost.png b/figures/equipmentRun_electicCost.png new file mode 100644 index 0000000..d3eabc0 Binary files /dev/null and b/figures/equipmentRun_electicCost.png differ diff --git a/figures/outdoorTemp_equipmentRun.png b/figures/outdoorTemp_equipmentRun.png new file mode 100644 index 0000000..96d6f6c Binary files /dev/null and b/figures/outdoorTemp_equipmentRun.png differ diff --git a/figures/outdoorTemp_equipmentRun_zone.png b/figures/outdoorTemp_equipmentRun_zone.png new file mode 100644 index 0000000..eb93636 Binary files /dev/null and b/figures/outdoorTemp_equipmentRun_zone.png differ diff --git a/figures/temp_time_all.png b/figures/temp_time_all.png new file mode 100644 index 0000000..fcaf76a Binary files /dev/null and b/figures/temp_time_all.png differ diff --git a/figures/temp_time_last_3_days.png b/figures/temp_time_last_3_days.png new file mode 100644 index 0000000..6695802 Binary files /dev/null and b/figures/temp_time_last_3_days.png differ