From ecc7a3ffa8153b7b1bf1e02cd17b0a60e1fc55bf Mon Sep 17 00:00:00 2001 From: Ben Varick Date: Tue, 7 Nov 2023 14:18:26 -0600 Subject: [PATCH] made end_time col accurate --- html/index.html | 430 +++++++++++++++++++++++++++++++++++++++++++++++ laundry_status.R | 45 +++-- 2 files changed, 457 insertions(+), 18 deletions(-) create mode 100644 html/index.html diff --git a/html/index.html b/html/index.html new file mode 100644 index 0000000..6b83fd1 --- /dev/null +++ b/html/index.html @@ -0,0 +1,430 @@ + + + + + + + + + + + + + +Laundry Status + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + +

updated: Tuesday 01:57 PM

+

this site updates every 5 minutes

+
+

The washing machine is currently: off

+
+
The washer ended its most recent cycle at Tuesday 08:05 AM
+
+353 minutes ago +
+
+
+
+

The dryer is currently: off

+
+
The dryer ended its most recent cycle at Tuesday 08:06 AM
+
+352 minutes ago +
+
+
+

This site tracks the washing machine and dryer of our building. My +hope is that it helps people find good times to do laundry.

+

+

+

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

+
+ + + + +
+ + + + + + + + + + + + + + + diff --git a/laundry_status.R b/laundry_status.R index 0b63dc6..9a03f20 100644 --- a/laundry_status.R +++ b/laundry_status.R @@ -19,11 +19,13 @@ home_assistant <- InfluxDBClient$new(url = "https://influxdb.dendroalsia.net", org = org) update_interval <- 5 cronjob_interval <- 60 +power_threshhold <- 5 # ---- set variables entities <- data.frame(name = c("washing machine", "dryer"), entity_id = c("lamp_a_power", "lamp_b_power")) update_data <- function(){ + run_time <- Sys.time() values <- home_assistant$query('from(bucket: "home_assistant") |> range(start: -7d) |> filter(fn: (r) => r["entity_id"] == "lamp_b_power" or r["entity_id"] == "lamp_a_power") |> filter(fn: (r) => r["_field"] == "value") |> filter(fn: (r) => r["_measurement"] == "W")', POSIXctCol = NULL) values <- bind_rows(values) @@ -33,38 +35,45 @@ update_data <- function(){ values <- values %>% mutate( time = as.POSIXct(time, tz = "America/Chicago"), - status = ifelse(value > 1, "on", "off")) %>% - mutate(end_time = time + minutes(1)) + status = ifelse(value > power_threshhold, "on", "off")) - washer_last_on <- values %>% filter(entity_id == entities$entity_id[1], value > 5) %>% tail(1) %>% pull(time) - washer_last_off <- values %>% filter(entity_id == entities$entity_id[1], value < 5) %>% tail(1) %>% pull(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) - dryer_last_on <- values %>% filter(entity_id == entities$entity_id[2], value > 5) %>% tail(1) %>% pull(time) - dryer_last_off <- values %>% filter(entity_id == entities$entity_id[2], value < 5) %>% tail(1) %>% pull(time) + washer_last_on <- values %>% filter(entity_id == entities$entity_id[1], value > power_threshhold) %>% tail(1) %>% pull(time) + washer_last_off <- values %>% filter(entity_id == entities$entity_id[1], value < power_threshhold) %>% tail(1) %>% pull(time) + + dryer_last_on <- values %>% filter(entity_id == entities$entity_id[2], value > power_threshhold) %>% tail(1) %>% pull(time) + dryer_last_off <- values %>% filter(entity_id == entities$entity_id[2], value < power_threshhold) %>% tail(1) %>% pull(time) # ---- generate html current_status <- as.list(NULL) for (entity in entities$entity_id){ - current_status[[entity]] <- ifelse(values %>% filter(entity_id %in% entity) %>% tail(1) %>% pull(value) > 5, "on", "off") + current_status[[entity]] <- ifelse(values %>% filter(entity_id %in% entity) %>% tail(1) %>% pull(value) > power_threshhold, "on", "off") } plot_1week <- ggplot(data = values) + - geom_rect(aes(xmin = end_time, - xmax = time, - ymin = ifelse(entity_id == "lamp_a_power", 0, 2) + 0.5, - ymax = ifelse(entity_id == "lamp_a_power", 0, 2) + 1.5, + geom_tile(aes(x = time + seconds(round(as.numeric(difftime(end_time, time, unit = "secs")))/2), + y = entity_id, + width = seconds(round(as.numeric(difftime(end_time, time, unit = "secs")))), + height = 0.5, fill = status)) + - scale_y_continuous() + + scale_y_discrete(breaks = entities$entity_id, labels = entities$name) + scale_x_datetime(date_breaks = "24 hours", date_labels = '%A', date_minor_breaks = "6 hours") + theme(axis.text.x = element_text(angle = 30, vjust = 0.5)) + - labs(title = "Last week") + labs(title = "The past week") plot_1day <- ggplot(data = values %>% filter(time >= max(values$time) - hours(24))) + - geom_rect(aes(xmin = end_time, - xmax = time, - ymin = ifelse(entity_id == "lamp_a_power", 0, 2) + 0.5, - ymax = ifelse(entity_id == "lamp_a_power", 0, 2) + 1.5, + geom_tile(aes(x = time + seconds(round(as.numeric(difftime(end_time, time, unit = "secs")))/2), + y = entity_id, + width = seconds(round(as.numeric(difftime(end_time, time, unit = "secs")))), + height = 0.5, fill = status)) + - scale_y_continuous() + + scale_y_discrete(breaks = entities$entity_id, labels = entities$name) + scale_x_datetime(breaks = seq(round_date(max(values$time), "4 hours") - hours(24), round_date(max(values$time), "4 hours"), by = "4 hours"), date_labels = '%I:%M %p', date_minor_breaks = "1 hours") + theme(axis.text.x = element_text(angle = 30, vjust = 0.5)) + labs(title = "Last 24 hours")