Skip to content

Instantly share code, notes, and snippets.

@kaihansen79
Created May 11, 2013 21:41
Show Gist options
  • Save kaihansen79/5561520 to your computer and use it in GitHub Desktop.
Save kaihansen79/5561520 to your computer and use it in GitHub Desktop.
if (((time.time() - sensorhistory.fiveminutetimer) >= 60.0)
and (currminute % 2 == 0)
):
wattsused = {}
whused = {}
# Taking avgwattover5min from ALL or only one sensor => erronious KWh
for history in sensorhistories.sensorhistories:
wattsused[history.sensornum] += history.avgwattover5min()
whused[history.sensornum]+= history.dayswatthr
kwhused1 = whused[1]/1000
avgwatt1 = sensorhistory.avgwattover5min()
cost1 = kwhused1 * ENERGY_PRICE
cost1 = "%.2f" % cost1
kwhused2 = whused[2]/1000
avgwatt2 = sensorhistory.avgwattover5min()
cost2 = kwhused2 * ENERGY_PRICE
cost2 = "%.2f" % cost2
kwhused3 = whused[3]/1000
avgwatt3 = sensorhistory.avgwattover5min()
cost3 = kwhused3 * ENERGY_PRICE
cost3 = "%.2f" % cost3
pac = eeml.Pachube(API_URL, API_KEY)
pac.update(eeml.Data(0,
avgwatt1,
minValue=0,
maxValue=None,
unit=eeml.Unit(name='watt',
type='derivedSI',
symbol='W',
)
)
)
# total KWh
pac.update(eeml.Data(1,
kwhused1,
minValue=0,
maxValue=None,
unit=eeml.Unit(name='kilowatthour',
type='derivedSI',
symbol='KWh',
)
)
)
# Cost
pac.update(eeml.Data(2,
cost1,
minValue=0,
maxValue=None,
unit=eeml.Unit(name='cost',
type='contextDependentUnits',
symbol='$'
)
)
)
pac.update(eeml.Data(3,
avgwatt2,
minValue=0,
maxValue=None,
unit=eeml.Unit(name='watt',
type='derivedSI',
symbol='W',
)
)
)
# total KWh
pac.update(eeml.Data(4,
kwhused2,
minValue=0,
maxValue=None,
unit=eeml.Unit(name='kilowatthour',
type='derivedSI',
symbol='KWh',
)
)
)
# Cost
pac.update(eeml.Data(5,
cost2,
minValue=0,
maxValue=None,
unit=eeml.Unit(name='cost',
type='contextDependentUnits',
symbol='$'
)
)
)
pac.update(eeml.Data(6,
avgwatt3,
minValue=0,
maxValue=None,
unit=eeml.Unit(name='watt',
type='derivedSI',
symbol='W',
)
)
)
# total KWh
pac.update(eeml.Data(7,
kwhused3,
minValue=0,
maxValue=None,
unit=eeml.Unit(name='kilowatthour',
type='derivedSI',
symbol='KWh',
)
)
)
# Cost
pac.update(eeml.Data(8,
cost3,
minValue=0,
maxValue=None,
unit=eeml.Unit(name='cost',
type='contextDependentUnits',
symbol='$'
)
)
)
try:
print "pachube update: try"
pac.put()
print "pachube update: OK"
except:
print "pachube update failed"
# Print out debug data, Wh used in last 5 minutes
avgwattsused = sensorhistory.avgwattover5min()
print time.strftime("%Y %m %d, %H:%M")+", "+str(sensorhistory.sensornum)+", "+str(sensorhistory.avgwattover5min())+"\n"
# Lets log it! Seek to the end of our log file
if logfile:
logfile.seek(0, 2) # 2 == SEEK_END. ie, go to the end of the file
logfile.write(time.strftime("%Y %m %d, %H:%M")+", "+
str(sensorhistory.sensornum)+", "+
str(sensorhistory.avgwattover5min())+"\n")
logfile.flush()
# Or, send it to the app engine
if not LOGFILENAME:
appengineauth.sendreport(xb.address_16, avgwattsused)
# Reset our 5 minute timer
sensorhistory.reset5mintimer()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment