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

Offline aa6vh

  • Hero Member
  • *****
  • Posts: 592
  • Karma: +12/-0
Re: Conditional Scene Execution: Some Examples
« Reply #240 on: June 20, 2016, 10:10:02 am »

Do i put the code in the LUUP Event box in the Triggers section, or
Above the existing code in the LUUP tab?


The LUA code associated with a trigger is executed before the main scene LUA code. Either code snippets can cancel running the scene. (Code associated with other triggers are not executed.)

If you place the time check code in the main LUA section, the scene can never run between the specified times. If you put it tin the trigger section, the scene will not run only when that specific trigger occurs, which would allow other triggers to continue to run the scene. Having the code in the trigger will also allow manual execution of the scene.

Offline tomgru

  • Hero Member
  • *****
  • Posts: 1402
  • Karma: +18/-6
Re: Conditional Scene Execution: Some Examples
« Reply #241 on: June 21, 2016, 10:05:22 am »

Do i put the code in the LUUP Event box in the Triggers section, or
Above the existing code in the LUUP tab?


The LUA code associated with a trigger is executed before the main scene LUA code. Either code snippets can cancel running the scene. (Code associated with other triggers are not executed.)

If you place the time check code in the main LUA section, the scene can never run between the specified times. If you put it tin the trigger section, the scene will not run only when that specific trigger occurs, which would allow other triggers to continue to run the scene. Having the code in the trigger will also allow manual execution of the scene.

Awesome explanation....thanks

Offline lotsaplots

  • Newbie
  • *
  • Posts: 13
  • Karma: +0/-0
Re: Conditional Scene Execution: Some Examples
« Reply #242 on: July 02, 2016, 05:05:13 pm »
What about a scene that can only run when you are in specific modes?

For instance: If motion sensor triggered in hallway, then turn on fireplace lights ONLY if Night Mode = true.

Not sure what the luup code would be for the condition of night mode returning true would be (also, where would I put this code... In the trigger, the action, or...??)

Offline lotsaplots

  • Newbie
  • *
  • Posts: 13
  • Karma: +0/-0
Re: Conditional Scene Execution: Some Examples
« Reply #243 on: July 02, 2016, 05:08:45 pm »
Never mind... that was a stupid question. This capability already exists without the need for coding.

Offline pbellouny

  • Newbie
  • *
  • Posts: 6
  • Karma: +0/-0
Re: Conditional Scene Execution: Some Examples
« Reply #244 on: August 04, 2016, 09:30:39 pm »
How would I separate multiple of these conditional scenes in one luup box?

Let's say I had a humidity condition and a temperature condition, how do I separate the two pieces of code?

Thanks in advance.

Offline svanni

  • Newbie
  • *
  • Posts: 9
  • Karma: +0/-0
Re: Conditional Scene Execution: Some Examples
« Reply #245 on: September 27, 2016, 11:52:13 am »
Great, great thread !!!
Thank you for the info.

I'm a Lua rookie, so please bear with me.
Can you provide an example of a script triggered by time of day ?
I know I can set this up thru the Schedule tab, but was wondering what the script would look like.

Thank you in advance.

Offline RichardTSchaefer

  • Master Member
  • *******
  • Posts: 9418
  • Karma: +715/-130
    • RTS Services Plugins
Re: Conditional Scene Execution: Some Examples
« Reply #246 on: September 27, 2016, 01:15:45 pm »
You can schedule a scene with a schedule and use LUA to cancel the scene actions using conditional LUA techniques

OR you can schedule a scene based on Device events (i.e. motion, temperature, etc)  and then use conditional LUA techniques to cancel the scene if not in  the correct time span.

People do not typically SCHEDULE the scene from LUA.

Offline jswim788

  • Sr. Member
  • ****
  • Posts: 385
  • Karma: +14/-2
Re: Conditional Scene Execution: Some Examples
« Reply #247 on: October 12, 2016, 11:42:47 am »
In the second post on this topic, there is an example of sunset to a specified time.  But that didn't work for me.  I traced the problem down to this line:

  local mStart = math.floor( (luup.sunset() % 86400) / 60 ) + pStart

