We have moved at community.getvera.com

Author Topic: Why won't this code work  (Read 222 times)

Offline m0jon

  • Full Member
  • ***
  • Posts: 113
  • Karma: +0/-0
  • @m0jon
Why won't this code work
« on: January 03, 2019, 12:23:12 pm »
Trying to get a scene to only run between two time periods. Ive just moved to UI7 from UI5 where the code below used to work!!!!

Code: [Select]
local function checkTime()
local pStart = "09:00"   -- Start of time period
local pEnd = "21:00"     -- 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

Offline rigpapa

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1121
  • Karma: +187/-3
Re: Why won't this code work
« Reply #1 on: January 03, 2019, 12:28:19 pm »
If this is the entire body of code in your scene, it won't work because all it does is define a function, it doesn't execute it. Do you have "return checkTime()" after the part you've posted?
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 m0jon

  • Full Member
  • ***
  • Posts: 113
  • Karma: +0/-0
  • @m0jon
Re: Why won't this code work
« Reply #2 on: January 03, 2019, 12:34:38 pm »
Thank you that's all i needed to get the grey matter working again. All working now....
Code: [Select]
local function checkTime()
local pStart = "20:00"   -- Start of time period
local pEnd = "07:30"     -- 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 lul_tmp = luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1" , "CurrentTemperature",84)

if checkTime() and (tonumber(lul_tmp) <36)

then

luup.call_action("urn:micasaverde-com:serviceId:HomeAutomationGateway1", "RunScene", { SceneNum= "79"}, 0)

else return false
end

Offline rigpapa

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1121
  • Karma: +187/-3
Re: Why won't this code work
« Reply #3 on: January 03, 2019, 01:04:11 pm »
By the way, the "allow" variable doesn't work the way the comments say, given the implementation. What it will do is invert the sense of the function, so rather than disabling the scene altogether, it will run the scene in the opposite period (that is, when allow=true, your function will return true between 2000 and 0730, but when allow=false, it will return true between 0730 and 2000 and the scene will thus run).

Edit: times to match most recent post
« Last Edit: January 03, 2019, 01:06:17 pm by rigpapa »
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.