Page 1 of 1

Inventory of devices

Posted: Wednesday 08 January 2025 22:21
by peterbos
Hi,

I want to make a csv file with an inventory of all devices in Domoticz. I wrote a dzVents script that finds all devices that are shown (used) on the tabs but not the ones that are hidden (unused) from the tabs:

Code: Select all

return {
    active = true, -- Activeer het script
    on = {
        timer = { 'every minute' } -- Voer het script elk uur uit (of pas dit naar wens aan)
    },
    execute = function(domoticz)
        -- Bestandspad waar de CSV wordt opgeslagen
        -- Open het bestand voor schrijven
        local file = io.open("/mnt/documents/Domoticz/apparaten.csv", "a+")

        if file then
            -- Schrijf de header van het CSV-bestand
            file:write('DeviceID;Name;Type;Hardware\n')

            -- Doorloop alle apparaten in Domoticz
            for i = 1, 250 do
                -- Haal apparaatdetails op
                if domoticz.utils.deviceExists(i) then
                    local device = domoticz.devices(i)
                    local deviceID = device.id
                    local name = device.name
                    local deviceType = device.deviceType
                    local hardware = device.hardwareName
                    -- Schrijf de gegevens naar het CSV-bestand
                    local s = deviceID .. ';' .. name ..';'.. deviceType ..';'.. hardware ..';'..'\n'
                    file:write(s)
                end
            end

            -- Sluit het bestand
            file:close()
        end
    end
}
How can I get the complete list of all devices, used or not used? A complete approach to get a csv file is also good.

Peter

Re: Inventory of devices

Posted: Wednesday 08 January 2025 23:14
by HvdW
Try this and look in the log.

Code: Select all

return {
    on = {
        devices = {'YourSwitch', 'YourSwitchSelector'}, 
        -- or timer = {'every 1 minutes'}
    },
    logging = {
        level = domoticz.LOG_INFO,
        marker = "Alle apparaten"
    },
    execute = function(domoticz)
        domoticz.devices().forEach(function(device)
            domoticz.log('Device ID: ' .. device.id .. ', Name: ' .. device.name .. ', Type: ' .. device.deviceType .. ', Hardware: ' .. device.hardwareName, domoticz.LOG_INFO)
        end)
    end
}
For experimenting I'm using YourSwitch and YourSwitchSelector to activate a test script.

Re: Inventory of devices

Posted: Thursday 09 January 2025 0:39
by waltervl
Why would you want that csv/Excel file on a regular base?
If it is just for one time copy the contents of page Setup - Devices with old fashioned select copy/paste into an Excel file.
Or open a recent database backup with sqlite browser and copy the contents of the device tables.
Another alternative is to use an API call for all devices and paste the resulting json in an Excel.

Re: Inventory of devices

Posted: Thursday 09 January 2025 8:37
by gizmocuz
Agreed, just use the JSON API to retrieve the list of devices/state in JSON and import this in Excel

Re: Inventory of devices

Posted: Sunday 12 January 2025 15:01
by peterbos
I used the JSON API call '/json.htm?type=command&param=getdevices&displayhidden=1'. In the wiki it also contains '&used=true' but if you add that it will filter out all unused devices. If you add '&used=false' it filters out all used devices. If you omit it, it does not filter and gives all devices. I converted the json file with an online converter to a csv file.

Thanks!
Peter