added R Markdown document

This commit is contained in:
Ben Varick 2023-11-06 16:18:30 -06:00
parent 40192f3814
commit 329af7d7be
Signed by: ben
SSH Key Fingerprint: SHA256:jWnpFDAcacYM5aPFpYRqlsamlDyKNpSj3jj+k4ojtUo
4 changed files with 505 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
data/*
figures/*

57
laundry_status.R Normal file
View File

@ -0,0 +1,57 @@
#setup ----
library(tidyverse)
library(influxdbclient)
library(rmarkdown)
setwd("~/Documents/dataProjects/laundry_status")
# 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)
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 <- bind_rows(values)
values <- values %>%
rename(value = "_value")
values <- values %>%
mutate(status = ifelse(value > 1, "on", "off"),
end_time = c(values$time + minutes(1)))
# ---- set variables
entities <- data.frame(name = c("washing machine", "dryer"), entity_id = c("lamp_a_power", "lamp_b_power"))
# ---- 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(date_breaks = "4 hours", date_labels = '%I:%M %p', 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")

26
laundry_status.Rmd Normal file
View File

@ -0,0 +1,26 @@
---
title: "Laundry Status"
output: html_document
---
updated: `r format(Sys.time(), format = "%A %I:%M %p")`
## The washing machine is currently: `r current_status[["lamp_a_power"]]`
## The dryer is currently: `r current_status[["lamp_b_power"]]`
--------
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
This site tracks the washing machine and dryer of our building. It is just a suggestion of when the machines might be free, not a guarantee.
```{r plot_1day, echo=FALSE}
plot(plot_1day)
```
```{r plot_1week, echo=FALSE}
plot(plot_1week)
```
If you have any issues or questions, please email `admin@dendroalsia.net`

418
laundry_status.html Normal file

File diff suppressed because one or more lines are too long