Help on a script involving .filter() or ipairs()

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
acaonweb
Posts: 78
Joined: Thursday 23 March 2017 14:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Help on a script involving .filter() or ipairs()

Post by acaonweb »

Hi guys,
i'm not so expert in Lua and i'm trying to improve my scripting level.
I'm trying to make a script that depending from a list of PIR status, tell me id there is people at home and relative groups of light are switched off.

now i wrote that.

Code: Select all

            local PIR = {
                "Body Sensor Scala",
                "Body Sensor Camera",
                "Body Sensor Soggiorno",
                "Body Sensor Bagno",
                "Body Sensor Cucina",
                "Body Sensor Sottotetto",
            }
            -- test presennce
            local emptyhome = true
            local PIRscan = dz.devices().filter(PIR)
            PIRscan.forEach(function(thisPIR)
                if(thisPIR.lastUpdate.minutesAgo < 10) then
                    dz.log(thisPIR.name)
                    emptyhome= false
                end
                -- check
                 if (emptyhome ) then 
                    dz.log("noone")
                    --variabile globale
                    dz.variables('pplAtHome').set(0)
                    dz.notify('Casa',"none",dz.PRIORITY_NORMAL,'noppl','iPhone6S',dz.NSS_PUSHOVER)
                 else
                    dz.log("someone")
                    --variabile globale
                    dz.variables('pplAtHome').set(1)
                    dz.notify('Casa',"someone",dz.PRIORITY_NORMAL,'nyesppl','iPhone6S',dz.NSS_PUSHOVER)                    
            end
i'm not able to associate the switch to close.
Infact the next step should be redefine PIR like this

Code: Select all


            local PIR = {
                {"Body Sensor Scala", {"switch1","switch2"}},
                {"Body Sensor Camera", {"switch3","switch4"}},
                {"Body Sensor Bagno", {"switch5","switch6"}},
                {"Body Sensor Cucina", {"switch7","switch8"}}, 
                {"Body Sensor Sottotetto", {"switch9","switch10"}}, 
            }
            -- test presennce
            local emptyhome = true
                -- HERE I NEED HELP
                -- check pir update, if false switch off corresponding swithces 

                 if (emptyhome ) then 
                    dz.log("noone")
                    --variabile globale
                    dz.variables('pplAtHome').set(0)
                    dz.notify('Casa',"none",dz.PRIORITY_NORMAL,'noppl','iPhone6S',dz.NSS_PUSHOVER)
                 else
                    dz.log("someone")
                    --variabile globale
                    dz.variables('pplAtHome').set(1)
                    dz.notify('Casa',"someone",dz.PRIORITY_NORMAL,'nyesppl','iPhone6S',dz.NSS_PUSHOVER)                    
            end            
Any ideas?

Thanks in advance
Fabrizio
User avatar
boum
Posts: 130
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: Help on a script involving .filter() or ipairs()

Post by boum »

I'd do something like that:

Code: Select all

            local PIR = {
                ["Body Sensor Scala"] = {"switch1","switch2"},
                ["Body Sensor Camera"] = {"switch3","switch4"},
                ["Body Sensor Bagno"] = {"switch5","switch6"},
                ["Body Sensor Cucina"] = {"switch7","switch8"}, 
                ["Body Sensor Sottotetto"] = {"switch9","switch10"}, 
            }
            -- test presence
            local emptyhome = true
                -- check pir update, if false switch off corresponding switches 
		for pirName, switches in pairs(PIR) do
		    local thisPIR = dz.devices(pirName)
                    if(thisPIR.lastUpdate.minutesAgo < 10) then
                        dz.log(thisPIR.name)
                        emptyhome= false
                    else
                        -- switch off the associated list of devices
                        for i, deviceName in ipairs(switches) do
                            dz.devices(deviceName).switchOff().checkFirst()
                        end
                    end
		end
		-- then same as before for notifications
I changed the table of tables with a single table as PIRnam -> table of devices
acaonweb
Posts: 78
Joined: Thursday 23 March 2017 14:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Help on a script involving .filter() or ipairs()

Post by acaonweb »

great!!!!
thanx
Post Reply

Who is online

Users browsing this forum: Bing [Bot], heggink and 1 guest