Help on LUA script

Moderator: leecollings

Post Reply
g00fy
Posts: 16
Joined: Friday 08 September 2017 8:27
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Location: Dordrecht, The Netherlands
Contact:

Help on LUA script

Post 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!
User avatar
jvdz
Posts: 2267
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Help on LUA script

Post 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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
JimmyH1969
Posts: 153
Joined: Tuesday 28 June 2016 16:38
Target OS: Windows
Domoticz version: Beta
Location: Amersfoort NL
Contact:

Re: Help on LUA script

Post by JimmyH1969 »

When using multiple commandarrays you need to use it like this...

commandArray[1]=
commandArray[2]=

Edit: to late ;)
g00fy
Posts: 16
Joined: Friday 08 September 2017 8:27
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Location: Dordrecht, The Netherlands
Contact:

Re: Help on LUA script

Post by g00fy »

Great! It's working now as expected.

Thanks guys!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest