Page 1 of 1

LUA If then problem

Posted: Monday 04 April 2016 0:31
by Henri
Hello,

I'm using 3.4834 running on Ubuntu 14.04 and this script was working before 3.4834.
The script is looking for a device change and if this device is an Door/Window contact (starting with DC in the device name) the alarm variable is set to 1.

commandArray = {}

Sound = uservariables["Sound"]
SMS = uservariables["SMS"]
Zwave_siren = uservariables["Zwave_siren"]
Ring = uservariables["Ring"]

local alarm = 0

Logging = true
d = otherdevices
device = ""
for i, v in pairs(devicechanged) do
if (#device == 0 or #i < #device) then device = i end
end
if (Logging) then print("*** Triggered by " .. device .. " now " .. d[device]) end

--
-- Main -------------------------------------
--

sm = device:sub(1,2)

if (sm == 'DC' or sm == 'MD') then

-- Check for Bell ring
if (globalvariables['Security'] == 'Disarmed') then
if ((d[device] == 'Open') or (d[device] == 'Alarm') and Ring == 1) then
print ('*** Ring the bell on contact *** '.. d[device])
os.execute('play /sounds/sound_doorbell.wav')
end
end

-- The problem starts inside this If then below , even when the d[device] = normal the alarm is set to 1

if (sm == 'DC') then
if ((d[device] == 'Open') and (globalvariables['Security'] == 'Armed Away') or (globalvariables['Security'] == 'Armed Home')) then
alarm = 1
print ('*** Attention : Alarm detected on Door/Window *** '.. device)
end
if ((d[device] == 'Alarm') and (globalvariables['Security'] == 'Armed Away') or (globalvariables['Security'] == 'Armed Home')) then
alarm = 1
print ('*** Attention : Alarm detected on Door/Window *** '.. device)
end
end

Thank you in advance for your help.
Regards Henri

Re: LUA If then problem

Posted: Monday 04 April 2016 18:45
by jvdz
Maybe the extra brackets are required.
Jos

By the way: I did a quick format to make the source a little more readable and noticed you are missing an end statement in the posted code.

Code: Select all

commandArray = {}

Sound = uservariables["Sound"]
SMS = uservariables["SMS"]
Zwave_siren = uservariables["Zwave_siren"]
Ring = uservariables["Ring"]

local alarm = 0

Logging = true
d = otherdevices
device = ""
for i, v in pairs(devicechanged) do
	if (#device == 0 or #i < #device) then device = i end
end
if (Logging) then print("*** Triggered by " .. device .. " now " .. d[device]) end

--
-- Main -------------------------------------
--

sm = device:sub(1, 2)

if (sm == 'DC' or sm == 'MD') then

	-- Check for Bell ring
	if (globalvariables['Security'] == 'Disarmed') then
		if ((d[device] == 'Open') or (d[device] == 'Alarm') and Ring == 1) then
			print('*** Ring the bell on contact *** '.. d[device])
			os.execute('play /sounds/sound_doorbell.wav')
		end
	end

	-- The problem starts inside this If then below , even when the d[device] = normal the alarm is set to 1

	if (sm == 'DC') then
		if ((d[device] == 'Open') and ((globalvariables['Security'] == 'Armed Away') or (globalvariables['Security'] == 'Armed Home'))) then
			alarm = 1
			print('*** Attention : Alarm detected on Door/Window *** '.. device)
		end
		if ((d[device] == 'Alarm') and ((globalvariables['Security'] == 'Armed Away') or (globalvariables['Security'] == 'Armed Home'))) then
			alarm = 1
			print('*** Attention : Alarm detected on Door/Window *** '.. device)
		end
	end
end

Re: LUA If then problem

Posted: Monday 04 April 2016 19:05
by Henri
Thank you, i will try it.

It works now with the extra brackets.