Conditional Scene Execution: Some Examples

When I said

The last one loaded gets to live.

I was referring to the case when several scenes contain a global function with the same name but different code. The last scene to run overwrites the function definition. Any pending delayed callbacks can end-up using the wrong version. It’s bad - don’t do it. :wink:

When a function uses local variables, each caller gets a fresh set on the stack so is isolated from other users. The combination of local variables and passed parameters allows general-purpose functions to be shared.

I'll post again in a week or two. :).
You don't have to read the whole book in one go. You will find the first twenty pages enlightening and, after fifty pages, it will all make much more sense.

Well, this makes me believe I’m on the right track. The checkLastTrip function can be used with different combinations of sensors, switches and delay (if I learn to pass variables) with just a few lines of code. This seems like it will minimize system resources used, and let me “tweak” as needed.

I was only planning on a couple of chapters when I said a week or two. Ok, I’m not always that slow but life tends to interfere with my hobbies at times. 8)

RexBeckett, thank you for your work on this. Due to your efforts I am jumping into the Lua language with both feet. I’m looking forward to understanding and exploring the whole home automation with Vera capabilities better. I really ( and truly) appreciate your writing in complete and coherent sentences. So many posters don’t do that. In other words, it’s all your fault. When she complains, I’ll give my wife your number. Of course I don’t have your number but she doesn’t know that. Thanks again.

When she complains, I'll give my wife your number.

I’m only an Engineer. I do alright with electronics, mechanics and software but I can’t fix wives. I’m still waiting for somebody to write a manual. ;D

This is EXACTLY what I need, BUT I have NO IDEA how to IMPLEMENT your instructions. I see I am supposed to put in the lump code but I don’t know how or where to do that. PLEASE put up step by step instructions: Go to the dashboard, click on Automation, etc.

Thank you so much!!!

[quote=“Donna, post:85, topic:178331”]This is EXACTLY what I need, BUT I have NO IDEA how to IMPLEMENT your instructions. I see I am supposed to put in the lump code but I don’t know how or where to do that. PLEASE put up step by step instructions: Go to the dashboard, click on Automation, etc.

Thank you so much!!![/quote]

Here is a set of instructions for Creating Scenes. You should copy/paste the Lua code onto the scene’s LUUP tab. Click Save Lua, then Confirm changes to save the scene. You will need to Save/Reload Vera before your new or edited scenes can be used.

I tried the virtual switch. From you post, I took it as:

  1. I open an new scene.
  2. go to the LUUP tab
  3. paste the code copied from your virtual switch post
  4. change the ID to the one I want in the scene
  5. Save
  6. test switch
    Operation that should occur. When I activate the scene once, if the device is off, it should turn on. If the device is on, it should turn off. Its an on/off virtual switch. That is what I assumed when I attempted to create this scene. I found the device ID by going to the advance tab, selecting the drop box with the list of all the devices and chose the number to the left of the name of the device.

I have successfully created a schedule for an outlet. It works properly. I’ve also created a scene that turns all devices off.

I’m missing steps or something.

Thank you.

@RexBeckett
Hi,
I arrived a little late just to congratulate you for this excellent work. I think it has or will help many of us.

Hi

I would like to NOT run a scene if my lamps are already on.

I have a lights on scene that runs at sunset, but if I have already turned on those lights manually I don’t want this scene to run.

I’ve looked at the code examples but not really sure how to do this?

Many thanks

Thanks for all your work on this Rexbeckett! This is exactly what I was looking for. Let’s me make more complex scenes without getting buried in code!

Sent from my iPad using Tapatalk

I have two questions. The first question is, I built an interface that allows me to set values into several Variable Containers with my Pronto remote. I would like to be able to something similar with the Day Or Night plugin. Is it possible to set a day and night offset into the Day Or Night Plugin with Lua? I can set the plugin status with:
luup.call_action(“urn:rts-services-com:serviceId:DayTime”, SetTarget {newTargetValue=“0”}, dID).
I could not find a way to enter either the day or night offset.

If I can’t get the code to accomplish the question above, I would like to get suggestions to accomplish the following. I have a time offset stored in a variable container. I have several scenes that I want to run before or after sunset using on the value stored in a variable container for the offset. Any help would be appreciated. :wink:

Thank you, Mark

[quote=“cw-kid, post:89, topic:178331”]Hi

I would like to NOT run a scene if my lamps are already on.

I have a lights on scene that runs at sunset, but if I have already turned on those lights manually I don’t want this scene to run.

I’ve looked at the code examples but not really sure how to do this?

Many thanks[/quote]

Add to your scene that runs at sunset Lua code that checks the state of the lamps. The appropriate example is that for a Z-Wave switch.

[quote=“MarkAgain, post:91, topic:178331”]I have two questions. The first question is, I built an interface that allows me to set values into several Variable Containers with my Pronto remote. I would like to be able to something similar with the Day Or Night plugin. Is it possible to set a day and night offset into the Day Or Night Plugin with Lua? I can set the plugin status with:
luup.call_action(“urn:rts-services-com:serviceId:DayTime”, SetTarget {newTargetValue=“0”}, dID).
I could not find a way to enter either the day or night offset.

If I can’t get the code to accomplish the question above, I would like to get suggestions to accomplish the following. I have a time offset stored in a variable container. I have several scenes that I want to run before or after sunset using on the value stored in a variable container for the offset. Any help would be appreciated. :wink:

