The Vera Community forums have moved!

Advanced => Programming => Scene Scripting => Topic started by: tscott on May 09, 2011, 12:28:48 pm

Title: Setting a scene for away
Post by: tscott on May 09, 2011, 12:28:48 pm
All - I'm trying to set up a scene to control my Thermostat and Lights when I'm away from the house. And a Home Scene when i'm home. 

From a different thread, i was advised how to program the Thermostat internally and placing it into Engergy Saving Mode.  This works great.  I click the away scene and sets the thermostat to EMS and Home Scene when i want the Thermostat to be in Normal Mode.

I would like to take the scene control one step farther.  I would like multiple lights to turn on and off at preset times to simulate that I'm home.  I have modules in my Entry Way, Study and Bedroom. 

Do i need to create seperate scenes to do this or can I program in the one Away scene? 

Here is what I want;
Away Scene
Thermostat - EMS
Entry Light to turn on at 6pm and turn off at 9pm
Study lamp to turn on at 7pm and turn off at 10pm
Bedrrom lamp to turn on at 9pm and turn off at 11pm.

Thanks in advance for any advise.
Title: Re: Setting a scene for away
Post by: anthonyris on May 09, 2011, 10:19:21 pm
A scene is a way of combining a number of commands, events, timers into one "Action". Being able to define basic scenes with multiple commands is the 1st step to having a powerful home automation setup.

For example, my "House Lockdown" scene:

I also have a Scene named Security Lights that turns on a few different lights X minutes after sunset, and then sets them back to their previous state after Y minutes. Each light has a different length of time it stays on. This Scene runs all by itself (because it's triggered by an Event and/or Timers).

You might want to check the documentation on Scenes. I know the docs aren't great, but they give you a start. Start here:
http://docs2.mios.com/doc.php?language=1&manual=1&platform=0&page=setup_wizard
and
http://docs2.mios.com/doc.php?language=1&manual=1&platform=0&page=scenes_tab
Title: Re: Setting a scene for away
Post by: wscannell on May 09, 2011, 10:40:29 pm
What you want to do is a bit complicated, but it can be done.  For each separate time that you want an action to be done, you need to have a separate scene.  I would create an on scene for each light and an off scene for each light.  You could have them turn on at a certain time and turn off at some delta time.  However, this is not 100% reliable.  If you lose power to your Vera or it happens to reboot, the things that are delayed will not happen.

Next you need to use a virtual switch to indicate whether you are away or home.  This is simply like a real switch but there is no actual switch attached to what shows in the dashboard.  More about this later.

Now when your scenes are created, they will run every day.  To allow them to run only when away, you turn the away virtual switch on.  Add some luup code to each scene that you want to operate only in away mode:

local away
away = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1","Status",nn)
if(away=="0")then
  return false
end

Where the nn appears above, insert the device ID of the virtual switch.  This can be found on the top of the settings page of the virtual switch.  This code will prevent the scene from doing anything if the virtual switch is in the off position.

For instructions on how to implement a virtual switch see replies 3 and 4 of:

http://forum.micasaverde.com/index.php?topic=6259.0

Title: Re: Setting a scene for away
Post by: anthonyris on May 09, 2011, 10:53:47 pm
wscannell: Pls don't take this the wrong way, but I don't understand why the recommendation to a clearly-brand-new-Vera-user would start with editing luup code and creating virtual switches. Yes, most of us do that, but then again we're (stupid/crazy/obsessed/relentless?!?) experienced Vera users with scars to prove it.

IMHO, most of what he wants to do can be done via the standard Scenes, Commands, Timers and Events.

Let's guide him, and others, to the 90% solution that doesn't require 3 eyes of newt and a cauldron of boiling batwing!  ;-)

.//A.
Title: Re: Setting a scene for away
Post by: oTi@ on May 09, 2011, 11:05:28 pm
For instructions on how to implement a virtual switch see replies 3 and 4 of:
http://forum.micasaverde.com/index.php?topic=6259.0
@tscott,

