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

Offline blairRosen

  • Newbie
  • *
  • Posts: 5
  • Karma: +0/-0
Re: Conditional Scene Execution: Some Examples
« Reply #255 on: May 18, 2017, 09:23:54 pm »
Thank you.  Wow. They couldn't make it less intuitive if they tried.  You are right, under Variable, if I hover of over "UI7check" it says "urn:upnp-org:serviceId:VSwitch1". Another question if you don't mind. Since I don't want to have the scene run because  I have set "local allow =  false" what is the function of the line "return ((status == "1") == allow)"

In any event , based on your comments, would it be correct to say the LUUP code for not allowing the scene to run when the Summer Virtual Switch ID 148 is on would be:

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

Thanks again.

Offline Don Phillips

  • Hero Member
  • *****
  • Posts: 906
  • Karma: +19/-18
    • Worthington Engineering, Inc.
Re: Conditional Scene Execution: Some Examples
« Reply #256 on: May 18, 2017, 09:34:07 pm »
+1 to jswim788.
Vera 3, UI7 1.7.947, CT101 t-stat, Everspring motion detector, GE/Jasco switch, Leviton outlet, AeonLabs door sensor, Insteon garage control, Blue Iris, Sricam SP011, iPhone locator, APCUPSD, VeraMate, VeraAlerts, PLEG, House Modes, Countdown Timer, DVR, Virtual & Multi Switch, Weather, Sys. Mon.

Offline blairRosen

  • Newbie
  • *
  • Posts: 5
  • Karma: +0/-0
Re: Conditional Scene Execution: Some Examples
« Reply #257 on: May 18, 2017, 09:58:28 pm »
Following Up:   This LUUA code did not work:

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

I got the message "error in lua for scenes and events"

Offline jswim788

  • Hero Member
  • *****
  • Posts: 538
  • Karma: +29/-2
Re: Conditional Scene Execution: Some Examples
« Reply #258 on: May 19, 2017, 12:24:32 am »
You are not including the "Summer: and Code" are you?  Not needed and will cause a syntax error.  And the "allow" stuff was to make it flexible, but it confuses more people than it helps.  Finally, you need the return to say whether the scene runs or not.  This is simpler:
Code: [Select]
local dID = 148
local status = luup.variable_get("urn:upnp-org:serviceId:VSwitch1","Status",dID)
return (status == "0")
By the way, run this in the test code window of Vera.  It should indicate a pass if your switch is off and a fail if the switch is on.

Change the above to check for "1" if you want your scene to run if the switch is on.

Offline blairRosen

  • Newbie
  • *
  • Posts: 5
  • Karma: +0/-0
Re: Conditional Scene Execution: Some Examples
« Reply #259 on: May 19, 2017, 08:23:57 am »
You are a genius.  When I removed the lines "Summer: and Code",  both variations of your code post, the original and the simpler one in your last post, worked without error.  Now hopefully everything will work as planned. I very much appreciate the time and effort you took to solve my problem.  One suggestion to help us newbies. When posting code, perhaps the programmers should distinguish between the text necessary to run and that which is not. A great way would be  by enclosing the executable lines between horizontal bars as in your last post. BTW this has convinced me that I ought to at least learn the elements of rudimentary programming for lua.  Can you suggest an online tutorial?

Offline jswim788

  • Hero Member
  • *****
  • Posts: 538
  • Karma: +29/-2
Re: Conditional Scene Execution: Some Examples
« Reply #260 on: May 19, 2017, 09:42:28 am »
This is a good starting point for Lua: https://www.lua.org/pil/1.html

I'd never heard of Lua until I stumbled into it with Vera.  I'm not sure where else it is used.

Offline aa6vh

  • Hero Member
  • *****
  • Posts: 602
  • Karma: +12/-0
Re: Conditional Scene Execution: Some Examples
« Reply #261 on: May 19, 2017, 10:19:41 am »
I'd never heard of Lua until I stumbled into it with Vera.  I'm not sure where else it is used.

The Watchmaker app for Android smartwatches uses the LUA Language. (Makes it a lot simplier when programming the smartwatch to control Vera stuff.)

Offline doinitsideways

  • Jr. Member
  • **
  • Posts: 61
  • Karma: +2/-0
Re: Conditional Scene Execution: Some Examples
« Reply #262 on: June 29, 2017, 12:15:57 am »
You are not including the "Summer: and Code" are you?  Not needed and will cause a syntax error.  And the "allow" stuff was to make it flexible, but it confuses more people than it helps.  Finally, you need the return to say whether the scene runs or not.  This is simpler:
Code: [Select]
local dID = 148
local status = luup.variable_get("urn:upnp-org:serviceId:VSwitch1","Status",dID)
return (status == "0")
By the way, run this in the test code window of Vera.  It should indicate a pass if your switch is off and a fail if the switch is on.

Change the above to check for "1" if you want your scene to run if the switch is on.

Hi there, im trying to get a scene to turn on a light when i turn my water feature on ONLY at night using the DayNight plugin.

Everytime i run the following through the test area in develop app it just says code sent succesfully, no pass or fail comment regardless of changing the status to 1 or 0

