I’m being driven crazy by the simplest of problems. I’m sure someone can help me see the wood for the trees…
After spending years with UI5 and PLEG I’m in the process of upgrading to UI7. Rather than continue to use PLEG for everything where I need to deploy boolean logic I thought I’d experiment with LUA and LUUP for nothing more than my own inquisitiveness. I want to conditionally run a scene based upon the state of a virtual switch. If the switch is on then the scene should abort without any further actions taking place. If it’s off then execute all subsequent scene actions. I attached the script below to the virtual switch trigger when compiling the scene using UI7’s screen editor. The script does exactly what it should but instead of aborting the scene it carries on. Essentially there is no difference between “return false” and “return true”.
I can only see two places where the script should be placed. On the virtual switch which is the trigger. Or at the end. I placed it on the former. Obviously. But in desperation I also tried at the end. That didn’t work. Obviously. I’ll continue to use PLEG but please could someone spend 10secs to tell me where I’m going wrong. Or mad! Thanks!
local outLate = luup.variable_get(“urn:upnp-org:serviceId:SwitchPower1”, “Status”, 482) --482 is the ‘Out Late’ Virtual Switch
– Now test to see if ‘Out Late’ is switched ON (set to 1). If so then this complete scene SHOULD immediately abort without executing any subsequent specified actions.
if outLate == “1” then
Don’t understand this statement. At “the end” of what? It should go in the scene Lua of the scene you are trying to control.
you have a return statement outside a function, and that won’t end the script.
the complete script will be executed, so you have to add the logic to make whatever you want to happen:
local outLate = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1", "Status", 482) --482 is the 'Out Late' Virtual Switch
if outLate == "1" then
-- do your outlate stuff
-- do your ~outlate stuff
-- do other stuff
It should, because the script Lua is executed in the context of a function. It will abort the execution of any actions that the scene has.
Of course, if there is more to be done within Lua code, then as you describe the code placement is correct.
I wonder if you have the correct serviceID for the Switch.
So it is always returning true.
If you work with LUA … you might want to add some luup.log statements
into your logic so you can see the flow, and maybe intermediate variable values.
Thanks to everyone for your very quick responses.
By “at the end” I mean I inserted the simple Lua script at the end of the scene using UI7’s wizard in desperation when it didn’t seem to work when attached to the initial trigger at the start. Which is stupid I know. But this was driving me crazy…
I simplified the script for purposes of this posting. But it essentially boils down to the fact that for me there is no difference between ‘return true’ and ‘return false’. The scene to which the script is attached carries on executing regardless of the state of the ‘return’ statement at the start of the scene.
I’ve stepped the script through a debugger (Zeobrane) and confirmed that the script flow is working correctly for a true (virtual switch on) status and false (virtual switch off) status.
I know I’m missing something fundamental. I thought that the scene as I compile it through UI7’s wizard should allow me to attach a Lua script to the trigger at the beginning that initiates the scene and by returning false in that script the scene would immediately abort before causing any device actions (light//locks to on/off) to occur.
I could write the whole functionality I want in Lua not bothering with the wizard selection of devices that UI7 provides. E.g. ‘If Out Late’ is off then setloadlevel of lights to zero else leave at 100. (But given the number of devices it’d be 10x quicker to do that in PLEG anyway.)
But all I want to do is to abort execution of a prewritten scene shortly after its start based upon a simple test. Which I thought would be easy to do and not frustrating. Not so! Once again, though thanks to all. Just waiting now for someone to hit me with the obvious! (; -)
Sent from my iPad using Tapatalk
So if you put “return false” as the only line in the scene lua, the scene runs? And nothing interesting in the log file when you do this?
Solved! My own stupidity! I should have been using VSwitch1 rather than SwitchPower1 as the serviceID. (I read elsewhere of previous confusion between these two types of serviceID.) But I could have resolved a lot sooner had I tried simply putting ‘return false’ in the script (and commenting out all other lines) just to confirm that the script does indeed abort the whole scene. Which it did when I tried it. (Thanks jswim788.) What also confused things was that the Lua engine kept restarting on the VeraPlus which I hadn’t noticed and I was getting a time-lag between repeated tests causing me to misinterpret results.
Now I have this resolved I can easily accomplish what I set out to do in the first place. Which is: I want to turn on a virtual switch (OutLate) remotely via my iPhone when I know I’m arriving late home enabling the house lights to remain on. Otherwise they switch off at a preset time. When the scheduled scene runs to turn off the lights there are two triggers within the scene joined by a logical ‘AND’ which are first evaluated. ‘tScheduledLightsOffTime AND !tOutLate’ will turn the lights off at the scheduled time. And ‘tScheduledLightsOffTime AND tOutLate’ will leave them on until I get home.
It would be so much easier if Vera would give the option of AND as well as OR when defining multiple triggers in their Scene Wizard. That’s one significant aspect where PLEG has them beaten hands-down every time!
Again, many thanks to all and apologies for my idiocy!
The AND might be coming soon. If you use the iOS app, you may be able to get it today. See: http://forum.micasaverde.com/index.php/topic,50015.0.html
You can also access the AND with the latest version of AltUI.