Page 1 of 1

Concatenate string through a loop

Posted: Thursday 12 September 2019 14:15
by zavjah
Hi,

I'd like to loop my devices and collect the names of those that are active and concatenate them I to one string. Looping and getting names is working but I am having trouble to put them I to one string and print it to log.

I'd like to something like this:

d.devices().filter(Geräte).forEach(function(device)
if (device.state=='On') then
string==string .. device.name
end
end
d.log(string)

How can I achieve this?
Thx,
zavjah

Re: Concatenate string through a loop

Posted: Thursday 12 September 2019 21:05
by waaren
zavjah wrote: Thursday 12 September 2019 14:15 I'd like to loop my devices and collect the names of those that are active and concatenate them I to one string. Looping and getting names is working but I am having trouble to put them I to one string and print it to log.
How can I achieve this?
Try this

Code: Select all

return 
{
    on = 
    {
        timer = 
        {
            'every minute',              -- causes the script to be called every minute
        },
    },
    execute = function(dz)
        local string = ''
        local myList = {2381, 2382, 2383, "mySensor" } -- List of idx or "names" of your devices
        
        dz.devices().filter(myList).forEach(function(dv)
            dz.log("Found: " .. dv.name .. "; nValue=" .. dv.nValue .. "; sValue=" .. dv.sValue,dz.LOG_DEBUG)
             if dv.state =='On' then
                 string = string .. '\n' .. dv.name .. ', '
            end
        end) 
        dz.log('All devices from table myList with state = "On" : ' ..  string, dz.LOG_FORCE)
    
    end
}


Re: Concatenate string through a loop  [Solved]

Posted: Friday 13 September 2019 10:02
by zavjah
Hi waaren,

thanks here again, it work perfectly :-)

Cheers,
zavjah