Responding to Elapsed Time

I feel like I’m probably asking every question that a new developer goes through…I’m trying to be a good citizen and do some due diligence searches first, but not turning up much.

Context: I’m sending a command to my serial device to “scan” for devices. Once I send the “scan for devices” command I set a state variable so that as responses come from the device, I can more appropriately handle them in the block. (The reponses usually come in about one every second or two as devices are found.)

Unfortunately, the serial device does not send any indication when it’s finished. The only way you know is that you don’t see any incoming responses for several seconds. I would like to take the Vera device out of “listen” mode automatically, rather than force the user to press a “stop” or off button. So in short, every time I process a message while my device is in “listen” mode, I want to reset a timer to say 10 seconds. And after that time expires perform an action.

Is there a best practice way to do something like this?

After each device use call_delay with a timeout of 10s.
But if you have 10 devices, you will get 10 callbacks … each callback will have to compare the current time to the time when the LAST device was added.


You can start and async 1 second timer, that renews itself until 10s has passed since the LAST device was added.


I can wrap my head around the first method easliy. I could conceive of cases where there up to maybe 50 devices / callbacks going on checking for the expiration.

I beleive I understand your second method would it be more or less like this?


[write a time-stamp to a state variable for “last message received”]
luup.call_timer (“checkScanDone”, 1, “1”, “”, “”)
[start polling mode and begin polling - as each message is read from the serial device, update the state variable time-stamp ]

function checkScanDone ()
if [current time] […using os.time ?] is greater then 10 seconds past the most recent time stamp then
else luup.call_timer ("checkScanDone", 1, "1", "", "") end end

So I guess the choice is 1 call backs per device, or potentially recursing 150+ calls to checkScanDone (assume 3 seconds between serial device responses? My gut tells me to go with option A, but is there a reason to go with one approach over the other?


There is no need to write the last timestamp to a state variable … just use a global variable … there is no value to this when Vera restarts.

Each plugin has it’s own LUA namespace … so you will not clash with anything else.

Thanks…related question…can the UI access global variables, or only state variables?

UI can access state variables
or Call a handler in your plugin and return what ever you want.

Thanks, RichardTSchaefer,

Yes, I currently access state variables from the UI. I just wanted to make sure I understood the scope of luup global variables. In other words, the only time to go through the hassle and resources of device state variables (if my understanding is correct) is

  1. If you want the variable to persist with the device (though restarts, etc.) and/or
  2. If you want to access the variable with the UI.

In all other cases a global variable can be used if you want to access it across various luup functions / actions / procedures. Though I understand that the “scene” space is different from the implementation space.

That sums it up.