Author Topic: Conditional Scene Execution: Some Examples  (Read 264408 times)

Offline zedrally

  • Hero Member
  • *****
  • Posts: 1152
  • Karma: +13/-5
  • Black Cat Control Systems
Re: Conditional Scene Execution: Some Examples
« Reply #270 on: November 05, 2017, 10:11:55 pm »
There is no benefit of one vs the other.
You already have LUA code .. so it's up to you.

Richard,
What would this look like in PLEG?
The similarity to my awning problem is striking and could be the solution I was looking for.
Living in the Land of Oz, give me a vegemite sandwich. Home Seer, Vera Lite & Edge, Popp, Black Cat Smart Hub & Vera G, Black Cat Lite 1 & 2's a Black Cat Dimmer or 2, Fantem Tec and then some  Black Cat Cat's Eye PIR's & Door-Window Sensors, RFXComm, Broadlink RMPro & Mini plus a Z-UNO or 2.

Offline RichardTSchaefer

  • Master Member
  • *******
  • Posts: 10091
  • Karma: +763/-142
Re: Conditional Scene Execution: Some Examples
« Reply #271 on: November 05, 2017, 11:06:47 pm »
Input
LightLevel        Current Light Level

Schedule
NextStepTimer            On - Self Trigger, Off After 30 sec interval
OffTimer                    On - Self Trigger, Off after 30 minutes

Condition
Start                  What every you use to start this (i.e. some Multi-Switch turns on
KeepGoing          (Start; !NextStepTimer) and !NextStepTimer and (LightLevel > 5) and (LightLevel <= 25)
AllOff                  (Start;OffTimer) and (LightLevel == 5)

Actions:
Start                  - Set light to 25 %    and trigger NextStepTimer with an interval of 30 and trigger OffTimer with an interval of 30:00
KeepGoing           Set Light to  {(LightLevel - 1}} and  trigger NextStepTimer with an interval of 30
AllOff                   Turn Off Light


NOTE: THis works even if Vera is reloaded!!!
With this code ... if you manually change the light > 25 ... this will disable the logic. You will have to retrigger the Start condition.

Offline babgvant

  • Sr. Newbie
  • *
  • Posts: 24
  • Karma: +0/-0
Re: Conditional Scene Execution: Some Examples
« Reply #272 on: November 17, 2017, 11:14:28 am »

For triggers:
   1) If Trigger LUA exists, run it, if it returns FALSE then stop,  otherwise (TRUE, or nothing returned) continue.
   2) If Scene LUA exists, run it, if it returns FALSE then stop, otherwise (TRUE, or nothing returned) continue.
   3) Run Actions (Immediate and delayed)

For Manual and Schedule started scenes, skip step 1.


Is 2 the highlighted section

Offline RichardTSchaefer

  • Master Member
  • *******
  • Posts: 10091
  • Karma: +763/-142
Re: Conditional Scene Execution: Some Examples
« Reply #273 on: November 17, 2017, 03:43:19 pm »
Yes!

I think MCV left out the ability for LUA on triggers like they did in UI5 ....
But the runtime still supports it. (i.e. Scenes migrated from UI5 to UI7 that used this feature still work).

I have not looked to see if AltUI provides scene definition with this ability.


Offline babgvant

  • Sr. Newbie
  • *
  • Posts: 24
  • Karma: +0/-0
Re: Conditional Scene Execution: Some Examples
« Reply #274 on: November 17, 2017, 04:14:44 pm »
Yes!

I think MCV left out the ability for LUA on triggers like they did in UI5 ....
But the runtime still supports it. (i.e. Scenes migrated from UI5 to UI7 that used this feature still work).

I have not looked to see if AltUI provides scene definition with this ability.

Thank you

Offline whited

  • Newbie
  • *
  • Posts: 16
  • Karma: +0/-0
