Logic Machine Forum
Short - Long press - 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: Short - Long press (/showthread.php?tid=4575)



Short - Long press - Joep - 09.02.2023

Here's a simple script that calculate the time difference between 1 (true) and 0 (false) of a 1 bit object.
I have extended it with some examples on how to use it to calculate the ON time of an object and as a working hour timer.
 
Code:
long = 10 --set long press in seconds

value = event.getvalue()
object = grp.find(event.dst)

if value then
  if not storage.get('start') then
    storage.set('start', object.updatetime)
  end
else
  start = storage.get('start')
  if start then
      diff = os.difftime(object.updatetime, start)
  end
  storage.delete('start')
end

if diff then
  if diff >= long then
    --long press time in seconds
      log('long press: '..diff..' seconds') 
    else
    --short press time in seconds
      log('short press: '..diff..' seconds')
  end
  --time the object was ON in seconds
  log('object was ON for: '..diff..' seconds')
 
    hour = math.floor((diff / 3600)+ 0.5)

    if not storage.get('counter') then
    counter = hour
      storage.set('counter', counter)
    else
      counter = storage.get('counter')
      counter = counter + hour
      storage.set('counter', counter)
    end
      --total time the object was ON in hours
      log('hour counter: '..counter..' hours')
end