Here are some scripts:
viewtopic.php?f=72&t=23798
I have a modified one for an Neo coolcam plug, which is a switch (plug) and a power measuring device. So 2 in 1.
If you use the Sonoff the same way you could use the script below, with thanks to the guys in the above link.
Code: Select all
local USAGE_DEVICES = {
['Wasmachine kWh Meter'] = 'Wasmachine', -- You need to have a inline wall plug that measures energy,
['Wasdroger kWh Meter'] = 'Wasdroger', -- here you make the link between the energy device and the wall plug.
['Airco kWh Meter'] = 'Airco' -- Adjust to your needs. Between every line you need to add a ",".
}
local USAGE_SwitchTimeOutMinutes = {
['Wasmachine'] = 5, -- Here you define how long no power is used per device.
['Wasdroger'] = 6, -- The period is in minutes.
['Airco'] = 3 -- Adjust to your needs. Between every line you need to add a ",".
}
local USAGE_MaxWatt = {
['Wasmachine'] = 5, -- Here you define the maximum amount of power a device uses when it is in standby.
['Wasdroger'] = 5, -- Some devices uses a little amount of power. Test it and put here the slightly higher power usage.
['Airco'] = 5 -- Adjust to your needs. Between every line you need to add a ",".
}
local USAGE_Notify = {
['Wasmachine'] = 'Yes', -- In some cases you want to be notified when a device is turned on and off.
['Wasdroger'] = 'No', -- Adjust to your needs. Between every line you need to add a ",".
['Airco'] = 'Yes'
}
return {
active = true,
-- active = false,
logging = {
level = domoticz.LOG_INFO, -- Uncomment to override the dzVents global logging setting
marker = 'POW'
},
on = {
devices = { -- Make sure that the devices are the same as above
'Wasmachine',
'Wasmachine kWh Meter',
'Wasdroger',
'Wasdroger kWh Meter',
'Airco',
'Airco kWh Meter'
},
},
data = { -- use exact device names to match USAGE_DEVICES
['Wasmachine kWh Meter'] = { history = true, maxMinutes = 6 },
['Wasdroger kWh Meter'] = { history = true, maxMinutes = 10 },
['Airco kWh Meter'] = { history = true, maxMinutes = 10 }
},
execute = function(dz, device)
-- local debug = true
local debug = false
if (USAGE_DEVICES[device.name] ~= nil) then
-- we have a usage sensor here
local switch = dz.devices(USAGE_DEVICES[device.name])
local history = dz.data[device.name]
local timeout = USAGE_SwitchTimeOutMinutes[USAGE_DEVICES[device.name]]
local watt = USAGE_MaxWatt[USAGE_DEVICES[device.name]]
if debug then
dz.log(switch.name .. " - Status: " .. switch.state .. " - Age: " .. switch.lastUpdate.minutesAgo .. " minutes")
dz.log("timeout = " .. timeout .. " - maxwatt = " .. watt .. " - current power: " ..device.usage .. " - Average: " .. dz.utils.round(history.avg()))
end
if switch.state == "On" then
history.add(device.usage)
if (history.avg() <= watt and switch.lastUpdate.minutesAgo >= timeout) then
dz.log("Switching " .. switch.name .. "Off")
switch.switchOff().checkFirst()
dz.data[device.name].reset()
end
end
else
-- device is a switch
local notify = USAGE_Notify[device.name]
if debug then
dz.log(device.name .. " notify = " .. notify)
end
if (device.state == "On" and notify == "Yes") then
dz.notify(
"ACTIVATE",
device.name .. " activated#I let you know when device " .. device.name .. " is turned off",
dz.PRIORITY_NORMAL,
dz.SOUND_SIREN,
"",
{ dz.NSS_HTTP, dz.NSS_GOOGLE_CLOUD_MESSAGING }
)
elseif (notify == "Yes") then
dz.notify(
"DEACTIVATED",
device.name .. " is klaar.",
dz.PRIORITY_HIGH,
dz.SOUND_INTERMISSION,
"",
{ dz.NSS_HTTP, dz.NSS_GOOGLE_CLOUD_MESSAGING }
)
end
end
end
}