iterate through a number of devices  [Solved]

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
MeAlbert
Posts: 65
Joined: Friday 06 December 2019 14:23
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Contact:

iterate through a number of devices

Post 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)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: iterate over a number of devices

Post 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.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
MeAlbert
Posts: 65
Joined: Friday 06 December 2019 14:23
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Contact:

Re: iterate through a number of devices  [Solved]

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

Who is online

Users browsing this forum: Bing [Bot] and 1 guest