Lua Door left open alarm

Moderator: Telldus

Post Reply
geirbakkw
Posts: 15
Joined: Fri Mar 17, 2023 9:45 am

Lua Door left open alarm

Post by geirbakkw »

hi, I wrote a script for checking if the door was left open.

Should I approach the script any other way?
was thinking about turning on an other dummy device and connecting a event to it so i can send push message to my mobile.

-- File: DoorOpen.lua


local deviceManager = require "telldus.DeviceManager"
local door = "GangDoor" --door sensor
local lys = "Gang" -- light to blink
local delay = 3 --blink delay
local alarmdelay = 180 --alarm wait
local aa = "DorAlarmAktiv" --dummy switch for activating the alarm (or disable if door should stay open)

function onDeviceStateChanged(device, state, stateValue)

local a = deviceManager:findByName(aa)
if a == nil then
print("Device not found")
end
print(a:state())
if (a:state() == 2) then
return
end
if running == true then
return
end
local sensor = deviceManager:findByName(door)
if sensor == nil then
print("Device not found")
end
local bryter = deviceManager:findByName(lys)
if bryter == nil then
print("Device not found")
end
if (sensor:state() == 1) then
--print("door open")
sleep(alarmdelay*1000)
print("sleep")
running = true

running = false
end
running = true
while (sensor:state() == 1) do -- As long as the door is open
print("Tick tack")
bryter:command("turnoff", nil, "lys")
sleep(delay*1000)
bryter:command("turnon", nil, "lys")
sleep(delay*1000)
end
running = false
end
micke.prag
Site Admin
Posts: 2243
Joined: Fri Mar 17, 2023 9:45 am
Location: Lund
Contact:

Re: Lua Door left open alarm

Post by micke.prag »

Some comments.
  • When DeviceManager::findByName() returns nil you should not continue the function
  • Check your running-variable, this one is probably not working as expected
  • If DorAlarmAktiv is turned off in the while loop your should probably exit.
Micke Prag
Software
Telldus Technologies
geirbakkw
Posts: 15
Joined: Fri Mar 17, 2023 9:45 am

Re: Lua Door left open alarm

Post by geirbakkw »

Hi, error corrected.

Do you se any other errors?

-- File: DoorOpen.lua


local deviceManager = require "telldus.DeviceManager"
local door = "GangDoor" --door sensor
local lys = "Gang" --light to blink
local delay = 3 --blink delay
local alarmdelay = 180 --alarm wait
local aa = "DorAlarmAktiv" --dummy switch for activating the alarm (or disable if door should stay open)

function onDeviceStateChanged(device, state, stateValue)

local a = deviceManager:findByName(aa)
if a == nil then
print("Device not found")
return
end

--print(a:state())

if (a:state() == 2) then
return
end

if running == true then
return
end

local sensor = deviceManager:findByName(door)
if sensor == nil then
print("Device not found")
return
end
local bryter = deviceManager:findByName(lys)
if bryter == nil then
print("Device not found")
return
end

if (sensor:state() == 1) then
--print("door open")
sleep(alarmdelay*1000)
--print("sleep")
running = true
end

running = true
while (sensor:state() == 1) do -- As long as the door is open
print("Tick tack")
bryter:command("turnoff", nil, "lys")
sleep(delay*1000)
bryter:command("turnon", nil, "lys")
sleep(delay*1000)
if (a:state() == 2) then break end --stop blinking if deactivate alarm activated during sequence
end
running = false

end
micke.prag
Site Admin
Posts: 2243
Joined: Fri Mar 17, 2023 9:45 am
Location: Lund
Contact:

Re: Lua Door left open alarm

Post by micke.prag »

I am still not sure about your running variable. Why do you set it to true after your sleep?
What about this instead?

Code: Select all

if (sensor:state() ~= 1) then
	return
end
	
running = true
--print("door open")
sleep(alarmdelay*1000)
--print("sleep")
while (sensor:state() == 1) do -- As long as the door is open
	print("Tick tack")
	bryter:command("turnoff", nil, "lys")
	sleep(delay*1000)
	bryter:command("turnon", nil, "lys")
	sleep(delay*1000)
	if (a:state() == 2) then break end --stop blinking if deactivate alarm activated during sequence
end
running = false
Micke Prag
Software
Telldus Technologies
geirbakkw
Posts: 15
Joined: Fri Mar 17, 2023 9:45 am

Re: Lua Door left open alarm

Post by geirbakkw »

Thx I changed the code as you suggested.

Code: Select all

-- File: DoorOpen.lua


	local deviceManager = require "telldus.DeviceManager"
	local door = "GangDoor" --sensor
	local lys = "Gang" --lys som skal blinke
	local delay = 3 --blinke frekvens
	local alarmdelay = 180 --alarm utsettelse
	local aa = "DorAlarmAktiv" --Deaktiver/aktiver
	
function onDeviceStateChanged(device, state, stateValue)

	local a = deviceManager:findByName(aa)
	if a == nil then
	   	print("Device not found")
    	return
	end
	
	--print(a:state())
	
	if (a:state() == 2) then
		return
	end
	
	if running == true then
		return
	end
	
	local sensor = deviceManager:findByName(door)
	if sensor == nil then
        print("Device not found")
		return
      end
	local bryter = deviceManager:findByName(lys)
	if bryter == nil then
        print("Device not found")
		return
      end
	
	if (sensor:state() ~= 1) then
   		return
	end
   
	running = true
		--print("door open")
	sleep(alarmdelay*1000)
		--print("sleep")
	while (sensor:state() == 1) do -- As long as the door is open
   		print("Tick tack")
   		bryter:command("turnoff", nil, "lys")
   		sleep(delay*1000)
   		bryter:command("turnon", nil, "lys")
   		sleep(delay*1000)
   			if (a:state() == 2) then break end --stop blinking if deactivate alarm activated during sequence
	end
		running = false
	
	
end
Post Reply