From 56a2d09dc6eae82bdf250a2df949e1c70baa784b Mon Sep 17 00:00:00 2001 From: Ben Varick Date: Tue, 21 Nov 2023 14:06:51 -0600 Subject: [PATCH] added door status --- laundry_status.R | 26 ++++++++++++++++++-------- laundry_status.Rmd | 16 ++++++++++++---- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/laundry_status.R b/laundry_status.R index 6629e6b..de1d708 100755 --- a/laundry_status.R +++ b/laundry_status.R @@ -19,7 +19,10 @@ home_assistant <- InfluxDBClient$new(url = "https://influxdb.dendroalsia.net", org = org) update_interval <- 5 cronjob_interval <- 60 -power_threshhold <- 10 +power_threshold_on <- 10 +power_threshold_wash_door <- 4 +power_threshold_dry_door <- 1.5 + # ---- set variables entities <- data.frame(name = c("washing machine", "dryer"), entity_id = c("washing_machine_power", "dryer_power")) @@ -57,7 +60,9 @@ update_data <- function(){ values <- values %>% mutate( time = as.POSIXct(time, tz = "America/Chicago"), - status = ifelse(value > power_threshhold, "on", "off")) + 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")) values_by_entity <- as.list(NULL) for(entity in entities$entity_id) { @@ -67,15 +72,20 @@ update_data <- function(){ } values <- bind_rows(values_by_entity) - washer_last_on <- values %>% filter(entity_id == entities$entity_id[1], value > power_threshhold) %>% tail(1) %>% pull(end_time) - washer_last_off <- values %>% filter(entity_id == entities$entity_id[1], value < power_threshhold) %>% tail(1) %>% pull(end_time) + last_change <- values %>% + group_by(entity_id, status) %>% + slice_max(n = 1, order_by = time) %>% + pivot_wider(id_cols = domain, names_from = c(entity_id, status), values_from = time) + + status_door <- values %>% + group_by(entity_id) %>% + slice_max(n = 1, order_by = time) %>% + select(entity_id, door) %>% + pivot_wider(names_from = entity_id, values_from = door) - dryer_last_on <- values %>% filter(entity_id == entities$entity_id[2], value > power_threshhold) %>% tail(1) %>% pull(end_time) - dryer_last_off <- values %>% filter(entity_id == entities$entity_id[2], value < power_threshhold) %>% tail(1) %>% pull(end_time) - 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) > power_threshhold, "on", "off") + current_status[[entity]] <- ifelse(values %>% filter(entity_id %in% entity) %>% tail(1) %>% pull(value) > power_threshold_on, "on", "off") } # ---- make plots diff --git a/laundry_status.Rmd b/laundry_status.Rmd index 50145b0..5a2f69b 100644 --- a/laundry_status.Rmd +++ b/laundry_status.Rmd @@ -10,19 +10,27 @@ updated: `r format(Sys.time(), format = "%A %I:%M %p", tz = "America/Chicago")` this site updates every `r update_interval` minutes +--- + ## The washing machine is currently: `r current_status[["washing_machine_power"]]` -The washer `r ifelse(current_status$washing_machine_power=="on", "started", "ended")` its most recent cycle at `r ifelse(current_status$washing_machine_power=="on", strftime(washer_last_off, format = "%A %I:%M %p", tz = "America/Chicago"), strftime(washer_last_on, format = "%A %I:%M %p", tz = "America/Chicago"))` +The washer `r ifelse(current_status$washing_machine_power=="on", "started", "ended")` its most recent cycle at `r ifelse(current_status$washing_machine_power=="on", strftime(last_change$washing_machine_power_off, format = "%A %I:%M %p", tz = "America/Chicago"), strftime(last_change$washing_machine_power_on, format = "%A %I:%M %p", tz = "America/Chicago"))` : -`r ifelse(current_status$washing_machine_power=="on", calculateTimeAgo(washer_last_off), calculateTimeAgo(washer_last_on))` +`r ifelse(current_status$washing_machine_power=="on", calculateTimeAgo(last_change$washing_machine_power_off), calculateTimeAgo(last_change$washing_machine_power_on))` + +`r ifelse(current_status$washing_machine_power=="off", paste0("The washing machine door is ", status_door$washing_machine_power), "")` + +--- ## The dryer is currently: `r current_status[["dryer_power"]]` -The dryer `r ifelse(current_status$dryer_power=="on", "started", "ended")` its most recent cycle at `r ifelse(current_status$dryer_power=="on", strftime(dryer_last_off, format = "%A %I:%M %p", tz = "America/Chicago"), strftime(dryer_last_on, format = "%A %I:%M %p", tz = "America/Chicago"))` +The dryer `r ifelse(current_status$dryer_power=="on", "started", "ended")` its most recent cycle at `r ifelse(current_status$dryer_power=="on", strftime(last_change$dryer_power_off, format = "%A %I:%M %p", tz = "America/Chicago"), strftime(last_change$dryer_power_on, format = "%A %I:%M %p", tz = "America/Chicago"))` : -`r ifelse(current_status$dryer_power=="on", calculateTimeAgo(dryer_last_off), calculateTimeAgo(dryer_last_on))` +`r ifelse(current_status$dryer_power=="on", calculateTimeAgo(last_change$dryer_power_off), calculateTimeAgo(last_change$dryer_power_on))` + +`r ifelse(current_status$dryer_power=="off", paste0("The washing machine door is ", status_door$dryer_power), "")` ---