Skip to content

Instantly share code, notes, and snippets.

@dmichael
Last active December 11, 2015 11:08
Show Gist options
  • Select an option

  • Save dmichael/4591756 to your computer and use it in GitHub Desktop.

Select an option

Save dmichael/4591756 to your computer and use it in GitHub Desktop.
# Thanks Seth Darlington!
price_per_kwh = 0.1248 # dollars
query = RealtimeMessage.this_month
previous = query.first
usage = query[1..query.count].reduce(0) do |sum, message|
elapsed_seconds = (message.created_at - previous.created_at)
elapsed_hours = elapsed_seconds/3600
kW = message.ch1_watts/1000.0
kWh = elapsed_hours * kW
previous = message
sum + kWh
end
cost = usage * price_per_kwh
elapsed = query.last.created_at - query.first.created_at
p "Usage: #{usage.round(4)} kWh @ $#{price_per_kwh} = $#{cost.round(2)}"
p "Accounting for #{(elapsed/86400).round(4)} days (#{(elapsed/3600).round(4)} hours) in #{Date::MONTHNAMES[Date.today.month]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment