edited recent times
This commit is contained in:
parent
aaaa55186c
commit
6284a05f47
776
.Rhistory
776
.Rhistory
@ -1,68 +1,26 @@
|
||||
values <- values %>%
|
||||
mutate(
|
||||
time = as.POSIXct(time, tz = "America/Chicago"),
|
||||
status = ifelse(value > 1, "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)
|
||||
washer_last_off <- values %>% filter(entity_id == entities$entity_id[1], value < 5) %>% 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")
|
||||
}
|
||||
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,
|
||||
fill = status)) +
|
||||
scale_y_continuous() +
|
||||
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")
|
||||
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,
|
||||
View(values)
|
||||
ggplot(data = values) +
|
||||
geom_tile(aes(x = time,
|
||||
y = entity_id,
|
||||
fill = status))
|
||||
ggplot(data = values) +
|
||||
geom_tile(aes(x = time,
|
||||
y = entity_id,
|
||||
width = 1,
|
||||
height = 1
|
||||
fill = status)) +
|
||||
scale_y_continuous() +
|
||||
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") +
|
||||
ggplot(data = values) +
|
||||
geom_tile(aes(x = time,
|
||||
y = entity_id,
|
||||
width = 1,
|
||||
height = 1,
|
||||
fill = status)) +
|
||||
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 24 hours")
|
||||
render("laundry_status.Rmd")
|
||||
}
|
||||
for(i in 1:8640){
|
||||
update_data()
|
||||
Sys.sleep(60*update_interval)
|
||||
}
|
||||
difftime(Sys.time(), washer_last_on, units = "mins")
|
||||
difftime(Sys.time(), washer_last_off, units = "mins")
|
||||
difftime(Sys.time(), washer_last_off)
|
||||
difftime(Sys.time(), washer_last_on, units = "mins")
|
||||
difftime(Sys.time(), washer_last_on)
|
||||
#setup ----
|
||||
library(tidyverse)
|
||||
library(influxdbclient)
|
||||
library(rmarkdown)
|
||||
if(test) {
|
||||
setwd("~/Documents/dataProjects/laundry_status")
|
||||
} else {
|
||||
setwd("/laundry_status")
|
||||
}
|
||||
Sys.setenv(TZ='America/Chicago')
|
||||
# parameters needed to make connection to Database
|
||||
token <- substr(read_file("data/api_key"), 1, 88)
|
||||
org = "home_assistant"
|
||||
bucket = "home_assistant"
|
||||
## make connection to the influxDB bucket
|
||||
home_assistant <- InfluxDBClient$new(url = "https://influxdb.dendroalsia.net",
|
||||
token = token,
|
||||
org = org)
|
||||
update_interval <- 5
|
||||
# ---- set variables
|
||||
entities <- data.frame(name = c("washing machine", "dryer"), entity_id = c("lamp_a_power", "lamp_b_power"))
|
||||
update_data <- function(){
|
||||
labs(title = "The past week")
|
||||
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)
|
||||
@ -73,62 +31,7 @@ values <- values %>%
|
||||
mutate(
|
||||
time = as.POSIXct(time, tz = "America/Chicago"),
|
||||
status = ifelse(value > 1, "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)
|
||||
washer_last_off <- values %>% filter(entity_id == entities$entity_id[1], value < 5) %>% 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")
|
||||
}
|
||||
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,
|
||||
fill = status)) +
|
||||
scale_y_continuous() +
|
||||
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")
|
||||
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,
|
||||
fill = status)) +
|
||||
scale_y_continuous() +
|
||||
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")
|
||||
render("laundry_status.Rmd")
|
||||
}
|
||||
for(i in 1:8640){
|
||||
update_data()
|
||||
Sys.sleep(60*update_interval)
|
||||
}
|
||||
#setup ----
|
||||
library(tidyverse)
|
||||
library(influxdbclient)
|
||||
library(rmarkdown)
|
||||
if(test) {
|
||||
setwd("~/Documents/dataProjects/laundry_status")
|
||||
} else {
|
||||
setwd("/laundry_status")
|
||||
}
|
||||
Sys.setenv(TZ='America/Chicago')
|
||||
# parameters needed to make connection to Database
|
||||
token <- substr(read_file("data/api_key"), 1, 88)
|
||||
org = "home_assistant"
|
||||
bucket = "home_assistant"
|
||||
## make connection to the influxDB bucket
|
||||
home_assistant <- InfluxDBClient$new(url = "https://influxdb.dendroalsia.net",
|
||||
token = token,
|
||||
org = org)
|
||||
update_interval <- 5
|
||||
# ---- set variables
|
||||
entities <- data.frame(name = c("washing machine", "dryer"), entity_id = c("lamp_a_power", "lamp_b_power"))
|
||||
update_data <- function(){
|
||||
mutate(end_time = values$time[-1])
|
||||
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)
|
||||
@ -139,62 +42,8 @@ values <- values %>%
|
||||
mutate(
|
||||
time = as.POSIXct(time, tz = "America/Chicago"),
|
||||
status = ifelse(value > 1, "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)
|
||||
washer_last_off <- values %>% filter(entity_id == entities$entity_id[1], value < 5) %>% 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")
|
||||
}
|
||||
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,
|
||||
fill = status)) +
|
||||
scale_y_continuous() +
|
||||
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")
|
||||
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,
|
||||
fill = status)) +
|
||||
scale_y_continuous() +
|
||||
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")
|
||||
render("laundry_status.Rmd")
|
||||
}
|
||||
for(i in 1:8640){
|
||||
update_data()
|
||||
Sys.sleep(60*update_interval)
|
||||
}
|
||||
#setup ----
|
||||
library(tidyverse)
|
||||
library(influxdbclient)
|
||||
library(rmarkdown)
|
||||
if(test) {
|
||||
setwd("~/Documents/dataProjects/laundry_status")
|
||||
} else {
|
||||
setwd("/laundry_status")
|
||||
}
|
||||
Sys.setenv(TZ='America/Chicago')
|
||||
# parameters needed to make connection to Database
|
||||
token <- substr(read_file("data/api_key"), 1, 88)
|
||||
org = "home_assistant"
|
||||
bucket = "home_assistant"
|
||||
## make connection to the influxDB bucket
|
||||
home_assistant <- InfluxDBClient$new(url = "https://influxdb.dendroalsia.net",
|
||||
token = token,
|
||||
org = org)
|
||||
update_interval <- 5
|
||||
# ---- set variables
|
||||
entities <- data.frame(name = c("washing machine", "dryer"), entity_id = c("lamp_a_power", "lamp_b_power"))
|
||||
update_data <- function(){
|
||||
mutate(end_time = c(values$time[-1], NA))
|
||||
View(values)
|
||||
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)
|
||||
@ -205,62 +54,8 @@ values <- values %>%
|
||||
mutate(
|
||||
time = as.POSIXct(time, tz = "America/Chicago"),
|
||||
status = ifelse(value > 1, "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)
|
||||
washer_last_off <- values %>% filter(entity_id == entities$entity_id[1], value < 5) %>% 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")
|
||||
}
|
||||
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,
|
||||
fill = status)) +
|
||||
scale_y_continuous() +
|
||||
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")
|
||||
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,
|
||||
fill = status)) +
|
||||
scale_y_continuous() +
|
||||
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")
|
||||
render("laundry_status.Rmd")
|
||||
}
|
||||
for(i in 1:8640){
|
||||
update_data()
|
||||
Sys.sleep(60*update_interval)
|
||||
}
|
||||
#setup ----
|
||||
library(tidyverse)
|
||||
library(influxdbclient)
|
||||
library(rmarkdown)
|
||||
if(test) {
|
||||
setwd("~/Documents/dataProjects/laundry_status")
|
||||
} else {
|
||||
setwd("/laundry_status")
|
||||
}
|
||||
Sys.setenv(TZ='America/Chicago')
|
||||
# parameters needed to make connection to Database
|
||||
token <- substr(read_file("data/api_key"), 1, 88)
|
||||
org = "home_assistant"
|
||||
bucket = "home_assistant"
|
||||
## make connection to the influxDB bucket
|
||||
home_assistant <- InfluxDBClient$new(url = "https://influxdb.dendroalsia.net",
|
||||
token = token,
|
||||
org = org)
|
||||
update_interval <- 5
|
||||
# ---- set variables
|
||||
entities <- data.frame(name = c("washing machine", "dryer"), entity_id = c("lamp_a_power", "lamp_b_power"))
|
||||
update_data <- function(){
|
||||
mutate(end_time = c(values$time[-1], Sys.time()))
|
||||
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)
|
||||
@ -271,62 +66,20 @@ values <- values %>%
|
||||
mutate(
|
||||
time = as.POSIXct(time, tz = "America/Chicago"),
|
||||
status = ifelse(value > 1, "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)
|
||||
washer_last_off <- values %>% filter(entity_id == entities$entity_id[1], value < 5) %>% 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")
|
||||
}
|
||||
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,
|
||||
fill = status)) +
|
||||
scale_y_continuous() +
|
||||
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")
|
||||
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,
|
||||
fill = status)) +
|
||||
scale_y_continuous() +
|
||||
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")
|
||||
render("laundry_status.Rmd")
|
||||
}
|
||||
for(i in 1:8640){
|
||||
update_data()
|
||||
Sys.sleep(60*update_interval)
|
||||
}
|
||||
#setup ----
|
||||
library(tidyverse)
|
||||
library(influxdbclient)
|
||||
library(rmarkdown)
|
||||
if(test) {
|
||||
setwd("~/Documents/dataProjects/laundry_status")
|
||||
} else {
|
||||
setwd("/laundry_status")
|
||||
}
|
||||
Sys.setenv(TZ='America/Chicago')
|
||||
# parameters needed to make connection to Database
|
||||
token <- substr(read_file("data/api_key"), 1, 88)
|
||||
org = "home_assistant"
|
||||
bucket = "home_assistant"
|
||||
## make connection to the influxDB bucket
|
||||
home_assistant <- InfluxDBClient$new(url = "https://influxdb.dendroalsia.net",
|
||||
token = token,
|
||||
org = org)
|
||||
update_interval <- 5
|
||||
# ---- set variables
|
||||
entities <- data.frame(name = c("washing machine", "dryer"), entity_id = c("lamp_a_power", "lamp_b_power"))
|
||||
update_data <- function(){
|
||||
mutate(end_time = c(values$time[-1], run_time))
|
||||
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)
|
||||
values <- values %>%
|
||||
rename(value = "_value",
|
||||
time = "_time")
|
||||
values <- values %>%
|
||||
mutate(
|
||||
time = as.POSIXct(time, tz = "America/Chicago"),
|
||||
status = ifelse(value > 1, "on", "off"))
|
||||
c(values$time[-1], run_time)
|
||||
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)
|
||||
@ -337,62 +90,8 @@ values <- values %>%
|
||||
mutate(
|
||||
time = as.POSIXct(time, tz = "America/Chicago"),
|
||||
status = ifelse(value > 1, "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)
|
||||
washer_last_off <- values %>% filter(entity_id == entities$entity_id[1], value < 5) %>% 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")
|
||||
}
|
||||
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,
|
||||
fill = status)) +
|
||||
scale_y_continuous() +
|
||||
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")
|
||||
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,
|
||||
fill = status)) +
|
||||
scale_y_continuous() +
|
||||
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")
|
||||
render("laundry_status.Rmd")
|
||||
}
|
||||
for(i in 1:8640){
|
||||
update_data()
|
||||
Sys.sleep(60*update_interval)
|
||||
}
|
||||
#setup ----
|
||||
library(tidyverse)
|
||||
library(influxdbclient)
|
||||
library(rmarkdown)
|
||||
if(test) {
|
||||
setwd("~/Documents/dataProjects/laundry_status")
|
||||
} else {
|
||||
setwd("/laundry_status")
|
||||
}
|
||||
Sys.setenv(TZ='America/Chicago')
|
||||
# parameters needed to make connection to Database
|
||||
token <- substr(read_file("data/api_key"), 1, 88)
|
||||
org = "home_assistant"
|
||||
bucket = "home_assistant"
|
||||
## make connection to the influxDB bucket
|
||||
home_assistant <- InfluxDBClient$new(url = "https://influxdb.dendroalsia.net",
|
||||
token = token,
|
||||
org = org)
|
||||
update_interval <- 5
|
||||
# ---- set variables
|
||||
entities <- data.frame(name = c("washing machine", "dryer"), entity_id = c("lamp_a_power", "lamp_b_power"))
|
||||
update_data <- function(){
|
||||
mutate(end_time = c(values$time[-1], run_time))
|
||||
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)
|
||||
@ -403,45 +102,241 @@ values <- values %>%
|
||||
mutate(
|
||||
time = as.POSIXct(time, tz = "America/Chicago"),
|
||||
status = ifelse(value > 1, "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)
|
||||
washer_last_off <- values %>% filter(entity_id == entities$entity_id[1], value < 5) %>% tail(1) %>% pull(time)
|
||||
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)
|
||||
mutate(end_time = c(values$time[-1], as.POSIXct(run_time)))
|
||||
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)
|
||||
values <- values %>%
|
||||
rename(value = "_value",
|
||||
time = "_time")
|
||||
values <- values %>%
|
||||
mutate(
|
||||
time = as.POSIXct(time, tz = "America/Chicago"),
|
||||
status = ifelse(value > power_threshhold, "on", "off")) %>%
|
||||
mutate(end_time = c(values$time[-1], c(run_time)))
|
||||
power_threshhold <- 5
|
||||
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)
|
||||
values <- values %>%
|
||||
rename(value = "_value",
|
||||
time = "_time")
|
||||
values <- values %>%
|
||||
mutate(
|
||||
time = as.POSIXct(time, tz = "America/Chicago"),
|
||||
status = ifelse(value > power_threshhold, "on", "off")) %>%
|
||||
mutate(end_time = c(values$time[-1], c(run_time)))
|
||||
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)
|
||||
values <- values %>%
|
||||
rename(value = "_value",
|
||||
time = "_time")
|
||||
values <- values %>%
|
||||
mutate(
|
||||
time = as.POSIXct(time, tz = "America/Chicago"),
|
||||
status = ifelse(value > power_threshhold, "on", "off"))
|
||||
values <- values %>%
|
||||
mutate(end_time = c(values$time[-1], run_time))
|
||||
ggplot(data = values) +
|
||||
geom_tile(aes(x = time + minutes(as.numerica(difftime(time, endtime, unit = "mins")))/2,
|
||||
y = entity_id,
|
||||
width = minutes(as.numerica(difftime(time, endtime, unit = "mins"))),
|
||||
height = 0.5,
|
||||
fill = status)) +
|
||||
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 = "The past week")
|
||||
ggplot(data = values) +
|
||||
geom_tile(aes(x = time + minutes(as.numeric(difftime(time, endtime, unit = "mins")))/2,
|
||||
y = entity_id,
|
||||
width = minutes(as.numeric(difftime(time, endtime, unit = "mins"))),
|
||||
height = 0.5,
|
||||
fill = status)) +
|
||||
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 = "The past week")
|
||||
ggplot(data = values) +
|
||||
geom_tile(aes(x = time + minutes(as.numeric(difftime(time, end_time, unit = "mins")))/2,
|
||||
y = entity_id,
|
||||
width = minutes(as.numeric(difftime(time, end_time, unit = "mins"))),
|
||||
height = 0.5,
|
||||
fill = status)) +
|
||||
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 = "The past week")
|
||||
ggplot(data = values) +
|
||||
geom_tile(aes(x = time + minutes(as.numeric(difftime(time, end_time, unit = "mins"))/2),
|
||||
y = entity_id,
|
||||
width = minutes(as.numeric(difftime(time, end_time, unit = "mins"))),
|
||||
height = 0.5,
|
||||
fill = status)) +
|
||||
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 = "The past week")
|
||||
values$time + minutes(as.numeric(difftime(values$time, values$end_time, unit = "mins"))/2)
|
||||
difftime(values$time, values$end_time, unit = "mins")
|
||||
as.numeric(difftime(values$time, values$end_time, unit = "mins"))
|
||||
ggplot(data = values) +
|
||||
geom_tile(aes(x = time + minutes(as.numeric(difftime(end_time, time, unit = "mins"))/2),
|
||||
y = entity_id,
|
||||
width = minutes(as.numeric(difftime(end_time, time, unit = "mins"))),
|
||||
height = 0.5,
|
||||
fill = status)) +
|
||||
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 = "The past week")
|
||||
ggplot(data = values) +
|
||||
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_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 = "The past week")
|
||||
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)
|
||||
values <- values %>%
|
||||
rename(value = "_value",
|
||||
time = "_time")
|
||||
values <- values %>%
|
||||
mutate(
|
||||
time = as.POSIXct(time, tz = "America/Chicago"),
|
||||
status = ifelse(value > power_threshhold, "on", "off"))
|
||||
values <- values %>%
|
||||
group_by(entity_id) %>%
|
||||
arrange(time) %>%
|
||||
mutate(end_time = c(values$time[-1], run_time))
|
||||
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)
|
||||
values <- values %>%
|
||||
rename(value = "_value",
|
||||
time = "_time")
|
||||
values <- values %>%
|
||||
mutate(
|
||||
time = as.POSIXct(time, tz = "America/Chicago"),
|
||||
status = ifelse(value > power_threshhold, "on", "off"))
|
||||
values <- values %>%
|
||||
group_by(entity_id) %>%
|
||||
mutate(end_time = c(values$time[-1], run_time))
|
||||
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)
|
||||
values <- values %>%
|
||||
rename(value = "_value",
|
||||
time = "_time")
|
||||
values <- values %>%
|
||||
mutate(
|
||||
time = as.POSIXct(time, tz = "America/Chicago"),
|
||||
status = ifelse(value > power_threshhold, "on", "off"))
|
||||
for(entity in entities$entity_id) {
|
||||
values_by_entity[[entity]] <- values %>%
|
||||
filter(entity_id %in% entity) %>%
|
||||
mutate(end_time = c(values$time[-1], run_time))
|
||||
}
|
||||
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)
|
||||
values <- values %>%
|
||||
rename(value = "_value",
|
||||
time = "_time")
|
||||
values <- values %>%
|
||||
mutate(
|
||||
time = as.POSIXct(time, tz = "America/Chicago"),
|
||||
status = ifelse(value > power_threshhold, "on", "off"))
|
||||
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_by_entity[[entity]] <- values %>%
|
||||
filter(entity_id %in% entity) %>%
|
||||
mutate(end_time = c(time[-1], run_time))
|
||||
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)
|
||||
values <- values %>%
|
||||
rename(value = "_value",
|
||||
time = "_time")
|
||||
values <- values %>%
|
||||
mutate(
|
||||
time = as.POSIXct(time, tz = "America/Chicago"),
|
||||
status = ifelse(value > power_threshhold, "on", "off"))
|
||||
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)
|
||||
View(values)
|
||||
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")
|
||||
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,
|
||||
labs(title = "The past week")
|
||||
ggplot(data = values) +
|
||||
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_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 = "The past week")
|
||||
ggplot(data = values %>% filter(time >= max(values$time) - hours(24))) +
|
||||
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_discrete(breaks = entities$entity_id, labels = entities$name) +
|
||||
scale_y_continuous() +
|
||||
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")
|
||||
render("laundry_status.Rmd")
|
||||
}
|
||||
for(i in 1:8640){
|
||||
update_data()
|
||||
Sys.sleep(60*update_interval)
|
||||
}
|
||||
Sys.info()
|
||||
Sys.info()[4]
|
||||
Sys.info()[4] == "pseudotsuga"
|
||||
ggplot(data = values %>% filter(time >= max(values$time) - hours(24))) +
|
||||
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_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")
|
||||
#setup ----
|
||||
library(tidyverse)
|
||||
library(influxdbclient)
|
||||
@ -461,9 +356,12 @@ home_assistant <- InfluxDBClient$new(url = "https://influxdb.dendroalsia.net",
|
||||
token = token,
|
||||
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)
|
||||
@ -473,40 +371,142 @@ time = "_time")
|
||||
values <- values %>%
|
||||
mutate(
|
||||
time = as.POSIXct(time, tz = "America/Chicago"),
|
||||
status = ifelse(value > 1, "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)
|
||||
washer_last_off <- values %>% filter(entity_id == entities$entity_id[1], value < 5) %>% tail(1) %>% pull(time)
|
||||
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)
|
||||
status = ifelse(value > power_threshhold, "on", "off"))
|
||||
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)
|
||||
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",
|
||||
x = NULL,
|
||||
y = NULL,
|
||||
fill = NULL)
|
||||
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")
|
||||
render("laundry_status.Rmd")
|
||||
labs(title = "Last 24 hours",
|
||||
x = NULL,
|
||||
y = NULL,
|
||||
fill = NULL)
|
||||
render("laundry_status.Rmd",
|
||||
output_dir = "html",
|
||||
output_file = "index.html")
|
||||
}
|
||||
for(i in 1:8640){
|
||||
for(i in 1:(cronjob_interval/update_interval)){
|
||||
message(Sys.time())
|
||||
update_data()
|
||||
Sys.sleep(60*update_interval)
|
||||
}
|
||||
round_date(max(values$time), "4 hours")
|
||||
#setup ----
|
||||
library(tidyverse)
|
||||
library(influxdbclient)
|
||||
library(rmarkdown)
|
||||
if(Sys.info()[4] == "pseudotsuga") {
|
||||
setwd("~/Documents/dataProjects/laundry_status")
|
||||
} else {
|
||||
setwd("/laundry_status")
|
||||
}
|
||||
Sys.setenv(TZ='America/Chicago')
|
||||
# parameters needed to make connection to Database
|
||||
token <- substr(read_file("data/api_key"), 1, 88)
|
||||
org = "home_assistant"
|
||||
bucket = "home_assistant"
|
||||
## make connection to the influxDB bucket
|
||||
home_assistant <- InfluxDBClient$new(url = "https://influxdb.dendroalsia.net",
|
||||
token = token,
|
||||
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)
|
||||
values <- values %>%
|
||||
rename(value = "_value",
|
||||
time = "_time")
|
||||
values <- values %>%
|
||||
mutate(
|
||||
time = as.POSIXct(time, tz = "America/Chicago"),
|
||||
status = ifelse(value > power_threshhold, "on", "off"))
|
||||
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)
|
||||
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) > power_threshhold, "on", "off")
|
||||
}
|
||||
plot_1week <- ggplot(data = values) +
|
||||
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_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 = "The past week",
|
||||
x = NULL,
|
||||
y = NULL,
|
||||
fill = NULL)
|
||||
plot_1day <- ggplot(data = values %>% filter(time >= max(values$end_time) - hours(24))) +
|
||||
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_discrete(breaks = entities$entity_id, labels = entities$name) +
|
||||
scale_x_datetime(breaks = seq(round_date(max(values$end_time), "4 hours") - hours(24), round_date(max(values$end_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",
|
||||
x = NULL,
|
||||
y = NULL,
|
||||
fill = NULL)
|
||||
render("laundry_status.Rmd",
|
||||
output_dir = "html",
|
||||
output_file = "index.html")
|
||||
}
|
||||
for(i in 1:(cronjob_interval/update_interval)){
|
||||
message(Sys.time())
|
||||
update_data()
|
||||
Sys.sleep(60*update_interval)
|
||||
}
|
||||
|
File diff suppressed because one or more lines are too long
@ -45,11 +45,11 @@ 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(time)
|
||||
washer_last_off <- values %>% filter(entity_id == entities$entity_id[1], value < power_threshhold) %>% tail(1) %>% pull(time)
|
||||
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)
|
||||
|
||||
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)
|
||||
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)
|
||||
# ---- generate html
|
||||
current_status <- as.list(NULL)
|
||||
for (entity in entities$entity_id){
|
||||
|
Loading…
x
Reference in New Issue
Block a user