Logic Machine Forum
Cancel the saved command line after the os.sleep() command - 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: Cancel the saved command line after the os.sleep() command (/showthread.php?tid=4186)



Cancel the saved command line after the os.sleep() command - phongvucba - 10.08.2022

Hi guys ! I am having a problem, and want to solve it. I have the following code:
--------------------------------------
value = event.getvalue()
if value==true then
  os.sleep(60) --1p
  grp.write('6/1/18',false)
end
--------------------------------------
When I enable the value 6/1/18 it runs 60 seconds then 6/1/18==false. In some cases I want to be on 6/1/18=true, then on the 50th second I'm 6/1/18==false, on the 55th I'm 6/1/18=true, and I want it = =true forever. But after 5 seconds, it turned off 6/1/18=false(automatically), because the initial command it saved for 1 minute was =false . So I want when I activate it, it only saves the last command line, not the previous command lines. For example, I wrote the following:
-------------------------------------
value = event.getvalue()
if value==true then
  os.sleep(60) --1p
  grp.write('6/1/18',false)
end
break;
---------------------------------------
Is it right?

Thank so much ! everyone Smile


RE: Cancel the saved command line after the os.sleep() command - Daniel - 10.08.2022

As I see here you are using event script and same object triggers the script and and you write to the same object which is triggering the script again. Lucky it will run only once as you have the condition there. The only way to stop event script is to kill PID which you can see in staircase timer example on the forum. https://forum.logicmachine.net/showthread.php?tid=737&pid=4175#pid4175

Why don't you do it like this
https://forum.logicmachine.net/showthread.php?tid=3538&pid=22863#pid22863


RE: Cancel the saved command line after the os.sleep() command - phongvucba - 13.08.2022

Hello Daniel !
I tried killing PID, but it gives error, do you know what is the error?
"User script:6: bad argument #1 to 'kill' (number expected, got nil)
stack traceback:
[C]: in function 'kill'
User script:6: in main chunk"
------------------event 6/1/18----
-- object mapped to this event must have its data type set
value = event.getvalue()
pid = os.getpid()
oldpid = storage.get('MyPID')
if oldpid ~= pid then
os.kill(oldpid, signal.SIGKILL)
os.sleep(60)
grp.write('6/1/18',false)
storage.set('MyPID', pid)
end
---------------------------------
Am I writing like this? I want that every 60 seconds without effect, it will be ==false. And if there is an impact, for example, the 55th second, if it has an effect, it will reset the time to be counted as 60 seconds later ==false
Thank so much!


RE: Cancel the saved command line after the os.sleep() command - admin - 15.08.2022

Use this:
Code:
key = 'pid_' .. event.dst

pid = storage.get(key)
if pid then
  os.kill(pid, signal.SIGKILL)
end

value = event.getvalue()
if value then
  storage.set(key, os.getpid())
  os.sleep(60)
  grp.write(event.dst, false)
end

storage.delete(key)



RE: Cancel the saved command line after the os.sleep() command - pioneersteffen - 17.08.2022

Are there other options to abort just the os.sleep command and let the script run further? Many thanks.


RE: Cancel the saved command line after the os.sleep() command - admin - 18.08.2022

@pioneersteffen, what do you want to achieve with this? One solution is a loop that checks the current object value with short sleeps in between. But if the object value toggles twice quickly the script won't be able to catch it. Another option is to use a resident script like in this example: https://forum.logicmachine.net/showthread.php?tid=4193&pid=27075#pid27075


RE: Cancel the saved command line after the os.sleep() command - pioneersteffen - 18.08.2022

For example I want to set the presence in the house to the status inactive if there is no movement in the house for 180s. Therefore I made short sleeps, reread the presence object, check the object (no movement present), next sleep etc. That‘s why I was wondering if there is a better approach to stop a long sleep interrupt?


RE: Cancel the saved command line after the os.sleep() command - admin - 18.08.2022

This example will stop previously running script when an object is updated again: https://forum.logicmachine.net/showthread.php?tid=4186&pid=27062#pid27062


RE: Cancel the saved command line after the os.sleep() command - pioneersteffen - 18.08.2022

Many thanks I will try this solution.


RE: Cancel the saved command line after the os.sleep() command - phongvucba - 26.08.2022

Hi everyone ..!
I have solved it ..Thank so much..Admin and everyone.
..Smile