Page 1 of 1

iterate through a number of devices

Posted: Thursday 12 November 2020 16:34
by MeAlbert
I have a number of device of which I want to push data to influx every minute.
I think I should use "domoticz.devices().forEach(function(device)"
But how do I get this function to perform for instance on device 3, 6 and 10?

devices = 3,6,10
domoticz.devices().forEach(function(device)

Or domoticz.devices(3,6,10).forEach(function(device)

And do I understand it correctly that when I use the correct function the execute(dz,device) is not needed anymore?
To push to influx I want to use this code

Code: Select all

	local function pushToInfluxdb(idx , nValue, rawData )
            local sValue = table.concat(rawData,';')
            local nValue = nValue or 0
           
            local url = dz.settings['Domoticz url'] ..  '/json.htm?type=command&param=udevice&idx=' .. idx  ..
                        '&nvalue=' .. nValue ..
                        '&svalue=' .. sValue
            dz.openURL(url)
            
        end
        
            pushToInfluxdb( item.idx, 0, item.rawData )
            dz.log('Device ' .. item.name ..  item.rawData[1], dz.LOG_DEBUG)

Re: iterate over a number of devices

Posted: Thursday 12 November 2020 17:09
by waaren
MeAlbert wrote: Thursday 12 November 2020 16:34 I have a number of device of which I want to push data to influx every minute.
I think I should use "domoticz.devices().forEach(function(device)"
forEach is one part but you need to filter first. See below script.

Code: Select all


return
{
    on =
    {
        timer =
        {
            'every minute' ,
        },
    },

    logging =
    {
        level = domoticz.LOG_ERROR, -- change to domoticz.LOG_DEBUG when not working as expected
        marker = 'forcePushToInfluxdb with filter Example',
    },

    execute = function(dz)

        local myInfluxDevices =
        {
            3,6,10,
        }

        local function forcePushToInfluxdb(dv)
            dz.log('Forcing push to influxDb of ' .. dv.name, dz.LOG_FORCE)

            local url = dz.settings['Domoticz url'] ..  '/json.htm?type=command&param=udevice&idx=' .. dv.idx  ..
                    '&nvalue=' .. ( dv.nValue or 0  ) ..
                    '&svalue=' .. ( dv.sValue or '' )
            dz.openURL(url)
        end

        dz.devices().filter(myInfluxDevices).forEach(function(dv)
            forcePushToInfluxdb(dv)
        end)
    end
}
And do I understand it correctly that when I use the correct function the execute(dz,device) is not needed anymore?
No. In a dzVents script you always need the execute function. That function is called by the dzVents framework when one or more of the conditions in the on = section evaluate to true. The 2nd and 3rd parameters of the execute function are optional and not needed here.

Re: iterate through a number of devices  [Solved]

Posted: Thursday 12 November 2020 22:04
by MeAlbert
waaren
Thanks for the help.
Getting the devices through filtering I could not have come to that. But now the iteration process is clear to me.

Hope other people also benefit from it. It works like a charm.