Updated the script, basically working now.
Except: it triggers itself again and thus creating a zero value.
Code: Select all
function timedifference(s)
year = string.sub(s, 1, 4)
month = string.sub(s, 6, 7)
day = string.sub(s, 9, 10)
hour = string.sub(s, 12, 13)
minutes = string.sub(s, 15, 16)
seconds = string.sub(s, 18, 19)
t1 = os.time()
t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
difference = os.difftime (t1, t2)
return difference
end
function timeCount(numSec)
local nSeconds = numSec
if nSeconds == 0 then
return "0seconds"
else
local sTime = ""
local nHours = math.floor(nSeconds/3600)
local nMins = math.floor(nSeconds/60 - (nHours*60))
if(nHours > 0) then
sTime = sTime .. nHours
if(nHours == 1) then
sTime = sTime .. "hour "
else
sTime = sTime .. "hours "
end
end
if(nMins > 0) then
sTime = sTime .. nMins
if(nMins == 1) then
sTime = sTime .. "minute "
else
sTime = sTime .. "minutes "
end
end
local nSecs = math.floor(nSeconds - nHours*3600 - nMins *60)
if(nSecs > 0) then
sTime = sTime .. nSecs
if(nSecs == 1) then
sTime = sTime .. "second"
else
sTime = sTime .. "seconds"
end
end
return sTime
end
end
function update(device, id, power, energy, index)
if (index < 3) then
commandArray[index] = {['UpdateDevice'] = id .. "|0|" .. power .. ";" .. energy}
else
commandArray[index] = {['UpdateDevice'] = id .. "|0|" .. energy}
end
return
end
commandArray = {}
local houseid = 112
local usedid = 111
if devicechanged['elecmeter'] then
elec = otherdevices_svalues['elecmeter']
print('elecmeter : '..elec)
words = {}
for w in string.gmatch(elec, "%d+%.?%d*") do
words[#words + 1] = w
end
nhousePower, houseEnergy = string.match(otherdevices_svalues['elecmeter'], "(%d+%.*%d*);(%d+%.*%d*)")
updateuse = houseEnergy
usedEnergy = string.match(otherdevices_svalues['Household'], "%d+%.*%d*")
consumed = houseEnergy - usedEnergy
interval = timedifference(otherdevices_lastupdate['elec_switch'])
print ('houseEnergy : '..houseEnergy)
print ('usedEnergy : '..usedEnergy)
print ('diff : '..consumed)
print ('interval : '..interval)
rate = (consumed / interval) * 3600
print ('rate : '..rate)
commandArray['elec_switch'] = 'On'
update("Household", houseid, 0, updateuse, 3)
update("Imported", usedid, 0, rate, 4)
end
return commandArray
The log:
2015-12-22 18:34:54.509 LUA: elecmeter : 0.000;17348642.000
2015-12-22 18:34:54.510 LUA: houseEnergy : 17348642.000
2015-12-22 18:34:54.510 LUA: usedEnergy : 17348630.000
2015-12-22 18:34:54.510 LUA: diff : 12
2015-12-22 18:34:54.510 LUA: interval : 285
2015-12-22 18:34:54.510 LUA: rate : 151.57894736842
Until here quite ok, need some cleanup of the "rate" value.
But then it triggers again, while elecmeter has not been updated at this point and thus creating a zero difference between the readings and therefor calculating a Zero rate (--> calculate consumption per hour)
2015-12-22 18:34:54.514 EventSystem: Script event triggered: /home/pi/domoticz/scripts/lua/script_device_elecmeter_log.lua
2015-12-22 18:34:54.547 LUA: elecmeter : 0.000;17348642.000
2015-12-22 18:34:54.547 LUA: houseEnergy : 17348642.000
2015-12-22 18:34:54.547 LUA: usedEnergy : 17348642.000
2015-12-22 18:34:54.547 LUA: diff : 0
2015-12-22 18:34:54.547 LUA: interval : 285
2015-12-22 18:34:54.547 LUA: rate : 0
2015-12-22 18:34:54.550 EventSystem: Script event triggered: /home/pi/domoticz/scripts/lua/script_device_elecmeter_log.lua
Got the feeling I am almost there, but not completely.
Maybe I shouldn't have thrown myself in LUA just yet.
Any hint welcome.