made update timing dynamic

This commit is contained in:
Ben Varick 2024-01-28 22:00:25 -06:00
parent 74b5dcd122
commit 6a4d2b3fb8
Signed by: ben
SSH Key Fingerprint: SHA256:jWnpFDAcacYM5aPFpYRqlsamlDyKNpSj3jj+k4ojtUo

View File

@ -17,8 +17,7 @@ bucket = "home_assistant"
home_assistant <- InfluxDBClient$new(url = "https://influxdb.dendroalsia.net", home_assistant <- InfluxDBClient$new(url = "https://influxdb.dendroalsia.net",
token = token, token = token,
org = org) org = org)
update_interval <- 5 longterm_run_time <- ymd_hms("2024-01-01:00:00:00", tz = "America/Chicago")
long_term_rounds <- 1440/update_interval
power_threshold_on <- 10 power_threshold_on <- 10
power_threshold_wash_door <- 4 power_threshold_wash_door <- 4
power_threshold_dry_door <- 1.5 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[[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 <- bind_rows(current_status)
current_status_update <<- ifelse((current_status$washing_machine_power == "on")|(current_status$dryer_power) == "on", TRUE, FALSE)
## add spanish translations ## add spanish translations
current_status <- current_status %>% current_status <- current_status %>%
mutate(dryer_power_es = ifelse(dryer_power == "on", "encendida", "apagada"), 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") Sys.setlocale("LC_TIME", "en_US.UTF-8")
run_time <- Sys.time() run_time <- Sys.time()
longterm_run_time <<- run_time
start_time <- floor_date(run_time - ddays(365), unit = "days") start_time <- floor_date(run_time - ddays(365), unit = "days")
values <- home_assistant$query(paste0('from(bucket: "home_assistant") |> range(start: ', values <- home_assistant$query(paste0('from(bucket: "home_assistant") |> range(start: ',
start_time, start_time,
@ -301,13 +301,18 @@ update_longterm_data <- function(){
} }
continue <- TRUE continue <- TRUE
i <- 0
while(continue){ while(continue){
message(Sys.time()) message(Sys.time())
update_data() 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() update_longterm_data()
} }
i <- i + 1 if (current_status_update == TRUE) {
Sys.sleep(60*update_interval) message("sleeping for 1 minute")
Sys.sleep(60*1)
} else {
message("sleeping for 5 minutes")
Sys.sleep(60*5)
}
} }