The sunset value returned is in UTC/GMT, but the rest of the code is using local time.  So I had to replace the above line with:

  local tStart = os.date("*t", luup.sunset())
  local mStart = (tStart.hour * 60) + tStart.min + pStart

Now it works fine.  But I'm surprised I had to do this.  Did anyone else notice this issue?  I have the timezone and location set properly and I have power cycled the Vera since getting those set (to make sure it knows the correct time).  It seems to me that the original code could only work if you were in GMT with no timezone offset.

Otherwise the example works great for me.  Many thanks for all of the effort on the examples!

Offline RichardTSchaefer

  • Master Member
  • *******
  • Posts: 9418
  • Karma: +715/-130
    • RTS Services Plugins
Re: Conditional Scene Execution: Some Examples
« Reply #248 on: October 12, 2016, 07:49:38 pm »
Rex lives in the UK!

Offline conchordian

  • Sr. Member
  • ****
  • Posts: 303
  • Karma: +4/-1
Re: Conditional Scene Execution: Some Examples
« Reply #249 on: October 12, 2016, 11:58:56 pm »
In the second post on this topic, there is an example of sunset to a specified time.  But that didn't work for me.   Did anyone else notice this issue?

Yes, http://forum.micasaverde.com/index.php/topic,27489.msg196160.html#msg196160 was how I did it.

Offline kwieto

  • Jr. Member
  • **
  • Posts: 66
  • Karma: +2/-2
Re: Conditional Scene Execution: Run a Scene when a Variable Changes
« Reply #250 on: January 23, 2017, 04:06:08 am »
A better technique is to watch the variable in question and have your scene run when it changes. As above, you can then use Lua code to decide if you want to execute some action(s). Vera provides a function for this very purpose. (...) If this code is placed in Vera's Startup Lua, it will set up a variable-watch for thermostat/thermometer device 123 current temperature. When this changes, scene number 12 will be executed.

I assume that the scene will be triggered the same way as I would press the "Run" button, so ignoring parameters defined by the triggers?
So to make it properly run I have to put these parameters into the lua code of the scene? (i.e. trigger defined as a "temperature below X")?

One addtitional question - This code will run the scene regardless if it is turned ON or OFF in the scenes section?
If yes, how to add additional condition, that if the scene is disabled, then it shouldn't be run?
« Last Edit: January 23, 2017, 04:09:33 am by kwieto »

Offline rafale77

  • Hero Member
  • *****
  • Posts: 652
  • Karma: +35/-20
Re: Conditional Scene Execution: Some Examples
« Reply #251 on: February 12, 2017, 08:06:16 pm »
On this Latest 1.7.19 firmware, I am finding some weird things. Maybe someone can help.
I am trying to trigger a scene through a luup http command but have it executed only in house modes 2 and 4.
The scene is an announcement and a vera notification.

When I run this code:
Code: [Select]
local http = require("socket.http")
local message = string.format("http://192.XX.XX.XX:5005/sayall/blah blah!")
local mode = luup.attr_get "Mode"
result, status = http.request(message)
return (tonumber(mode)==2 or tonumber(mode)==4)

With the notification set in the scene editor, I get a notification regardless of the house mode and... the http request is also executed even though it returns a "false" (Verified in LuaTest) since I am in house mode 1.

When I run this
Code: [Select]
local http = require("socket.http")
local message = string.format("http://192.xx.xx.xx:5005/sayall/blah blah")
local mode = luup.attr_get "Mode"
if  (tonumber(mode)==2 or tonumber(mode)==4) then
result, status = http.request(message)
end

Then it returns a nil, The http request does not run but I still get the notification. (I saw some other people observing that the notification is not tied to the scene execution but to the trigger)
Now I remember seeing somewhere that there is a http put I can send to emit a notification within the scene lua instead of being sent by the trigger. Anyone would have it by any chance?

