Energy consumption script

Posted on
Sun Jun 26, 2016 6:30 am
Asconasny offline
Posts: 161
Joined: Jan 16, 2015

Energy consumption script

Hi

I have a A/C unit connected to a Fibaro plug and i would like know the daily, yesterday, weekly ,monthly and if possible yearly energy consumption.
I belive a script would be the best option which would calculate and feed the numbers into variables so i could use it on a control page.

Anyone made something similar to this and want to share so i can get some pointers? :)

regards
Asconasny

Posted on
Sun Jun 26, 2016 7:22 am
kw123 offline
User avatar
Posts: 8360
Joined: May 12, 2013
Location: Dallas, TX

Re: Energy consumption script

Wrote this some time ago. works for my energy meters.

this is my input device and state. It uses the "accumulated energy" state
dEnergy = "MeterTotalLeftBox" # this is the device name from where we read the energy info
dEnergyState = "accumEnergyTotal" # this is the state name of the device we read

if your also delivers energy total you only need to change the devices name and state in the script and run it every 1/5/10/.. minutes (up to you how often probably 10 is fine)


Karl

Code: Select all
## if you reset your energy device at midnight, this must run AFTER the reset, call from a schedule at 0:01 + every x minutes
## calculates:
#   1. energy consumed yesterday
#   2. energy consumed today
#   3. energy consumption prediction for today, linear extrapolation from #2 and seconds since midnight
#


## name or id of your energy device name with "";  id just the number not quotes
dEnergy             = "MeterTotalLeftBox"    # this is the device name from where we read the energy info
dEnergyState         = "accumEnergyTotal"     # this is the state name of the device we read
##


## define your variable names here, the default should work fine
vEnergyYesterday      = "EnergyYesterday"        # this is the name of the variable that shows the KwH consumed yesterday
vEnergyToday          = "EnergyToday"          # this is the name of the variable that shows todays current engery consumed in KwH
vEnergyTodayPredicted    = "EnergyTodayPredicted" # this is the name of the variable that shows todays predicted total engery consumed in KwH
vEnergyDayNumber      = "EnergyDayNumber"      # this is the name of the variable that is used to keep track if we have calculated yesterdays consumption. it is the day in the month number
vEnergyMonth         = "EnergyMonth"          # this is the name of the variable that is used energy consumed this month
vEnergyDay            = "EnergyDay"            # this is the name of the variables that is used for Day_1, Day_2...
vEnergyHiddenN         = "EnergyHiddenN"        # this is the name of the variable that is used to keep track of hidden numbers


########### constants
daysInHours=4
daysInDays = 10

## import libraries
import simplejson as json
import datetime


# get dates and seconds
rightNow          = datetime.datetime.now()
secondsSinceMidnight = (rightNow.hour * 3600) + (rightNow.minute * 60) + rightNow.second
secondsInAday       = float(60*60*24)

## get the values and if the variables dont exits, create them
try:     xxx= indigo.variables[vEnergyYesterday]
except:  indigo.variable.create(vEnergyYesterday,"0")
##
try:    xxx = indigo.variables[vEnergyTodayPredicted]
except: indigo.variable.create(vEnergyTodayPredicted,"0")
##
try:    xxx = indigo.variables[vEnergyToday]
except: indigo.variable.create(vEnergyToday,"0")
##
try:    xxx = indigo.variables[vEnergyMonth]
except: indigo.variable.create(vEnergyMonth,"0")
##
try:    xxx = indigo.variables[vEnergyDay+"_1"]
except:
   indigo.variable.create(vEnergyDay+"_1","0")
   indigo.variable.create(vEnergyDay+"_2","0")
   indigo.variable.create(vEnergyDay+"_3","0")
   indigo.variable.create(vEnergyDay+"_4","0")
   indigo.variable.create(vEnergyDay+"_5","0")
   indigo.variable.create(vEnergyDay+"_6","0")
   indigo.variable.create(vEnergyDay+"_7","0")
   indigo.variable.create(vEnergyDay+"_8","0")
   indigo.variable.create(vEnergyDay+"_9","0")

try:     xxx= indigo.variables[vEnergyHiddenN]
except:   indigo.variable.create(vEnergyHiddenN,json.dumps({"theDay":-1, "dayV":[ [0,0,0] for i in range(daysInDays)],"theHour":-1, "hourV":[  [  [0,0,0] for k in range(24)  ] for i in range(daysInHours)] ,"monthRaw":0}))
####################                                 current day#          #ofM,maxV,rawV for daysInDays days    current hour#        #ofM,maxV,rawV  for 24 hours for 3 days day=0 = today   raw number from beginning of month


## read new energy
energyNow         = float(indigo.devices[dEnergy].states[dEnergyState]) #  this is the new raw number from the device


EnergyHiddenN = json.loads(indigo.variables[vEnergyHiddenN].value)


# now calculate


