Page 1 of 1

ERROR attempt to call field 'devices' (a table value)

Posted: Friday 07 July 2017 19:42
by jkimmel
This is my script:

Code: Select all

local BOILER_DEVICE = 'FB 21 Heizkreis Salon' -- switch device
local TEMPERATURE_SENSOR = 'Salon'
local RUECKLAUF_SENSOR = 'WP Ruecklauf'
local SMOOTH_FACTOR = 3
local LOGGING = true

return {
	on = {
		devices = {
			TEMPERATURE_SENSOR,
			RUECKLAUF_SENSOR,
		},
		timer = {
			 'every minute',
		}	 
	},
	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)
			local ruecklauf = domoticz.devices(RUECKLAUF_SENSOR)

			if (ruecklauf.changed) then
				local ruecklaufT = ruecklauf.temperature
				
			if (sensor.changed ) 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
	
		end

		-- now determine what to do

		local boiler = domoticz.devices(BOILER_DEVICE)
		local dewpoint = sensor.dewpoint
		 
		-- determine at which temperature the boiler should be
		-- switched on
		

		-- 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('Current boiler state: ' .. boiler.state, domoticz.LOG_INFO)
		end

		if ((avgTemp <= dewpoint or ruecklaufT <= dewpoint) and boiler.state == 'On') then
			if LOGGING then domoticz.log('Target temperature reached, boiler off') end
			boiler.switchOff()
		end

		if ((avgTemp > dewpoint or ruecklaufT > dewpoint) and boiler.state == 'Off') then
			if LOGGING then domoticz.log('Heating is required, boiler switched on') end
				boiler.switchOn()
		
		end
		end
end
end
} 
This error is related to line 30 I have no idea why.

So help is very much appreciated.

Re: ERROR attempt to call field 'devices' (a table value)

Posted: Friday 07 July 2017 22:15
by dannybloe
Ok, first of all, I see this is based on the example and I discovered there's an error in there. In 2.0.1 I have it changed. I think you have to adapt your script a bit otherwise your avgTemp may become nil if your timer kicks in before you had a change to store your first temperature reading in the history:

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

return {
	on = {
		['timer'] = {
			'every minute',
		},
		devices = {
			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
		local sensor = domoticz.devices(TEMPERATURE_SENSOR)
		local current = sensor.temperature
		local boiler = domoticz.devices(BOILER_DEVICE)
		local setpoint = domoticz.devices(SETPOINT_DEVICE)

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

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

				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
		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, current) -- fallback to current when history is empty

		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
}
So, having said that.. can you please post the exact error from the logs as I don't see anything wrong on line 33.

Re: ERROR attempt to call field 'devices' (a table value)

Posted: Saturday 08 July 2017 1:43
by jkimmel
Here is my log:

Code: Select all

2017-07-08 01:36:46.709 LUA: Handling events for: "WP Ruecklauf_Temperature", value: "23.39999961853"
2017-07-08 01:36:46.709 LUA: =====================================================
2017-07-08 01:36:46.710 LUA: >>> Handler: Kuehlung Salon
2017-07-08 01:36:46.710 LUA: >>> Device: "WP Ruecklauf" Index: 339
2017-07-08 01:36:46.710 LUA: .....................................................
2017-07-08 01:36:46.717 LUA: An error occured when calling event handler Kuehlung Salon
2017-07-08 01:36:46.717 LUA: scripts/lua/Kuehlung Salon.lua:30: attempt to call field 'devices' (a table value)
2017-07-08 01:36:46.717 LUA: .....................................................
2017-07-08 01:36:46.717 LUA: <<< Done
2017-07-08 01:36:46.717 LUA: -----------------------------------------------------
2017-07-08 01:36:46.823 (RFX) Temp (WP Ruecklauf)
2017-07-08 01:36:48.300 (RFX) Temp (WP Vorlauf)
2017-07-08 01:36:49.597 LUA: Handling events for: "Bad_Temperature", value: "29.799999237061"
2017-07-08 01:36:49.597 LUA: =====================================================
2017-07-08 01:36:49.597 LUA: >>> Handler: Bad Infrarot
2017-07-08 01:36:49.597 LUA: >>> Device: "Bad" Index: 623
2017-07-08 01:36:49.598 LUA: .....................................................
2017-07-08 01:36:49.643 LUA: .....................................................
2017-07-08 01:36:49.643 LUA: <<< Done
2017-07-08 01:36:49.643 LUA: -----------------------------------------------------
2017-07-08 01:36:49.644 LUA: =====================================================
2017-07-08 01:36:49.644 LUA: >>> Handler: Heizung Bad
2017-07-08 01:36:49.644 LUA: >>> Device: "Bad" Index: 623
2017-07-08 01:36:49.644 LUA: .....................................................
2017-07-08 01:36:49.661 LUA: Average: 29.833332697551
2017-07-08 01:36:49.661 LUA: Setpoint: 20
2017-07-08 01:36:49.661 LUA: Current boiler state: Off
2017-07-08 01:36:49.662 LUA: Switch-on temperature: 19.5
2017-07-08 01:36:49.689 LUA: .....................................................
2017-07-08 01:36:49.689 LUA: <<< Done
2017-07-08 01:36:49.690 LUA: -----------------------------------------------------
2017-07-08 01:36:49.778 (RFX) Temp (Bad)
2017-07-08 01:36:50.808 (RFX) Temp + Humidity (Buero)
2017-07-08 01:36:51.805 (RFX) Temp + Humidity (Aussen)
2017-07-08 01:37:01.517 LUA: =====================================================
2017-07-08 01:37:01.517 LUA: >>> Handler: Bad Infrarot
2017-07-08 01:37:01.517 LUA: .....................................................
2017-07-08 01:37:01.578 LUA: .....................................................
2017-07-08 01:37:01.578 LUA: <<< Done
2017-07-08 01:37:01.578 LUA: -----------------------------------------------------
2017-07-08 01:37:01.578 LUA: =====================================================
2017-07-08 01:37:01.578 LUA: >>> Handler: Heizung Bad
2017-07-08 01:37:01.579 LUA: .....................................................
2017-07-08 01:37:01.611 LUA: Average: 29.833332697551
2017-07-08 01:37:01.611 LUA: Setpoint: 20
2017-07-08 01:37:01.611 LUA: Current boiler state: Off
2017-07-08 01:37:01.612 LUA: Switch-on temperature: 19.5
2017-07-08 01:37:01.619 LUA: .....................................................
2017-07-08 01:37:01.638 LUA: <<< Done
2017-07-08 01:37:01.638 LUA: -----------------------------------------------------
2017-07-08 01:37:01.638 LUA: =====================================================
2017-07-08 01:37:01.638 LUA: >>> Handler: Heizung Gaestebad
2017-07-08 01:37:01.638 LUA: .....................................................
2017-07-08 01:37:01.699 LUA: .....................................................
2017-07-08 01:37:01.699 LUA: <<< Done
2017-07-08 01:37:01.699 LUA: -----------------------------------------------------
2017-07-08 01:37:01.700 LUA: =====================================================
2017-07-08 01:37:01.700 LUA: >>> Handler: Kuehlung Salon
2017-07-08 01:37:01.700 LUA: .....................................................
2017-07-08 01:37:01.726 LUA: .....................................................
2017-07-08 01:37:01.727 LUA: <<< Done
2017-07-08 01:37:01.727 LUA: -----------------------------------------------------
2017-07-08 01:37:01.727 LUA: =====================================================
2017-07-08 01:37:01.727 LUA: >>> Handler: waermepumpe
2017-07-08 01:37:01.727 LUA: .....................................................
2017-07-08 01:37:01.728 LUA: .....................................................
2017-07-08 01:37:01.728 LUA: <<< Done
2017-07-08 01:37:01.728 LUA: -----------------------------------------------------
2017-07-08 01:37:03.508 LUA: Handling events for: "Gaestebad_Temperature", value: "28.700000762939"
2017-07-08 01:37:03.508 LUA: =====================================================
2017-07-08 01:37:03.508 LUA: >>> Handler: Heizung Gaestebad
2017-07-08 01:37:03.508 LUA: >>> Device: "Gaestebad" Index: 280
2017-07-08 01:37:03.526 LUA: .....................................................
2017-07-08 01:37:03.570 LUA: .....................................................
2017-07-08 01:37:03.570 LUA: <<< Done
2017-07-08 01:37:03.570 LUA: -----------------------------------------------------
2017-07-08 01:37:03.708 (RFX) Temp + Humidity (Gaestebad)
2017-07-08 01:37:31.737 (RFX) Temp + Humidity (Buero)
2017-07-08 01:37:33.078 (RFX) Temp + Humidity (Aussen)
2017-07-08 01:37:39.646 LUA: Handling events for: "Salon_Dewpoint", value: "15.851413726807"
2017-07-08 01:37:39.647 LUA: =====================================================
2017-07-08 01:37:39.647 LUA: >>> Handler: Kuehlung Salon
2017-07-08 01:37:39.647 LUA: >>> Device: "Salon" Index: 569
2017-07-08 01:37:39.647 LUA: .....................................................
2017-07-08 01:37:39.661 LUA: An error occured when calling event handler Kuehlung Salon
2017-07-08 01:37:39.661 LUA: scripts/lua/Kuehlung Salon.lua:30: attempt to call field 'devices' (a table value)
2017-07-08 01:37:39.661 LUA: .....................................................
2017-07-08 01:37:39.661 LUA: <<< Done
2017-07-08 01:37:39.661 LUA: -----------------------------------------------------
2017-07-08 01:37:39.750 (RFX) Temp + Humidity (Salon)
2017-07-08 01:37:44.447 (RFX) Temp + Humidity (Schlafzimmer)
2017-07-08 01:37:46.440 LUA: Handling events for: "Gaestebad_Dewpoint", value: "16.935287475586"
2017-07-08 01:37:46.440 LUA: =====================================================
2017-07-08 01:37:46.440 LUA: >>> Handler: Heizung Gaestebad
2017-07-08 01:37:46.440 LUA: >>> Device: "Gaestebad" Index: 280
2017-07-08 01:37:46.441 LUA: .....................................................
2017-07-08 01:37:46.477 LUA: .....................................................
2017-07-08 01:37:46.477 LUA: <<< Done
2017-07-08 01:37:46.477 LUA: ------------------------------

Re: ERROR attempt to call field 'devices' (a table value)

Posted: Saturday 08 July 2017 11:41
by dannybloe
Aha! You are still using 1.x. I was under the impression you were using 2.0. Can you upgrade to latest domoticz beta?

Re: ERROR attempt to call field 'devices' (a table value)

Posted: Saturday 08 July 2017 13:15
by jkimmel
I upgraded and no error anymore!

So "Kuehling Salon" is the only script I adapted to 2.0, I'm wondering all the others run without adaptation?
Anyway should I delete all Dzvents files?

edit:
No, they run but don't do anything just not producing errors :(