Author Topic: Please help linking functions together  (Read 498 times)

Offline nwtundra

  • Newbie
  • *
  • Posts: 8
  • Karma: +0/-0
Please help linking functions together
« on: October 22, 2018, 10:40:27 am »
Hi, I would really appreciate if you could help me to get this to work.  I have 3 conditions that need to happen for this scene to run but get stuck on how to link them together. I red through many posts to try to figure it out and learned a lot from you already. Thank you for everything and I hope someone can help.

local function checkTime()
   local pStart = "17:00"   -- Start of time period
   local pEnd = "17:35"     -- End of time period
   local allow = true       -- true runs scene during period, false blocks it
   local hS, mS = string.match(pStart,"(%d+)%:(%d+)")
   local mStart = (hS * 60) + mS
   local hE, mE = string.match(pEnd,"(%d+)%:(%d+)")
   local mEnd = (hE * 60) + mE
   local tNow = os.date("*t")
   local mNow = (tNow.hour * 60) + tNow.min
   if mEnd >= mStart then
       return (((mNow >= mStart) and (mNow <= mEnd)) == allow)
   else
       return (((mNow >= mStart) or (mNow <= mEnd)) == allow)
   end
end
local function checkHum()
     local dID = 105            -- Device ID of Hum sensor
     local lLow = 80           -- Lowest level of range
     local lHigh = 100         -- Highest level of range
     local allow = true       -- true runs scene when in range, false blocks it
     local lCurrent = tonumber((luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature",dID)))
     return (((lCurrent >= lLow) and (lCurrent <= lHigh)) == allow)
end
local function checkAway()
  local deviceNo = 4          -- Device ID of armed sensor
  local SS_SID = "urn:micasaverde-com:serviceId:SecuritySensor1" -- Security Sensor Service ID
  local armed = luup.variable_get (SS_SID, "Armed", deviceNo)
  local allow = false    -- true runs scene if switch is on, false blocks it
  local status = luup.variable_get("urn:micasaverde-com:serviceId:SecuritySensor1","Status",dID)
  return ((status == "1") == allow)
end

Offline reneboer

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1540
  • Karma: +107/-31
Re: Please help linking functions together
« Reply #1 on: October 23, 2018, 05:10:16 am »
Hi,

You would add a line like this at the end of your example to have the scene continue if all three conditions are true.
Code: [Select]
return (checkTime() and checkHum() and checkAway())
If you need different condition state it could read.
Code: [Select]
if  (checkTime() and (checkHum() or checkAway())) then
  return false
else
  return true
end

Have fun.

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

Offline nwtundra

  • Newbie
  • *
  • Posts: 8
  • Karma: +0/-0
Re: Please help linking functions together
« Reply #2 on: November 08, 2018, 11:48:10 am »
Thank you so much Rene.  I have been out of town traveling and just got back to this.  Code passes the test in Lua test app but the scene dos not work.  I must be missing something.  If you see something obvious please help again. Thank you!!


function checkTime()
   local pStart = "17:15"   -- Start of time period
   local pEnd = "17:35"     -- End of time period
   local allow = true       -- true runs scene during period, false blocks it
   local hS, mS = string.match(pStart,"(%d+)%:(%d+)")
   local mStart = (hS * 60) + mS
   local hE, mE = string.match(pEnd,"(%d+)%:(%d+)")
   local mEnd = (hE * 60) + mE
   local tNow = os.date("*t")
   local mNow = (tNow.hour * 60) + tNow.min
   if mEnd >= mStart then
       return (((mNow >= mStart) and (mNow <= mEnd)) == allow)
   else
       return (((mNow >= mStart) or (mNow <= mEnd)) == allow)
   end
end
local function checkHum()
     local dID = 105            -- Device ID of your Hum sensor
     local lLow = 50           -- Lowest level of range
     local lHigh = 100         -- Highest level of range
     local allow = true       -- true runs scene when in range, false blocks it
     local lCurrent = tonumber((luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature",dID)))
     return (((lCurrent >= lLow) and (lCurrent <= lHigh)) == allow)
end
local function checkAway()
  local deviceNo = 4          -- Device ID of your MultiSwitch
  local SS_SID = "urn:micasaverde-com:serviceId:SecuritySensor1" -- Security Sensor Service ID
  local armed = luup.variable_get (SS_SID, "Armed", deviceNo)
  local allow = false    -- true runs scene if switch is on, false blocks it
  local status = luup.variable_get("urn:micasaverde-com:serviceId:SecuritySensor1","Status",dID)
  return ((status == "1") == allow)
end
if  (checkTime() and (checkHum() and checkAway())) then
  return true
end

Offline rigpapa

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1047
  • Karma: +168/-1
Re: Please help linking functions together
« Reply #3 on: November 08, 2018, 01:07:46 pm »
There's a possible exit from the scene Lua without a return value, which Luup may/will interpret as "false" and not run your scene actions. You're missing the "else" cause Rene suggested at the end.
Author of Reactor, DelayLight, SiteSensor, Rachio, Deus Ex Machina II, Intesis WMP Gateway, Auto Virtual Thermostat and VirtualSensor plugins. Vera Plus w/100+ Z-wave devices. Vera3, Lite. Hassio, Slapdash.

Offline nwtundra

  • Newbie
  • *
  • Posts: 8
  • Karma: +0/-0
Re: Please help linking functions together
« Reply #4 on: November 20, 2018, 04:26:58 pm »
Hi, stupid question but I can't get this to work. It clears the Lua code test with no errors like everything is okay but when I add it to luup section in my scene nothing ever happens? No errors but also no results.  Does this code needs to be added to startup Lua?  Thank you in advance.

Offline reneboer

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1540
  • Karma: +107/-31
Re: Please help linking functions together
« Reply #5 on: November 21, 2018, 04:54:27 am »
Hi,

It needs to go in your scene LUA code and the scene needs a trigger. This code will just determine if the rest of the scene (activities) will be completed (when returned true) or not (when returned false).

You have to change the last bit so you always return a value:
Code: [Select]
if  (checkTime() and (checkHum() and checkAway())) then
  return true
else
  return false
end

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

Offline nwtundra

  • Newbie
  • *
  • Posts: 8
  • Karma: +0/-0
Re: Please help linking functions together
« Reply #6 on: November 26, 2018, 12:42:09 pm »
Hi, When I add the last few lines as suggested I can't get this to pass the Luup code test.  Without the 3 last lines it passes the test. What is returning an error in these lines please??  Also, should there be another parenthesis before and after checkAway? Thank you so much.

if  (checkTime() and (checkHum() and checkAway())) then
  return true

else
  return false
end

Offline reneboer

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1540
  • Karma: +107/-31
Re: Please help linking functions together
« Reply #7 on: November 27, 2018, 06:02:23 am »
It means the return value is false, i.e. your conditions are not all true.

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