Virtual Switch Questions

Thanks to ashwalb for showing me this (http://forum.micasaverde.com/index.php/topic,9219.msg60722.html#msg60722).

I just was curious what scenarios this would be good for. I’ve got it setup in my Vera2 UI5 but not sure how I’d really use it, for my needs. I wanted to see what situations those of you
who have set up and how it works for you.

right now I can’t think of how I’d benefit from this and just wanted to get some idea’s.

I’m currently controlling about 21nodes. nothing spectacular in how I have my scenes setup just mostly stuff of the outside lights and a few on the inside along with schedules for the hot water heater.

I’m really enjoying my Vera and having fun playing around with different things but I wanted to know how I could use this virtual switch in a practical way.

hopefully this isn’t to broad of a request just curious what this would be good for vs using schedules.

Thanks
Dave

The virtual switch is most useful in conjunction with some Luup code. A couple of good uses for it are when you have a number of scenes that happen throughout the day. If you go on vacation, you might not want those scenes to run or you might have some scenes that should only run in vacation mode. You can create a virtual switch that is vacation mode when on and normal mode when off (or vice versa). Based on the switch setting, you can cause scenes to stop running before they run the commands.

Another possibility is if you control lights and/or thermostat at night so that things are shut off when you are sleeping. If you have guests that are over past your normal time, a virtual switch can override those scenes to prevent them from making changes while you are still entertaining.

I have a group of timed scenes to simulate occupation when no one is home but dont want them to run when we are home, this switch can turn them all on or off in one step

In addition to the above examples I use it a lot to trigger scenes. So I created a button called HOME, and I use it to let the vera know if I am home or not. This does not only stop the scenes which randomly switch on lights, it also triggers a scene which arms all motion sensors (I do not want my pets to turn on lights with motion when I am not home). The same button deactivates all the push messages (prowl) I like to get when I am not home, but I don’t need do get an information about an opened door when I am home.

I also have some scenes which are supposed to turn off lights automatically if there’s no motion, but I only want this to happen if the lights were turned on automatically by motion, not if they were manually turned on. Therefore I use a virtual switch called AutoLight which gets set by the scenes which turn on the lights automatically. So this button basically always “knows” if the lights were turned on manually or automatically.

One could say that only your fantasy limits the possible uses for a virtual switch :wink:

Thanks!

great ideas and you guys helped me think of things I wouldn’t have normally thought about… good stuff and I appreciate the repsonses

dave

I’m trying to set up a scene whereby at a certain time on a certain day (SCHEDULE) a light will turn on only if it is cloudy outside (TRIGGER) based on the whether underground app. I can currently do either one but not make it conditional. I’m wondering if a virtual switch may help somehow.

-Stefan

Search for PLEG

[quote=“chixxi, post:4, topic:170378”]In addition to the above examples I use it a lot to trigger scenes. So I created a button called HOME, and I use it to let the vera know if I am home or not. This does not only stop the scenes which randomly switch on lights, it also triggers a scene which arms all motion sensors (I do not want my pets to turn on lights with motion when I am not home). The same button deactivates all the push messages (prowl) I like to get when I am not home, but I don’t need do get an information about an opened door when I am home.

I also have some scenes which are supposed to turn off lights automatically if there’s no motion, but I only want this to happen if the lights were turned on automatically by motion, not if they were manually turned on. Therefore I use a virtual switch called AutoLight which gets set by the scenes which turn on the lights automatically. So this button basically always “knows” if the lights were turned on manually or automatically.

One could say that only your fantasy limits the possible uses for a virtual switch ;)[/quote]

Hi Chixxi

I’m trying to do the same thing with motions detectors and am trying to use two virtual switches. Could you provide an example of your code… and explain how to use two switches? Thanks

Don

@Don Diego: I switched over to using PLEG for my logic, also with virtual switches. What exactly do you need? Than I can see for what I have code examples…

Hi Chixxi

I have two virtual code segments with virtual switches, both within the same lua code segment. One works one doesn’t.

The one that doesn’t work:

local status = luup.variable_get(“urn:upnp-org:serviceId:VSwitch1”,“Status”, 237)

if(status==“1”)then

if ( luup.is_night() ) then

  luup.call_action("urn:upnp-org:serviceId:SwitchPower1", "SetTarget",

{newTargetValue = “1”}, 17)
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”, “SetTarget”,
{newTargetValue = “1”}, 18)
return true
else
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”, “SetTarget”,
{newTargetValue = “0”}, 17)
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”, “SetTarget”,
{newTargetValue = “0”}, 18)
return false
end
else
return false
end

The one that works:

local DEV_ID = 205

status = luup.variable_get(“urn:upnp-org:serviceId:VSwitch1”,“Status”, 194)

if(status==“1”) then
luup.call_action(“urn:micasaverde-com:serviceId:HomeAutomationGateway1”, “RunScene”, {SceneNum = “71”}, 0)
else
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”, “SetTarget”,
{newTargetValue = “0”}, DEV_ID )
end

