I am trying to write a code that will only allow a scene to run if the power usage is equal to or above 15 watts. Below is the code I wrote it is not working and I am not sure where I went wrong.
local PL = luup.variable_get("urn:micasaverde-com:serviceID:EnergyMetering1", "Watts", 30)
if (PL >= "15") then
return false
end if
Ok I corrected that but still getting a fail when I try to test it
local PL = luup.variable_get( "urn:micasaverde-com:serviceId:EnergyMetering1", "Watts", 30)
if (tonumber(PL) > 15) then
return false
end if
In case you still can’t see it: there is no “D” in “serviceId”.
Also be aware that string comparisons are not a good idea when you are looking at numbers. “105” > “15” is false. Use the tonumber() function to convert your strings to numbers.
Edit: oh, you figured those out already. Now you are getting “code failed” because the Test Lua Code box has the annoying habit of reporting failure when the code returns false. It doesn’t mean your code is wrong.
Defensive programming advice: put a “return true” at the very end so that you always know exactly what your code is returning.
Edit edit: “end if” is spelled “end” in Lua.
you also let an ‘if’ dangle
[quote=“hippieh8er15, post:3, topic:180957”]Ok I corrected that but still getting a fail when I try to test it
local PL = luup.variable_get( "urn:micasaverde-com:serviceId:EnergyMetering1", "Watts", 30)
if (tonumber(PL) > 15) then
return false
end if
[/quote]
should be
local PL = luup.variable_get( "urn:micasaverde-com:serviceId:EnergyMetering1", "Watts", 30)
if (tonumber(PL) > 15) then
return false
end
or even friendlier:
local PL = luup.variable_get( "urn:micasaverde-com:serviceId:EnergyMetering1", "Watts", 30)
if (tonumber(PL) > 15) then
return false
else
return true
end
Added: Sorry futzle… didn’t realize you made the edit
That got it, thank you all for your help I had been trying all kinds of variations with tonumber without tonumber, true statements, false statments etc… trying to get this thing to work. In the end the thing stumping me was the "end if. Thanks again to all.