From f650c7cb9521ca842a9abb4db5c7b758444a94fe Mon Sep 17 00:00:00 2001 From: Ben Varick Date: Wed, 17 Jan 2024 13:55:13 -0600 Subject: [PATCH] added longterm data page --- html/longterm_data.html | 410 ++++++++++++++++++++++++++++++++++++++ laundry_longterm_data.Rmd | 21 ++ laundry_status.R | 69 ++++++- laundry_status.Rmd | 5 + 4 files changed, 498 insertions(+), 7 deletions(-) create mode 100644 html/longterm_data.html create mode 100644 laundry_longterm_data.Rmd diff --git a/html/longterm_data.html b/html/longterm_data.html new file mode 100644 index 0000000..bb2b822 --- /dev/null +++ b/html/longterm_data.html @@ -0,0 +1,410 @@ + + + + + + + + + + + + + +Laundry Longterm Data + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + +

updated: Wednesday 01:54 PM

+

this site updates every day

+
+

+

If you have any issues or questions, please email ben@dendroalsia.net

+

The code that I wrote to generate this website is available under the +GPLv3 license here: https://git.dendroalsia.net/ben/laundry_status

+ + + + +
+ + + + + + + + + + + + + + + diff --git a/laundry_longterm_data.Rmd b/laundry_longterm_data.Rmd new file mode 100644 index 0000000..6cd6e3c --- /dev/null +++ b/laundry_longterm_data.Rmd @@ -0,0 +1,21 @@ +--- +title: "Laundry Longterm Data" +output: + html_document: + includes: + in_header: favicon_header.html +--- + +updated: `r format(Sys.time(), format = "%A %I:%M %p", tz = "America/Chicago")` + +this site updates every day + +--- + +```{r plot_all_week_days, echo=FALSE} +plot(plot_all_week_days) +``` + +If you have any issues or questions, please email [ben@dendroalsia.net](mailto:ben@dendroalsia.net) + +The code that I wrote to generate this website is available under the GPLv3 license here: [https://git.dendroalsia.net/ben/laundry_status](https://git.dendroalsia.net/ben/laundry_status) diff --git a/laundry_status.R b/laundry_status.R index 2fda992..df19b5e 100755 --- a/laundry_status.R +++ b/laundry_status.R @@ -18,7 +18,7 @@ home_assistant <- InfluxDBClient$new(url = "https://influxdb.dendroalsia.net", token = token, org = org) update_interval <- 5 -cronjob_interval <- 60 +long_term_rounds <- 1440/update_interval power_threshold_on <- 10 power_threshold_wash_door <- 4 power_threshold_dry_door <- 1.5 @@ -135,15 +135,70 @@ update_data <- function(){ } -# for(i in 1:(cronjob_interval/update_interval)){ -# message(Sys.time()) -# update_data() -# Sys.sleep(60*update_interval) -# } +update_longterm_data <- function(){ + + run_time <- Sys.time() + start_time <- floor_date(run_time - ddays(365), unit = "days") + values <- home_assistant$query(paste0('from(bucket: "home_assistant") |> range(start: ', + start_time, + ') |> filter(fn: (r) => r["entity_id"] == "washing_machine_power" or r["entity_id"] == "dryer_power") |> filter(fn: (r) => r["_field"] == "value") |> filter(fn: (r) => r["_measurement"] == "W")'), + POSIXctCol = NULL) + values <- bind_rows(values) + values <- values %>% + rename(value = "_value", + time = "_time") + values <- values %>% + mutate( + time = as.POSIXct(time, tz = "America/Chicago"), + status = ifelse(value > power_threshold_on, "on", "off")) %>% + mutate(door_threshold = ifelse(entity_id == "washing_machine_power", power_threshold_wash_door, power_threshold_dry_door)) %>% + mutate(door = ifelse(value < door_threshold, "open", "closed")) %>% + filter(time >= start_time) + + values_by_entity <- as.list(NULL) + for(entity in entities$entity_id) { + values_by_entity[[entity]] <- values %>% + filter(entity_id %in% entity) %>% + mutate(end_time = c(time[-1], run_time)) + } + values <- bind_rows(values_by_entity) + + values["weekday"] <- wday(values$time, label = TRUE) + values["hourofday"] <- floor_date(ymd_hms(paste("2023_01_01",strftime(values$time, format="%H:%M:%S"))), unit = "hours") + long_term <- values %>% + filter(status =="on") %>% + group_by(weekday, hourofday, entity_id, status) %>% + summarise(count = n()) + # ---- make plots + plot_all_week_days <- ggplot(data = long_term) + + geom_tile(aes(x = weekday, + y = hourofday, + alpha = count, + fill = status)) + + facet_grid(entity_id ~ .) + + scale_y_datetime(date_breaks = "4 hours", date_labels = '%I:%M %p', minor_breaks = "2 hours", expand = expansion(mult = 0)) + + scale_x_discrete() + + scale_fill_manual(values = colors$color[2]) + + theme(axis.text.x = element_text(angle = 30, vjust = 0.5)) + + labs(title = "The past year", + x = "Weekday", + y = "Time of Day", + fill = NULL) + + # ---- generate html + render("laundry_longterm_data.Rmd", + output_dir = "html", + output_file = "longterm_data.html") +} continue <- TRUE +i <- 0 while(continue){ message(Sys.time()) update_data() + if (i %% long_term_rounds == 0) { + update_longterm_data() + } + i <- i + 1 Sys.sleep(60*update_interval) -} \ No newline at end of file +} diff --git a/laundry_status.Rmd b/laundry_status.Rmd index 2b9eee8..617b9b3 100644 --- a/laundry_status.Rmd +++ b/laundry_status.Rmd @@ -46,6 +46,11 @@ plot(plot_1day) ```{r plot_1week_days, echo=FALSE} plot(plot_1week_days) ``` +--- +For longterm data, click [here](https://laundry.dendroalsia.net/longterm_data.html) +--- + + If you have any issues or questions, please email [ben@dendroalsia.net](mailto:ben@dendroalsia.net)