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

List offline and try to reconnect to them


Question

Posted (edited)

Hi, i am trying to make a scene that goes true all my devices and alert me if some are dead/offline.

 

fibaro.debug("SCENE294", "Start")
 
-- Configuration
local emailRecipient = "[email]"
local offlineDevices = {} -- Local variable to store offline devices
local onlineCount = 0 -- Variable to count online devices
 
-- Function to send email
local function sendEmail(subject, body)
    fibaro.call(2, "sendEmail", emailRecipient, subject, body)
end
 
-- Function to list offline devices, count online ones, and attempt to wake up dead devices
local function listDevicesStatus()
    offlineDevices = {} -- Clear the offline devices list
    onlineCount = 0 -- Reset online count
 
    local devices = api.get("/devices")
 
    for _, device in ipairs(devices) do
        local deviceStatus = "Online"
 
        -- Check if the device is dead or not responding
        if device.properties and device.properties.dead then
            deviceStatus = "Offline"
            table.insert(offlineDevices, device)
        elseif device.interfaces and #device.interfaces > 0 then
            -- Some devices might have interfaces indicating connectivity issues
            for _, interface in ipairs(device.interfaces) do
                if interface == "notResponding" then
                    deviceStatus = "Offline"
                    table.insert(offlineDevices, device)
                    break
                end
            end
        end
 
        if deviceStatus == "Online" then
            onlineCount = onlineCount + 1
        end
    end
 
    -- Display the number of online devices
    fibaro.debug("SCENE294", "Number of online devices: " .. onlineCount)
 
    -- Display the number of offline devices
    local offlineCount = #offlineDevices
    fibaro.debug("SCENE294", "Number of offline devices: " .. offlineCount)
 
    -- If there are offline devices, attempt to wake them up and list them
    if offlineCount > 0 then
        fibaro.debug("SCENE294", "Offline Devices Report:")
        for _, device in ipairs(offlineDevices) do
            fibaro.debug("SCENE294", "ID: " .. device.id .. ", Name: " .. device.name .. " - Attempting to wake up...")
            fibaro.wakeUpDeadDevice(device.id)
            fibaro.debug("SCENE294", "Waking up device ID: " .. device.id .. " - " .. device.name)
        end
    else
        fibaro.debug("SCENE294", "No offline devices found.")
    end
 
    -- Recheck devices after 5 minutes
    fibaro.setTimeout(5 * 60 * 1000, function() recheckDevices() end)
end
 
-- Function to recheck device status after delay
function recheckDevices()
    local stillOfflineDevices = {}
 
    for _, device in ipairs(offlineDevices) do
        local currentDevice = api.get("/devices/" .. device.id)
        if currentDevice and currentDevice.properties and currentDevice.properties.dead then
            table.insert(stillOfflineDevices, currentDevice)
        elseif currentDevice.interfaces and #currentDevice.interfaces > 0 then
            for _, interface in ipairs(currentDevice.interfaces) do
                if interface == "notResponding" then
                    table.insert(stillOfflineDevices, currentDevice)
                    break
                end
            end
        end
    end
 
    if #stillOfflineDevices > 0 then
        local body = "The following devices are still offline:\n"
        for _, device in ipairs(stillOfflineDevices) do
            body = body .. "ID: " .. device.id .. ", Name: " .. device.name .. "\n"
        end
        sendEmail("Offline Devices Alert", body)
    end
end
 
-- Run the function to list all devices and report their status
listDevicesStatus()
 
fibaro.debug("SCENE294", "done")

any suggested improvements?
Edited by Forall

1 answer to this question

Recommended Posts

  • 0
Posted

Hi @Forall Thanks for sharing your code. After some testing I made some adjustments and now it seems to be trying to wake up all the dead nodes.  I had 16 dead nodes and the log shows 16 attempts of waking up all those devices.  Please note that there are some error messages when you run the code, but I think this version might serve for our pouposes: 

 

fibaro.debug("41", "Start")
 
-- Configuration
local emailRecipient = "[[email protected]]"
local offlineDevices = {} -- Local variable to store offline devices
local onlineCount = 0 -- Variable to count online devices
 
-- Function to send email
local function sendEmail(subject, body)
    fibaro.call(2, "sendEmail", emailRecipient, subject, body)
end
 
-- Function to list offline devices, count online ones, and attempt to wake up dead devices
local function listDevicesStatus()
    offlineDevices = {} -- Clear the offline devices list
    onlineCount = 0 -- Reset online count
 
    local devices = api.get("/devices")
 
    for _, device in ipairs(devices) do
        local deviceStatus = "Online"
 
        -- Check if the device is dead or not responding
        if device.properties and device.properties.dead then
            deviceStatus = "Offline"
            table.insert(offlineDevices, device)
        elseif device.interfaces and #device.interfaces > 0 then
            -- Some devices might have interfaces indicating connectivity issues
            for _, interface in ipairs(device.interfaces) do
                if interface == "notResponding" then
                    deviceStatus = "Offline"
                    table.insert(offlineDevices, device)
                    break
                end
            end
        end
 
        if deviceStatus == "Online" then
            onlineCount = onlineCount + 1
        end
    end
 
    -- Display the number of online devices
    fibaro.debug("SCENE294", "Number of online devices: " .. onlineCount)
 
    -- Display the number of offline devices
    local offlineCount = #offlineDevices
    fibaro.debug("SCENE294", "Number of offline devices: " .. offlineCount)
 
    -- If there are offline devices, attempt to wake them up and list them
    if offlineCount > 0 then
        fibaro.debug("SCENE294", "Offline Devices Report:")
        for _, device in ipairs(offlineDevices) do
            fibaro.debug("SCENE294", "ID: " .. device.id .. ", Name: " .. device.name .. " - Attempting to wake up...")
            fibaro.wakeUpDeadDevice(device.id)
            fibaro.debug("SCENE294", "Waking up device ID: " .. device.id .. " - " .. device.name)
        end
    else
        fibaro.debug("SCENE294", "No offline devices found.")
    end
 
    -- Recheck devices after 5 minutes
    fibaro.setTimeout(5 * 60 * 1000, function() recheckDevices() end)
end
 
-- Function to recheck device status after delay
function recheckDevices()
    local stillOfflineDevices = {}
 
    for _, device in ipairs(offlineDevices) do
        local currentDevice = api.get("/devices/" .. device.id)
        if currentDevice and currentDevice.properties and currentDevice.properties.dead then
            table.insert(stillOfflineDevices, currentDevice)
        elseif currentDevice.interfaces and #currentDevice.interfaces > 0 then
            for _, interface in ipairs(currentDevice.interfaces) do
                if interface == "notResponding" then
                    table.insert(stillOfflineDevices, currentDevice)
                    break
                end
            end
        end
    end
 
    if #stillOfflineDevices > 0 then
        local body = "The following devices are still offline:\n"
        for _, device in ipairs(stillOfflineDevices) do
            body = body .. "ID: " .. device.id .. ", Name: " .. device.name .. "\n"
        end
        sendEmail("Offline Devices Alert", body)
    end
end
 
-- Run the function to list all devices and report their status

listDevicesStatus()
 
fibaro.debug("41", "done")

Please login or register to see this code.

 

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