Code: [Select]
local dID = 11
local allow = false
local status = luup.variable_get("urn:schemas-rts-services-com:device:DayTime1","Status",dID)
return ((status == "1") == allow)

Not sure what i am doing wrong. The dID is from the device ID tab for the day night plugin and the urn.... is copied directly from the Advanced-Device Type field...

Cheers,
Josh

Offline jswim788

  • Hero Member
  • *****
  • Posts: 538
  • Karma: +29/-2
Re: Conditional Scene Execution: Some Examples
« Reply #263 on: June 29, 2017, 05:02:06 pm »
Are you sure about that URN?  I may have a different version of the DayTime plugin, but mine is "urn:rts-services-com:serviceId:DayTime"

Did you hover the mouse over the "Status" variable?  That's the way to see the URN.  (Or open up the plugin's files.)

If you had the URN incorrect, it would result in the behavior you see.  If this doesn't work, I'd really recommend installing AltUI and using its debug window which is really helpful.  Or you can use luup.log and check your log file.  But my guess is the URN.

On a side note, if you only want to check for night, there is the built in "luup.is_night()".  But I suspect you want more than that which is why you are using DayTime.

Offline RichardTSchaefer

  • Master Member
  • *******
  • Posts: 9741
  • Karma: +737/-136
    • RTS Services Plugins
Re: Conditional Scene Execution: Some Examples
« Reply #264 on: June 30, 2017, 07:54:59 pm »
@jswim788 is correct ... you have a device URN and not a service URN ... these are different things in Vera.

Offline JCH

  • Jr. Member
  • **
  • Posts: 51
  • Karma: +0/-0
Re: Conditional Scene Execution: Some Examples
« Reply #265 on: November 04, 2017, 04:11:19 am »
Hi
I am trying to create a Scene for kids bedtime.
I have modifed the code to Dim the lights to 25% then every 30 seconds dim another 1% until it reaches 5%...
That is all working as expected but... I want to turn the lights completly off after 30 mins. What is the best way to achieve this? Should i add a delay in the scene? or a delay in the code?

Code: [Select]
local dID = 66 -- Device ID of Device to be Dimmed
luup.call_action("urn:upnp-org:serviceId:Dimming1", "SetLoadLevelTarget", {newLoadlevelTarget = "25"}, dID) -- Set Device to 25% then Dim Slowly to 5%
luup.call_delay("delayDim",30,dID) -- 30 Second Delay Between Dimming

function delayDim(dev)
     local devno = tonumber(dev)
     local lls = tonumber((luup.variable_get("urn:upnp-org:serviceId:Dimming1", "LoadLevelStatus", devno)))
     local newlls = lls - 1   -- 1% Increments
     if newlls < 5 then newlls = 5 end  -- Dont Dim Lower Than 5%
     luup.call_action("urn:upnp-org:serviceId:Dimming1", "SetLoadLevelTarget", {newLoadlevelTarget = newlls}, devno)
     if newlls > 5 then luup.call_delay("delayDim",30,dev) end 
end

Offline RichardTSchaefer

  • Master Member
  • *******
  • Posts: 9741
  • Karma: +737/-136
    • RTS Services Plugins
Re: Conditional Scene Execution: Some Examples
« Reply #266 on: November 04, 2017, 12:27:14 pm »
There is no benefit of one vs the other.
You already have LUA code .. so it's up to you.

NOTE: If Vera restarts during ANY of this ...  your timing logic will stop where its at.
PLEG is more robust in the context of Vera restarts.
 

Offline JCH

  • Jr. Member
  • **
  • Posts: 51
  • Karma: +0/-0
Re: Conditional Scene Execution: Some Examples
« Reply #267 on: November 04, 2017, 06:43:12 pm »
Thanks Richard

I usually would use PLEG as i do for 99% of my automation, But i was really struggling to work out a way to do this with PLEG?

Would a Vera restart affect the timing logic even if I put the delay in the luup code?

Offline JCH

  • Jr. Member
  • **
  • Posts: 51
  • Karma: +0/-0
Re: Conditional Scene Execution: Some Examples
« Reply #268 on: November 05, 2017, 04:07:27 am »
Also i cant figure out how to stop the code running if the lights are dim back up to full brightness (above 25%),
It just keeps dimming until it eventually gets down to 5%.
I would like it that if i want to stop the Dimming Scene i can set the lights back to full brightness and the scene stops?\

Can anyone help me out here?

Offline RichardTSchaefer

  • Master Member
  • *******
  • Posts: 9741
  • Karma: +737/-136
    • RTS Services Plugins
Re: Conditional Scene Execution: Some Examples
« Reply #269 on: November 05, 2017, 07:58:51 pm »
In LUA you need to have the various events set state in global variables.
Those state variables can be accessed by the delayed code ... when it wakes up to decide what to do next ... i.e. stop, keep working, ...
Of course certain events will start the timers ... so you need to make sure you do not start a new timer if one is already running ... more global state is needed to keep track of this.

You are not getting takers because this problem is beyond the scope of simple cut/paste LUA developers ... it requires a good understanding of event driven code, and good design techniques to manage the global state.  Note: It's another level of complexity to make sure this logic would survive a restart.