Author Topic: Combining LUA codes  (Read 946 times)

Offline 3rdStng

  • Full Member
  • ***
  • Posts: 119
  • Karma: +1/-0
Combining LUA codes
« on: January 02, 2017, 09:18:48 pm »
I know this is possible with PLEG. Just haven't ventured down that path yet. What I'm after is to have a scene trigger only during a set date range AND if one of my virtual switches is not on. I'm not the best when it comes to programming, which is why I need to embrace PLEG. But how could I mash the two LUA codes together to get what I'm after? I don't think it is as simple and putting it all in the LUA code area and hitting save.

Code: [Select]
local dID = 57          -- Device ID of your VirtualSwitch
local allow = false     -- true runs scene if switch is on, false blocks it
local status = luup.variable_get("urn:upnp-org:serviceId:VSwitch1","Status",dID)
return ((status == "1") == allow)

Code: [Select]
local mdStart = "10/15"  -- Start of period (MM/DD)
local mdEnd = "12/31"    -- End of period (MM/DD)
local allow = true       -- true runs scene during period, false blocks it
local smS, sdS = string.match(mdStart,"(%d+)%/(%d+)")
local smE, sdE = string.match(mdEnd,"(%d+)%/(%d+)")
local mS = tonumber(smS)
local dS = tonumber(sdS)
local mE = tonumber(smE)
local dE = tonumber(sdE)
local tNow = os.date("*t")
local mN = tNow.month
local dN = tNow.day
if (mE > mS) or ((mE == mS) and (dE >= dS)) then
     return (((mN > mS) or ((mN == mS) and (dN >= dS))) and ((mN < mE) or ((mN == mE) and (dN <= dE))) == allow)
else
     return (((mN > mS) or ((mN == mS) and (dN >= dS))) or ((mN < mE) or ((mN == mE) and (dN <= dE))) == allow)
end

Offline MLabuda

  • Sr. Newbie
  • *
  • Posts: 36
  • Karma: +0/-2
Re: Combining LUA codes
« Reply #1 on: January 02, 2017, 11:44:31 pm »
I'll be the first to admit I'm not an expert with LUA code.  I'm sure it's possible to achieve what you want to do with code alone, but as you said yourself, you should embrace PLEG.  It really makes things much easier and I highly recommend it.

Having said that, I don't think you can define a local variable twice within the same code, so having
Code: [Select]
local allow =in there twice probably won't work.
« Last Edit: January 03, 2017, 12:04:49 am by MLabuda »

Offline cybrmage

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1104
  • Karma: +113/-43
Re: Combining LUA codes
« Reply #2 on: January 03, 2017, 12:08:54 am »
Code: [Select]

function AllowBySwitchState()
  local dID = 57          -- Device ID of your VirtualSwitch
  local allow = false     -- true runs scene if switch is on, false blocks it
  local status = luup.variable_get("urn:upnp-org:serviceId:VSwitch1","Status",dID)
  return ((status == "1") == allow)
end

function AllowByDateRange()
  local mdStart = "10/15"  -- Start of period (MM/DD)
  local mdEnd = "12/31"    -- End of period (MM/DD)
  local allow = true       -- true runs scene during period, false blocks it
  local smS, sdS = string.match(mdStart,"(%d+)%/(%d+)")
  local smE, sdE = string.match(mdEnd,"(%d+)%/(%d+)")
  local mS = tonumber(smS)
  local dS = tonumber(sdS)
  local mE = tonumber(smE)
  local dE = tonumber(sdE)
  local tNow = os.date("*t")
  local mN = tNow.month
  local dN = tNow.day
  if (mE > mS) or ((mE == mS) and (dE >= dS)) then
       return (((mN > mS) or ((mN == mS) and (dN >= dS))) and ((mN < mE) or ((mN == mE) and (dN <= dE))) == allow)
  else
       return (((mN > mS) or ((mN == mS) and (dN >= dS))) or ((mN < mE) or ((mN == mE) and (dN <= dE))) == allow)
  end
end

return (AllowByDateRange() and AllowBySwitchState())

Offline 3rdStng

  • Full Member
  • ***
  • Posts: 119
  • Karma: +1/-0
Re: Combining LUA codes
« Reply #3 on: January 03, 2017, 01:23:08 am »

Thank you! I was semi-close with smashing the two together.