Page 1 of 1

Need some help with basic LUA

Posted: Wednesday 13 July 2016 9:48
by tullgren
Hi,

I'm trying to get started with LUA.
Grateful if anyone can help me in the right direction.

The script runs and lights the lamp when the temperature is above 20 degrees.
But I get an error message in the log. I suspect that 20 is a string value. But do not understand how to convert the value.

The script:

Code: Select all

commandArray = {}

if devicechanged['Sovrum_Temperature'] > 20 then
    commandArray['Skrivbord']='On'
end

return commandArray
Error message:
Error: EventSystem: in SovrumTermostat: [string "--..."]:7: attempt to compare number with nil

Best regards, Mikael

Re: Need some help with basic LUA

Posted: Wednesday 13 July 2016 10:34
by cyberclwn
I don't think the state of the device changes when it's data changes, when it is a thermo-device.

You better use it combined with a switch or other action. so you can use the "otherdevices"-array with data

Code: Select all

if ((tonumber(otherdevices_temperature['Thermo']) < tonumber(20) then
Maybe on the wiki is more information to help you further:
https://www.domoticz.com/wiki/Events

Re: Need some help with basic LUA

Posted: Wednesday 13 July 2016 17:31
by jannl
Or use a time based script

Re: Need some help with basic LUA

Posted: Wednesday 13 July 2016 20:28
by georgesattali
Hello,
I think that the Sovrum is a Thermostat (is that true ?), therefore 'Sovrum_Temperature' does not exists and is valued to nil.
When you compare nil > 20 you get the message "attempt to compare number with nil".
Adding tonumber(xxx), although correct in principle, is useless (and ugly from my point of view), Lua perfectly understands you want to compare numbers.
I don't how to get rid of this nil. some more research needed...
See U, GD.

Re: Need some help with basic LUA

Posted: Thursday 14 July 2016 8:21
by dannybloe
I'd suggest to look at dzVents. It makes Lua scripting for Domoticz almost trivial. No more juggling with all those tables.

Re: Need some help with basic LUA

Posted: Thursday 04 August 2016 13:32
by jjdom
hello,

I don't speak english very well, so I'am not sure to understand what you realy whant to do.
But if the problem is only du Nil value, why don't make like this:

Code: Select all

commandArray = {}

if devicechanged['Sovrum_Temperature'] ~= nil and
   devicechanged['Sovrum_Temperature'] > 20 then
    commandArray['Skrivbord']='On'
end

return commandArray