Page 1 of 1

Check doors and send command

Posted: Tuesday 04 April 2017 22:49
by Martijn85
I use this script to check if my backdoor is open, if so then it wil send a message and set the setpoint to 12°C.
This works good, but i also have front door. If i use the same script for the front door (in total 2 different scripts) only the backdoor working.

When i rename the frontdoor scripts name so the front door is above the backdoor script, only the frontdoor is working.

Can you only run one "time difference" script?

How can i merge this scripts so it checks the back and front door?

Code: Select all

-- Automatically adjust heating setpoint to 12 when back door is left open.

t1 = os.time()
s = otherdevices_lastupdate['Achterdeur']
heater = otherdevices_svalues['Toon Thermostaat']

year = string.sub(s, 1, 4)
month = string.sub(s, 6, 7)
day = string.sub(s, 9, 10)
hour = string.sub(s, 12, 13)
minutes = string.sub(s, 15, 16)
seconds = string.sub(s, 18, 19)

commandArray = {}

t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
difference = (os.difftime (t1, t2))

if (otherdevices['Achterdeur'] == 'Open' and difference > 59 and difference < 65) then
        commandArray['SendNotification']='Achterdeur waarschuwing#De Achterdeur staat al meer dan 5 minuten open'
        commandArray['UpdateDevice']='335|0|12.00'
        print("De Achterdeur staat open, zet de thermostraat op 12 graden celsius")
end

return commandArray

Re: Check doors and send command

Posted: Wednesday 05 April 2017 8:39
by tontze
I think other script says door is closed, dont do anything, and other says door open, do something --> conflict ?

Im not guru on these things, so i might be wrong :D

Re: Check doors and send command

Posted: Wednesday 05 April 2017 14:22
by emme
is the front door a door contact or a simple switch?

the value could change from Open/Closed to On/Off

Re: Check doors and send command

Posted: Saturday 08 April 2017 23:08
by Martijn85
emme wrote:is the front door a door contact or a simple switch?
It's a door contact, so it will be open or closed.

Maybe someone can tell me how to putt a second device in the script and check the times for both devices and then send a command for one or both devices if there are longer than 5 minutes open?

Re: Check doors and send command

Posted: Sunday 09 April 2017 0:42
by Siewert308SW
Maybe something like this

Code: Select all


--[[
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-

	@ script_time_doors.lua
	@ Check doors
	
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
--]]

--
-- **********************************************************
-- Time Difference
-- **********************************************************
--

function timedifference(s)
	year = string.sub(s, 1, 4)
	month = string.sub(s, 6, 7)
	day = string.sub(s, 9, 10)
	hour = string.sub(s, 12, 13)
	minutes = string.sub(s, 15, 16)
	seconds = string.sub(s, 18, 19)
	t1 = os.time()
	t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
	difference = os.difftime (t1, t2)
	return difference
end

time_open_max = 300 --300 seconds is 5min
	  
commandArray = {}
-- Frontdoor		
if otherdevices['Voordeur'] == 'Open' 
and timedifference(otherdevices_lastupdate['Voordeur']) > time_open_max 
then	
commandArray['SendNotification']='Voordeur waarschuwing#De voordeur staat al meer dan 5 minuten open'
commandArray['UpdateDevice']='335|0|12.00'
print("De voordeur staat open, zet de thermostraat op 12 graden celsius")
end
		
-- BackDoor		
if otherdevices['Achterdeur'] == 'Open' 
and timedifference(otherdevices_lastupdate['Achterdeur']) > time_open_max 
then	
commandArray['SendNotification']='Achterdeur waarschuwing#De Achterdeur staat al meer dan 5 minuten open'
commandArray['UpdateDevice']='335|0|12.00'
print("De Achterdeur staat open, zet de thermostraat op 12 graden celsius")
end
		
return commandArray

Re: Check doors and send command

Posted: Sunday 09 April 2017 9:02
by gerard76

Code: Select all

commandArray = {}
doors               = {'Voordeur','Achterdeur'}
max_open        = 300 -- in seconds

function timedifference(timestamp)
  y, m, d, H, M, S = timestamp:match("(%d+)-(%d+)-(%d+) (%d+):(%d+):(%d+)")
  return os.difftime(os.time(), os.time{year=y, month=m, day=d, hour=H, min=M, sec=S})
end

for i, door in pairs(doors) do
  if otherdevices[door] == 'Open' and timedifference(otherdevices_lastupdate[door]) > max_open then
    commandArray['SendNotification'] = 'Waarschuwing#De '..door..' staat al meer dan 5 minuten open'
    commandArray['UpdateDevice']='335|0|12.00'
    print("De "..door.." staat open, zet de thermostraat op 12 graden celsius")
  end
end

return commandArray

Re: Check doors and send command

Posted: Sunday 09 April 2017 22:48
by Martijn85
Wow! Thanks for sharing, works great guys :D

Re: Check doors and send command

Posted: Monday 17 April 2017 17:36
by mvveelen
Nice, and may come in handy But, what if you receive the message, close the door.....it should return to the old (temp) setting, shouldn't it? So the script might be incomplete to accomplish a full automated system in this case.