Page 1 of 1

Question about the heating example script

Posted: Saturday 12 November 2016 1:23
by elmortero
Hi Danny,

I have been using dzvents from the beginning but I have limited coding skills.
Now winter is about to start I am implementing a heating script, based on the example you provided (simple room heating with hysteresis control).
I want to add a condition to the script: no heating when a window is open (dt5)
Simple enough, or so I thought. Because if the window is open when the switchOnTemp is reached the heating does not start. But I also want the heater to turn if the window is openend while the heater was already started.

Can you tell me where to put the window-open-check to achieve this?

Re: Question about the heating example script  [Solved]

Posted: Monday 14 November 2016 1:17
by jkimmel
Assuming there is a device 'Window' you could trigger that device and also question state of this device, something like this:

Code: Select all

-- assumptions:
-- the setpoint is set by a selector dummy device where the values are numeric temperatures
-- but you can easily change it to a setpoint device

local BOILER_DEVICE = 'Boiler' -- switch device
local SETPOINT_DEVICE = 'Setpoint' -- selector dummy device
local TEMPERATURE_SENSOR = 'Temperature'
local HYSTERESIS = 0.5 -- temp has to drop this value below setpoint before boiler is on again
local SMOOTH_FACTOR = 3
local LOGGING = true
local WINDOW_DEVICE = 'Window' -- !!!

return {
	on = {
		['timer'] = 'every minute',
		TEMPERATURE_SENSOR,
		SETPOINT_DEVICE,
		WINDOW_DEVICE   -- Triggers Window state
	},
	data = {
		temperatureReadings = { history = true, maxItems = SMOOTH_FACTOR }
	},
	active = true,
	execute = function(domoticz, device, triggerInfo)

		local avgTemp
		local temperatureReadings = domoticz.data.temperatureReadings

		-- first check if the sensor got a new reading or the setpoint was changed:
		if (triggerInfo.type == domoticz.EVENT_TYPE_DEVICE) then

			local sensor = domoticz.devices[TEMPERATURE_SENSOR]

			if (sensor.changed and sensor.attributeChanged('temperature')) then
				-- sensor just reported a new reading
				-- add it to the readings table

				local current = sensor.temperature

				if (current ~= 0 and current ~= nil) then
					temperatureReadings.add(current)
				else
					-- no need to be here, weird state detected
					domoticz.log('Strange sensor reading.. skiping', domoticz.LOG_ERROR)
					return
				end

			elseif (domoticz.devices[SETPOINT_DEVICE].changed) then
				-- a new setpoint was set
				if LOGGING then domoticz.log('Setpoint was set to ' .. device.state) end
			else
				-- no business here, bail out...
				return
			end
		end

		-- now determine what to do

		local boiler = domoticz.devices[BOILER_DEVICE]
		local setpoint = domoticz.devices[SETPOINT_DEVICE]

		if (setpoint.state == nil or setpoint.state == 'Off') then
			boiler.switchOff()
			return -- we're done here
		end

		local setpointValue = tonumber(setpoint.state)

		-- determine at which temperature the boiler should be
		-- switched on
		local switchOnTemp = setpointValue - HYSTERESIS

		-- don't use the current reading but average it out over
		-- the past <SMOOTH_FACTOR> readings (data smoothing) to get rid of noise, wrong readings etc
		local avgTemp = temperatureReadings.avg(1, SMOOTH_FACTOR)

		if LOGGING then
			domoticz.log('Average: ' .. avgTemp, domoticz.LOG_INFO)
			domoticz.log('Setpoint: ' .. setpointValue, domoticz.LOG_INFO)
			domoticz.log('Current boiler state: ' .. boiler.state, domoticz.LOG_INFO)
			domoticz.log('Switch-on temperature: ' .. switchOnTemp, domoticz.LOG_INFO)
		end

		if (avgTemp >= setpointValue and boiler.state == 'On')  then
			if LOGGING then domoticz.log('Target temperature reached, boiler off') end
			boiler.switchOff()
		end

		if (avgTemp < setpointValue and boiler.state == 'Off') then
			if (avgTemp < switchOnTemp) then
				if LOGGING then domoticz.log('Heating is required, boiler switched on') end
				boiler.switchOn()
			else
				if LOGGING then domoticz.log('Average temperature below setpoint but within hysteresis range, waiting for temperature to drop to ' .. switchOnTemp) end
				if WINDOW_DEVICE.state == 'Open' then boiler.switchOff() end -- !!!
			end
		end
	end
}
You could refine by inserting something like only if 'window is open longer then x min'

Re: Question about the heating example script

Posted: Wednesday 04 January 2017 22:43
by elmortero
Thanks, did just that.
Only if the window stays open for more than 3 minutes (or in case of the bathroom, the door) the heating will be switched off because of that.

Sorry for the late reply.