Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


  • 0

Set global variable from scene


BErdmann

Question

Hi, iam trying to set a global variable between open and closed, from this scene that checks the doors, but i can´t get it to work, any suggestions?

 

-- Scene File    : Door-Windows-Monitoring.lua
-- Purpose       : Check doors and windows at the given interval.
-- Version       : 0.2.6-en
-- Release Date  : 18 Jan. 2021
-- Trigger       : autostart
-- License       : Copyleft {jwi} 
 
-- Either you enter all the door / window IDs in line 29, or you
-- let the script run in automatic mode. Save the line 29, so
-- you do not have to pick all the IDs again,



 
local Debug = function ( color, message ) 
   fibaro.debug(string.format('<%s style="color:%s;">%s'"span", color, message, "span") )
end
 
local manually    = true -- | true
if manually then
   devices = {632,338--enter your device IDs here
   print('Checking is in manual mode.')
else  
   devices = fibaro.getDevicesID({ enabled = true, visible = true, baseType = "com.fibaro.doorWindowSensor" })
   print('Checking is in automatic mode.' )
end 
 
local interval    = 60      -- Interval after which the first push message is sent.
local numPushMsg  = 3       -- number of push messages
local pushAgain   = 3600    -- Activate pushMsg again after specified time. 1h
local IDtempOut   = 3       -- IMPORTANT! ID of your outside temperatur sensor (e.g. Yahoo)
local TempOutType = 'Temperature' -- IMPORTANT! 'Temperature' or 'value'
local excludedev  = {} --This device(s) are exclcuded temporary from check
local ignoredev   = {410,393}      -- This devices are never checked
local begPeriod   = "08:00" -- During this period, no check is performed on the 
local endPeriod   = "20:30" -- sensors contained in the variable "excludedev".
local tThreshold  = 15   -- nn° Celsius
local newInterval = 1    -- minutes to increase or decrease interval counter  
local phoneID     = {407,451}   -- mobile phone-IDs 
-- local siren       =     -- Neo Coolcam optional device id for sound output after 'numPushMsg' 
local debug       = true  -- | true  -- Debug messages yes/no 
local advdebug    = false  -- | true  -- advanced debug
local soundOn     = false
 
-- No changes needed below this line.
local version = '0.2.6'
local pushMsg = 1
local warning = 1
local counter = 0  
local lastopen = 0
local prevwarning = 0
 
print("Check for open doors and windows... Version ".. version)
 
if (fibaro.getValue(IDtempOut, TempOutType)) == nil then
   Debug('red''No outside temperature detected!')
   Debug('red''Please correct. Scene is canceled!'
   fibaro.abort() 
end    
 
local function validate(phoneID) 
  local ios = api.get('/iosDevices')
  local nID = 0
  for n = 1,#phoneID do
     for _,i in pairs(ios) do
       if phoneID[n] == i.id then
          nID = nID+1
       end 
     end --for
  end --for
  if #phoneID ~= nID then print('Warning - unknown PhoneID detected, please correct!'end
end --validate()
 
local function excludecheck(excludedev, devid)
  if (os.date("%H:%M", os.time())) > begPeriod and (os.date("%H:%M", os.time())) < endPeriod then
     -- workaround for checking time after midnight 
     -- or (os.date("%H:%M", os.time())) > "00:00" and (os.date("%H:%M", os.time())) < "06:30"  then
     for i=1,#excludedev do
        if excludedev[i] == devid then 
           return true
        end
     end --for
  end  
  return false
end  
 
local function ignorecheck(ignoredev, devid)
  for i=1,#ignoredev do
        if ignoredev[i] == devid then 
           return true
        end
     end --for
  return false
end  
 
local function tempcheck()
  local outsideTemp = tonumber( fibaro.getValue(IDtempOut, TempOutType));
  local stringTemp = string.format("%.2f", outsideTemp)
  if outsideTemp > tThreshold then
      currInterval = interval + newInterval
    elseif  outsideTemp < tThreshold  then
      currInterval = interval - newInterval   
  else
      currInterval = interval    
  end  
  if (currInterval < 5then 
     currInterval = 5
  end  
  return stringTemp, currInterval
end
 
local p = validate(phoneID)
local outsideTemp = tempcheck()
 
print("Outside temperature is " .. tempcheck() .. '°C')
print("Current Interval is: " .. currInterval) 
if not debug then print('Debug is disabled.'end;
 
function checkOpenDW()
  local outsideTemp,curInterval = tempcheck()
  local currentDate = os.date("*t");
  local datum = os.date("%d.%m.%Y")
  counter = counter + 1
  devopen = 0
  for id = 1, #devices do
     if (fibaro.getValue(devices[id], 'value')) == true then
        local name  = fibaro.getName(devices[id])
        local room  = fibaro.getRoomNameByDeviceID(devices[id])
        if excludecheck(excludedev, devices[id]) ~= true 
           and ignorecheck(ignoredev,devices[id]) ~= true then      
           devopen = devopen + 1
           status  = " Warning! " ..name.. " in room " ..room.. " is open! Temp. = " .. outsideTemp.."°"
           print(datum .. status)
           warning = warning + 1
        end  
     end
 
     if (warning == prevwarning and warning > 0then
         -- Door/window was closed
         warning = 1; counter = 1; prevwarning = 0; pushMsg = 1
     end     
     
     if (warning == 0then 
        -- No door/window open, restart counter 
         counter = 1; prevwarning = 0; pushMsg = 1
     end
      
     if (counter > currInterval+1 and warning > 0then 
         -- Door/windows was opened and closed, resstart warnings   
         counter = 1; warning = 1; prevwarning = 0; pushMsg =1;
     end 
  end --for
  if devopen == 0 then
     counter = 1; warning = 1; prevwarning = 0; pushMsg =1;
     if debug then print('All doors and windows are closed.'end;
  end  
  prevwarning = prevwarning +1  
  -- Door/windows  open longer than interval. Send push. 
  if counter > currInterval then
     if (devopen > 1then
        status = status .. ' Total: '.. devopen 
     end
     if pushMsg <= numPushMsg then
        print(datum .. ' Door/window for more than '.. curInterval ..' minutes open, send push Nr. '..pushMsg) 
        status = status .. ',Push-Nr.: ' .. pushMsg
        if pushMsg == numPushMsg then status = status ..' Latest!' 
           lastopen = devopen
           if soundOn then
              fibaro.call(398'turnOn'-- Siren / sounder
              end
        end 
        pushMsg = pushMsg + 1
        uxtime = os.time(dt);
        for k=1, #phoneID do
           if phoneID[k] ~= nil then
              fibaro.call(phoneID[k], 'sendPush', status )
           end
        end --for
     end
     warning = 1; counter = 1; prevwarning = 0
  end 
  
  if advdebug then Debug('withe''Counter: '.. counter) end;
  if advdebug then Debug('withe''Warning: '.. warning) end;
  if advdebug then Debug('withe''PreWarning: '.. prevwarning) end;
  if advdebug then Debug('withe''PushMsg : '.. pushMsg) end;
  if advdebug then Debug('cyan''Device open: ' .. devopen) end
  if advdebug then Debug('cyan''Last  open: ' .. lastopen) end
  
  if pushMsg > numPushMsg and lastopen ~= devopen then
     pushMsg = 1 ; lastopen = 0
  end 
  if pushMsg > numPushMsg and (os.time() - uxtime) >  pushAgain and pushAgain > 0 then 
     pushMsg = 1 
    end
    if devopen > 1 then hub.setGlobalVariable("Door""Open");
    if devopen == 0 then hub.setGlobalVariable("Door""Closed");
end
end
 
    
   
   fibaro.setTimeout(60000, checkOpenDW)
    end --function
checkOpenDW()
Link to comment
Share on other sites

7 answers to this question

Recommended Posts

  • 0

I think there will be a problem with the two “end” what belongs to the if in the line of the hub.setGlobalVariable

Link to comment
Share on other sites

  • 0

I Just took a quick lok at this code, and I assume this is on HC3, so

First of all, i think in the line  

if devopen > 1 then hub.setGlobalVariable("Door""Open");  You should have >= operator not >, unless you dont want to be notifed if theres only 1 device opened..

also i think fibaro.abort is not a working function anymore (i think),

and the push notification call  fibaro.call(phoneID[k], 'sendPush', status ) is incorrect too. At least i always use the way i show below.

Sending push is done by fibaro.alert("push",IDS table,text)..

Edited by Neo Andersson
Link to comment
Share on other sites

  • 0
  • Inquirer
  • Hi, the scene runs without errors, i get push notifications, but the global variable are not changing.

     

     

     

    Link to comment
    Share on other sites

    • 0
    1 hour ago, BErdmann said:

    Hi, the scene runs without errors, i get push notifications, but the global variable are not changing.

     

     

     

    Yes. I know. This is why I wrote that there is a problem with the “end”s.

    Link to comment
    Share on other sites

    • 0

    No, the code is syntactically correct (but a bit strangely indented)
    I would guess that you have a runtime error and you don't see it because and error inside a function called by setTimeout doesn't always show up in the log...
    Test this - put this code first in your code. It redefines setTimeout to catch the error and report it. See if you get an error in the console...
     

    Please login or register to see this code.

     

    Link to comment
    Share on other sites

    • 0
    11 hours ago, jgab said:

    No, the code is syntactically correct (but a bit strangely indented)
    I would guess that you have a runtime error and you don't see it because and error inside a function called by setTimeout doesn't always show up in the log...
    Test this - put this code first in your code. It redefines setTimeout to catch the error and report it. See if you get an error in the console...
     

    Please login or register to see this code.

     

    The syntax is correct, but there is a logical problem at the end of the scene. 

    There are two setGlobalVariable with two if.

    The first if is not closed with an end befor the secound if starts, so the global variable never will modified to “Closed”

    Link to comment
    Share on other sites

    • 0
    4 hours ago, Milan Takacs said:

    The syntax is correct, but there is a logical problem at the end of the scene. 

    There are two setGlobalVariable with two if.

    The first if is not closed with an end befor the secound if starts, so the global variable never will modified to “Closed”

    Ah, I see it now too - yes that’s probably it

    Link to comment
    Share on other sites

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Answer this question...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

    ×
    ×
    • Create New...