Author Topic: Turning on light if Motion and Brightness below X  (Read 263 times)

Offline tanwk3

  • Newbie
  • *
  • Posts: 2
  • Karma: +0/-0
Turning on light if Motion and Brightness below X
« on: February 12, 2018, 02:38:29 am »
Hi Expert
I am trying to write the above however I hit error when testing the Luup code from if onwards.
Anyone able to advise?

It seems stating a variable cannot have any formula e.g.
local brightness = tonumber(luup.variable_get("urn:micasaverde-com:serviceId:LightSensor:1","CurrentLevel",sensorID))


local lightID = 292
local sensorID = 205
local dim = 4
local status =luup.variable_get("urn:upnp-org:serviceId:SwitchPower1", "Status", lightID)
local brightness = luup.variable_get("urn:micasaverde-com:serviceId:LightSensor:1","CurrentLevel",sensorID)
local isNight = luup.is_night()
local bright = tonumber(brightness)
if status == "0" then
    if bright < dim or isNight then
   return true
   end
else
   return false
end
end

Offline reneboer

  • Hero Member
  • *****
  • Posts: 1301
  • Karma: +72/-30
Re: Turning on light if Motion and Brightness below X
« Reply #1 on: February 12, 2018, 06:52:36 am »
Hi,

Luup.variable_get returns two values on success. The value and a timestamp. tonumber takes two arguments, the number and the base. For the latter you want to use 10, but you are passing the timestamp in your example so tonumber will fail. You can rewrite in one line as follows:
Code: [Select]
local brightness = tonumber((luup.variable_get("urn:micasaverde-com:serviceId:LightSensor:1","CurrentLevel",sensorID)),10)

However, if your luup.variable_get fails your code will still fail.

Cheers Rene
2xVeraLite, VeraEdge, openLuup, ALTUI, 20 switches, 10 dimmers, 20 sensors, 10 scene controllers, 1 Harmony Hub, many plug-ins. Not enough time.