if EnergyHiddenN["theHour"] != rightNow.hour:
    EnergyHiddenN["theHour"] = rightNow.hour

    if EnergyHiddenN["theDay"] != rightNow.day:   # if this is a new day, update yesterdays value
        EnergyHiddenN["theDay"] = rightNow.day
        indigo.variable.updateValue(vEnergyYesterday   , ("%6.1f"%(energyNow - EnergyHiddenN["dayV"][0][2])).strip(" "))

        del EnergyHiddenN["dayV"][daysInDays-1]
        EnergyHiddenN["dayV"] =[[0,0,energyNow]]+EnergyHiddenN["dayV"]# copy the last raw value and reset number of M and measurement from yesterday

        for ii in range(1,daysInDays):
            indigo.variable.updateValue(vEnergyDay+"_"+str(ii), ("%6.1f"%(EnergyHiddenN["dayV"][ii][1])) )


        if energyNow < EnergyHiddenN["dayV"][1][2]: # need to reset?  works if reset happens at midnight, if it hapens during the day this is wrong, but you can only take care of some many variations ;-(
            EnergyHiddenN["dayV"][0][2]=0

        del EnergyHiddenN["hourV"][daysInHours-1]
        EnergyHiddenN["hourV"] =[[[0,0,0] for k in range(24)]]+EnergyHiddenN["hourV"]# copy the last raw value and reset number of M and measurement

        if rightNow.day ==1:
         EnergyHiddenN["monthRaw"] =  energyNow
    
    EnergyHiddenN["hourV"][0][rightNow.hour][2]= energyNow

       
EnergyHiddenN["dayV"][0][0]  +=1
EnergyHiddenN["dayV"][0][1]   = energyNow -  EnergyHiddenN["dayV"][0][2]

EnergyHiddenN["hourV"][0][rightNow.hour][0] +=1
EnergyHiddenN["hourV"][0][rightNow.hour][1]  = energyNow -  EnergyHiddenN["hourV"][0][rightNow.hour][2]

avHourLastdays=[0. for i in range(24)]
t=0
av=0
an=0
for i in range(24):
   n=0
   t=0.
   a=0
   if EnergyHiddenN["hourV"][1][i][0] >0:
      a += EnergyHiddenN["hourV"][1][i][1]
      n+=1
      av+=EnergyHiddenN["hourV"][1][i][1]
      an+=1
   if EnergyHiddenN["hourV"][2][i][0] >0:
      a += EnergyHiddenN["hourV"][2][i][1]
      n+=1
      av+=EnergyHiddenN["hourV"][2][i][1]
      an+=1
   avHourLastdays[i]= a/max(1.0,n)
   t+=a
total= max(t,1.)
average= av/max(1.,an)
w= total
for i in range(rightNow.hour):
   w+=EnergyHiddenN["hourV"][0][i][1] * average /(max(1.,avHourLastdays[i]) )

w+= EnergyHiddenN["hourV"][0][rightNow.hour][1]   *   ( 60./(max(1.,rightNow.minute)) )   *   ( average /(max(1.,avHourLastdays[rightNow.hour])) )
EnergyTodayPredicted =w* secondsSinceMidnight/(3600.*24)


#indigo.server.log("dayV      "+ json.dumps( EnergyHiddenN["dayV"][0] )  )

EnergyToday   = energyNow - EnergyHiddenN["dayV"][0][2]
EnergyMonth   = energyNow - EnergyHiddenN["monthRaw"]

indigo.variable.updateValue(vEnergyHiddenN, json.dumps(EnergyHiddenN))
 
#indigo.server.log("EnergyHiddenN         "+ json.dumps(EnergyHiddenN, indent=4))

#EnergyTodayPredicted    = EnergyToday * secondsInAday/max(secondsSinceMidnight,1.)
#EnergyTodayPredicted    = ((lastE)+ (EnergyToday-lastE)* 60./max(rightNow.second,1.)) *max( EH[0][23][1],  EH[0][[1])/(max( EH[1],1))

indigo.variable.updateValue(vEnergyToday,          ("%6.1f"%EnergyToday).strip(" "))
#indigo.variable.updateValue(vEnergyTodayPredicted, ("%6.1f"%EnergyTodayPredicted).strip(" "))


#indigo.server.log("rightNow             "+str(rightNow))
#indigo.server.log("rightNow.day               "+str(rightNow.day))
#indigo.server.log("secondsSinceMidnight "+str(secondsSinceMidnight))
#indigo.server.log("EnergyToday          "+str(EnergyToday))
#indigo.server.log("EnergyTodayPredicted "+str(EnergyTodayPredicted))

Posted on
Sun Jun 26, 2016 7:32 am
Asconasny offline
Posts: 161
Joined: Jan 16, 2015

Re: Energy consumption script

Sweet!
Will try it out asap :)
Thanks!

Regards
Asconasny

Posted on
Thu Jun 30, 2016 5:09 pm
kw123 offline
User avatar
Posts: 8360
Joined: May 12, 2013
Location: Dallas, TX

Re: Energy consumption script

you could also try this:
http://forums.indigodomo.com/viewtopic.php?f=188&t=15241

it creates variable with min/max/ave for
hour//lasthour/day/lastday/thisweek/lastweek/this month/lastmonth


you could use average and multiply with time, then you have your consumption.
eg ave consumption was 5kw for 1 week then multiply with 24*7 and you have kwh = 168*5 = 840kwh


Karl

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 4 guests