Re: Conditional Scene Execution: Some Examples
« Reply #275 on: December 18, 2017, 10:29:37 am »
Hi,
I am trying to execute my irrigation every other day at 3am. I am running UI5. I believe there are several ways to go about this.
I have first attempted in modifying your code and plugging it in to startup luup without success.
Code: [Select]
-- Schedule irrigation (Scene#94) every other day at 3am.
local tStart = "03:00"   -- Start watering
local tDelay = 48        -- Delay until next time watered in hours
local hS, mS = string.match(tStart,"(%d+):(%d+)")
local mStart = (hS * 60) + mS
local tNow = os.date("*t")
local mNow = (tNow.hour * 60) + tNow.min
local varD94 = 0
local lastHourD94 = 0
if (tNow.hour ~= lastHourD94) then
varD94 = varD94 + 1
lastHourD94 = tNow.hour
end
if ((varD94 >= tDelay) and (mNow >= mStart)) then
varD94 = 0
luup.call_action("urn:micasaverde-com:serviceId:HomeAutomationGateway1", "RunScene", { SceneNum= "94"}, 0)
end
I then tried allowing the scene scheduled to run every day at 3am then added luup code within the scene to return false every other time. This attempt was successful when triggering the scene manually but when run through the schedule, it never gets executed.
Code: [Select]
-- Bypass scene every other day
if (everyOtherDayScene94 == 0) then
everyOtherDayScene94 = 1
else
everyOtherDayScene94 = 0
return false
end

Offline RichardTSchaefer

  • Master Member
  • *******
  • Posts: 10091
  • Karma: +763/-142
Re: Conditional Scene Execution: Some Examples
« Reply #276 on: December 18, 2017, 11:31:02 pm »
Your assuming your Vera does not restart ...
Every time it does ... the next time it evaluates:
      (everyOtherDayScene94 == 0)

This will be false ... because  everyOtherDayScene94 is undefined.
So it will skip ... and if Vera restarts the next day ... it will skip again.
If you really want schedule/timer reliability ... you should use PLEG

Offline whited

  • Newbie
  • *
  • Posts: 16
  • Karma: +0/-0
Re: Conditional Scene Execution: Some Examples
« Reply #277 on: December 19, 2017, 10:14:34 am »
Richard,
I agree with you 100% about vera restarting and having an undefined value.
What if I somehow used luup.variable_set?

I found this link addressing the memory problem that you and Rex discussed:
http://forum.micasaverde.com/index.php/topic,17791.0.html

I am still working on a solution.

Offline whited

  • Newbie
  • *
  • Posts: 16
  • Karma: +0/-0
Re: Conditional Scene Execution: Some Examples
« Reply #278 on: December 19, 2017, 11:00:46 am »
Can a variable be saved in eePROM or to usb memory stick?

Offline whited

  • Newbie
  • *
  • Posts: 16
  • Karma: +0/-0
Re: Conditional Scene Execution: Some Examples
« Reply #279 on: December 19, 2017, 06:16:11 pm »
I believe I may have solved my problem. I like to keep my system with minimal apps so I found one I am currently using called MultiString.
It is a variable container. I changed my code to this:
Code: [Select]
-- Bypass scene every other day
local value = luup.variable_get("urn:upnp-org:serviceId:VContainer1", "Variable5", YourDeviceID) or "0"
if (value == "0") then
luup.variable_set("urn:upnp-org:serviceId:VContainer1", "Variable5", "1", YourDeviceID)
else
luup.variable_set("urn:upnp-org:serviceId:VContainer1", "Variable5", "0", YourDeviceID)
return false
end
I have tested it by rebooting my Vera3 and the value in Variable5 doesn't change, which is a good thing!
I will keep you informed if it doesn't work after a few days. :o

Offline stefanradu5

  • Sr. Newbie
  • *
  • Posts: 26
  • Karma: +0/-0
  • Vera Plus UI7
Re: Conditional Scene Execution: Some Examples
« Reply #280 on: February 01, 2018, 05:14:18 am »
Hello,
I am trying to make a scene for AC / HEATING that is blocked by two security sensors (Fibaro door window sensors).
When any of the windows are open ..the scene must be blocked from running.
The scene's trigger is a temperature sensor.
I inserted the Luup code in the Trigger's "Luup Event" but i receive an error in Events and Scenes.
I am beginner and don't fully understand the architecture of the code but I am still trying.
So, what I am doing wrong? If someone can help me, please.
Thank you.

Code: [Select]
local function windowbedroom()
local dID = 32
local allow = true
local tripped = luup.variable_get("urn:micasaverde-com:serviceId:SecuritySensor1", "Tripped", dID)
end

local function windowlinvingroom()
local dID = 51
local allow = true
local tripped = luup.variable_get("urn:micasaverde-com:serviceId:SecuritySensor1", "Tripped", dID)
end

return windowbedroom((status == "0") and windowlivingroom((status == "0") == allow)



« Last Edit: February 01, 2018, 05:16:23 am by stefanradu5 »

Offline kwieto

  • Hero Member
  • *****
  • Posts: 612
  • Karma: +27/-15
Re: Conditional Scene Execution: Some Examples
« Reply #281 on: February 01, 2018, 05:44:28 am »
If you embed the code within a function, you have to call it to make it work.
That's the first thing.
Second is that you have the same local variables for both of your sensors (including "allow") which in turn will give you a result that only the most recent one will be considered. Then you use "status" variable in the return function, which is also not declared anywhere.

To make your code work I would do it as follow:

Code: [Select]
local tripped_bedroom = luup.variable_get("urn:micasaverde-com:serviceId:SecuritySensor1", "Tripped", 32)
local tripped_livingroom = luup.variable_get("urn:micasaverde-com:serviceId:SecuritySensor1", "Tripped", 51)
local allow = true

return (((tripped_bedroom == "0") and (tripped_livingroom == "0")) == allow)

I put device ID's directly to the "variable_get command", as you need them only there and I don't see a need to declare them as local variables

From logical point it works like that:
first you check the trip status of your devices and store it into local variables (data from device 32 is stored in "tripped_bedroom" and data from device 51 in "tripped_livingroom")
Then you make a check of both variables and run the scene only if both are not tripped.
« Last Edit: February 01, 2018, 05:47:42 am by kwieto »

Offline stefanradu5

  • Sr. Newbie
  • *
  • Posts: 26
  • Karma: +0/-0
  • Vera Plus UI7
Re: Conditional Scene Execution: Some Examples
« Reply #282 on: February 01, 2018, 06:05:19 am »
Thank you for help and tutorial.

Now I make some logic about the syntax.
I have just inserted the code , no errors yet.
 
It should work if any of the windows are opened?... or it will block the scene if only one sensor is tripped (window open).

Offline kwieto

  • Hero Member
  • *****
  • Posts: 612
  • Karma: +27/-15
Re: Conditional Scene Execution: Some Examples
« Reply #283 on: February 01, 2018, 07:11:38 am »
The meaning of the logic:
Code: [Select]
(tripped_bedroom == "0") and (tripped_livingroom == "0")is that none of your sensor should be tripped to make scene run ("0" in device's "Tripped" variable mens "untripped" while "1" is "tripped").

If you want to allow the scene to run if at least one of your sensors is untripped it should be:
Code: [Select]
(tripped_bedroom == "0") or (tripped_livingroom == "0")
For the condition that at least one of your devices is tripped (in this case the same result as code for "at least one untripped", but sometimes it makes difference) it should be:
Code: [Select]
(tripped_bedroom == "1") or (tripped_livingroom == "1")
Then the last case is the condition where you want to run your scene only if both devices are tripped:
Code: [Select]
(tripped_bedroom == "1") and (tripped_livingroom == "1")
« Last Edit: February 01, 2018, 07:20:42 am by kwieto »

Offline stefanradu5

  • Sr. Newbie
  • *
  • Posts: 26
  • Karma: +0/-0
  • Vera Plus UI7
Re: Conditional Scene Execution: Some Examples
« Reply #284 on: February 01, 2018, 08:48:23 am »
The first case is the solutions.
When both sensors are "0" (both windows closed)