On a separate note, I also realized that the "this scene run in house mode" is completely ignored for lua codes. It only affects actions added by the editor. I have a scene with both and even though I am in the excluded house mode, it will send the notification, run the lua code but not execute the device action.

Edit:

I found a lot of unanswered posts asking for the Lua code to issue a mcv notification. I found a workaround. I created a scene which contains only the notification I want to issue and then execute that scene in the original scene via Lua... Not super elegant but it works. The notification is now conditional...
« Last Edit: February 12, 2017, 10:09:27 pm by rafale77 »
127 zwave nodes, 8 zigbee nodes, 256 devices, 17 apps, 128 scenes on a Vera Plus linked with Echo and HomeKit through bridges

Offline petequintanilla

  • Jr. Member
  • **
  • Posts: 61
  • Karma: +0/-0
Re: Conditional Scene Execution: Some Examples
« Reply #252 on: March 01, 2017, 01:20:11 pm »
Im having trouble to setup a scene that used to run real well in UI5, Im now using Ui7 its a conditional scene for something to happen only during certain time of the day, but i can get it to work now.

the l?a im using is
Code: [Select]
local function checkTime()
     local pStart = "16:00"   -- Start of time period
     local pEnd = "23:29"     -- End of time period
     local allow = true       -- true runs scene during period, false blocks it
     local hS, mS = string.match(pStart,"(: )/(e )")
     local mStart = (hS * 60)   mS
     local hE, mE = string.match(pEnd,"(v )S(H )")
     local mEnd = (hE * 60)   mE
     local tNow = os.date("*t")
     local mNow = (tNow.hour * 60)   tNow.min
     if mEnd >= mStart then
          return (((mNow >= mStart) and (mNow <= mEnd)) == allow)
     else
          return (((mNow >= mStart) or (mNow <= mEnd)) == allow)
     end
end

local function checkDay()
     local dFirst = 2         -- Start day of period (1-7) Sunday = 1
     local dLast = 4          -- End day of period (1-7) Sunday = 1
     local allow = true       -- true runs scene during period, false blocks it
     local tNow = os.date("*t")
     local dNow = tNow.wday
     if dLast >= dFirst then
          return (((dNow >= dFirst) and (dNow <= dLast)) == allow)
     else
          return (((dNow >= dFirst) or (dNow <= dLast)) == allow)
     end
end

return checkTime() and checkDay()
could some one please advise?
« Last Edit: March 01, 2017, 01:23:28 pm by petequintanilla »

Offline blairRosen

  • Newbie
  • *
  • Posts: 5
  • Karma: +0/-0
Re: Conditional Scene Execution: Some Examples
« Reply #253 on: May 18, 2017, 10:28:43 am »
I am quite the newbie and appreciate the effort that went into your instructions.  However, I am not sure how to determine the "Service ID" of my device.  I have created a virtual switch named summer, and wish to run my heating scenes when it is "off" and my cooling when it is on. Is the service id the entry in the device parameters entitled "device_type"? in my case the device type of the virtual switch is urn:schemas-upnp-org:device:VSwitch:1 with a device id of 148. Would the correct syntax for Luup command to prevent running a scene if the summer switch is in the off state be:

Summer:
Code: [Select]
local dID = 148
local allow = false
local status = luup.variable_get("urn:schemas-upnp-org:device:VSwitch:1,"Status",dID)
return ((status == "1") == allow)


Offline jswim788

  • Sr. Member
  • ****
  • Posts: 385
  • Karma: +14/-2
Re: Conditional Scene Execution: Some Examples
« Reply #254 on: May 18, 2017, 11:09:38 am »
You can go to the advanced tab of the device and hover your mouse over the variable to get the service ID.  (Yes, this is silly, and you can't capture with a cut/paste this way.  There must be a better way to do this.)  Otherwise you can download the implementation file and look directly inside it.  Note that the service ID is case sensitive.  It's really easy to make a mistake with it.  I think you need: "urn:upnp-org:serviceId:VSwitch1"