Description

Question is: when is optimal time fo using electricity produced by fotovoltaic system. Problem: outdated utility grid causing inverter shutdown. I use dataset which contain observations from 1.04.2022 to 31.03.2023. This observations have made every hour, throut year.

install.packages('tidyverse')
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.2     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
install.packages("ggpmisc")                         # Install & load ggpmisc package
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)
library(ggpmisc)
## Loading required package: ggpp
## 
## Attaching package: 'ggpp'
## 
## The following object is masked from 'package:ggplot2':
## 
##     annotate
## 
## Registered S3 method overwritten by 'ggpmisc':
##   method                  from   
##   as.character.polynomial polynom
install.packages("patchwork")                       # Install & load patchwork
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)
library(patchwork)

Dataset setup

fotovolt<-read_csv('energia2022_2023.csv', col_names=TRUE)
## Rows: 9480 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl  (3): In_kWh", ECO_kWh, Out_kWh
## dttm (1): Time
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
head(fotovolt,5)
## # A tibble: 5 × 4
##   Time                `In_kWh"` ECO_kWh Out_kWh
##   <dttm>                  <dbl>   <dbl>   <dbl>
## 1 2022-04-01 01:00:00      1.08       0    1.08
## 2 2022-04-01 02:00:00      0.94       0    0.94
## 3 2022-04-01 03:00:00      0.99       0    0.99
## 4 2022-04-01 04:00:00      1.08       0    1.08
## 5 2022-04-01 05:00:00      1.2        0    1.2
# There are mistakes in names of two columns: 2 and 4. It's my fault. Now I need to change this:

colnames(fotovolt)[2]<-'In_kWh'
colnames(fotovolt)[4]<-'total_kWh'
colnames(fotovolt)
## [1] "Time"      "In_kWh"    "ECO_kWh"   "total_kWh"
options(repr.plot.width =15, repr.plot.height =10)

glimpse(fotovolt)
## Rows: 9,480
## Columns: 4
## $ Time      <dttm> 2022-04-01 01:00:00, 2022-04-01 02:00:00, 2022-04-01 03:00:…
## $ In_kWh    <dbl> 1.08, 0.94, 0.99, 1.08, 1.20, 1.11, 1.06, 1.10, 1.17, 1.10, …
## $ ECO_kWh   <dbl> 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.08, …
## $ total_kWh <dbl> 1.08, 0.94, 0.99, 1.08, 1.20, 1.11, 1.06, 1.10, 1.17, 1.02, …
fotovolt_date<-fotovolt %>% 
  mutate(new_date=as.POSIXlt(Time))
# -----------------------------------------------------------------------
fotovolt_new_date<-fotovolt_date%>%
  mutate(year=new_date$year+1900, month=new_date$mon+1, week=strftime(Time,format="%W"), day=new_date$mday, hour=new_date$hour)%>%
  mutate(week=as.numeric(week)+1)
head(fotovolt_new_date)
## # A tibble: 6 × 10
##   Time                In_kWh ECO_kWh total_kWh new_date             year month
##   <dttm>               <dbl>   <dbl>     <dbl> <dttm>              <dbl> <dbl>
## 1 2022-04-01 01:00:00   1.08       0      1.08 2022-04-01 01:00:00  2022     4
## 2 2022-04-01 02:00:00   0.94       0      0.94 2022-04-01 02:00:00  2022     4
## 3 2022-04-01 03:00:00   0.99       0      0.99 2022-04-01 03:00:00  2022     4
## 4 2022-04-01 04:00:00   1.08       0      1.08 2022-04-01 04:00:00  2022     4
## 5 2022-04-01 05:00:00   1.2        0      1.2  2022-04-01 05:00:00  2022     4
## 6 2022-04-01 06:00:00   1.11       0      1.11 2022-04-01 06:00:00  2022     4
## # ℹ 3 more variables: week <dbl>, day <int>, hour <int>
glimpse(fotovolt_new_date)
## Rows: 9,480
## Columns: 10
## $ Time      <dttm> 2022-04-01 01:00:00, 2022-04-01 02:00:00, 2022-04-01 03:00:…
## $ In_kWh    <dbl> 1.08, 0.94, 0.99, 1.08, 1.20, 1.11, 1.06, 1.10, 1.17, 1.10, …
## $ ECO_kWh   <dbl> 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.08, …
## $ total_kWh <dbl> 1.08, 0.94, 0.99, 1.08, 1.20, 1.11, 1.06, 1.10, 1.17, 1.02, …
## $ new_date  <dttm> 2022-04-01 01:00:00, 2022-04-01 02:00:00, 2022-04-01 03:00:…
## $ year      <dbl> 2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022, …
## $ month     <dbl> 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, …
## $ week      <dbl> 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, …
## $ day       <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
## $ hour      <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 1…