Should you be interested in noodling with virtual switches, see reply 5 (http://forum.micasaverde.com/index.php?topic=6259.msg40184#msg40184) for the latest implementation (http://forum.micasaverde.com/index.php?topic=6375.msg40051#msg40051) that should work out of the box.
Title: Re: Setting a scene for away
Post by: tscott on May 10, 2011, 10:50:17 am
All thanks for the help.  I think i understand.  I have used the d_ and I_ statedevice.xlm files ans created the virtual switch.  I have created a scene with a timer to turn on a light.  I'm a bit lost now on how to link the together.  I'm not a programmer by any means, but can navigate through it with a little direction.

I will continue to figure this out.  I appreciate all the help so far.  The forum is full of great information; just finding what information can help is a bit difficult.
Title: Re: Setting a scene for away
Post by: JOD on May 10, 2011, 03:33:51 pm
Quote
I'm a bit lost now on how to link the together.

When you've created the scenes you want to run when you are "away" you need an indicator that shows when you are away. Thats where the virtual / state device comes into play.

We will assume when the state device is "On" you are home and when it's 'Off" you are away. Only when the State device is off is when your away scenes will run.

Drop the code that @wscannell posted above into the Luup area of each "Away" scene and change the "nn" to that of the state devices device number:
Code: [Select]
away = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1","Status",nn)
if(away=="0")then
  return false
end

Click save on the Luup tab and then save on the dashboard.

Check the scenes. Turn the virtual device "Off" (you are away) and click run scene. The scene should run.
Turn the virtual device "On" (you are home) click run scene. The scene should not run.

Hope that helps.

*Edit. As oTi@ points out below, this scenario is the reverse of what was called for and is my "Home" setup not "Away"  Please adjust your scene setup accordinly.


JOD.
Title: Re: Setting a scene for away
Post by: JimMac on May 10, 2011, 03:59:30 pm
You'll find more info and examples of virtual devices and some usage examples here:
http://forum.micasaverde.com/index.php?topic=2826.0
Title: Re: Setting a scene for away
Post by: tscott on May 10, 2011, 05:07:46 pm
Quick Questions:

In the Lump Code, Does 1 = on and 0 = off?

Is there a way to change "on" to read Away and "off" to read Home on the virtual buttons?

 
Title: Re: Setting a scene for away
Post by: oTi@ on May 10, 2011, 05:44:24 pm
In the Lump Code, Does 1 = on and 0 = off?
Yes.

Quote
Is there a way to change "on" to read Away and "off" to read Home on the virtual buttons?
No, not currently, I believe. (@JOD was looking into it.)

I guess @JOD's example may do the opposite of what was promised. It returns false when the state device has status 0, meaning the scene is aborted (i.e. the commands not executed) when the state device is off.

Of course it all depends on what you want, and what you want Home / Away to be.

Do check out @JimMac's stuff too, as suggested; lots of info there.
Title: Re: Setting a scene for away
Post by: JOD on May 10, 2011, 06:49:24 pm
I guess @JOD's example may do the opposite of what was promised.
Hey hey hey, I made no promises!! Actually I made assumptions.
But thats a typical scenario, you get a piece of code and you work with it and make adjustments. OR, as oTi@ points out you make the poster eat crow.  :P
Ok, so if it's inverted as to how you want the Virtual Device to display (the light is on but no one is home) then you could install the inverted D_ file which should give you the opposite effect.

*Edit. As oTi@ points out above, this scenario is the reverse of what was called for and is my "Home" setup not "Away"  Please adjust your scene setup accordingly.
OR you can change the code from (away=="0") to (away~="0")

JOD.

Title: Re: Setting a scene for away
Post by: tscott on May 10, 2011, 09:50:12 pm
Got it - The Virtual Switch Works Perfectly.  The Lights are turning on / off with the timers based on the on / off on the switch.

Now my last question.  I have two scene for the my Thermostat.  One sets to EMS and the other sets to Normal.  Can I link the Virtual Switch to those scenes?  Is it possible to run the scene based on the input from the virtual switch?

Thanks
Title: Re: Setting a scene for away
Post by: oTi@ on May 10, 2011, 09:56:23 pm
In the thermostat scenes, use an event based on whether the virtual switch is switched on or off.
Title: Re: Setting a scene for away
Post by: tscott on May 11, 2011, 07:38:58 pm
I'mnot  having success to get the scene to run when I change the Virtual Switch on/off. 

Is there anything i need to program to force the scene to run?
Title: Re: Setting a scene for away
Post by: JOD on May 11, 2011, 08:09:56 pm
Is the scene functional without code?
Did you add the code to the Luup tab?
Does the code have the State Device device number?

JOD.

Title: Re: Setting a scene for away
Post by: wscannell on May 11, 2011, 10:39:39 pm
You do not need any Luup code for the away thermostat scenes.  Add an event to your scene in the event tab of the scene.  Then select the virtual switch. Select event type "a device is turned on or off".   For the scene that you want to run when you go away, set the which mode to on and save.
The scene should run when you turn the virtual switch on.  You can turn it on from the dashboard.  I have something like this in my setup and I just tested it.

If that does not work, test the scene by clicking on the run button from the dashboard.  See if it does the correct thing.
Title: Re: Setting a scene for away
Post by: tscott on May 12, 2011, 11:03:29 am
Err - Frustrating.  I understand exactly what is being said.  I also think it should work.  I run the scenes "away" and it set my Thermostats at ESM, and run "Home" it set back to normal.  The scenes are working.

I link the scenes to the Virtual Switch - "Energy Saving Mode" and nothing happens to the scenes.  I have attached a couple screen shots of the Dashboard, My Virtual Switch Advanced Tab, Away Sceen Event Tab, Away Summary showing 2 commands and 1 event linked. 

The two commands is for two thermostats. 

The scenes work on their own when run, however do not trigger from the virtual switch.  The other frustating bit is I have the virtual switch contoling three lights which are on timer scenes.  When i press on, the lights follow the timers.  At least i think they do, the lights come on like they should, i assume the timers are functioning. 

I know i'm close to having it working.   
Title: Re: Setting a scene for away
Post by: JimMac on May 12, 2011, 11:24:03 am
What code do you have in the Luup tab of your scene?  How are you toggling the state of your virtual switch?

Try toggling your "Energy Saving Mode" virtual switch using Luup code.  I have had issues where switching it in the command section does not always work.

The best way to test this out is to keep it simple.  Add the code below to the Luup tab in a new scene.  Run the scene and observe the status of your "Energy Saving Mode" switch.  It should change from off to on/on to off every time your run the scene.   If this works then your virtual switch is working.

local Energy_Mode=luup.variable_get("urn:upnp-org:serviceId:SwitchPower1","Status",11)

if (Energy_Mode == "1") then
luup.variable_set("urn:upnp-org:serviceId:SwitchPower1","Status",0,11)
else
luup.variable_set("urn:upnp-org:serviceId:SwitchPower1","Status",1,11)
end
Title: Re: Setting a scene for away
Post by: oTi@ on May 12, 2011, 11:31:53 am
I know i'm close to having it working.   
Are you sure you picked up the v0.1 zip from here (http://forum.micasaverde.com/index.php?topic=6375.msg40051#msg40051)?
Title: Re: Setting a scene for away
Post by: tscott on May 12, 2011, 11:48:30 am
JmMac - I have completed the test.  The switch turns off/on when i set up the test scene using your codes. 

OTI@ - i'm pretty sure i'm running v1. 

Title: Re: Setting a scene for away
Post by: tscott on May 12, 2011, 12:04:37 pm
Having downloaded the last files from OTI@ and uploaded into my files.  Everything seems to be working.  The virtual switch is now activating the ThermoStat scenes.  Awesome -

Thanks for all the help.


Title: Re: Setting a scene for away
Post by: oTi@ on May 12, 2011, 12:33:17 pm
[...] The virtual switch is now activating the ThermoStat scenes.  Awesome -
At last! I guess you were missing the boiling batwing (http://forum.micasaverde.com/index.php?topic=6505.msg40182#msg40182). And now have your (first) scars (http://forum.micasaverde.com/index.php?topic=6505.msg40182#msg40182). ;)
Title: Re: Setting a scene for away
Post by: shahhim on May 18, 2011, 09:24:46 pm
Can some one help! How do I implement a virtual switch. Is the any link that goes over step by step instructions ?
Title: Re: Setting a scene for away
Post by: oTi@ on May 18, 2011, 11:32:45 pm
Can some one help! How do I implement a virtual switch. Is the any link that goes over step by step instructions ?
The plug-ins can be found here (http://wiki.micasaverde.com/index.php/Luup_plugins_and_Luup_code). Search for 'Virtual StateDevice'.

Generic instructions how to install the files are here (http://forum.micasaverde.com/index.php?topic=6569.msg40688#msg40688).
Title: Re: Setting a scene for away
Post by: axill on May 19, 2011, 01:59:15 am
I had the similar idea - for Vera to identify automaticaly away.
First of all I create a virtual status device which represent away status (in my case OFF means away).
I used StateDevice described in enother thread.

Second I create two scenes:

1. Trigerring entrance and setting my status to away OFF (someone entering house).
This one is simple - I add events for all my 10 motion sensors to trigger in Tripped = ON

2. More complicated. It is a timer based scene (in my case run each 30 minutes).
In this scene I go thouth the circle on luup.devices and looking to Tripped status and LastTrip timestamp of all my devices with devices[].device_type coresponding to motion sensor.
Within this circle I calculates a minumum difference beetwen current time and LastTrip.
As soon as this minimum difference is above threshold (I set 30 minutes for daylight time and 6 hours for night) the scene is setting my virtual status to away ON

I can share my Lua code if it will be interesting
Title: Re: Setting a scene for away
Post by: shahhim on May 28, 2011, 08:18:16 pm
tscott,
So all said and done what exactly did you do for your thermostats so that now  they are working. I have a virtual switch thats working but I am a little confused at to what needs to be done next. Do I need to create another scens or what ? and then do exactly what
shahhim
Title: Re: Setting a scene for away
Post by: MNB on May 28, 2011, 09:15:01 pm
@axill, yes please would be interested in seeing both of you scenes, Mike
Title: Re: Setting a scene for away
Post by: oTi@ on May 29, 2011, 08:26:50 am
[...] I have a virtual switch thats working but I am a little confused at to what needs to be done next. Do I need to create another scens or what ? and then do exactly what
In general, it's a 3 step process:

1) Create virtual switch (your Home/Away light).
2) Set up scenes with triggers to turn on/off the Home/Away light.
3) Set up scenes that are triggered by the Home/Away light being switched on/off.

Think of the virtual switch as a light that you turn on to indicate that you're home, and turn off to indicate that you're away. (Or the other way around; whatever you prefer.)

So, what you need is events/triggers to turn the light on or off. In its simplest form you could switch the light manually through the Dashboard. But you could use more advanced triggers, like motion sensors, a lock being lock/unlocked, a button on a scene controller, etc. Whatever works for your situation.

The hard part is coming up with a good set of triggers that work reliably. E.g., a motion sensor that triggered could be an indication that you're home (or a burglar?), if you have motion sensors covering all your entrances. Etc.

Once you have that sorted, the other part is using the state switches (on/off) of the light as events in scenes to do whatever you want to do when you go away or arrive home.

So if you wanted to do something with your thermostats, you'd create a scene for that and use 'a device is turned on/off' and the appropriate 'on'/'off' for the Home/Away light as the event.


Start simple /  go in iterations. You could skip step 2 and do a single, simple scene in step 3. Test the scene with the 'Run' button. Then test it by switching the Home/Away light. Then add more complicated events and scenes.
Title: Re: Setting a scene for away
Post by: Henk on May 29, 2011, 09:46:20 am
[...] I have a virtual switch thats working but I am a little confused at to what needs to be done next. Do I need to create another scens or what ? and then do exactly what
In general, it's a 3 step process:

1) Create virtual switch (your Home/Away light).
2) Set up scenes with triggers to turn on/off the Home/Away light.
3) Set up scenes that are triggered by the Home/Away light being switched on/off.

