script: 0-energy-turn-device-off

Moderator: leecollings

Post Reply
wizzard72
Posts: 116
Joined: Friday 20 December 2013 7:45
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

script: 0-energy-turn-device-off

Post by wizzard72 »

Below is my dzVents "0-energy-turn-device-off" script just in case it interests someone.

I wanted to create a script that turns off my washing machine and send me a notification when finished. For this script you need a wall plug that measured power usage. See also viewtopic.php?t=20786#p160725 for the initial start of the script.

I put this script also on github:
https://github.com/Wizzard72/domoscript ... device-off

Code: Select all

--[[	This script switches off a device after it has consumed no or almost no power for a certain period of time.

	Prerequisits
	==================================
	Domoticz v3.8837 or later (dzVents version 2.3 or later)

	CHANGE LOG: See [url]https://www.domoticz.com/forum/posting.php?mode=post&f=72[/url]

-- Authors  ----------------------------------------------------------------
	V1.0 - Wizzard72 with the help of dannybloe
]]--

-- create a lookup table that matches a usage
-- device to the accompanying switch
local USAGE_DEVICES = {
	['Wasmachine (W)'] = 'Wasmachine',					-- You need to have a inline wall plug that measures energy,
	['Slaapkamer Paul Schakelaar (W)'] = 'Slaapkamer Paul Schakelaar',	-- here you make the link between the energy device and the wall plug.
	['Espressomachine (W)'] = 'Espressomachine'				-- Adjust to your needs. Between every line you need to add a ",".
}

local USAGE_SwitchTimeOutMinutes = {
	['Wasmachine'] = 6,							-- Here you define how long no power is used per device.
	['Slaapkamer Paul Schakelaar'] = 10,					-- The period is in minutes.
	['Espressomachine'] = 30						-- Adjust to your needs. Between every line you need to add a ",".
}

local USAGE_MaxWatt = {
	['Wasmachine'] = 0,							-- Here you define the maximum amount of power a device uses when it is in standby.
	['Slaapkamer Paul Schakelaar'] = 1,					-- Some devices uses a little amount of power. Test it and put here the slightly higher power usage.
	['Espressomachine'] = 1							-- 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.
	['Slaapkamer Paul Schakelaar'] = 'No',					-- Adjust to your needs. Between every line you need to add a ",".
	['Espressomachine'] = 'No'
}

return {
	logging = {
		--level = domoticz.LOG_INFO, 					-- Uncomment to override the dzVents global logging setting
		marker = '0-energy'
	},
	on = {
		devices = {							-- Make sure that the devices are the same as above
			'Wasmachine (W)',
			'Wasmachine',
			'Slaapkamer Paul Schakelaar',
			'Slaapkamer Paul Schakelaar (W)',
			'Espressomachine',
			'Espressomachine (W)'
		},
	},
	data = { 								-- use exact device names to match USAGE_DEVICES
		['Wasmachine (W)'] = { history = true, maxMinutes = 6 },
		['Slaapkamer Paul Schakelaar (W)'] = { history = true, maxMinutes = 10 },
		['Espressomachine (W)'] = { history = true, maxMinutes = 10 }
	},

	execute = function(domoticz, device)

		if (USAGE_DEVICES[device.name] ~= nil) then
			-- we have a usage sensor here
			local switch = domoticz.devices(USAGE_DEVICES[device.name])
			local history = domoticz.data[device.name]
			local timeout = USAGE_SwitchTimeOutMinutes[USAGE_DEVICES[device.name]]
			local watt = USAGE_MaxWatt[USAGE_DEVICES[device.name]]
			domoticz.log("timeout = " .. timeout)
			domoticz.log("watt = " .. watt)
			history.add(device.WhActual)
			if (switch.active and history.avg() <= watt and switch.lastUpdate.minutesAgo >= timeout) then
			 	switch.switchOff().checkFirst()
			end
		else
			-- device is a switch
			local notify = USAGE_Notify[device.name]
			domoticz.log("notify = " .. notify)
			if (device.active and notify == "Yes") then
				domoticz.notify(
					"ACTIVATE", 
					device.name .. " activated#I let you know when device " .. device.name .. " is turned off", 
					domoticz.PRIORITY_EMERGENCY, 
					domoticz.SOUND_SIREN
				)
			elseif (notify == "Yes") then
				domoticz.notify(
					"DEACTIVATED", 
					device.name .. " is deactivated",
					domoticz.PRIORITY_EMERGENCY, 
					domoticz.SOUND_SIREN
				)
			end
		end
	end
}
User avatar
papoo
Posts: 126
Joined: Friday 22 January 2016 22:14
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10
Location: France
Contact:

Re: script: 0-energy-turn-device-off

Post by papoo »

thanks
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest