Page 1 of 1

Beginner Troubles

Posted: Tuesday 10 October 2017 20:20
by Bernhard1
Hi, I need Help by my project. I will program a 2 point controller for a heating system.

Here my code:

Code: Select all

commandArray = {}

if (devicechanged['Tempsensor']) then
    
    local hysteresis = 0.2
    
    if (otherdevices['Modus'] == 'MANUAL') then
        local setpoint = otherdevices['Setpoint MANUAL']
    elseif (otherdevices['Modus'] == 'AUTOMATIC') then
        local setpoint = otherdevices['Setpoint AUTOMATIC']
    end
  
    if (otherdevices['Tempsensor'] < (setpoint - hysteresis)) then
        commandArray['Output'] = 'On'
        print('ON')
    elseif (otherdevices['Tempsensor'] > (setpoint + hysteresis)) then
        commandArray['Output'] = 'Off'
        print('OFF')
    end
end
return commandArray
by changing the Tempsensor value I got this Error:

Code: Select all

2017-10-10 20:13:36.868 Error: EventSystem: in 001: [string "commandArray = {}..."]:13: attempt to perform arithmetic on global 'setpoint' (a nil value)
and every minute i got this one:

Code: Select all

2017-10-10 20:14:00.489 Error: EventSystem: in 001: [string "commandArray = {}..."]:3: attempt to index global 'devicechanged' (a nil value)


Thanks!

Re: Beginner Troubles

Posted: Tuesday 10 October 2017 20:35
by BlueMoon
Try to declare setpoint before your if/else statement, so right after you declared hysteresis. Then give it its value within the if/else

I'm not a lua expert, but I believe the scope of the setpoint value is now only the within the if block, and another one within the else block.

Re: Beginner Troubles

Posted: Tuesday 10 October 2017 21:06
by Bernhard1
I have changed the code:

Code: Select all

commandArray = {}

if (devicechanged['Tempsensor']) then
    
    local hysteresis = 0.2
    local setpoint = 0
    
    if (otherdevices['Modus'] == 'MANUAL') then
        setpoint = otherdevices['Setpoint MANUAL']
    elseif (otherdevices['Modus'] == 'AUTOMATIC') then
        setpoint = otherdevices['Setpoint AUTOMATIC']
    end
  
    if (otherdevices['Tempsensor'] < (setpoint - hysteresis)) then
        commandArray['Output'] = 'On'
        print('ON')
    elseif (otherdevices['Tempsensor'] > (setpoint + hysteresis)) then
        commandArray['Output'] = 'Off'
        print('OFF')
    end
end

return commandArray
and the first error changed to

Code: Select all

2017-10-10 19:47:49.311 Error: EventSystem: in 001: [string "commandArray = {}..."]:16: attempt to compare string with nil
the secound is the same :(

Re: Beginner Troubles

Posted: Wednesday 11 October 2017 11:53
by BlueMoon
Hmm, I looked over that one. Is your script a 'device' script? You should see it in the filename of the script.

It seems to complain about all device-related stuff, which not allowed of the script of the wrong type.

Re: Beginner Troubles

Posted: Wednesday 11 October 2017 12:02
by emme
what kind of device are setpoint MANUAL and setpoint AUTOMATIC ?


try also to declare them forcing the tonumber conversion:

Code: Select all

...
setpoint = tonumber(otherdevices['Setpoint MANUAL'])
...

Code: Select all

...
setpoint = tonumber(otherdevices['Setpoint AUTOMATIC'])
...
ciao
M

Re: Beginner Troubles

Posted: Wednesday 11 October 2017 21:39
by Bernhard1
Thanks a lot now it works perfekt!

I have changed the settings from "All" to "Device" which one caused

Code: Select all

Error: EventSystem: in 001: [string "commandArray = {}..."]:3: attempt to index global 'devicechanged' (a nil value)
and i have added the tonumber function several times in my code wich caused the secound error


Thanks!

Re: Beginner Troubles

Posted: Thursday 12 October 2017 8:46
by dannybloe
I strongly suggest that you start writing scripts using dzVents as that is much easier for beginners (an much more powerful). Check the wiki for dzVents and the forum for dzVents.

Re: Beginner Troubles

Posted: Thursday 12 October 2017 10:47
by emme
dannybloe wrote: Thursday 12 October 2017 8:46 I strongly suggest that you start writing scripts using dzVents as that is much easier for beginners (an much more powerful). Check the wiki for dzVents and the forum for dzVents.
dzVents would look something like this...

Code: Select all

return {
	on = {
	    devices = {
			'Tempsensor',
		},
	}
	execute = function(dz, devTemp)
		local devModus = dz.devices('Modus').value 
		local setPoint = 18   -- let's give at least a value to it to avoid nil errors
		local hysteresis = 0.2
		
		if devModus == 'MANUAL' then
			setPoint = dz.devices('Setpoint MANUAL').setPoint 
		elseif devModus == 'AUTOMATIC' then
			setPoint = dz.devices('Setpoint AUTOMATIC').setPoint 
		end 
		
		if devTemp.temperature < (setPoint - hysteresis) then 
			dz.devices('Output').switchOn().checkFirst()
			dz.log('It's getting cold here... Let's heat it up for a while...',dz.LOG_FORCE)
		elseif devTemp.temperature > (setPoint + hysteresis) then 
			dz.devices('Output').switchOff().checkFirst()
			dz.log('Uh... I'm fine... Let me stop this...',dz.LOG_FORCE)
		end 
	end 
}

Re: Beginner Troubles

Posted: Saturday 14 October 2017 14:41
by Bernhard1
Thanks for your suggestion! dzVents is better for beginners in domoticz, Wiki for it is really great Thanks!!!