Thank you, Mark[/quote]

The DayTime plugin does not support actions for changing the offsets from Lua.

The most reliable alternative would be to have a scene that runs, say, every five minutes. In the scene’s Luup, get the time of the next sunset using luup.sunset() and compare that with the current time (os.time()) and the value of your saved offset. Be aware that after sunset, the time returned by luup.sunset() will be for the next day so you will need to deal with that. You will also need to bypass the code once it has detected sunset until the following day.

I suggest the periodic scene because it will survive a Vera restart without the need for special restart processing.

Welcome back Rex! Thank you for the reply! ;D

I setup several scenes using the Virtual Clock plugin for the trigger. Took a little while to figure how to set the end time.
Needed to get the current clock start time, calc the seconds to end time, set the duration (seconds) and then re-save the start time.

Thanks again, Mark

Virtual Clock is a good solution. It does a good job of managing restarts. As you have found out, you need to send the SetAlarmTime action as the last step in order to restart the timer with the new times.

[quote=“RexBeckett, post:3, topic:178331”]local pStart = -15 -- Start of time period, minutes offset from sunset local pEnd = "23:30" -- End of time period local allow = true -- true runs scene during period, false blocks it local mStart = math.floor( (luup.sunset() % 86400) / 60 ) + pStart local hE, mE = string.match(pEnd,"(%d+)%:(%d+)") 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[/quote]

I’m trying to get this to work from 15mins before sunset to 11:30PM the same night, but the scene seems to run all the time. I’ve looked at the kwikLog output and everything looks correct for my timezone, etc.
Any ideas what’s going on, please?

Cheers

50 10/09/14 14:32:10.447 luup_log:0: Scene Debug: mStart= 457;mNow= 872; mEnd = 1410 <0x31df3680>

The example doesn’t seem to work, but this seems to work, so far…

[code]local currentTime = os.date(“%H%M”) + 0
local sunset = os.date(“%H%M”,luup.sunset()) + 0
local endTime = “2330” + 0
Offset = “-15” + 0
sunsetOffset = sunset + Offset

luup.log("Scene Debug: sunset = " … sunset … "; sunsetOffset = " … sunsetOffset … ";currentTime = " … currentTime … "; endTime = " … endTime)

if ((currentTime >= sunsetOffset) and (currentTime <= endTime))

then return true
else return false

end[/code]

50 10/09/14 14:36:44.057 luup_log:0: Scene Debug: sunset = 1752; sunsetOffset = 1737;currentTime = 1436; endTime = 2330 <0x31df3680>

I hope this hasn’t been answered, but I think this might be the solution to my problem…

I have a qubino dimmer. I1 (the physical one) is connected to my dimmable lights in the kitchen. I2 and I3 should be used to turn my ceiling lights on/off - however, I2 and I3 are only “motion detectors” so switching them on trips the dector while turning them off “un-trips” them. This would be easy enough to use, BUT…if I turn them on with the switch, then turn them off through a scene then the physical switch is in the wrong position. Also, as all my other switches are monostable (I’ve fitted a return spring) it would fit my setup a lot better if these could be monostable as well.

Which brings me to my question - is it possible to use luup to turn them on if they’re off or off if they’re on? I can make two scenes with the same trigger (I2 detector triggered/turned on), but I want one of them to not execute based on the status of the lights.

If this is possible I would greatly appreciate it if I could get some help figuring this out.

Cheers!

http://forum.micasaverde.com/index.php/topic,8630.msg82895.html#msg82895 with GUI or

http://forum.micasaverde.com/index.php/topic,11349.msg81403.html#msg81403 with LUUP might help

http://forum.micasaverde.com/index.php/topic,8630.msg82895.html#msg82895 with GUI or

http://forum.micasaverde.com/index.php/topic,11349.msg81403.html#msg81403 with LUUP might help[/quote]
Thanks - that helped a lot! :slight_smile: Now I just need to learn LUUP so I can set my wall switch to step up the lights in 20% increments until it’s 100% - when I turn it off completely and allow a new step up :slight_smile:

Cheers :slight_smile:

[quote=“tiwas, post:97, topic:178331”]I hope this hasn’t been answered, but I think this might be the solution to my problem…

I have a qubino dimmer. I1 (the physical one) is connected to my dimmable lights in the kitchen. I2 and I3 should be used to turn my ceiling lights on/off - however, I2 and I3 are only “motion detectors” so switching them on trips the dector while turning them off “un-trips” them. This would be easy enough to use, BUT…if I turn them on with the switch, then turn them off through a scene then the physical switch is in the wrong position. Also, as all my other switches are monostable (I’ve fitted a return spring) it would fit my setup a lot better if these could be monostable as well.

Which brings me to my question - is it possible to use luup to turn them on if they’re off or off if they’re on? I can make two scenes with the same trigger (I2 detector triggered/turned on), but I want one of them to not execute based on the status of the lights.

If this is possible I would greatly appreciate it if I could get some help figuring this out.

Cheers![/quote]

The following luup code provides a toggle action. If you trigger the scene when your pseudo-motion-sensor trips, it should do what you want.

local dID = 123 -- Device ID of your Z-Wave Switch local status = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1","Status",dID) if status == "1" then luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{newTargetValue=0},dID) else luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{newTargetValue=1},dID) end