Concatenate string through a loop  [Solved]

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

Moderator: leecollings

Post Reply
zavjah
Posts: 36
Joined: Tuesday 13 August 2019 10:20
Target OS: Raspberry Pi / ODroid
Domoticz version:

Concatenate string through a loop

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

Re: Concatenate string through a loop

Post 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
}

Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
zavjah
Posts: 36
Joined: Tuesday 13 August 2019 10:20
Target OS: Raspberry Pi / ODroid
Domoticz version:

Re: Concatenate string through a loop  [Solved]

Post by zavjah »

Hi waaren,

thanks here again, it work perfectly :-)

Cheers,
zavjah
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest