diff --git a/laundry_status.R b/laundry_status.R index 40c53c3..6706875 100755 --- a/laundry_status.R +++ b/laundry_status.R @@ -17,8 +17,7 @@ bucket = "home_assistant" home_assistant <- InfluxDBClient$new(url = "https://influxdb.dendroalsia.net", token = token, org = org) -update_interval <- 5 -long_term_rounds <- 1440/update_interval +longterm_run_time <- ymd_hms("2024-01-01:00:00:00", tz = "America/Chicago") power_threshold_on <- 10 power_threshold_wash_door <- 4 power_threshold_dry_door <- 1.5 @@ -110,7 +109,7 @@ update_data <- function(){ current_status[[entity]] <- ifelse(values %>% filter(entity_id %in% entity) %>% tail(1) %>% pull(value) > power_threshold_on, "on", "off") } current_status <- bind_rows(current_status) - + current_status_update <<- ifelse((current_status$washing_machine_power == "on")|(current_status$dryer_power) == "on", TRUE, FALSE) ## add spanish translations current_status <- current_status %>% mutate(dryer_power_es = ifelse(dryer_power == "on", "encendida", "apagada"), @@ -210,6 +209,7 @@ update_longterm_data <- function(){ Sys.setlocale("LC_TIME", "en_US.UTF-8") run_time <- Sys.time() + longterm_run_time <<- run_time start_time <- floor_date(run_time - ddays(365), unit = "days") values <- home_assistant$query(paste0('from(bucket: "home_assistant") |> range(start: ', start_time, @@ -301,13 +301,18 @@ update_longterm_data <- function(){ } continue <- TRUE -i <- 0 while(continue){ message(Sys.time()) update_data() - if (i %% long_term_rounds == 0) { + if (as.double(difftime(Sys.time(), longterm_run_time, units = "hours", tz = "America/Chicago")) > 24) { update_longterm_data() } - i <- i + 1 - Sys.sleep(60*update_interval) + if (current_status_update == TRUE) { + message("sleeping for 1 minute") + Sys.sleep(60*1) + } else { + message("sleeping for 5 minutes") + Sys.sleep(60*5) + } + }