Think of the virtual switch as a light that you turn on to indicate that you're home, and turn off to indicate that you're away. (Or the other way around; whatever you prefer.)

So, what you need is events/triggers to turn the light on or off. In its simplest form you could switch the light manually through the Dashboard. But you could use more advanced triggers, like motion sensors, a lock being lock/unlocked, a button on a scene controller, etc. Whatever works for your situation.

The hard part is coming up with a good set of triggers that work reliably. E.g., a motion sensor that triggered could be an indication that you're home (or a burglar?), if you have motion sensors covering all your entrances. Etc.

Once you have that sorted, the other part is using the state switches (on/off) of the light as events in scenes to do whatever you want to do when you go away or arrive home.

So if you wanted to do something with your thermostats, you'd create a scene for that and use 'a device is turned on/off' and the appropriate 'on'/'off' for the Home/Away light as the event.


Start simple /  go in iterations. You could skip step 2 and do a single, simple scene in step 3. Test the scene with the 'Run' button. Then test it by switching the Home/Away light. Then add more complicated events and scenes.

Nice description oTi@
You should go into the How To writing business ;-)
Title: Re: Setting a scene for away
Post by: oTi@ on May 29, 2011, 11:21:04 am
You should go into the How To writing business ;-)
Yep, I was actually thinking of throwing this onto the HowTo pages at some point. But if you and/or @huogas think it's good to go there as is, feel free.
Title: Re: Setting a scene for away
Post by: Henk on May 29, 2011, 11:26:04 am
You should go into the How To writing business ;-)
Yep, I was actually thinking of throwing this onto the HowTo pages at some point. But if you and/or @huogas think it's good to go there as is, feel free.