Start analize

Yearly electricity demand

2022

cons_en_2022<-fotovolt_new_date%>%
  filter(year=='2022')%>%
  group_by(month)%>%
  summarize(total_ECO_kWh=sum(ECO_kWh), total_In_kWh=sum(In_kWh))%>%
  mutate(proc=round(total_ECO_kWh/total_In_kWh,3))

# Maximum,minimum and average electricity demand - year 2022
cons_en_2022_desc<-fotovolt_new_date%>%
  filter(year=='2022')%>%
  group_by(month)%>%
  summarize(ECO_kWh_max=max(ECO_kWh),
            ECO_kWh_avg=mean(ECO_kWh),
            ECO_kWh_median=median(ECO_kWh)
  )

Main value plot

Plot with table [https://CRAN.R-project.org/package=ggpmisc)

cons_en_2022_hist<-fotovolt_new_date%>%
  filter(year=='2022')

cons_en_2022_count_ECO<-fotovolt_new_date%>%
  filter(year=='2022')%>%
  group_by(ECO_kWh)%>%
  count(ECO_kWh)%>%
  mutate(proc_of=round(n/nrow(cons_en_2022_hist),3)*100)%>%
  arrange(desc(n))

head(cons_en_2022_count_ECO, 15)
## # A tibble: 15 × 3
## # Groups:   ECO_kWh [15]
##    ECO_kWh     n proc_of
##      <dbl> <int>   <dbl>
##  1    0     3762    57  
##  2    0.01   227     3.4
##  3    0.02   137     2.1
##  4    0.03    96     1.5
##  5    0.04    83     1.3
##  6    0.05    81     1.2
##  7    0.06    58     0.9
##  8    0.07    58     0.9
##  9    0.08    55     0.8
## 10    0.15    53     0.8
## 11    0.12    48     0.7
## 12    0.1     43     0.7
## 13    0.13    43     0.7
## 14    0.11    40     0.6
## 15    0.18    37     0.6
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

cons_en_2023<-fotovolt_new_date%>%
  filter(year=='2023')%>%
  filter(month=='1'|month=='2'|month=='3'|month=='4')%>%
  group_by(month)%>%
  summarize(total_ECO_kWh=sum(ECO_kWh), total_In_kWh=sum(In_kWh))%>%
  mutate(proc_ECO=round(total_ECO_kWh/total_In_kWh,3)*100)

Maximum,minimum and average electricity demand - year 2023

cons_en_2023_desc<-fotovolt_new_date%>%
  filter(year=='2023')%>%
  group_by(month)%>%
  summarize(ECO_kWh_max=max(ECO_kWh),
            ECO_kWh_avg=mean(ECO_kWh),
            ECO_kWh_median=median(ECO_kWh)
  )

head(cons_en_2023)
## # A tibble: 4 × 4
##   month total_ECO_kWh total_In_kWh proc_ECO
##   <dbl>         <dbl>        <dbl>    <dbl>
## 1     1          62.4         305.     20.5
## 2     2          67.3         289.     23.3
## 3     3         107.          370.     28.9
## 4     4         100.          282.     35.6

Only observ. with value 0 ECO_kWh

cons_en_2022_ECO_0<-fotovolt_new_date%>%
  filter(year=='2022') %>% 
  filter(ECO_kWh==0)
  
cons_en_2022_ECO_0_count<-fotovolt_new_date%>%
  filter(year=='2022') %>% 
  filter(ECO_kWh==0) %>% 
  group_by(month,hour) %>% 
  count(month,hour)


ggplot(cons_en_2022_ECO_0)+
  geom_bar(aes(hour))

cons_en_2022_ECO_0_h<-cons_en_2022_ECO_0 %>% 
  filter(hour>=6&hour<=21)

ggplot(cons_en_2022_ECO_0_h)+
  geom_bar(aes(hour))

cons_en_2022_ECO_0_mh<-cons_en_2022_ECO_0 %>% 
  filter(hour>=6&hour<=22) %>% 
  group_by(month,hour) %>% 
  count(month,hour)


ggplot(cons_en_2022_ECO_0_mh)+  #first method
  geom_col(aes(x=hour, y=n, color=hour))+
  facet_wrap(~month)

ggplot(cons_en_2022_ECO_0_mh, aes(x=hour, y=n, fill=factor(hour)))+  #second method
  geom_col(data=subset(cons_en_2022_ECO_0_mh, month>=4&month<=12))+
  facet_wrap(~month)

# ---------------------------------------------------------------

ECO electricity

2022, months: 4-12

2023, months: 1-3

“Black” electricity

“Black” vs “green” electricity

2023, months: 1-3

### 2022, months: 4-12

## Monthly electricity demand

cons_en_month<-fotovolt_new_date%>%
  filter(year=='2022')%>%
  group_by(month)%>%
  summarize(total_ECO_kWh=sum(ECO_kWh), total_In_kWh=sum(In_kWh))%>%
  mutate(proc_ECO=round(total_ECO_kWh/total_In_kWh,3)*100)

head(cons_en_month,10)
## # A tibble: 9 × 4
##   month total_ECO_kWh total_In_kWh proc_ECO
##   <dbl>         <dbl>        <dbl>    <dbl>
## 1     4         104.          595.     17.5
## 2     5         140.          154.     91  
## 3     6         180.          116.    155. 
## 4     7         141.          116.    122. 
## 5     8         169.          101.    167  
## 6     9         168.          169.     99.7
## 7    10         143.          266.     53.8
## 8    11          79.0         526.     15  
## 9    12          31.1         320.      9.7
summary(cons_en_month)
##      month    total_ECO_kWh     total_In_kWh      proc_ECO     
##  Min.   : 4   Min.   : 31.09   Min.   :101.1   Min.   :  9.70  
##  1st Qu.: 6   1st Qu.:104.31   1st Qu.:116.4   1st Qu.: 17.50  
##  Median : 8   Median :141.46   Median :169.0   Median : 91.00  
##  Mean   : 8   Mean   :128.52   Mean   :262.6   Mean   : 81.12  
##  3rd Qu.:10   3rd Qu.:168.42   3rd Qu.:320.2   3rd Qu.:121.70  
##  Max.   :12   Max.   :180.12   Max.   :594.6   Max.   :167.00

Weeks 2022

cons_en_weeks_2022<-fotovolt_new_date%>%
  filter(year=='2022')%>%
  group_by(week)%>%
  summarize(total_ECO_kWh=sum(ECO_kWh), total_In_kWh=sum(In_kWh))%>%
  mutate(proc_ECO=round(total_ECO_kWh/total_In_kWh,3)*100)
head(cons_en_weeks_2022, 10)
## # A tibble: 10 × 4
##     week total_ECO_kWh total_In_kWh proc_ECO
##    <dbl>         <dbl>        <dbl>    <dbl>
##  1    14          1.62         90.2      1.8
##  2    15         25.6         156.      16.4
##  3    16         22.3         163.      13.7
##  4    17         28.2         102.      27.6
##  5    18         31.6          91.0     34.7
##  6    19         32.5          59.4     54.7
##  7    20         36.1          24.7    146. 
##  8    21         26.9          31.4     85.7
##  9    22         30.5          23.0    133. 
## 10    23         33.7          31.0    108.
#-----------------------------------------------------------------
cons_en_weeks_2022_desc<-fotovolt_new_date%>%
  filter(year=='2022')%>%
  group_by(week)%>%
  summarize(ECO_kWh_max=max(ECO_kWh),
            ECO_kWh_avg=mean(ECO_kWh),
            ECO_kWh_median=median(ECO_kWh)
  )
#-----------------------------------------------------------------
head(cons_en_weeks_2022_desc, 10)
## # A tibble: 10 × 4
##     week ECO_kWh_max ECO_kWh_avg ECO_kWh_median
##    <dbl>       <dbl>       <dbl>          <dbl>
##  1    14        0.21      0.0228           0   
##  2    15        1.23      0.152            0   
##  3    16        1.23      0.132            0   
##  4    17        1.46      0.168            0.01
##  5    18        1.13      0.188            0.02
##  6    19        1.83      0.193            0.02
##  7    20        1.94      0.215            0.01
##  8    21        1.19      0.160            0.01
##  9    22        1.22      0.182            0.03
## 10    23        1.9       0.200            0.02

Weeks- 2023

cons_en_weeks_2023<-fotovolt_new_date%>%
  filter(year=='2023')%>%
  group_by(week)%>%
  summarize(total_ECO_kWh=sum(ECO_kWh), total_In_kWh=sum(In_kWh))%>%
  mutate(proc_ECO=round(total_ECO_kWh/total_In_kWh,3)*100)
head(cons_en_weeks_2023, 10)
## # A tibble: 10 × 4
##     week total_ECO_kWh total_In_kWh proc_ECO
##    <dbl>         <dbl>        <dbl>    <dbl>
##  1     1          4.2          5.87     71.6
##  2     2         23.4         60.8      38.5
##  3     3         19.2         53.4      36.1
##  4     4         11.3         75.9      14.8
##  5     5          3.23        87.8       3.7
##  6     6          5.81        76.2       7.6
##  7     7         28.9         78.3      36.9
##  8     8         11.9         72.0      16.5
##  9     9         17.2         64.4      26.7
## 10    10         15.5         86.8      17.9
#-----------------------------------------------------------------
cons_en_weeks_2023_desc<-fotovolt_new_date%>%
  filter(year=='2023')%>%
  group_by(week)%>%
  summarize(ECO_kWh_max=max(ECO_kWh),
            ECO_kWh_avg=mean(ECO_kWh),
            ECO_kWh_median=median(ECO_kWh),
            ECO_kWh_sd=sd(ECO_kWh)
  )


head(cons_en_weeks_2023_desc, 10)
## # A tibble: 10 × 5
##     week ECO_kWh_max ECO_kWh_avg ECO_kWh_median ECO_kWh_sd
##    <dbl>       <dbl>       <dbl>          <dbl>      <dbl>
##  1     1        1.4       0.175               0     0.401 
##  2     2        2.01      0.139               0     0.365 
##  3     3        2.17      0.115               0     0.344 
##  4     4        2.07      0.0671              0     0.290 
##  5     5        0.77      0.0192              0     0.0863
##  6     6        0.93      0.0346              0     0.111 
##  7     7        2.18      0.172               0     0.384 
##  8     8        1.43      0.0708              0     0.206 
##  9     9        0.86      0.102               0     0.182 
## 10    10        0.91      0.0924              0     0.208

#—————————————————————–

## Warning: Removed 1 row containing missing values (`geom_line()`).

Specific month - 7.2022

cons_en_month_7<-fotovolt_new_date%>%
  filter(year=='2022')%>%
  filter(month=='7')%>%
  group_by(day)%>%
  summarize(total_ECO_kWh=sum(ECO_kWh), total_In_kWh=sum(In_kWh))%>%
  mutate(proc_ECO=round(total_ECO_kWh/total_In_kWh,3)*100)

head(cons_en_month_7)
## # A tibble: 6 × 4
##     day total_ECO_kWh total_In_kWh proc_ECO
##   <int>         <dbl>        <dbl>    <dbl>
## 1     1          4.98         3.81    131. 
## 2     2          2.33         5.33     43.7
## 3     3          4.57         3.92    117. 
## 4     4          3.67         2.86    128. 
## 5     5          1.07         5.03     21.3
## 6     6          3.92         4.83     81.2
summary(cons_en_month_7)
##       day       total_ECO_kWh    total_In_kWh      proc_ECO     
##  Min.   : 1.0   Min.   :0.570   Min.   :1.460   Min.   : 12.40  
##  1st Qu.: 8.5   1st Qu.:3.480   1st Qu.:2.330   1st Qu.: 82.15  
##  Median :16.0   Median :4.570   Median :3.810   Median :122.00  
##  Mean   :16.0   Mean   :4.563   Mean   :3.749   Mean   :158.00  
##  3rd Qu.:23.5   3rd Qu.:5.560   3rd Qu.:4.710   3rd Qu.:210.05  
##  Max.   :31.0   Max.   :8.260   Max.   :9.200   Max.   :521.20

Checking hours - month 7.2022

cons_en_month_7_h<-fotovolt_new_date%>%
  filter(year=='2022')%>%
  filter(month=='7')%>%
  group_by(hour)%>%
  summarize(total_h_ECO_kWh=sum(ECO_kWh), total_h_In_kWh=sum(In_kWh))%>%
  mutate(proc_ECO=round(total_h_ECO_kWh/total_h_In_kWh,3)*100)

head(cons_en_month_7_h,24)
## # A tibble: 24 × 4
##     hour total_h_ECO_kWh total_h_In_kWh proc_ECO
##    <int>           <dbl>          <dbl>    <dbl>
##  1     0            0              4.64      0  
##  2     1            0              4.39      0  
##  3     2            0              3.83      0  
##  4     3            0              3.72      0  
##  5     4            0              4.09      0  
##  6     5            0              4.47      0  
##  7     6            0.02           4.98      0.4
##  8     7            1.1            3.91     28.1
##  9     8            4.87           7.16     68  
## 10     9           16.9            3.04    556. 
## # ℹ 14 more rows

cons_en_month_7_h_5_21<-cons_en_month_7_h%>%
  filter(hour>7&hour<21)

head(cons_en_month_7_h_5_21,20)
## # A tibble: 13 × 4
##     hour total_h_ECO_kWh total_h_In_kWh proc_ECO
##    <int>           <dbl>          <dbl>    <dbl>
##  1     8            4.87           7.16     68  
##  2     9           16.9            3.04    556. 
##  3    10           21.6            2.44    887. 
##  4    11           13.9            4.08    340. 
##  5    12           12.0            4.09    292. 
##  6    13           11.7            5.46    215. 
##  7    14           13.3            5.68    234. 
##  8    15           14.2            5.36    264. 
##  9    16           13.4            4.89    273. 
## 10    17            8.64           5.75    150. 
## 11    18            7.18           5.07    142. 
## 12    19            2.16           3.94     54.8
## 13    20            0.57           4.53     12.6

ECO -