Beginner Troubles Topic is solved

Moderator: leecollings

Post Reply
Bernhard1
Posts: 6
Joined: Sunday 08 October 2017 15:59
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8356
Contact:

Beginner Troubles

Post 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!
BlueMoon
Posts: 9
Joined: Tuesday 03 October 2017 21:15
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Contact:

Re: Beginner Troubles

Post 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.
Bernhard1
Posts: 6
Joined: Sunday 08 October 2017 15:59
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8356
Contact:

Re: Beginner Troubles

Post 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 :(
BlueMoon
Posts: 9
Joined: Tuesday 03 October 2017 21:15
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Contact:

Re: Beginner Troubles

Post 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.
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: Beginner Troubles

Post 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
The most dangerous phrase in any language is:
"We always done this way"
Bernhard1
Posts: 6
Joined: Sunday 08 October 2017 15:59
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8356
Contact:

Re: Beginner Troubles

Post 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!
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Beginner Troubles

Post 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.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: Beginner Troubles

Post 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 
}
The most dangerous phrase in any language is:
"We always done this way"
Bernhard1
Posts: 6
Joined: Sunday 08 October 2017 15:59
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8356
Contact:

Re: Beginner Troubles

Post by Bernhard1 »

Thanks for your suggestion! dzVents is better for beginners in domoticz, Wiki for it is really great Thanks!!!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest