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

Getting the value for memory usage


natrimed

Question

Hi all

 

Trying to set an auto reboot if the Fibaro free memory gets too low. I am using the following script but get an error. Please let me know how to correct the error.

 

Lua code:

 

--[[
%% properties
%% time
%% events
%% globals
--]]

-- Function to check free memory percentage
local function checkFreeMemory()
    -- Get current free memory percentage
  
local totalMemory = fibaro:getGlobalValue("memoryTotal")
local freeMemory = fibaro:getGlobalValue("memoryFree")

local totalMemoryNum = tonumber(totalMemory)
local freeMemoryNum = tonumber(freeMemory)

local freeMemoryPercentage = (freeMemoryNum / totalMemoryNum) * 100
    fibaro:debug("Free memory percentage: " .. freeMemoryPercentage .. "%")
    
    -- Check if free memory is below the threshold (15%)
    if freeMemoryPercentage and freeMemoryPercentage < 70 then
        fibaro:debug("Free memory is below 15%. Pressing button 1 on virtual device 533...")
        fibaro:call(533, "pressButton", "1")
    else
        fibaro:debug("Free memory is within acceptable limits.")
    end
end

-- Function to schedule memory check at 3 AM every day
local function scheduleMemoryCheck()
    -- Schedule memory check at 3 AM
    local currentDate = os.date("*t")
    local checkTime = os.time({year=currentDate.year, month=currentDate.month, day=currentDate.day, hour=3, min=0, sec=0})
    local currentTime = os.time()

    -- Calculate time until next scheduled check
    local timeUntilCheck = checkTime - currentTime

    -- Schedule memory check
    if timeUntilCheck > 0 then
        fibaro:debug("Next memory check scheduled for 3 AM.")
        setTimeout(function()
            checkFreeMemory()
        end, timeUntilCheck * 1000)
    else
        fibaro:debug("Invalid check time. Please set a valid time.")
    end
end

-- Call the function to schedule memory check
scheduleMemoryCheck()
 

 

ERROR is:

 

[DEBUG] 18:15:01: 2024-05-11 18:15:01.004377 [ error] timer handler failed with error: /opt/fibaro/scenes/196.lua:18: attempt to perform arithmetic on local 'freeMemory' (a nil value)

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0
  • Inquirer
  • @Sankotronic @clowg

     

    Any idea with the above.

     

    I also tried the following scene but it also does not work. When I press the VD button it works but the scene does not seem to press the VD button.

     

    Thank you

     

    --[[
    %% properties
    %% events
    %% globals
    --]]

    --[[
      Press Button on Virtual Device at 3:15 AM Every Sunday
      This scene presses a button on a virtual device at 3:15 AM every Sunday.

    ]]

    local VIRTUAL_DEVICE_ID = <533> -- Replace <VIRTUAL_DEVICE_ID> with the ID of your virtual device

    function pressButton(deviceID)
      fibaro:call(deviceID, "pressButton", "1")
    end

    function isSundayAt3_15AM()
      local currentDate = os.date("*t")
      return (currentDate.wday == 1) and (currentDate.hour == 3) and (currentDate.min == 15)
    end

    function main()
      if isSundayAt3_15AM() then
        pressButton(VIRTUAL_DEVICE_ID)
      end
    end

    while true do
      main()
      fibaro:sleep(60 * 1000) -- Check every minute
    end

    Link to comment
    Share on other sites

    • 0

    Hello @natrimed ,

     

    Checking HC2 free memory and eventually rebooting it solution is provided with my UHAS for HC2. There is System Status VD that regularly checks HC2 memory and CPU status and connection to the internet. If free memory is too low then will either immediately reboot HC2 with Reboot scene or will send interactive push notifications asking you if you want to reboot HC2. If answer is YES then Remote reboot scene will reboot HC2.

     

    Since I have expanded HC2 RAM memory to 2Gb I never had this problem. With 116 z-wave devices, 148 VD's and over 100 scenes free memory is never less than 45% :-)

     

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Hi @Sankotronic

     

    Thanks for your help and letting me know about the reboot option in System Status VD in UHAS. 

     

    Just confirming that if I set "local red    = 5" in the main loop, then the system will reboot automatically if the free memory falls below 5%.

     

    Link to comment
    Share on other sites

    • 0

    Hi @natrimed ,

     

    Here is my settings from System status VD main loop:

    Please login or register to see this code.

     

    Thanks to your question I checked System Status VD code and found that yellow setting only sets VD icon, while red setting sends interactive push notification and popup notification. Unfortunately I found bug in code. Code is missing part to setup which scene to run with interactive push notification if user decide to reboot HC2. Popup notification is setup properly.

     

    Here is corrected System Status VD main loop code. Just paste it over and setup as required:

     

    Please login or register to see this attachment.

     

    For interactive push notification and HC2 rebooting to work UHAS ANC Notifications VD and scene and Remote reboot scene must be installed.

    You can also use code from UHAS and make your own solution.

     

    Thank you!

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Hi @Sankotronic

     

    Thanks for the updated VD.

     

    I set the values high to do a test and it works. Thank you.

     

    How often does the System Status main loop run?

    Link to comment
    Share on other sites

    • 0

    Hi @natrimed ,

     

    System Status VD refresh every 30 seconds by default. If it is set to be refreshed by FTBE scene and main loop in that case is stopped then it is refreshed every 1 minute.

     

    It can be set to refresh every 15 seconds by changing code line 31: local _rpt=27 to local _rpt=12, but since checking of internet connection can take up to 10 seconds (if online) to 2 minutes (if offline) it is not recommended.

    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...