Lua Scripting w/Virtual Device - SOLVED :)

Need some newbie Lua scripting help please!!! :slight_smile:

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:

[b][This always turns the light on, and doesnt seem to return false when device#94 actually has a status of "0".][/b]

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!!!

You want the [tt]urn:upnp-org:serviceId:SwitchPower1[/tt] 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…

@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

[quote=“iflyM3, post:3, topic:168203”]@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[/quote]

@iflyM3 would you share your final working code and lessons learnt with the LUA challenged folks, many thanxs in advance

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”.

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

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 :wink: 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

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

sure thing. I used the Virtual Switch device found on this thread [url=http://forum.micasaverde.com/index.php?topic=2495.msg37300#msg37300]http://forum.micasaverde.com/index.php?topic=2495.msg37300#msg37300[/url].

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.

Thank you iflyM3, it works very good.

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

Thanks @Henk.

Also check out this discussion.