Bathroom fan control

I have read through every piece of related code I can find and am still not getting what is wrong.

My code:

[code]luup.call_delay(“lightOff”,300)

function lightOff()
local statusL = luup.variable_get(“urn:upnp-org:serviceId:SwitchPower1”,“Status”,211)
local statusF = luup.variable_get(“urn:upnp-org:serviceId:SwitchPower1”,“Status”,212)
if ((statusL == 0) and (statusF == 1)) then
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,{ newTargetValue=0 },212)
end
end[/code]

This code is run by a trigger of turning the light off. A previous trigger in the same scene turns the fan on after a set time of the light being on. I give the fan 5 minuets to clear the air then want to see if the fan is still on and that the light is off before sending a command to turn the fan off. I keep getting: ERROR : Error in lua for scenes and events

Any help would be appreciated.

I cannot see anything wrong with your syntax. It is possible that some extraneous characters are getting inserted when you paste the code into the scene. Use a clean editor like Notepad++ to avoid this.

You may find LuaTest helpful. It is intended specifically for testing and debugging Lua for scenes. See LuaTest.

Further thought: Your function lightOff() is defined globally - and has to be for the delay callback to work. Do make sure that you only have one function with this name.

For posterity I have included my code here. It’s always nice to search out code others have tested.


Trigger 1: Light turned on

luup.call_delay(“smwcfanOn”,120)

function smwcfanOn()
local statusL = luup.variable_get(“urn:upnp-org:serviceId:SwitchPower1”,“Status”,211)
local statusF = luup.variable_get(“urn:upnp-org:serviceId:SwitchPower1”,“Status”,212)
if ((statusL == “1”) and (statusF == “0”)) then
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,{ newTargetValue=1 },212)
end
end

Trigger 2: Light turned off

luup.call_delay(“smwcfanOff”,300)

function smwcfanOff()
local statusL = luup.variable_get(“urn:upnp-org:serviceId:SwitchPower1”,“Status”,211)
local statusF = luup.variable_get(“urn:upnp-org:serviceId:SwitchPower1”,“Status”,212)
if ((statusL == “0”) and (statusF == “1”)) then
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,{ newTargetValue=0 },212)
end
end

The problem was something hidden in the spaces before the indented lines. It did not show up on a text editor or notepad so I placed the code in the test Lua box under app development and removed then re-added all spaces and it fixed the error. Thanks RexBeckett for your post, it put me on to the right path.

Now for some reason neither the on or off will work. I have confirmed the device ID’s and that I have full control of through vera.

Update: I made the corrections suggested below to the code above and it works perfectly.

I’m glad to hear that you found the problem.

Now for some reason neither the on or off will work. I have confirmed the device ID's and that I have full control of through vera.

All your luup.variable_get(…) calls are addressing the variable status. This should be Status - with a capital letter.

And statusL & statusF are strings not numbers, so in your tests you need to compare to strings or convert them to numbers first.

http://wiki.micasaverde.com/index.php/Luup_Lua_extensions#function:_variable_get

[quote=“a-lurker, post:5, topic:182593”]And statusL & statusF are strings not numbers, so in your tests you need to compare to strings or convert them to numbers first.

http://wiki.micasaverde.com/index.php/Luup_Lua_extensions#function:_variable_get[/quote]

That was the final key. Thanks.

I decided to add a bit of complexity to my code. The above works for half-bathes but not for my two with showers. I would like to extend the above code by tracking how long the light is on then when it is turned off if the total light on time is <=5 minuets turn off the fan after 5 minuets but if the light was on >5 minuets turns off the fan after 30 minuets. Can anyone suggest how I would modify the above code to track the total light on time?

Thanks

There is a simple PLEG solution to this here.

If you must do it with Lua, you could use the function os.time() to get the current time (in seconds since the epoch). If you save this in a global variable when the on trigger happens, you could then subtract it from the time when the off trigger occurs. The result is seconds that the light was on and with simple comparisons you can select how long to run the fan timer.