Author Topic: Script only works if functions are global ??  (Read 185 times)

Offline parkerc

  • Sr. Hero Member
  • ******
  • Posts: 2366
  • Karma: +33/-45
  • Life Moves Pretty Fast....
    • Node Central
Script only works if functions are global ??
« on: September 22, 2017, 10:56:52 am »
HI

Hopefully a quick one for someone to help me out on.

I have the following code running within a scene triggered when a particular time is reached.

Code: [Select]
local vol = 20
local dFirst = 2         -- Start day of period (1-7) Monday = 2
local dLast = 6         -- End day of period (1-7) Friday = 6
local allow = true       -- true runs scene during period, false blocks it
local tNow = os.date("*t")
local dNow = tNow.wday
local vswitch = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1","Status",110)

   if dNow == 1 or dNow == 7 then
     print ("Today is a weekend so do not run")
   return false
else
   print ("Today is a weekday so you can run")
   Polly ('Commencing kids bedtime countdown routine... This is your 10 minute bedtime countdown notification', vol )
   luup.call_delay( 'call_polly', 300)  -- next one to be 5 minute later
   end
   
function call_polly()
   local vswitch = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1","Status",110)
      print (vswitch)
      if vswitch == "1" then
        Polly('Bedtime countdown has been aborted', vol)
      return false
  else
     Polly('There are now 5 minutes before bedtime - so make them count!', vol)
     luup.call_delay( 'call_polly1', 120)  -- next 2 minute, 120 seconds delay   
     end 
   end   

function call_polly1()
   local vswitch = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1","Status",110)
      print (vswitch)
      if vswitch == "1" then
        Polly('Bedtime countdown has been aborted', vol)
      return false
  else   
   Polly('There are now 3 minutes before bedtime. Countdown is progressing, 2 minutes, 56 seconds and counting', vol)
   luup.call_delay( 'call_polly2', 120)  -- next 2 minute, 120 seconds delay
 end
 end

function call_polly2()
  local vswitch = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1","Status",110)
      print (vswitch)
      if vswitch == "1" then
        Polly('Bedtime countdown has been aborted', vol)
      return false
  else
   Polly('There is only 1 minute to go now. Thats not long at all, so start to get ready!', vol)
   luup.call_delay( 'call_polly3', 120)  -- next 1 minute, 60 seconds delay
 end
 end

function call_polly3()
     local vswitch = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1","Status",110)
      print (vswitch)
      if vswitch == "1" then
        Polly('Bedtime countdown has been aborted', vol)
      return false
  else
   Polly('Thats it - its now time for bed. help each other upstairs!', vol)
 end
 end

I noticed that all the functions are declared as global, so I thought it would be better to make them all local, so I prefixed the word "local" to the front of each function..

But, in doing that the above code does not work ? Is that to be expected ? Is the  luup.call_delay to a local function different to a global one ?

Returning them to being global again and it works fine ?

Offline akbooer

  • Master Member
  • *******
  • Posts: 5274
  • Karma: +227/-67
  • "Less is more"
Re: Script only works if functions are global ??
« Reply #1 on: September 22, 2017, 11:08:38 am »
The callback functions, names as strings in call_delay(), have to be global. Nothing else does, but you must make sure local functions are defined before you call them.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P.
Razberry, MySensors Arduino, HomeWave, AltUI, DataYours, openLuup, ZWay, ZeroBrane Studio.