How can I make a timer that can be reset by one or more tripped sensor?
Thanks!
How can I make a timer that can be reset by one or more tripped sensor?
Thanks!
[quote=“dawiinci, post:1, topic:179444”]How can I make a timer that can be reset by one or more tripped sensor?
Thanks![/quote]
Use Program Logic Timer Switch (PLTS). It is now available for UI6.
If you must do it using Lua, you could use an interval schedule or luup.call_delay(…) as a timebase for a counter. Then add Lua for your sensor triggers to reset the counter. If the counter reaches the target, initiate the required action.
Thanks.
If I use luup.call_delay(…, delay) and delay is a global variable I can use a Trigger (Event luup) to change this delay even if the scene (luup) is already running?
[quote=“dawiinci, post:3, topic:179444”]Thanks.
If I use luup.call_delay(…, delay) and delay is a global variable I can use a Trigger (Event luup) to change this delay even if the scene (luup) is already running?[/quote]
The call starts a timer. Changing the value of the variable used to pass the required delay will have no effect on the running timer.
Ok, so how can I cange this delay?
Once it has started running, you can’t. That’s why I suggested using it as a timebase and counting it.
Another approach would be to use the optional string parameter in luup.call_delay(…) as a timer number. Then you could initiate a new timer with an incremented number and have your code ignore the call-back from the old, redundant timer.
I think I understand what you suggest.
So I define a variable timestamp, and a timespan in seconds (delay).
Every 10 sec, I loop the check if the difference from timestamp to actual time is > than timespan and if so, switch off the light, otherwise loop again. I can then change the timespan variable if needed (trigger).
So in code it would look like this:
function timestamp() -- defines start time
function check() -- check if timespan has passed , looping
function trigger() -- new trigger changes variable timestamp (might have to be inside check function)
I’m not completely sure about how functions are processed. Is this possible to have one function looping when the next one already runs? Are they run sequentially or parallel?
Also I have some other questions about code I hope to clarify. Maybe you can help me out with this too.
If i use x=2 in one of my scenes, x can be printed from every other scene running afterward, even if the first one ended already?
If I do this in a function, do I have two variables - one local (not a number) and one global (number)?
local lastTrip = luup.variable_get (…) or os.time()
lastTrip = tonumber (lastTrip)
If I declare the local variable in a scene but never use end (beside of functions) in my code will it be available for other scenes?
If I declare the local variable inside a function it is available ONLY in this function.
Will global variables ever expire? (reboot, time, …)
Do I have to “exit” a scene? Should I use return true/false?
Return false cancels the scene, but what about return true. What does it do if I only use luup code?
Thanks!
Let’s start with Scene Lua 1.0.1. I also recommend getting a copy of the book Programming in Lua which I think is also downloadable.
If i use x=2 in one of my scenes, x can be printed from every other scene running afterward, even if the first one ended already?If you don't declare a variable as [i]local[/i] and it is not defined in a function or loop, it is global so could be accessed by another scene - even if the first scene has ended. This is because all scenes are run in the same Lua environment - along with Startup Lua.
If I do this in a function, do I have two variables - one local (not a number) and one global (number)? local lastTrip = luup.variable_get (...) or os.time() lastTrip = tonumber (lastTrip)No. Once you have declared [i]lastTrip[/i] as [i]local[/i], subsequent references within the same scope (e.g. scene or function) will be to that same variable. A locally declared variable hides a global one with the same name. After the second statement, [i]lastTrip[/i] is a local variable of type number.
If I declare the local variable in a scene but never use end (beside of functions) in my code will it be available for other scenes?Local variables are destroyed when the scene code exits. This will be when a [b]return[/b] statement is encountered or there are no further statements to be executed. The [b]end[/b] statement is used to terminate code blocks - like functions, if structures, for loops, etc. It doesn't mean [i]stop[/i].
If I declare the local variable inside a function it is available ONLY in this function.Yes. It's scope is the function.
Will global variables ever expire? (reboot, time, ...)They will cease to exist when Vera is restarted. If they have not been initialized, they will return a value of nil. You can, of course, define global variables in Startup Lua so they will always be available to scene code. If you want the value to persist across restarts, you will need to save and restore it from a file, device variable, VariableContainer, etc.
Do I have to "exit" a scene? Should I use return true/false?Scene code will terminate when there are no more statements to be executed or a [b]return[/b] is encountered. There is no need to [i]exit[/i] a scene. The [b]return[/b] statement is used to control scene device actions (see next).
Return false cancels the scene, but what about return true. What does it do if I only use luup code?A [b]return false[/b] in a trigger's [b]Luup event[/b] causes that trigger to be ignored. A [b]return true[/b] allows the trigger to be processed. A [b]return false[/b] in the main scene [b]Luup[/b] prevents the scene's device actions from being [i]fired[/i]. A [b]return true[/b] allows the actions to be executed. If you are not using scene device actions, you don't need any return at all.
I think I understand what you suggest.So I define a variable timestamp, and a timespan in seconds (delay).
Every 10 sec, I loop the check if the difference from timestamp to actual time is > than timespan and if so, switch off the light, otherwise loop again. I can then change the timespan variable if needed (trigger).So in code it would look like this:
Code: [Select]
function timestamp() – defines start time
function check() – check if timespan has passed , looping
function trigger() – new trigger changes variable timestamp (might have to be inside check function)
[code]function xyzStartTimer(count)
xyzCount = 0
xyzTimer = count
xyzAbort = false
luup.call_delay(“xyzTick”,10)
end
function xyzStopTimer()
xyzAbort = true
end
function xyzChangeTimer(count)
xyzCount = 0
xyzTimer = count
end
function xyzTick()
if xyzAbort then return end
xyzCount = xyzCount + 1
if xyzCount >= xyzTimer then
– Required actions
else
luup.call_delay(“xyzTick”,10)
end
end[/code]
I'm not completely sure about how functions are processed. Is this possible to have one function looping when the next one already runs? Are they run sequentially or parallel?Normal Lua functions in a Vera scene will run sequentially. The code should be designed to run and terminate quickly to avoid blocking other scenes and functions.
A function called by a call-back from luup.call_delay(…) and other similar luup functions is a special case. It will continue to happen even though the scene code has finished execution. The call-back is made in global scope so none of the scene’s local variables will be visible to the function and may, if the code has terminated, not even exist.
Best Home Automation shopping experience. Shop at Ezlo!
© 2024 Ezlo Innovation, All Rights Reserved. Terms of Use | Privacy Policy | Forum Rules