changed download behavior to cache data in month chunks, changed graphs
Signed-off-by: Ben Varick <ben@dendroalsia.net>
119
ecobee.R
@ -6,22 +6,10 @@ library(jsonlite)
|
|||||||
library(plotly)
|
library(plotly)
|
||||||
library(scales)
|
library(scales)
|
||||||
library(readODS)
|
library(readODS)
|
||||||
|
library(ggrepel)
|
||||||
|
|
||||||
setwd("~/Documents/dataProjects/ecobee")
|
setwd("~/Documents/dataProjects/ecobee")
|
||||||
|
|
||||||
pause = function()
|
|
||||||
{
|
|
||||||
if (interactive())
|
|
||||||
{
|
|
||||||
invisible(readline(prompt = "Press <Enter> to continue..."))
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
cat("Press <Enter> to continue...")
|
|
||||||
invisible(readLines(file("stdin"), 1))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#make a file called 'apiKey' in your working directory that has your api key for your ecobee
|
#make a file called 'apiKey' in your working directory that has your api key for your ecobee
|
||||||
if(file.exists('authTokens.Rda')) {
|
if(file.exists('authTokens.Rda')) {
|
||||||
#if the authToken file already exists, then refresh it
|
#if the authToken file already exists, then refresh it
|
||||||
@ -57,7 +45,7 @@ if(file.exists('authTokens.Rda')) {
|
|||||||
|
|
||||||
# load data ----
|
# load data ----
|
||||||
|
|
||||||
startdate <- ymd('2021-11-13')
|
startdate <- ymd('2021-11-01')
|
||||||
|
|
||||||
enddate <- Sys.Date()
|
enddate <- Sys.Date()
|
||||||
columns <- c('zoneClimate',
|
columns <- c('zoneClimate',
|
||||||
@ -73,33 +61,37 @@ columns <- c('zoneClimate',
|
|||||||
'zoneHumidity',
|
'zoneHumidity',
|
||||||
'zoneHvacMode')
|
'zoneHvacMode')
|
||||||
|
|
||||||
months <- data.frame(start = seq(floor_date(startdate, unit = "months"),
|
months <- data.frame(start = seq(startdate, enddate, by = "month"))
|
||||||
floor_date(enddate, unit = "months"),
|
|
||||||
length.out = interval(floor_date(startdate, unit = "months"), enddate) %/% months(1) + 1))
|
|
||||||
months <- months %>%
|
months <- months %>%
|
||||||
mutate(end = start + months(1) - 1)
|
mutate(end = start + months(1) - 1)
|
||||||
|
|
||||||
ecobee_new <- list(NULL)
|
ecobee <- list(NULL)
|
||||||
for(i in 1:nrow(months)){
|
for(i in 1:nrow(months)){
|
||||||
message(paste("Downloading", months$start[[i]], "to", months$end[[i]]))
|
if(file.exists(paste0('data/',months$start[i], '.Rda')) && Sys.Date() > months$end[i]){
|
||||||
|
|
||||||
url <- paste0('https://api.ecobee.com/1/runtimeReport?format=json&body={"startDate":',
|
message(paste("Loading", months$start[[i]], "to", months$end[[i]]))
|
||||||
paste0('\"',as.Date(months$start[i]),'\",'),
|
|
||||||
'"endDate":',
|
load(file=paste0('data/',months$start[i], '.Rda'))
|
||||||
paste0('\"',as.Date(months$end[i]),'\",'),
|
} else{
|
||||||
'"columns":',
|
message(paste("Downloading", months$start[[i]], "to", months$end[[i]]))
|
||||||
paste0('\"',paste0(columns, collapse = ","),'\"'),
|
|
||||||
',"selection":{"selectionType":"thermostats","selectionMatch":"413788899054"}}')
|
url <- paste0('https://api.ecobee.com/1/runtimeReport?format=json&body={"startDate":',
|
||||||
ecobee_month <- httr::GET(
|
paste0('\"',as.Date(months$start[i]),'\",'),
|
||||||
url = url,
|
'"endDate":',
|
||||||
add_headers('Content-Type' = 'application/json;charset=UTF-8',
|
paste0('\"',as.Date(months$end[i]),'\",'),
|
||||||
'Authorization' = paste0('Bearer ', access_token)
|
'"columns":',
|
||||||
)
|
paste0('\"',paste0(columns, collapse = ","),'\"'),
|
||||||
) %>% httr::content()
|
',"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 ----
|
# parse data ----
|
||||||
|
|
||||||
ecobee_new[[i]] <- tibble(ecobee_month[["reportList"]][[1]][["rowList"]]) %>%
|
ecobee_month <- tibble(ecobee_month[["reportList"]][[1]][["rowList"]]) %>%
|
||||||
separate(sep = ",",
|
separate(sep = ",",
|
||||||
col = 1,
|
col = 1,
|
||||||
into = c('date', 'time', columns),
|
into = c('date', 'time', columns),
|
||||||
@ -108,10 +100,15 @@ for(i in 1:nrow(months)){
|
|||||||
"dateTime" = ymd_hms(paste(date, time)),
|
"dateTime" = ymd_hms(paste(date, time)),
|
||||||
outdoortemp_bin = round(outdoorTemp)) %>%
|
outdoortemp_bin = round(outdoorTemp)) %>%
|
||||||
filter(ymd(date) <= Sys.Date())
|
filter(ymd(date) <= Sys.Date())
|
||||||
}
|
|
||||||
ecobee <- bind_rows(ecobee_new)
|
|
||||||
|
|
||||||
save(ecobee, file = 'data/ecobee.Rda')
|
if(Sys.Date() > months$end[i]){
|
||||||
|
save(ecobee_month,
|
||||||
|
file = paste0('data/',months$start[i], '.Rda'))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ecobee[[i]] <- ecobee_month
|
||||||
|
}
|
||||||
|
ecobee <- bind_rows(ecobee)
|
||||||
|
|
||||||
daterange <- data.frame(start = min(ecobee$date), end = max(ecobee$date))
|
daterange <- data.frame(start = min(ecobee$date), end = max(ecobee$date))
|
||||||
|
|
||||||
@ -167,6 +164,12 @@ ggplot() +
|
|||||||
theme(legend.title = element_blank()) +
|
theme(legend.title = element_blank()) +
|
||||||
labs(x = 'Date',
|
labs(x = 'Date',
|
||||||
y = 'Temperature (\u00B0F)')
|
y = 'Temperature (\u00B0F)')
|
||||||
|
ggsave(filename = "temp_time_all.png" ,
|
||||||
|
path = paste(getwd(),"/figures/", sep = ""),
|
||||||
|
device = "png",
|
||||||
|
width = 11,
|
||||||
|
height = 8.5,
|
||||||
|
units = "in")
|
||||||
|
|
||||||
ggplot() +
|
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"),
|
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()) +
|
theme(legend.title = element_blank()) +
|
||||||
labs(x = 'Date',
|
labs(x = 'Date',
|
||||||
y = 'Temperature (\u00B0F)')
|
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 %>%
|
ggplot(data = ecobee %>%
|
||||||
filter(date != "2021-11-13",
|
filter(date != "2021-11-13",
|
||||||
@ -204,6 +213,12 @@ ggplot(data = ecobee %>%
|
|||||||
scale_y_continuous(labels = label_percent(), expand = expansion(mult = c(0,0))) +
|
scale_y_continuous(labels = label_percent(), expand = expansion(mult = c(0,0))) +
|
||||||
labs(x = 'Outdoor Temperature (\u00B0F)',
|
labs(x = 'Outdoor Temperature (\u00B0F)',
|
||||||
y = 'Percent of the time the equipment runs')
|
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() +
|
ggplot() +
|
||||||
geom_col(data = ecobee %>% pivot_longer(cols = c(compHeat1, auxHeat1, compCool1), names_to = "equipment", values_to = "active_sec"),
|
geom_col(data = ecobee %>% pivot_longer(cols = c(compHeat1, auxHeat1, compCool1), names_to = "equipment", values_to = "active_sec"),
|
||||||
@ -219,6 +234,12 @@ ggplot() +
|
|||||||
y = 'Time equipment runs (hours)') +
|
y = 'Time equipment runs (hours)') +
|
||||||
scale_y_continuous(sec.axis = sec_axis(trans = ~ .x*5,
|
scale_y_continuous(sec.axis = sec_axis(trans = ~ .x*5,
|
||||||
name = "Temperature (\u00B0F)"))
|
name = "Temperature (\u00B0F)"))
|
||||||
|
ggsave(filename = "dateDay_timeRunning.png" ,
|
||||||
|
path = paste(getwd(),"/figures/", sep = ""),
|
||||||
|
device = "png",
|
||||||
|
width = 11,
|
||||||
|
height = 8.5,
|
||||||
|
units = "in")
|
||||||
|
|
||||||
ggplot() +
|
ggplot() +
|
||||||
geom_col(data = ecobee %>% pivot_longer(cols = c(compHeat1, auxHeat1, compCool1), names_to = "equipment", values_to = "active_sec"),
|
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)') +
|
y = 'Time equipment runs (hours)') +
|
||||||
scale_y_continuous(sec.axis = sec_axis(trans = ~ .x*5/7,
|
scale_y_continuous(sec.axis = sec_axis(trans = ~ .x*5/7,
|
||||||
name = "Temperature (\u00B0F)"))
|
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 %>%
|
ggplot(data = ecobee %>%
|
||||||
filter(date != "2021-11-13",
|
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)) +
|
scale_color_manual(labels = equipment_types$label, values = equipment_types %>% pull(color, column)) +
|
||||||
labs(x = 'Outdoor Temperature (\u00B0F)',
|
labs(x = 'Outdoor Temperature (\u00B0F)',
|
||||||
y = 'Time equipment runs (hours)')
|
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")
|
||||||
|
BIN
figures/dateDay_timeRunning.png
Normal file
After Width: | Height: | Size: 255 KiB |
BIN
figures/dateWeek_timeRunning.png
Normal file
After Width: | Height: | Size: 247 KiB |
BIN
figures/equipmentRun_electicCost.png
Normal file
After Width: | Height: | Size: 95 KiB |
BIN
figures/outdoorTemp_equipmentRun.png
Normal file
After Width: | Height: | Size: 233 KiB |
BIN
figures/outdoorTemp_equipmentRun_zone.png
Normal file
After Width: | Height: | Size: 325 KiB |
BIN
figures/temp_time_all.png
Normal file
After Width: | Height: | Size: 359 KiB |
BIN
figures/temp_time_last_3_days.png
Normal file
After Width: | Height: | Size: 177 KiB |