startup lua and libraries with openLuup

I realized that the device implementation files either run before or don’t have access to the startup lua. What is the best way to create a library of lua functions that the device implementation can access? And can the scene lua have access to the same library? I’m asking in the openLuup context, but I’m curious about Vera too. Thanks.

You have to appreciate that each device runs in its own global context, and that is different too from the shared scenes/startup/test one.

Of course, any module may use require to include functionality from other modules, and Lua’s package loader won’t load it multiple times (in a single Lua context.)

In Vera, plugins run in separate Lua instances, so they should be entirely separate. In openLuup, everything actually runs in one thread, although their global contexts are separate. This means that you’ll probably get a different result if you dig deep enough between Vera and openLuup.

A simple module like this, with a persistent variable:

local state 

return {
  put = function (x) state = x; end,
  get = function ( ) return state end,
}

can be used to experiment.

What I think you’d find on openLuup is that any scene/device using this module will share the same variable. On Vera, they won’t.

In fact, they ONLY truly ‘global’ context that I know of in Vera is that of the device variables which, of course, gets shared across the system, and this is the ‘right’ way to do it. I’d caution building something around undocumented quirks of any system. What is it, in fact, that you’re trying to do?

[quote=“akbooer, post:2, topic:196233”]What is it, in fact, that you’re trying to do?[/quote] Trying to avoid replicating functions in various places with the same functionality. Your suggestion of ‘require’ looks like what I need. Thanks!

Yes, absolutely right. If it’s functions, not data, that you need to share, that’s the way to do it.

However, because of changes Vera have made to the module/require functionality, you have to be a bit careful if you want exactly the same code to work in Vera and regular Lua (i.e. openLuup)

Ask if you really need to know.