Author Topic: Delayed action with pre condition  (Read 135 times)

Offline mouch

  • Newbie
  • *
  • Posts: 2
  • Karma: +0/-0
Delayed action with pre condition
« on: March 16, 2017, 03:17:14 pm »
I need help setting up a scene...
I have a relay controlling the power of my home's ventilation system...
I'd like the system to turn off 2h after I left home but only when I'm still away...

I setup a scene that triggers when I'm switching to away using HouseMode Plugin.
The scene has a delayed action to turn off the ventilation after 2h and should run only when I'm in away mode.

My expectation is that the delayed action should check again whether I'm home, but so far it looks like this is checked only before running the full scene and not specific actions.

Does anyone have an idea how I could solve this problem?

Offline Z-Waver

  • Master Member
  • *******
  • Posts: 4340
  • Karma: +238/-118
Re: Delayed action with pre condition
« Reply #1 on: March 18, 2017, 09:14:00 am »
That's how scenes work. Their logic is processed instantly and they end. In your case, the scene runs, starts the 2 hour count down and ends. Nothing will stop that countdown except a Vera engine restart.

There is a way to accomplish your goal using Lua code in a scene, but it is somewhat complex. See: Conditional Scene Execution: Some Examples by @RexBeckett.

The other and relatively easier way is to use the Program Logic Event Generator(PLEG) plugin. You can replace your scene with a few simple PLEG conditions and get just what you want. @RexBeckett wrote a PLEG primer as well, PLEG Basics - An Introduction to the Program Logic Event Generator

Offline RichardTSchaefer

  • Master Member
  • *******
  • Posts: 9104
  • Karma: +690/-126
    • RTS Services Plugins
Re: Delayed action with pre condition
« Reply #2 on: March 18, 2017, 11:14:20 am »
If you want to do conditional at delayed time ...
Then you want to run a second Scene 2 hours later.
That second scene could have some conditional logic in it to cancel the scene.

However, if Vera restarts during that two hours ... that second scene will never run.

That's one of the benefits of using PLEG. Timers work even if vera restarts.


Offline mouch

  • Newbie
  • *
  • Posts: 2
  • Karma: +0/-0
Re: Delayed action with pre condition
« Reply #3 on: March 18, 2017, 06:04:50 pm »
Great, many thanks for the tips... Will try it out right away

Offline ember1205

  • Sr. Member
  • ****
  • Posts: 297
  • Karma: +4/-17
Re: Delayed action with pre condition
« Reply #4 on: March 18, 2017, 06:46:44 pm »
You can do this with variables and such.

Scene starts and logic is processed (as stated). The scene shouldn't "do" anything (immediately or delayed). Use LUA code to affect what you want like this:

Code: [Select]
luup.call_delay ("eval_HVAC", xxx, "")

function eval_HVAC (empty)
     local mode = luup.attr_get "Mode"
     if (mode == "2") then
          luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="0" },<device_id>)
     end
end
return (true)

Or something reasonably close to that. Change xxx to the number of seconds you want it to 'sleep' for (7200 for two hours). Keep in mind that you can't stop the delay or timer. So, if you leave, come back in an hour, and leave again, it will still shut down two hours after the FIRST time you left.

Offline jswim788

  • Full Member
  • ***
  • Posts: 232
  • Karma: +7/-1
Re: Delayed action with pre condition
« Reply #5 on: March 18, 2017, 10:23:31 pm »
Or use the countdown timer plugin.  You start it when you leave with a 2 hour countdown.  Then at expiry, it triggers another scene that checks to see if you are still away before doing the action.  Note that the countdown timer can be reset as well, so you can instead trigger on the change to home to stop the timer.  My understanding is that the count down timer will survive a lua restart (can anyone confirm?), but the luup.call_delay will not.

Or use PLEG as mentioned.

Offline RichardTSchaefer

  • Master Member
  • *******
  • Posts: 9104
  • Karma: +690/-126
    • RTS Services Plugins
Re: Delayed action with pre condition
« Reply #6 on: March 19, 2017, 10:01:19 am »
Yes the Countdown Timer WILL survive a LUA restart.
Enhancing the  Coundown Timer and Combination Switch plugins where part of my motivation for PLTS and later PLEG.