Author Topic: Conditional execution of a scene - aborting early  (Read 217 times)

Offline Brighp

  • Newbie
  • *
  • Posts: 3
  • Karma: +0/-0
Conditional execution of a scene - aborting early
« on: August 06, 2017, 04:22:44 pm »
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
  return false
else
  return true
end

Offline akbooer

  • Master Member
  • *******
  • Posts: 5131
  • Karma: +221/-67
  • "Less is more"
Re: Conditional execution of a scene - aborting early
« Reply #1 on: August 07, 2017, 02:56:43 am »
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.

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. 
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P.
Razberry, MySensors Arduino, HomeWave, AltUI, DataYours, openLuup, ZWay, ZeroBrane Studio.

Offline BulldogLowell

  • Hero Member
  • *****
  • Posts: 1571
  • Karma: +190/-85
Re: Conditional execution of a scene - aborting early
« Reply #2 on: August 07, 2017, 07:29:33 am »
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:

Code: [Select]
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
else
  -- do your ~outlate stuff
end

-- do other stuff

Offline akbooer

  • Master Member
  • *******
  • Posts: 5131
  • Karma: +221/-67
  • "Less is more"
Re: Conditional execution of a scene - aborting early
« Reply #3 on: August 07, 2017, 07:42:31 am »
you have a return statement outside a function, and that won't end the script.

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.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P.
Razberry, MySensors Arduino, HomeWave, AltUI, DataYours, openLuup, ZWay, ZeroBrane Studio.

Offline RichardTSchaefer

  • Master Member
  • *******
  • Posts: 9572
  • Karma: +729/-136
    • RTS Services Plugins
Re: Conditional execution of a scene - aborting early
« Reply #4 on: August 07, 2017, 09:55:01 am »
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.

Offline Brighp

  • Newbie
  • *
  • Posts: 3
  • Karma: +0/-0
Re: Conditional execution of a scene - aborting early
« Reply #5 on: August 07, 2017, 08:10:48 pm »
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

Offline jswim788

  • Sr. Member
  • ****
  • Posts: 483
  • Karma: +23/-2
Re: Conditional execution of a scene - aborting early
« Reply #6 on: August 07, 2017, 08:41:49 pm »
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?

Offline Brighp

  • Newbie
  • *
  • Posts: 3
  • Karma: +0/-0
Re: Conditional execution of a scene - aborting early
« Reply #7 on: August 08, 2017, 09:47:01 am »
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!

Offline jswim788

  • Sr. Member
  • ****
  • Posts: 483
  • Karma: +23/-2
Re: Conditional execution of a scene - aborting early
« Reply #8 on: August 08, 2017, 11:57:22 am »
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

Offline akbooer

  • Master Member
  • *******
  • Posts: 5131
  • Karma: +221/-67
  • "Less is more"
Re: Conditional execution of a scene - aborting early
« Reply #9 on: August 08, 2017, 12:17:00 pm »
You can also access the AND with the latest version of AltUI.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P.
Razberry, MySensors Arduino, HomeWave, AltUI, DataYours, openLuup, ZWay, ZeroBrane Studio.