Send monthly reports

This commit is contained in:
Aart van Halteren
2023-02-21 22:43:22 +01:00
parent ccb8589a4b
commit 534e8dc294
4 changed files with 182 additions and 12 deletions

View File

@@ -26,10 +26,12 @@ class ReadingsMailer
# Fetch today's usage
usage_today = Reading.diff_on(date)
consumption_today, production_today = Reading.consumed_and_produced_for_diff(usage_today)
net_consumption_high, net_consumption_low = Reading.net_consumed_high_and_low_for_diff(usage_today)
# Calculate costs for oxxio and easy energy
c = Cost.new
oxxio_cost = c.oxxio_energy_cost(date.to_s,usage_today[:total_kwh_consumed_high]-usage_today[:total_kwh_produced_high], usage_today[:total_kwh_consumed_low]-usage_today[:total_kwh_produced_low])
oxxio_normaal_cost, oxxio_dal_cost = c.oxxio_energy_cost(date.to_s,net_consumption_high,net_consumption_low)
oxxio_cost = oxxio_normaal_cost+oxxio_dal_cost
easy_cost = c.easy_energy_cost_barplot(date) # side effect: generates a PNG
# rounding
oxxio_cost = oxxio_cost.round(2)
@@ -55,8 +57,8 @@ class ReadingsMailer
html_part do
content_type 'text/html; charset=UTF-8'
body "<h1>Summary for #{date}</h1>" +
"<p>Total kWH electricity consumed: #{usage_today[:total_kwh_consumed_high] + usage_today[:total_kwh_consumed_low]}</p>" +
"<p>Total kWH electricity produced: #{usage_today[:total_kwh_produced_high] + usage_today[:total_kwh_produced_low]}</p>" +
"<p>Total kWH electricity consumed: #{consumption_today}</p>" +
"<p>Total kWH electricity produced: #{production_today}</p>" +
"<p>Total m3 gas consumed: #{usage_today[:total_m3_gas_consumed]}</p>" +
"</br>" +
"<p>kWH cost (Oxxio): <b>EUR #{ oxxio_cost} </b></p>" +
@@ -70,5 +72,75 @@ class ReadingsMailer
mail.deliver!
end
# default is current year
def deliver_for_month(month, year=nil)
return if (month <1 || month >12)
year = Date.today.year unless year
date = Date.parse("%s%.2d01" % [year,month])
date_str = date.strftime("%B %Y")
# Read SMTP options from smtp.yml
smtp_opts = YAML::load(File.open('config/smtp.yml')).symbolize_keys
smtp_opts.merge!(SSL_OPTS) if smtp_opts[:ssl] && smtp_opts[:ssl_verify_mode].eql?("none")
# Fetch today's usage
usage_month = Reading.diff_in_month(date)
consumption_month, production_month = Reading.consumed_and_produced_for_diff(usage_month)
net_consumption_normaal, net_consumption_dal = Reading.net_consumed_high_and_low_for_diff(usage_month)
# Calculate costs for oxxio and easy energy
c = Cost.new
vat = 1 + c.vat_at(date)
# cost with VAT, but without energiebelasting.
oxxio_raw_cost = c.oxxio_energy_cost(date.to_s,net_consumption_normaal, net_consumption_dal, false).sum.round(2)
easy_energy_raw_cost = c.easy_energy_raw_cost_in_month(date).compact.sum.round(2)
# cost with VAT and with energiebelasting
oxxio_cost = c.oxxio_energy_cost(date.to_s,net_consumption_normaal, net_consumption_dal).sum.round(2)
easy_energy_cost = c.easy_energy_cost_in_month(date).compact.sum.round(2)
# opslag Easy Energy
easy_energy_opslag = (c.easy_energy_rate(date.to_s)*vat*(net_consumption_normaal + net_consumption_dal)).round(2)
mail = Mail.new do
delivery_method :smtp, smtp_opts
to 'a.t.van.halteren@vu.nl'
from 'SmartMeter <aart@van-halteren.net>'
subject "SmartMeter Month report for #{date_str}"
text_part do
body "Summary for #{date_str}\n
-------------------------------\n\n
Total kWH electricity consumed: #{consumption_month}\n
Total kWH electricity produced: #{production_month}\n
Total m3 gas consumed: #{usage_month[:total_m3_gas_consumed]}\n\n
Levering kWH cost (Oxxio) : EUR #{ oxxio_raw_cost }\n
Levering kWH cost (EasyEnergy): EUR #{ easy_energy_raw_cost }\n
Total kWH cost (Oxxio) : EUR #{ oxxio_cost }\n
Total kWH cost (EasyEnergy): EUR #{ easy_energy_cost }, inclusief opslag van EUR #{ easy_energy_opslag }\n
"
end
html_part do
content_type 'text/html; charset=UTF-8'
body "<h1>Summary for #{date_str}</h1>" +
"<p>Total kWH electricity consumed: #{consumption_month}</p>" +
"<p>Total kWH electricity produced: #{production_month}</p>" +
"<p>Total m3 gas consumed: #{usage_month[:total_m3_gas_consumed]}</p>" +
"</br>" +
"<p>Levering kWH cost (Oxxio): <b>EUR #{ oxxio_raw_cost} </b></p>" +
"<p>Levering kWH cost (EasyEnergy): <b>EUR #{ easy_energy_raw_cost} </b></p>" +
"<p>Total kWH cost (Oxxio): <b>EUR #{ oxxio_cost} </b></p>" +
"<p>Total kWH cost (EasyEnergy): <b>EUR #{ easy_energy_cost} </b>, inclusief opslag van <b>EUR #{ easy_energy_opslag }</b></p>"
end
end
mail.deliver!
end
end
end