Does it need maturing and more steps/examples added?
Do you have enough information to fill out this example?

example (http://wiki.micasaverde.com/index.php/1._HOWTO_page_layout)
Title: Re: Setting a scene for away
Post by: RastusB2 on July 08, 2011, 06:05:23 pm
I have set up my virtual switch and can toggle it on & off using a pin code from my Schlage lock
I entered the following code in my Trane thermostat 3:00 scene: ((I have -4- scenes for differant times))

local away_home=luup.variable_get("urn:upnp-org:serviceId:SwitchPower1","Status",53)

if (away_home == "1") then
luup.variable_set("urn:upnp-org:serviceId:SwitchPower1","Status",0,53)
else
luup.variable_set("urn:upnp-org:serviceId:SwitchPower1","Status",1,53)
end
So far the only thing that works is the virtual switch.
When I click the Energy button on the TRANE GUI, it will switch to the ESM settings.
When I click the Normal button, it will go to the Normal set points.
But the LuuP code won't change the ESM to Normal or vice versa.
Also, how do you get the ESM scene to put the fan on auto and the normal scene to run the fan constant?
Title: Re: Setting a scene for away
Post by: rogerl on August 16, 2011, 02:16:51 pm
[...] The virtual switch is now activating the ThermoStat scenes.  Awesome -
At last! I guess you were missing the boiling batwing (http://forum.micasaverde.com/index.php?topic=6505.msg40182#msg40182). And now have your (first) scars (http://forum.micasaverde.com/index.php?topic=6505.msg40182#msg40182). ;)

I guess I have a substandard batwing. I have a virtual switch as well, using the same code, that works using a Luup test as above. However, when the state of the virtual switch changes, the scene that should be triggered from the switch is not triggered (I guess - at least the state of the relevant device does not change). Screen shots below. What am I missing?
Title: Re: Setting a scene for away
Post by: JOD on August 16, 2011, 03:08:19 pm
@rogerl,

Did you boil the batwing?

Your scene setup is correct for turning the living room lights on if the Virtual Device is turned on.
What did you want the scene to accomplish?
Are you using a timer?
Can you post your code?
Did you change the device number in your code to match your VD #14?

JOD.
Title: Re: Setting a scene for away
Post by: rogerl on August 16, 2011, 09:41:50 pm
All I wanted the scene to accomplish was to turn the living room lights on when the virtual device is switched on. What I posted is all the code related to these two things - except for the code for the virtual device itself, which came from Henk yesterday. The test code that actuates the virtual device works correctly (and yes, I did change the device number in that code to 14); it is copied from the code higher up in this thread. But whichever method I use to turn the virtual device on or off seems to have no effect on the LR light setting.

Is there some way I can actually tell if the scene (the one imaginatively named New Scene in the above screen shots) fires or not? (I don't see how it could be firing - when I run it by pressing the "Run" button, the LR light turns on).
Title: Re: Setting a scene for away
Post by: oTi@ on August 16, 2011, 11:51:34 pm
[...] I have a virtual switch as well, using the same code, [...]
Did you download the virtual switch code from this location (http://forum.micasaverde.com/index.php?topic=6375.msg40051#msg40051)?
Title: Re: Setting a scene for away
Post by: rogerl on August 17, 2011, 12:04:11 am
@oTi, no, from this one: http://forum.micasaverde.com/index.php?topic=6650.15, which was posted yesterday.
Title: Re: Setting a scene for away
Post by: oTi@ on August 17, 2011, 07:01:12 am
@oTi, no, from this one: http://forum.micasaverde.com/index.php?topic=6650.15[...]
OK; that is the same set.

Could you try unpacking that archive and uploading the files to Vera one more time? The symptom you're describing sounds like you may not have the correct files somehow.
Title: Re: Setting a scene for away
Post by: rogerl on August 17, 2011, 08:37:58 am
@oTi, I will, but perhaps you can also tell me:
1. When I unpack that archive there are only two files there: I_... and D_... . I could have sworn that there was also a D_BinaryLight1.json file before.
2. I currently have two separate D_BinaryLight1.json files on my Vera. How can I remove one of them (at least), and where should I find the correct file to use?
Title: Re: Setting a scene for away
Post by: rogerl on August 17, 2011, 08:42:11 am
Wow, that solved the problem. I guess in the end it was user error, but I am (of course) certain I did just now exactly what I did before. Thanks.
Title: Re: Setting a scene for away
Post by: rogerl on August 17, 2011, 05:00:59 pm
One last (I hope) question. Now that I have a virtual home/away switch, I'd like to build a second virtual switch, for "is it winter weather". I don't particularly want to use the calendar to determine that; I'd rather click it on and off myself. What do I have to change in the D_ and I_StateDevice files? My thinking is that in D_StateDevice I need to change the Service List section,

    <serviceList>
      <service>
        <serviceType>urn:schemas-upnp-org:service:SwitchPower:1</serviceType>
        <serviceId>urn:upnp-org:serviceId:SwitchPower1</serviceId>
        <controlURL>/upnp/control/SwitchPower1</controlURL>
        <eventSubURL>/upnp/event/SwitchPower1</eventSubURL>
        <SCPDURL>S_SwitchPower1.xml</SCPDURL>
      </service>
    </serviceList>
to read

    <serviceList>
      <service>
        <serviceType>urn:schemas-upnp-org:service:SwitchPower:1</serviceType>
        <serviceId>urn:upnp-org:serviceId:SwitchPower2</serviceId>
        <controlURL>/upnp/control/SwitchPower2</controlURL>
        <eventSubURL>/upnp/event/SwitchPower2</eventSubURL>
        <SCPDURL>S_SwitchPower1.xml</SCPDURL>
      </service>
    </serviceList>
and change all the references to SwitchPower1 in the implementation file to refer instead to SwitchPower2 (and of course upload it to Vera as a different file). Make sense?

Roger
Title: Re: Setting a scene for away
Post by: JOD on August 17, 2011, 09:27:19 pm
Mios developers => Create device, add D_StateDevice.xml to the UpnpDevFilename area.
Click Create device.

You should now have another State Device.

JOD.