Page 1 of 1

Help on LUA script

Posted: Friday 01 December 2017 16:44
by g00fy
Hello,

I am using a LUA script to turn the lights on and off when some parameters have a certain value. This was working fine untill I wanted to add another switch to the script. When I add this only the second command is working.

This is the script:

Code: Select all

function updatedomoticz(idx,switchcmd)
        commandArray['OpenURL'] = 'http://10.27.0.20/json.htm?type=command&param=switchlight&idx='..idx..'&switchcmd='..switchcmd;
end

function setvariabele(key,value)
        commandArray['Variable:'..key]=tostring(value)
end

commandArray = {}
-- Variabelen ophalen
LUX = tonumber(otherdevices['Zonnestraling'])
LUXaan = tonumber(25)
LUXuit = tonumber(25) 
verlichtingwoonkamer = otherdevices['Lamp Plafond Woonkamer']
verlichtingwoonkamerstat = uservariables["verlichting_wk"]
aanwezig = otherdevices['Iemand thuis']
afspraak = uservariables["afspraak"]
afspraak_status = uservariables["afspraak_status"]

if      (LUX <= LUXaan and verlichtingwoonkamerstat == 0 and aanwezig == 'On') or
        (afspraak == 1 and LUX <= LUXaan and afspraak_status == 0 and aanwezig == 'On') then
                updatedomoticz(68,"On")
                setvariabele("verlichting_wk",1)
                if (afspraak == 1) then
                        setvariabele("afspraak_status",1)
                end
                print ("Verlichting Woonkamer aan")
elseif  (LUX > LUXuit and verlichtingwoonkamerstat == 1) or
                (aanwezig == 'Off' and verlichtingwoonkamerstat == 1) then
                        updatedomoticz(68,"Off")
                        setvariabele("verlichting_wk",0)
                    print ("Verlichting Woonkamer uit")
end
When I put another update command after the line 'updatedomoticz(68,"On")', for example 'updatedomoticz(69,"On")', only the device with IDX 69 is switching. When I remove this line IDX 68 is working again.
I solved it with a new script that switches IDX 69 on when IDX 68 is set to on and that works. But it would be nicer to put it in one script. Also , I'd like to understand why it isn't working as I thought it would.

Thanks in advance!

Re: Help on LUA script

Posted: Friday 01 December 2017 17:21
by jvdz
The commandArray table entry is overwritten with the second command as the key is the same. To avoid that use this syntax:

Code: Select all

function updatedomoticz(idx, switchcmd)
    commandArray[#commandArray + 1] = {['OpenURL']='http://10.27.0.20/json.htm?type=command&param=switchlight&idx='..idx..'&switchcmd='..switchcmd}
end
Jos

Re: Help on LUA script

Posted: Friday 01 December 2017 17:25
by JimmyH1969
When using multiple commandarrays you need to use it like this...

commandArray[1]=
commandArray[2]=

Edit: to late ;)

Re: Help on LUA script

Posted: Monday 04 December 2017 14:05
by g00fy
Great! It's working now as expected.

Thanks guys!