Page 1 of 1

Lua - loop through devices

Posted: Saturday 19 August 2017 19:26
by tequila
I cannot figure out how to loop through devices to determine whether any motion sensor is on (update variable 'PRESENCE_DETECTED' to 'on') or whether ALL of them are off (then I would need to update 'PRESENCE_DETECTED' variable to 'off').

The first part (updating variable to ON if any of the sensors is on) is working fine.
However, I am having issues with the other part - if all sensors are off, then update variable to off

My script:
Devices I need to loop through

Code: Select all

--list of all my motion sensors
motion_triggers ={
                    'OB_MS6 - Motion Sensor', 
                    'LO_FGMS001 - Sensor',
                    'KP_FGMS001 - Sensor',
                    'CH_FGMS001 - Sensor',
                }  
The working part:

Code: Select all

for i, motion_trigger in ipairs(motion_triggers) do --loop through motion_triggers
    if devicechanged[motion_trigger] then --any sensor changed
	    if otherdevices[motion_trigger] == 'On' then --any sensor is ON 
	        if uservariables['PRESENCE_DETECTED'] ~= 'on' then --only update variable if it is not equal to on (to keep update time)
                commandArray['Variable:PRESENCE_DETECTED'] = 'on' --update PRESENCE_DETECTED variable to ON
            break            
            end
        end
    end    
end 
How do I modify the code to update the 'PRESENCE_DETECTED' variable to off if all sensors are off?
I have spent all day on this, so any help would be welcome.

Re: Lua - loop through devices

Posted: Saturday 19 August 2017 19:58
by DomoFrank
You can try to use this:

Code: Select all

local S1 = domoticz.devices.('OB_MS6')
local S2 = domoticz.devices.('LO_FGMS001')
local S3 = domoticz.devices.('KP_FGMS001')
local S4 = domoticz.devices.('CH_FGMS001')

If (S1.state == 'Off' and  S2.state == 'Off' and S3.state == 'Off' and S4.state == 'Off') then 		-- (if all sensors are off...)
	if uservariables['PRESENCE_DETECTED'] == 'On' then 
		commandArray['Variable:PRESENCE_DETECTED'] = 'Off'
	end   
end

Re: Lua - loop through devices

Posted: Saturday 19 August 2017 20:44
by dannybloe
Use dzvents. It's trivial to do it.

Re: Lua - loop through devices

Posted: Saturday 19 August 2017 21:46
by tequila
Thanks DomoFrank,

I actually had a similar script before but wanted to get rid of changing all my IF scripts when adding new sensors to the system.

So I am trying to do this via looping through a list of devices since it is easier when adding new sensors.

Dannybloe, thanks for a tip, I will explore dzvents.

Re: Lua - loop through devices

Posted: Sunday 20 August 2017 6:53
by tequila
in the end I solved this by using additional virtual switch with off-delay and having another script that based on this virtual switch updates the PRESENCE_DETECTED variable.
I will have a look into dzvents anyway.

Thank you both!

Re: Lua - loop through devices

Posted: Sunday 20 August 2017 8:37
by darlomrh

Re: Lua - loop through devices

Posted: Sunday 20 August 2017 11:06
by tequila
darlomrh wrote: Sunday 20 August 2017 8:37 Hi,
Would this help?
Interesting! I will definitely have a look.