luup code another question

Take a look and help me correct:
Luup newbie examples…link to download[url=http://www.box.net/shared/1kl0s49uux]http://www.box.net/shared/1kl0s49uux[/url]

set dimmers:
luup.call_action(“urn:upnp-org:serviceId:Dimming1”,“SetLoadLevelTarget”,{newLoadlevelTarget=“0”},0)

Read Dimmers:
local lul_temp = luup.variable_get(“urn:upnp-org:serviceId:Dimming1”,“LoadLevelTarget”,0)
add on
if( (tonumber(lul_temp)<90
and tonumber(lul_temp)>25) then
do something here
return false
end

Switch on/off:
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,{ newTargetValue=“0”},0)

Trigger Scene:
luup.call_action(“urn:micasaverde-com:serviceId:HomeAutomationGateway1”,“RunScene”,{ SceneNum=“0”},0)

Set Thermostat:
luup.call_action(“urn:upnp-org:serviceId:TemperatureSetpoint1_Heat”,“SetCurrentSetpoint”,{NewCurrentSetpoint=“44”},5)

Get Sensor:
local lul_temp=luup.variable_get(“urn:upnp-org:serviceId:TemperatureSensor1”,“CurrentTemperature”,0)
add on
if( (tonumber(lul_temp)<80
and tonumber(lul_temp)>70) then
do something here
return false
end

Get Security Zone:
local lul_temp=luup.variable_get(“urn:micasaverde-com:serviceId:SecuritySensor1”,“Tripped”,0)
add on
if( lul_tmp==“1” ) then
–something to do goes here
end

This is code generated from a program I am working on…good for Newbies like me, please help me by correcting anything wrong you see!
Regards
Tim Alls

You need to use consistent variables in the code.

eg. use “[tt]temp[/tt]” instead of a mix of “[tt]lu_temp[/tt]” and “[tt]lu_tmp[/tt]”

The variable names must match, otherwise an error will occur (the callee/consumer will see a [tt]nil [/tt] value). It doesn’t really matter what name you use, but they must match.

I dislike MCV’s [tt]lu_[/tt] prefixes on this stuff, and tend to use more natural names.

Also, for numerics, like this:

local temp=luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature", 0) if (tonumber(temp) < 80 ...) then
can be done as:

local temp = tonumber(luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature", 0)) if (temp < 80 ...) then
which is a little easier to read without the clutter.

@guessed,
Just the kind of feedback I need…Thanks
It makes good sense to convert “tonumber” early so that the “tonumber” routine isn’t called over and over…looks cleaner too.
Variables match and simple…no problem.

In regards to time functions…the examples are so long and drawn out because of all the conversions…what if a code generator took time and converted it into seconds so that code is simple and clean…what do you think?

Tim

Haven’t really done a lot of stuff with time, bt I’d look at what the time lib had if I needed to do that. It would mean an extra install step for folks wanting to use it but it would be worth it. Over time (sigh) the lib could be made of MCVs default install (since it was written by their team anyhow)

@Guessed
I tried evry way I could to incorporate the tonumber into line one as you showed but the code fails…I ended up doing it in two steps like this:
local lul_temp = luup.variable_get(“urn:upnp-org:serviceId:Dimming1”,“LoadLevelStatus”,23)
local TEMP = tonumber (lul_temp)

Also the code tester under the MIOS tab sometimes returns a code failed even though it executes all the commands…doesn’t make sense to me!

Example:

local lul_temp = luup.variable_get(“urn:upnp-org:serviceId:Dimming1”,“LoadLevelStatus”,23)
local TEMP = tonumber (lul_temp)
if( TEMP>10) then luup.call_action(“urn:micasaverde-com:serviceId:HomeAutomationGateway1”,“RunScene”,{ SceneNum=“7”},0)
return false
end

Executes scene 7 but still returns code failed??

Regards
Tim

The status you get back (also) represents what you have the Luup return. So if the Luup returns true, you’d get a successful; if it returns false, a fail.

Try these one-liners:

return false

→ fail

return true

→ success

return (luup.is_night())

→ success if you do this at night, fail otherwise

return not (luup.is_night())

→ success if you do this during the day, fail otherwise

I was coming to that conclusion but it still confused me!
I thought the purpose was to test the code for errors…regardless of the return value of the expression. Haha on me!
Talk about learning the hard way…
Thanks
Tim (AKA confuserated2) :slight_smile:

what about using the tonumber in the first part of the expression like Guessed recomended? How do you know when it is the code failing or it is a returned expression? sorry if I am missing something stupid here!

Can you look at this code my program generated (Guessed, Ap15e, oTi@ and you other experts floating around!) Any more tune ups needed?

local DIM = luup.variable_get(“urn:upnp-org:serviceId:Dimming1”,“LoadLevelTarget”,23)
local DIMNUM = tonumber (DIM)
if( DIMNUM<50 and DIMNUM>20 )
then luup.call_action(“urn:micasaverde-com:serviceId:HomeAutomationGateway1”,“RunScene”,{ SceneNum=“5”},0)
end

Looks at a range of values and executes a scene if true.

Thanks
Tim

Ah yes, I forgot that [tt]luup.variable_get[/tt] returns two values, not one. The first is the value, the second is supposed the be the timestamp that the value last changed.

So you do need to do it in separate lines, as you’ve done, but you can use one variable to do it. Also, in Scene-Luup, you should always return [tt]true[/tt] or [tt]false[/tt], explicitly. The behavior has been indeterminate in prior releases if you don’t, I believe this is what oTi@ is indicating in their post.

It should end up looking something like:

[tt] local level = luup.variable_get(“urn:upnp-org:serviceId:Dimming1”, “LoadLevelTarget”, 23)
level = tonumber(level)
if (level < 50 and level > 20) then
luup.call_action(“urn:micasaverde-com:serviceId:HomeAutomationGateway1”, “RunScene”,
{SceneNum=“5”}, 0)
end

return true[/tt]

Typically “upper case” variables are used in Programming for constants, so I changed those, and renamed the variable after the function (light-level or whatever)

@guessed,
Ok, I am getting closer. I was trying to make the variables stand out with the CAPS for the Newbies but I will find another way.
Return True …will continue scene code
Return False …will terminate the scene code
Is that correct?
Regards
Tim

Tim,
Here’s the relevant snippet from the doco:
http://wiki.micasaverde.com/index.php/Luup_Scenes_Events

[hr]
The Lua code is run before the commands that you included in the scene. If your Lua code ends with this: “[tt]return false[/tt]” then the commands in the scene will not be run. [hr]

If I want to write code so that 30 minutes before sunrise turn on virtual switch one…where would you place the code? in the startup tab? What functions in regards to time are rock solid…don’t get reset by a reboot?

Thanks
Tim

[ul][li][/li]
[li][/li][/ul]

[quote=“TimAlls, post:12, topic:168274”]If I want to write code so that 30 minutes before sunrise turn on virtual switch one…where would you place the code? in the startup tab? What functions in regards to time are rock solid…don’t get reset by a reboot?

Thanks
Tim[/quote]

No code. It’s a Timer-based Scene, just select “Day of Week” when creating the timer, then there’s a drop-down that lets you select Before/After Sunrise/Sunset, and the time-offset to use.

Then you just use the regular Commands section to setup the VD the way you want it.