made end_time col accurate
This commit is contained in:
parent
e22826e492
commit
ecc7a3ffa8
430
html/index.html
Normal file
430
html/index.html
Normal file
File diff suppressed because one or more lines are too long
@ -19,11 +19,13 @@ home_assistant <- InfluxDBClient$new(url = "https://influxdb.dendroalsia.net",
|
|||||||
org = org)
|
org = org)
|
||||||
update_interval <- 5
|
update_interval <- 5
|
||||||
cronjob_interval <- 60
|
cronjob_interval <- 60
|
||||||
|
power_threshhold <- 5
|
||||||
# ---- set variables
|
# ---- set variables
|
||||||
entities <- data.frame(name = c("washing machine", "dryer"), entity_id = c("lamp_a_power", "lamp_b_power"))
|
entities <- data.frame(name = c("washing machine", "dryer"), entity_id = c("lamp_a_power", "lamp_b_power"))
|
||||||
|
|
||||||
update_data <- function(){
|
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")',
|
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)
|
POSIXctCol = NULL)
|
||||||
values <- bind_rows(values)
|
values <- bind_rows(values)
|
||||||
@ -33,38 +35,45 @@ update_data <- function(){
|
|||||||
values <- values %>%
|
values <- values %>%
|
||||||
mutate(
|
mutate(
|
||||||
time = as.POSIXct(time, tz = "America/Chicago"),
|
time = as.POSIXct(time, tz = "America/Chicago"),
|
||||||
status = ifelse(value > 1, "on", "off")) %>%
|
status = ifelse(value > power_threshhold, "on", "off"))
|
||||||
mutate(end_time = time + minutes(1))
|
|
||||||
|
|
||||||
washer_last_on <- values %>% filter(entity_id == entities$entity_id[1], value > 5) %>% tail(1) %>% pull(time)
|
values_by_entity <- as.list(NULL)
|
||||||
washer_last_off <- values %>% filter(entity_id == entities$entity_id[1], value < 5) %>% tail(1) %>% pull(time)
|
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)
|
washer_last_on <- values %>% filter(entity_id == entities$entity_id[1], value > power_threshhold) %>% tail(1) %>% pull(time)
|
||||||
dryer_last_off <- values %>% filter(entity_id == entities$entity_id[2], value < 5) %>% 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
|
# ---- generate html
|
||||||
current_status <- as.list(NULL)
|
current_status <- as.list(NULL)
|
||||||
for (entity in entities$entity_id){
|
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) +
|
plot_1week <- ggplot(data = values) +
|
||||||
geom_rect(aes(xmin = end_time,
|
geom_tile(aes(x = time + seconds(round(as.numeric(difftime(end_time, time, unit = "secs")))/2),
|
||||||
xmax = time,
|
y = entity_id,
|
||||||
ymin = ifelse(entity_id == "lamp_a_power", 0, 2) + 0.5,
|
width = seconds(round(as.numeric(difftime(end_time, time, unit = "secs")))),
|
||||||
ymax = ifelse(entity_id == "lamp_a_power", 0, 2) + 1.5,
|
height = 0.5,
|
||||||
fill = status)) +
|
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") +
|
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)) +
|
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))) +
|
plot_1day <- ggplot(data = values %>% filter(time >= max(values$time) - hours(24))) +
|
||||||
geom_rect(aes(xmin = end_time,
|
geom_tile(aes(x = time + seconds(round(as.numeric(difftime(end_time, time, unit = "secs")))/2),
|
||||||
xmax = time,
|
y = entity_id,
|
||||||
ymin = ifelse(entity_id == "lamp_a_power", 0, 2) + 0.5,
|
width = seconds(round(as.numeric(difftime(end_time, time, unit = "secs")))),
|
||||||
ymax = ifelse(entity_id == "lamp_a_power", 0, 2) + 1.5,
|
height = 0.5,
|
||||||
fill = status)) +
|
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") +
|
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)) +
|
theme(axis.text.x = element_text(angle = 30, vjust = 0.5)) +
|
||||||
labs(title = "Last 24 hours")
|
labs(title = "Last 24 hours")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user