The Vera Community forums have moved!

Advanced => Programming => Scene Scripting => Topic started by: iflyM3 on April 24, 2011, 09:53:45 pm

Title: Lua Scripting w/Virtual Device - SOLVED :)
Post by: iflyM3 on April 24, 2011, 09:53:45 pm
Need some newbie Lua scripting help please!!!  :)


I have tried this numerous ways using a virtual switch/device and this doesnt seem to be working.  I am not doing something right if someone would be so kind to point me in the right direction or to correct me please?  

The virtual switch is a binarylight device representing if the house is occupied or not.  Virtual switch is labeled as Inhabited Device, Device#94 - Status: 0 = Unoccupied, 1 = Occupied.

I am trying to make a scene where the lights will be triggered by a timer, based on whether or not the house is occupied or not.  I have not yet added the timer as I could not get this to work without the timer.  

If device#94 has a status of 1, meaning occupied, then the light will come on.  If device#94 has a status of 0, meaning unoccupied, then the light will not turn on.

urn:schemas-upnp-org:device:BinaryLight:1
 
This is what I have tried thus far:
Quote
[This always turns the light on, and doesnt seem to return false when device#94 actually has a status of "0".]

local in_device=luup.variable_get("urn:upnp-org:serviceId:BinaryLight1","Status",94)

if (in_device == "0") then
   return false
 else
   return true
end


[This never seems to activate the light, and doesnt seem to return true when device#94 actually has a status of "1".]

local in_device=luup.variable_get("urn:upnp-org:serviceId:BinaryLight1","Status",94)

if (in_device == "1") then
   return true
 else
   return false
end


[This never seems to activate the light, and doesnt seem to return true when device#94 actually has a status of "1".]

local in_device=luup.variable_get("urn:upnp-org:serviceId:BinaryLight1","Status",94)

if not (in_device == "1") then
   return false
end


[please excuse the rather messy scripting below, sought in desperation. This below is the same as the two above.  Doesnt seem to return true when device#94 actually has a status of "1".]

local in_device=luup.variable_get("urn:upnp-org:serviceId:BinaryLight1","Status",94)

if not (in_device == "1") then
   return false
 else
   return true
end

Is the service ID that I am using incorrect or is the {"Status"} that I am using incorrect?
Please help if anyone can, thank you so much!!!

Title: Re: Lua Scripting w/Virtual Device - HELP PLEASE!!! :)
Post by: guessed on April 25, 2011, 12:11:06 am
You want the urn:upnp-org:serviceId:SwitchPower1 serviceId, from these examples:

    http://wiki.micasaverde.com/index.php/Luup_Scenes_Events

If you go into the Advanced tab of any device, and "hover" over the Variable name, it'll print out the corresponding serviceId as a Bubble help...
Title: Re: Lua Scripting w/Virtual Device - HELP PLEASE!!! :)
Post by: iflyM3 on April 25, 2011, 01:13:26 am
@guessed

I KNEW IT!!!  that's where I messed up.   Thank you so much! You rock!!!  I was scratching my head so much I thought that I was going to make myself bald.  lmao!

 ;D
Title: Re: Lua Scripting w/Virtual Device - SOLVED :)
Post by: MNB on April 25, 2011, 01:17:00 pm
@guessed

I KNEW IT!!!  that's where I messed up.   Thank you so much! You rock!!!  I was scratching my head so much I thought that I was going to make myself bald.  lmao!

 ;D

@iflyM3 would you share your final working code and lessons learnt with the LUA challenged folks, many thanxs in advance
Title: Re: Lua Scripting w/Virtual Device - SOLVED :)
Post by: iflyM3 on April 25, 2011, 03:25:03 pm
absolutely, here is my final newbie lua script.  I added another conditional to basically check if the living room dimmer switch (for recessed lighting) is off or has a status of "0" and if it is then it will return true and the living room light (lamps with energy efficient bulbs) will turn on as long as the virtual switch (in_device) also has a status of "1" - meaning the house is occupied or inhabited.   I added a timer in the event to run the scene 30 minutes before sunset, I plan to get an HSM100 sensor in the near future to check the light level within the room to get away from a timer triggered event.  

What I did wrong was that I used the incorrect serviceId, I used BinaryLight1 and it should have been "SwitchPower1".


Quote
local in_device=luup.variable_get("urn:upnp-org:serviceId:SwitchPower1","Status",94)

local watching_movie=luup.variable_get("urn:upnp-org:serviceId:Dimming1","LoadLevelStatus",62)

if (in_device == "1") and (watching_movie == "0") then
   return true
 else
   return false
end
Title: Re: Lua Scripting w/Virtual Device - SOLVED :)
Post by: MNB on April 25, 2011, 03:45:46 pm
Many thanxs for sharing and taking the time to explain.

As to HSM-100.. I have one and using the the light level takes a little "trial and error" aka my office according to HSM-100 light sensing may provide a reading of 65 but when I'm actually in the office it's a little bit dark to my eye's taste ;) so I will turn on my office light depending on  the time of day. also you might want to add a power source (non-battery) to get real time sampling. My two cents
Title: Re: Lua Scripting w/Virtual Device - SOLVED :)
Post by: ofonseca on April 25, 2011, 07:17:31 pm
Hi iflyM3,

I find your example very interesting but I´m very new to lua scripting so I got a basic question for you:

- In order to verify if your house is empty or not, you have to modify the status of the virtual switch (1 or 0), how you do it?

Thanks in advanced
Title: Re: Lua Scripting w/Virtual Device - SOLVED :)
Post by: iflyM3 on April 25, 2011, 10:22:41 pm
sure thing.  I used the Virtual Switch device found on this thread http://forum.micasaverde.com/index.php?topic=2495.msg37300#msg37300 (http://forum.micasaverde.com/index.php?topic=2495.msg37300#msg37300). 

I also have another scene called "All Off" where it turns off all of the devices including this Virtual Switch.  I use this virtual switch as a flag for some scenes that are running on timers so that if nobody is home then those scenes on timers won't run.  The "All Off" scene turns the Virtual Device Off giving you the "0" status.  Seeing this thread again, I realize once more where I messed up.  lmao!  "SwitchPower1" NOT BinaryLight1 as I was trying to use, DOH!. lol. 
Title: Re: Lua Scripting w/Virtual Device - SOLVED :)
Post by: ofonseca on April 26, 2011, 07:52:35 pm
Thank you iflyM3, it works very good.
Title: Re: Lua Scripting w/Virtual Device - SOLVED :)
Post by: Henk on May 08, 2011, 02:58:16 pm
Ok, Tying in post on three or 4 different threads,

Heres the xml files for a virtual state device, updated with the json statement as being made by Ap15e in other threads.

I took the liberty to call it version 0.1

I hope this will be the kick-off for posting working packages / plugings on the forum for UI4.

Any code changes necassary? Feel free to alter the code, test it and make sure you upload the changes as a full package (preferably zipped) to the appropriate thread.

Regards,

Henk
Title: Re: Lua Scripting w/Virtual Device - SOLVED :)
Post by: oTi@ on May 08, 2011, 04:11:47 pm
Thanks @Henk.

I hope this will be the kick off for posting working packages / plugings on the forum for UI4.
Also check out this discussion (http://forum.micasaverde.com/index.php?topic=6280.msg38119#msg38119).