Lua - loop through devices

Moderator: leecollings

Post Reply
tequila
Posts: 60
Joined: Tuesday 02 August 2016 17:08
Target OS: -
Domoticz version:
Contact:

Lua - loop through devices

Post 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.
DomoFrank
Posts: 2
Joined: Friday 11 August 2017 21:48
Target OS: Linux
Domoticz version:
Contact:

Re: Lua - loop through devices

Post 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
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Lua - loop through devices

Post by dannybloe »

Use dzvents. It's trivial to do it.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
tequila
Posts: 60
Joined: Tuesday 02 August 2016 17:08
Target OS: -
Domoticz version:
Contact:

Re: Lua - loop through devices

Post 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.
tequila
Posts: 60
Joined: Tuesday 02 August 2016 17:08
Target OS: -
Domoticz version:
Contact:

Re: Lua - loop through devices

Post 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!
User avatar
darlomrh
Posts: 35
Joined: Monday 23 May 2016 8:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: UK
Contact:

Re: Lua - loop through devices

Post by darlomrh »

tequila
Posts: 60
Joined: Tuesday 02 August 2016 17:08
Target OS: -
Domoticz version:
Contact:

Re: Lua - loop through devices

Post by tequila »

darlomrh wrote: Sunday 20 August 2017 8:37 Hi,
Would this help?
Interesting! I will definitely have a look.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest