Heating DZvents Multi Zone

Post Reply
dawsmac
Posts: 9
Joined: Tuesday 22 December 2015 16:08
Target OS: Raspberry Pi
Domoticz version:
Contact:

Heating DZvents Multi Zone

Post by dawsmac » Thursday 19 January 2017 0:18

Hi not sure if any one can help I have tried to amending the heating script that I download from DZvents. but got a little stuck. am I doing this all wrong ?

Cheers

Rich

Code: Select all


local BOILER_DEVICE = 'Boiler call' -- switch device
local TPV_DS = 'Downstairs Rad' -- Downstairs two port valve
local TPV_UP = 'Upstairs Rad' -- Upstairs two port valve
local SETPOINT_DS = 'Downstairs SetPoint' -- Upstairs rads
local SETPOINT_US = 'Upstairs SetPoint' -- Downstairs rads
local SETPOINT_UFH = 'UFH SetPoint' -- Under floor heating
local TEMPERATURE_DS = 'Downstairs Temperature' -- Upstairs Temp
local TEMPERATURE_US = 'Upstairs Temperature' -- Downstairs Temp
local TEMPERATURE_UFH = 'UFH Temperature' -- Under floor heating Temp
local HW_Thermostat = 'H/W Thermostat' -- Hot water Thermostat call
local HYSTERESIS = 0.5 -- temp has to drop this value below setpoint before boiler is on again
local SMOOTH_FACTOR = 3
local LOGGING = true

return {
	on = {
		['timer'] = 'every minute',
		SETPOINT_DS,
		SETPOINT_US,
		SETPOINT_UFH,
		TEMPERATURE_DS,
		TEMPERATURE_US,
		TEMPERATURE_UFH
			},
	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 sensor1 = domoticz.devices[TEMPERATURE_DS]
			local sensor2 = domoticz.devices[TEMPERATURE_US]
			local sensor3 = domoticz.devices[TEMPERATURE_UFH]

			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
			end
		end
	end
}

elmortero
Posts: 174
Joined: Sunday 29 November 2015 21:46
Target OS: Raspberry Pi
Domoticz version: 2.3852
Location: Spain
Contact:

Re: Heating DZvents Multi Zone

Post by elmortero » Thursday 19 January 2017 10:31

Do you get any errors in the log?
That sure would help us help you :-)

dawsmac
Posts: 9
Joined: Tuesday 22 December 2015 16:08
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Heating DZvents Multi Zone

Post by dawsmac » Saturday 21 January 2017 17:43

Thanks elmortero...

As this is my 1st Lua script I thought it might be better to start a little simpler.. :) Below is the script but still getting error I have attached the logs also.





local TPV_Down = 'Downstairs_Rad' -- switch device
local SETPOINT_DEVICE = 'Downstairs SetPoint' -- selector dummy device
local TEMPERATURE_SENSOR = 'Downstairs Temperature'
local HYSTERESIS = 0.5 -- temp has to drop this value below setpoint before Downstairs_Rad is on again
local SMOOTH_FACTOR = 3
local LOGGING = true

return {
on = {
['timer'] = 'every minute',
TEMPERATURE_SENSOR,
SETPOINT_DEVICE
},
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 Downstairs_Rad = domoticz.devices[TPV_UP]
local setpoint = domoticz.devices[SETPOINT_DEVICE]

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

local setpointValue = tonumber(setpoint.state)

-- determine at which temperature the Downstairs_Rad 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 Downstairs_Rad state: ' .. Downstairs_Rad.state, domoticz.LOG_INFO)
domoticz.log('Switch-on temperature: ' .. switchOnTemp, domoticz.LOG_INFO)
end

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

if (avgTemp < setpointValue and Downstairs_Rad.state == 'Off') then
if (avgTemp < switchOnTemp) then
if LOGGING then domoticz.log('Heating is required, Downstairs_Rad switched on') end
Downstairs_Rad.switchOn()
else
if LOGGING then domoticz.log('Average temperature below setpoint but within hysteresis range, waiting for temperature to drop to ' .. switchOnTemp) end
end
end
end
}


-21 15:37:00.623 LUA: -----------------------------------------------------
2017-01-21 15:37:32.116 Under-floor Heating OFF
2017-01-21 15:37:32.516 Boiler Call ON
2017-01-21 15:37:32.520 Boiler Call OFF
2017-01-21 15:38:00.493 LUA: =====================================================
2017-01-21 15:38:00.493 LUA: >>> Handler: Downstairs_Rads
2017-01-21 15:38:00.493 LUA: .....................................................
2017-01-21 15:38:00.496 LUA: An error occured when calling event handler Downstairs_Rads
2017-01-21 15:38:00.497 LUA: /home/pi/domoticz/scripts/lua/scripts/Downstairs_Rads.lua:73: attempt to concatenate local 'avgTemp' (a nil value)
2017-01-21 15:38:00.498 LUA: .....................................................
2017-01-21 15:38:00.498 LUA: <<< Done
2017-01-21 15:38:00.498 LUA: -----------------------------------------------------
2017-01-21 15:39:00.362 LUA: =====================================================
2017-01-21 15:39:00.363 LUA: >>> Handler: Downstairs_Rads
2017-01-21 15:39:00.363 LUA: .....................................................
2017-01-21 15:39:00.366 LUA: An error occured when calling event handler Downstairs_Rads
2017-01-21 15:39:00.366 LUA: /home/pi/domoticz/scripts/lua/scripts/Downstairs_Rads.lua:73: attempt to concatenate local 'avgTemp' (a nil value)
2017-01-21 15:39:00.366 LUA: .....................................................
2017-01-21 15:39:00.366 LUA: <<< Done
2017-01-21 15:39:00.367 LUA: -----------------------------------------------------

elmortero
Posts: 174
Joined: Sunday 29 November 2015 21:46
Target OS: Raspberry Pi
Domoticz version: 2.3852
Location: Spain
Contact:

Re: Heating DZvents Multi Zone

Post by elmortero » Sunday 22 January 2017 2:51

I think avgTemp is nil because the persitent variable 'temperatureReadings' is not being populated.
Can you check if the exact name of your temperature sensero is 'Downstairs Temperature'? Keep in mind that Lua is case sensitive..

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest