20.04.2022, 08:48
(This post was last modified: 25.04.2022, 08:09 by davidchispas.)
Hello, using this great script I would like to modify it with your help.
So far this script is executed with the detection object. In my case, this object, through another script, is just a 1-second toggle to correct issues that sometimes occur when the detector does not send 'no detection'. The output object is modified with a checkwrite so that it only sends it once.
On the other hand, I have a Lock object and I want to use it to lock the detector and this script.
I would like if it is possible that this Blocking object, apart from blocking the script, could reset the time to zero and not modify the output object..
So far this script is executed with the detection object. In my case, this object, through another script, is just a 1-second toggle to correct issues that sometimes occur when the detector does not send 'no detection'. The output object is modified with a checkwrite so that it only sends it once.
On the other hand, I have a Lock object and I want to use it to lock the detector and this script.
I would like if it is possible that this Blocking object, apart from blocking the script, could reset the time to zero and not modify the output object..
Code:
-- Set output address
AddressOutput = '39/0/1' -- 01. 1 bit (boolean)
--ID Detector Zone
i = 1
-- Set input address
AddressInput = '9/1/'..i -- 01. 1 bit (boolean)
-- Set external time address (optional)
AddressExternalTime = '38/1/'..i -- 07. 2 byte unsigned integer
-- Set feedback adress of time left indication (optional)
AddressTimeLeft = '38/2/'..i -- 255 byte string
-- Seconds or Minutes
SetSec = '38/3/'..i -- Set to false for Minutes or true for seconds
-- Set blocking address
AddressBlocking = '38/4/'..i -- 01. 1 bit (boolean)
-- Use time left indication
UseTimeLeft = true -- Set to false if no time left indication is used
-- Set time delay (Used when external time is not available)
SetDelay = 120 -- 2 minutes
-- Set factor delay (Multiplies Delay)
SetFac = 1
-- Logic can be turned of by value 0
Off_by_Value_Zero = false
-- ************************************** END PARAMETERS ************************************ --
-- *************************** DON'T CHANGE ANYTHING UNDER THIS LINE ************************ --
Input_Value = event.getvalue()
if Off_by_Value_Zero == false and (Input_Value == false or Input_Value == 0) then
return
else
tpid = storage.get('PID:' .. _SCRIPTNAME)
if tpid == nil then
pid = os.getpid()
storage.set('PID:' .. _SCRIPTNAME, pid)
else
pid = os.getpid()
storage.set('PID:' .. _SCRIPTNAME, pid)
os.kill(tpid, signal.SIGKILL)
end
blocking = grp.getvalue(AddressBlocking)
if not blocking then
return
end
function Calculate_Time(StairCaseTime)
if StairCaseTime > (86400 -1) then
StairCaseTime = (86400 -1)
end
local hours = math.floor(StairCaseTime / 3600 % 24)
local minutes = math.floor(StairCaseTime / 60 % 60)
local seconds = math.floor(StairCaseTime % 60)
time = {
day = 0,
hour = hours,
minute = minutes,
second = seconds,
}
time =string.format('%02d:%02d:%02d',time.hour,time.minute,time.second)
return time
end
if Input_Value == true or Input_Value == 1 then
ValueExternalTime = grp.getvalue(AddressExternalTime) or SetDelay
if grp.getvalue(SetSec) then
Multiply_Seconds = 1
else
Multiply_Seconds = 60
end
StairCaseTime = ValueExternalTime * Multiply_Seconds * SetFac
grp.checkwrite(AddressOutput, true)
if UseTimeLeft == true then
if StairCaseTime > 0 then
time = Calculate_Time(StairCaseTime)
grp.write(AddressTimeLeft, time)
repeat
StairCaseTime = StairCaseTime - 1
time = Calculate_Time(StairCaseTime)
grp.write(AddressTimeLeft, time)
os.sleep(1)
until StairCaseTime == 0
end
else
os.sleep(StairCaseTime)
end
grp.write(AddressOutput, false)
elseif Input_Value == false or Input_Value == 0 then
ValueOutput = grp.getvalue(AddressOutput)
if ValueOutput == false then
if UseTimeLeft == true then
time = Calculate_Time(0)
grp.write(AddressTimeLeft, time)
end
grp.write(AddressOutput, false)
end
end
storage.delete('PID:' .. _SCRIPTNAME)
end