Page 1 of 1

Help for script dzvent

Posted: Sunday 19 September 2021 21:00
by bricololo
Hello,
I made a script in DZVENT.
It works as follows:
Triggering on a detection
Extraction of the list of sensors and the lastupdate values in seconds
Memorization in a table
In orders the array in ascending order on the lastupdate value

Code: Select all

return
{
    on =
    {
        devices = {               -- detection
            'myDetector1',
            'myDetector2'
    }
},

    execute = function(domoticz, detec)

     local message=""  
     local capteurs = {}      

     --FILTER ON DEVICE SWITCH TYPE
     local Detec_Devices = domoticz.devices().filter(function(device)
         return (device.switchType == 'Door Contact' or device.switchType == 'Motion Sensor')
     end)
    

     --EXTRACT DEVICES AND LASTUPDATE IN SEC
      Detec_Devices.forEach(function(deadDevice)  -- Derniere detection device
          local level=""
          level = tostring(deadDevice.lastUpdate.secondsAgo.." sec") -- affiche en sec
          local devname = tostring(deadDevice.name)          -- nom device
          message = message ..'- '..devname..' : '..level..'<br>'
          -- FILL TABLE
          table.insert(capteurs , num1 = level, val2 = devname)
      end)


          -- READ TABLE
          -- ORDER ASCENDING ON num1
          table.sort(capteurs, function(a,b) return a.num1 < b.num1 end)

          for i, capteurs in ipairs(widgets) do
              print("SENSORS : "..widget.num1);
          end

    end
}

I have an error on the storage in the table.
This ligne :
table.insert(capteurs , num1 = level, val2 = devname)
my_script_6.lua:31: ')' expected near '='
I will need help if possible.
Thank you

Re: Help for script dzvent

Posted: Monday 20 September 2021 8:58
by jannl
Looks like a } too few at de devices section.

Re: Help for script dzvent

Posted: Tuesday 21 September 2021 8:52
by rrozema
No, the indentation is a little bit off but those { } are matched.

I don't really get what you're trying to achieve here, but I think this code does what you were trying to write:

Code: Select all

return
{
    on =
    {
        devices = {
            'myDetector1',
            'myDetector2'
        }
    },

    execute = function(domoticz, detec)

        local message=""  
        local capteurs = {}      

        --FILTER ON DEVICE SWITCH TYPE AND INSERT THEM ORDERED BY THEIR lastUpdate
        local Detec_Devices = domoticz.devices().forEach(
            function(device)
                if (device.switchType == 'Door Contact' or device.switchType == 'Motion Sensor') then
                    if nil == device.lastUpdate then
                        domoticz.log( 'Ignoring device ' .. device.name .. ' because it does not have a valid lastUpdate.', domoticz.LOG_WARNING)
                    else
                        local i = 1
                        while i <= #capteurs and device.lastUpdate.secondsAgo < capteurs[i].lastUpdate.secondsAgo do
                            i = i + 1
                        end
                        table.insert( capteurs, i, device )
                    end
                end
            end
        )
     
        local j = 1
        while j <= #capteurs do
            print("SENSORS : ".. capteurs[j].name .. ', last updated ' .. tostring(capteurs[j].lastUpdate.secondsAgo) .. ' seconds ago.');
            j = j + 1
        end
    end
}

Re: Help for script dzvent

Posted: Tuesday 21 September 2021 14:05
by bricololo
thank you so much. :D :D :D
I've been trying to do this for days.
It's great.

Re: Help for script dzvent

Posted: Saturday 25 September 2021 20:13
by rrozema
What does it do? I mean, why do you need to know in which order the devices were last triggered?

Re: Help for script dzvent

Posted: Saturday 25 September 2021 22:05
by bricololo
It's for a OLED display alarm.
It's send me the activate device in the ordrer.

Re: Help for script dzvent

Posted: Saturday 25 September 2021 22:25
by rrozema
Ah. Ok.☺️