I have include an optimalisation to the script adding a counter for keeping track of the number of active devices so to be able to skip the timer bit of the code if there are no active devices. Not thorough testing done yet
- Spoiler: show
-
Code: Select all
-- create a lookup table that matches a usage -- device to the accompanying switch local USAGE_DEVICES = { ['SC-Wasdroger verbruik'] = 'Wasdroger', -- You need to have a inline wall plug that measures energy, ['SC-Wasmachine verbruik'] = 'Wasmachine', -- here you make the link between the energy device and the wall plug. } local USAGE_SwitchTimeOutMinutes = { ['Wasdroger'] = 6, -- Here you define how long no power is used per device. ['Wasmachine'] = 6, -- The period is in minutes. Adjust to your needs. Between every add a ",". } local USAGE_MaxWatt = { ['Wasdroger'] = 3, -- Here you define the maximum amount of power a device uses when it is in standby. ['Wasmachine'] = 3, -- Some devices uses a little amount of power. Test it and a slightly higher usage. } local USAGE_Notify = { ['Wasdroger'] = 'Yes', -- In some cases you want to be notified when a device is turned on and off. ['Wasmachine'] = 'Yes', -- Adjust to your needs. Between every line you need to add a ",". } return { logging = { -- level = domoticz.LOG_INFO, -- Uncomment to override the dzVents global logging setting -- marker = 'POW' }, on = { timer = { 'every 5 minutes' }, devices = { -- Make sure that the devices are the same as above 'SC-Wasdroger verbruik', 'SC-Wasmachine verbruik', }, }, data = { -- use exact device names to match USAGE_DEVICES ['CountDevices'] = {initial=0}, ['SC-Wasdroger verbruik'] = { history = true, maxMinutes = 10 }, ['SC-Wasmachine verbruik'] = { history = true, maxMinutes = 10 } }, execute = function(domoticz, device) if (device.isTimer) then if domoticz.data['CountDevices'] > 0) then -- we need to check for expiration of grace period domoticz.log("timer trigger") for i, machine in pairs(USAGE_DEVICES) do local usage = "SC-" .. machine.. " verbruik" domoticz.log("device = " .. machine) local actual = domoticz.devices(usage).WhActual local average = domoticz.data[usage].avg() domoticz.data[usage].add(actual) domoticz.log("actual = " .. actual) domoticz.log("average = " .. tostring(average)) if actual == 0 then domoticz.log(machine .. " gebruikt geen energie, dus is uit") domoticz.devices(machine).switchOff().checkFirst() elseif actual < USAGE_MaxWatt[machine] then domoticz.log(machine .. " gebruikt weinig energie, is de machine klaar?") local timeout = USAGE_SwitchTimeOutMinutes[machine] domoticz.log(machine .. " gebruikte gemiddeld: " .. tostring(domoticz.data[usage].avg)) if (average <= USAGE_MaxWatt[machine]) then domoticz.log(machine .. " lijkt standby te staan, we nemen aan klaar.") domoticz.devices(machine).switchOff().checkFirst() domoticz.data[usage].reset() else domoticz.log(machine .. " maar nog te kort om klaar te zijn.") domoticz.devices(machine).switchOn().checkFirst() end else domoticz.log(machine .. " gebruikt energie, dus is (nog) bezig.") domoticz.devices(machine).switchOn().checkFirst() end end end elseif (USAGE_DEVICES[device.name] ~= nil) then -- we have a usage sensor here domoticz.log("usage trigger") local switch = domoticz.devices(USAGE_DEVICES[device.name]) local timeout = USAGE_SwitchTimeOutMinutes[USAGE_DEVICES[device.name]] local watt = USAGE_MaxWatt[USAGE_DEVICES[device.name]] domoticz.data[device.name].add(device.WhActual) local history = domoticz.data[device.name] domoticz.log("device = " .. device.name) if (switch.active) then domoticz.log("Switch is active, check average use and last update") domoticz.log("usage last update: " .. tostring(switch.lastUpdate.minutesAgo)) domoticz.log("usage = " .. tostring(device.WhActual)) domoticz.log("average = " .. tostring(history.avg())) if (history.avg() <= watt and device.WhActual <= watt and switch.lastUpdate.minutesAgo >= timeout) then switch.switchOff().checkFirst() domoticz.log("Turn switch off") if domoticz.data['CountDevices'] > 0 then domoticz.data['CountDevices'] = domoticz.data['CountDevices'] - 1 end domoticz.data[device.name].reset() end else domoticz.log("Switch not on") if (device.WhActual > watt) then domoticz.log("but power use is over treshold, turn switch on") domoticz.data['CountDevices'] = domoticz.data['CountDevices'] + 1 switch.switchOn().checkFirst() end end end end }