-
Notifications
You must be signed in to change notification settings - Fork 4
/
script_device_ergy.lua
90 lines (69 loc) · 2.61 KB
/
script_device_ergy.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
-- This program is free software; you can redistribute it and/or
-- modify it under the terms of the GNU General Public License
-- version 2 as published by the Free Software Foundation.
-- Author: epierre
-- script to register a fixed power consumption usage based on the duration activity
-- reguires dynamic libraries in lua to have sqlite support
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
commandArray = {}
-- initialize libraries
local sqlite3 = require("luasql.sqlite3")
env = assert(sqlite3.sqlite3())
-- initialize vars
local s=0
local hamt
local switchid=91
local usageid=128
local wattage=700
if (devicechanged['FGS211'] == 'Off') then
con = assert (env:connect("/home/pi/domoticz/domoticz.db") )
cur = assert (con:execute('select Date from LightingLog where DeviceRowID='..switchid..' and nValue=1 order by Date desc limit 1'))
row = cur:fetch ({}, "a")
s=row.Date
cur:close()
con:close()
difference = timedifference(s)
con = assert (env:connect("/home/pi/domoticz/domoticz.db") )
cur = assert (con:execute('select Value from Meter where DeviceRowID='..usageid..' order by Date desc limit 1'))
row = cur:fetch ({}, "a")
hamt=row.Value/100
cur:close()
con:close()
env:close()
local amt=math.floor(difference*(wattage/3600)+0.5)/1000
local famt=hamt+amt
print ("T="..t1.." T0="..t2.." "..s.." %"..difference.." - "..hamt.." + "..amt.."="..famt)
commandArray['UpdateDevice'] = usageid .. "|0|" .. 0 .. ";" .. famt
end
if (devicechanged['FGS211'] == 'On') then
con = assert (env:connect("/home/pi/domoticz/domoticz.db") )
cur = assert (con:execute('select Date from LightingLog where DeviceRowID='..switchid..' and nValue=1 order by Date desc limit 1'))
row = cur:fetch ({}, "a")
s=row.Date
cur:close()
con:close()
difference = timedifference(s)
con = assert (env:connect("/home/pi/domoticz/domoticz.db") )
cur = assert (con:execute('select Value from Meter where DeviceRowID='..usageid..' order by Date desc limit 1'))
row = cur:fetch ({}, "a")
hamt=row.Value/100
cur:close()
con:close()
env:close()
local amt=math.floor(difference*(wattage/3600)+0.5)/1000
local famt=hamt+amt
print ("T="..t1.." T0="..t2.." "..s.." %"..difference.." - "..hamt.." + "..amt.."="..famt)
commandArray['UpdateDevice'] = usageid .. "|0|" .. wattage .. ";" .. famt
end
return commandArray