forked from jellewie/Domoticz-Is-the-washing-machine-done
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVbscript.lua
98 lines (91 loc) · 4.89 KB
/
Vbscript.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
89
90
91
92
93
94
95
96
97
98
--"Is the washing machine done?"
--Code Tweaked by JelleWho https://github.com/jellewie/Domoticz-Is-the-washing-machine-done
local DEVICES = {
--█Change the below values to EXACTLY match the names/numbers set in Domoticz as device Name/Idx. (SwitchName and MeterName) can either the name '<exact name>' |or| number <Number>
--Random ,SwitchName , MeterName , TimeOut, StandbyMaxWatt
['a'] = {'Wasmachine', 'Wasmachine usage', 5 , 4},
['b'] = {'Wasdroger' , 'Wasdroger usage' , 5 , 1},
--['c'] = {'3D_Printer', '3D_Printer usage', 5 , 12}, --Another example
--█Also add these names to "data = {" in the format of "['SwitchName'] = {history = true, maxMinutes = 10}," !
}
local LogDebugging = false --Set to TRUE to receive more information in the log, this includes most values of each status check
local LogChecking = true --Set to TRUE to log what the result of the check was (machine was on/off/idle etc)
return {
logging = {
--level = domoticz.LOG_INFO, --█Uncomment to override the dzVents global logging setting
marker = 'POW'
},
on = {
timer = {'every 1 minutes'}, --█Every x, call the 'execute' function with 'devices' variable listed below
},
data = {
--█use exact SwitchName to match DEVICES
['Wasmachine'] = {history = true, maxMinutes = 10}, --Log the values and store them here, remove all data after 10 min
['Wasdroger'] = {history = true, maxMinutes = 10},
--['3D_Printer'] = {history = true, maxMinutes = 10},
},
--You can stop reading now, from here on out its just code
execute = function(domoticz)
function status(machine)
local SwitchName = machine[1] -- name of physical power measuring device
local MeterName = machine[2] -- name of physical power measuring device
local TimeOut = machine[3]+0 -- amount of time the power consumption needs to be constant
local StandbyMaxWatt = machine[4]+0 -- threshold for standby
local Meter = domoticz.devices(MeterName)
local Switch = domoticz.devices(SwitchName)
local power_average = domoticz.data[SwitchName].avg()+0 -- the average power consumption in the last 10 minutes
--lastUpdate.minutesAgo = the time in minutes the device is unchanged
--WhActual = the actual power consumption of the device
if LogDebugging then
domoticz.log(' - Switch name='..SwitchName..', Meter name='..MeterName)
domoticz.log(' - Usage='..Meter.WhActual..', Treshold='..StandbyMaxWatt..', Average='..power_average)
domoticz.log(' - Last read='..Meter.lastUpdate.minutesAgo..', Timout after='..TimeOut..', Last switch update='..Switch.lastUpdate.minutesAgo)
end
domoticz.data[SwitchName].add(Meter.WhActual)
if (Switch.active) then
if Meter.WhActual > StandbyMaxWatt then --Device is already on
return('Already on')
end
local Reason = ""
if (Switch.lastUpdate.minutesAgo > TimeOut) then --If the button has not changed for more than x minutes
if (Meter.WhActual == 0) then
Reason = "No ActPower"
elseif (Meter.WhActual <= StandbyMaxWatt) then
if (power_average <= StandbyMaxWatt) then
Reason = "ActPower<Standby & Poweravg<Standby"
elseif (Meter.lastUpdate.minutesAgo > TimeOut) then
Reason = "ActPower<Standby & update TimeOut"
end
end
if (Reason ~= "") then
Switch.switchOff() --Device is off or on standby
domoticz.data[SwitchName].reset() --Reset history
return('Off: '..Reason)
end
end
if (Switch.lastUpdate.minutesAgo <= TimeOut) then
Reason = "Wait for Switch idle:"..tostring(TimeOut-Switch.lastUpdate.minutesAgo).."min"
elseif(Meter.WhActual > StandbyMaxWatt) then
if (power_average > StandbyMaxWatt) then
Reason = "Wait for Poweravg<Standby OR TimeOut:"..tostring(Meter.WhActualo).."<"..tostring(StandbyMaxWatt).."W | "..tostring(Meter.lastUpdate.minutesAgo)..">"..tostring(TimeOut).."min"
end
end
return('Idle: '..Reason)
end
--Note: switch is not active
if Meter.WhActual > StandbyMaxWatt and Meter.WhActual < 3840 then --Device is active (and no reading error above 240V@16A)
Switch.switchOn() --Turn the virtual switch on
return('Switching On: Act_Power='..tostring(Meter.WhActual)..'>'..tostring(StandbyMaxWatt))
end
--Note: switch and machine are not active
if power_average > 0 then --Switch is off but average is not reset
domoticz.data[SwitchName].reset() --Reset history (and the new average of NULL data is ofc 0)
end
return('Off') --Device is off
end
for i, machine in pairs(DEVICES) do --Loop through all the devices
checked = status(machine) --Check the status of each device
if LogChecking then domoticz.log('Status of '..machine[1]..'='..checked) end--Log the status of each device
end
end
}