added longterm data page
This commit is contained in:
		
							parent
							
								
									a64d577abb
								
							
						
					
					
						commit
						f650c7cb95
					
				
					 4 changed files with 498 additions and 7 deletions
				
			
		
							
								
								
									
										410
									
								
								html/longterm_data.html
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										410
									
								
								html/longterm_data.html
									
										
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							
							
								
								
									
										21
									
								
								laundry_longterm_data.Rmd
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								laundry_longterm_data.Rmd
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,21 @@ | ||||||
|  | --- | ||||||
|  | title: "Laundry Longterm Data" | ||||||
|  | output: | ||||||
|  |     html_document: | ||||||
|  |     includes: | ||||||
|  |        in_header: favicon_header.html | ||||||
|  | --- | ||||||
|  | 
 | ||||||
|  | updated: `r format(Sys.time(), format = "%A %I:%M %p", tz = "America/Chicago")` | ||||||
|  | 
 | ||||||
|  | this site updates every day | ||||||
|  | 
 | ||||||
|  | --- | ||||||
|  | 
 | ||||||
|  | ```{r plot_all_week_days, echo=FALSE} | ||||||
|  | plot(plot_all_week_days) | ||||||
|  | ``` | ||||||
|  | 
 | ||||||
|  | If you have any issues or questions, please email [ben@dendroalsia.net](mailto:ben@dendroalsia.net) | ||||||
|  | 
 | ||||||
|  | The code that I wrote to generate this website is available under the GPLv3 license here:  [https://git.dendroalsia.net/ben/laundry_status](https://git.dendroalsia.net/ben/laundry_status) | ||||||
|  | @ -18,7 +18,7 @@ home_assistant <- InfluxDBClient$new(url = "https://influxdb.dendroalsia.net", | ||||||
|                                      token = token, |                                      token = token, | ||||||
|                                      org = org) |                                      org = org) | ||||||
| update_interval <- 5 | update_interval <- 5 | ||||||
| cronjob_interval <- 60 | 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 | ||||||
|  | @ -135,15 +135,70 @@ update_data <- function(){ | ||||||
|    |    | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| # for(i in 1:(cronjob_interval/update_interval)){ | update_longterm_data <- function(){ | ||||||
| #   message(Sys.time()) |    | ||||||
| #   update_data() |   run_time <- Sys.time() | ||||||
| #   Sys.sleep(60*update_interval) |   start_time <- floor_date(run_time - ddays(365), unit = "days") | ||||||
| # } |   values <- home_assistant$query(paste0('from(bucket: "home_assistant") |> range(start: ',  | ||||||
|  |                                         start_time, | ||||||
|  |                                         ') |> filter(fn: (r) => r["entity_id"] == "washing_machine_power" or r["entity_id"] == "dryer_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_threshold_on, "on", "off")) %>% | ||||||
|  |     mutate(door_threshold = ifelse(entity_id == "washing_machine_power", power_threshold_wash_door, power_threshold_dry_door)) %>% | ||||||
|  |     mutate(door = ifelse(value < door_threshold, "open", "closed")) %>% | ||||||
|  |     filter(time >= start_time) | ||||||
|  |    | ||||||
|  |   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) | ||||||
|  |    | ||||||
|  |   values["weekday"] <- wday(values$time, label = TRUE) | ||||||
|  |   values["hourofday"] <- floor_date(ymd_hms(paste("2023_01_01",strftime(values$time, format="%H:%M:%S"))), unit = "hours") | ||||||
|  |   long_term <- values %>% | ||||||
|  |     filter(status =="on") %>% | ||||||
|  |     group_by(weekday, hourofday, entity_id, status) %>% | ||||||
|  |     summarise(count = n()) | ||||||
|  |     # ---- make plots | ||||||
|  |   plot_all_week_days <- ggplot(data = long_term) + | ||||||
|  |     geom_tile(aes(x = weekday, | ||||||
|  |                   y = hourofday, | ||||||
|  |                   alpha = count, | ||||||
|  |                   fill = status)) + | ||||||
|  |     facet_grid(entity_id ~ .) + | ||||||
|  |     scale_y_datetime(date_breaks = "4 hours", date_labels = '%I:%M %p',  minor_breaks = "2 hours", expand = expansion(mult = 0)) + | ||||||
|  |     scale_x_discrete() + | ||||||
|  |     scale_fill_manual(values = colors$color[2]) + | ||||||
|  |     theme(axis.text.x = element_text(angle = 30, vjust = 0.5)) + | ||||||
|  |     labs(title = "The past year", | ||||||
|  |          x = "Weekday", | ||||||
|  |          y = "Time of Day", | ||||||
|  |          fill = NULL) | ||||||
|  |    | ||||||
|  |   # ---- generate html | ||||||
|  |   render("laundry_longterm_data.Rmd", | ||||||
|  |          output_dir = "html", | ||||||
|  |          output_file = "longterm_data.html") | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
| 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) { | ||||||
|  |     update_longterm_data() | ||||||
|  |   } | ||||||
|  |   i <- i + 1 | ||||||
|   Sys.sleep(60*update_interval) |   Sys.sleep(60*update_interval) | ||||||
| } | } | ||||||
|  | @ -46,6 +46,11 @@ plot(plot_1day) | ||||||
| ```{r plot_1week_days, echo=FALSE} | ```{r plot_1week_days, echo=FALSE} | ||||||
| plot(plot_1week_days) | plot(plot_1week_days) | ||||||
| ``` | ``` | ||||||
|  | --- | ||||||
|  | For longterm data, click [here](https://laundry.dendroalsia.net/longterm_data.html) | ||||||
|  | --- | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
| If you have any issues or questions, please email [ben@dendroalsia.net](mailto:ben@dendroalsia.net) | If you have any issues or questions, please email [ben@dendroalsia.net](mailto:ben@dendroalsia.net) | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue