Logic Machine Forum
Understanding timed scripting - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Understanding timed scripting (/showthread.php?tid=250)



Understanding timed scripting - FatMax - 19.03.2016

I´m having some problems understanding why my tried timers don´t work as I want them to.

I have these two scripts, in two scheduled scripts:

Code:
-- Trigger
trig = '3/5/7'
-- set 100%
max = '3/5/6'
-- store scene
lagre = '1/4/5'
-- get scene
hent = '1/4/6'
--status
status = '3/4/38'
-- locking PIRs
PIRlock = '1/3/19'
-- find required object
obj1 = grp.find(trig)
-- object exists and current state is "on"
if obj1 and obj1.data then
grp.write(lagre, 0, dt.uint8)
grp.write(max, 100, dt.scale)
grp.write(status, 1, dt.bool)
grp.write(PIRlock, 1, dt.bool)
-- delta is in seconds
delta = os.time() - obj1.updatetime
-- switch off when timer expires
if delta >= 60 * 60 then
grp.write(hent, 128, dt.uint8)
grp.write(status, 0, dt.bool)
grp.write(PIRlock, 0, dt.bool)
end
end

The first one is to trigger 100% lighting for one hour, for cleaning purposes. It also locks the PIRs, saves the lighting state before setting 100% and sets a status object so we know it is in play.

However, when I activate the script, everything just goes to 100% immediately. If i try to put it in a normal event script, nothing happens when I push the button....
Code:
-- group address or name
addr1 = '3/5/8'
addr2 = '1/1/50'
addr3 = '3/5/9'
-- find required object
obj = grp.find(addr1)
-- object exists and current state is "on"
if obj and obj.data then
grp.write(addr2, 28, dt.scale)
grp.write(addr3, true)
-- delta is in seconds
delta = os.time() - obj.updatetime
-- switch off when timer expires
if delta >= 6 * 60 then
grp.write(addr2, 22, dt.scale)
end
if delta >= 12 * 60 then
grp.write(addr2, 16, dt.scale)
end
if delta >= 18 * 60 then
grp.write(addr2, 10, dt.scale)
end
if delta >= 24 * 60 then
grp.write(addr2, 4, dt.scale)
end
if delta >= 30 * 60 then
grp.write(addr2, 0, dt.scale)
grp.write(addr3, false)
end
end

This last one is for slowly dimming down the lights in a kids room. However, when I activate this script, whenever the lights is turned on normal, it will always dim down and go dark after a while....

What am I missing here..?


RE: Understanding timed scripting - admin - 21.03.2016

First script should be split into one event and one scheduled. Event should handle levels and PIR lock depending on received value (true/false). Scheduled script should just send false if event script control object has been on for 1 hour. You can use these examples for auto-off functionality: http://forum.logicmachine.net/showthread.php?tid=43

Second script will always work when 3/5/8 is on. Shouldn't the script write false to 3/5/8 in the end? Also, your if sequence is somewhat incorrect, as with delta increasing more ifs will be triggered and unwanted telegrams will be sent. You need to reverse it and use elseif:
Code:
-- group address or name
trigger = '3/5/8'
control = '1/1/50'
status = '3/5/9'
-- find required object
obj = grp.find(trigger)

-- object exists and current state is "on"
if obj and obj.data then
  -- delta is in seconds
  delta = os.time() - obj.updatetime

  if delta >= 30 * 60 then
    value = 0

    grp.write(status, false, dt.bool)
    grp.write(trigger, false, dt.bool)
  elseif delta >= 24 * 60 then
    value = 4
  elseif delta >= 18 * 60 then
    value = 10
  elseif delta >= 12 * 60 then
    value = 16
  elseif delta >= 6 * 60 then
    value = 22
  else
    value = 28

    -- send status update only when status state is off
    statvalue = grp.getvalue('status')
    if not statvalue then
      grp.write(status, true, dt.bool)
    end
  end

  grp.write(control, value, dt.scale)  
end