Thanks in advance for your help.

Don

hey don, I am very busy the moment, so I don’t really have time to look for errors.

However, a nice way to debug such scripts is to use the log of the vera by using code like this to print debugging information:

luup.log("The current status is: " .. status)

This will print a line in the log of the vera with the value of the variable “status”. By adding messages like these you can figure out where your code fails.

EDIT:
You may access your log by a webbrwoser “http://VERA_IP/cgi-bin/cmh/log.sh?Device=0” or by using the plugin “InfoViewer” which is available in this forum.

[quote=“Don Diego, post:10, topic:170378”]Hi Chixxi

I have two virtual code segments with virtual switches, both within the same lua code segment. One works one doesn’t.

The one that doesn’t work:

local status = luup.variable_get(“urn:upnp-org:serviceId:VSwitch1”,“Status”, 237)

if(status==“1”)then

if ( luup.is_night() ) then

  luup.call_action("urn:upnp-org:serviceId:SwitchPower1", "SetTarget",

{newTargetValue = “1”}, 17)
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”, “SetTarget”,
{newTargetValue = “1”}, 18)
return true
else
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”, “SetTarget”,
{newTargetValue = “0”}, 17)
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”, “SetTarget”,
{newTargetValue = “0”}, 18)
return false
end
else
return false
end

The one that works:

local DEV_ID = 205

status = luup.variable_get(“urn:upnp-org:serviceId:VSwitch1”,“Status”, 194)

if(status==“1”) then
luup.call_action(“urn:micasaverde-com:serviceId:HomeAutomationGateway1”, “RunScene”, {SceneNum = “71”}, 0)
else
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”, “SetTarget”,
{newTargetValue = “0”}, DEV_ID )
end

Thanks in advance for your help.

Don[/quote]

@Don Diego, when you say:

I have two virtual code segments with virtual switches, both within the same lua code segment.

Do you mean that both pieces of code are in the same Luup tab of the same scene? If so, can you post the whole code? Note that, once Lua encounters a return statement, further code is not executed.

I cannot see any syntax errors in the first code chunk. In what way does it not behave as you expect?

[quote=“RexBeckett, post:12, topic:170378”][quote=“Don Diego, post:10, topic:170378”]Hi Chixxi

I have two virtual code segments with virtual switches, both within the same lua code segment. One works one doesn’t.

The one that doesn’t work:

local status = luup.variable_get(“urn:upnp-org:serviceId:VSwitch1”,“Status”, 237)

if(status==“1”)then

if ( luup.is_night() ) then

  luup.call_action("urn:upnp-org:serviceId:SwitchPower1", "SetTarget",

{newTargetValue = “1”}, 17)
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”, “SetTarget”,
{newTargetValue = “1”}, 18)
return true
else
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”, “SetTarget”,
{newTargetValue = “0”}, 17)
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”, “SetTarget”,
{newTargetValue = “0”}, 18)
return false
end
else
return false
end

The one that works:

local DEV_ID = 205

status = luup.variable_get(“urn:upnp-org:serviceId:VSwitch1”,“Status”, 194)

if(status==“1”) then
luup.call_action(“urn:micasaverde-com:serviceId:HomeAutomationGateway1”, “RunScene”, {SceneNum = “71”}, 0)
else
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”, “SetTarget”,
{newTargetValue = “0”}, DEV_ID )
end

Thanks in advance for your help.

Don[/quote]

@Don Diego, when you say:

I have two virtual code segments with virtual switches, both within the same lua code segment.

Do you mean that both pieces of code are in the same Luup tab of the same scene? If so, can you post the whole code? Note that, once Lua encounters a return statement, further code is not executed.

I cannot see any syntax errors in the first code chunk. In what way does it not behave as you expect?[/quote]

Hi Rex

Thanks for your reply. Yes they are both in the same tab. I’m trying to control two things at the same time. The second virtual switch doesn’t seem to control anything. Thee first one works fine. Thought maybe the then… else… ends were not correct. The returns are supposed to end the execution (in other works the second code segment is really the first that’s executed). Wanted to end the segment with the returns… but think they really do not matter.

Again thanks for your help. I’ve been over this a number of times.

Don

Unless you have action in the scene that you want to allow or inhibit, you don’t need to use return statements in the Lua.

Your problem may be due to how you have defined status. The lower segment treats it as global but the upper one defines it as local. I believe you are actually running the lower code first? You should define it as local before it is first used. Alternatively, use two different variable names and define them both as local.

[quote=“RexBeckett, post:14, topic:170378”]Unless you have action in the scene that you want to allow or inhibit, you don’t need to use return statements in the Lua.

Your problem may be due to how you have defined status. The lower segment treats it as global but the upper one defines it as local. I believe you are actually running the lower code first? You should define it as local before it is first used. Alternatively, use two different variable names and define them both as local.[/quote]

Hi Rex

Thanks

 Don