Help for script dzvent

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

Moderator: leecollings

Post Reply
bricololo
Posts: 13
Joined: Tuesday 18 February 2014 11:51
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: France
Contact:

Help for script dzvent

Post 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
jannl
Posts: 673
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Geleen
Contact:

Re: Help for script dzvent

Post by jannl »

Looks like a } too few at de devices section.
rrozema
Posts: 470
Joined: Thursday 26 October 2017 13:37
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Delft
Contact:

Re: Help for script dzvent

Post 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
}
bricololo
Posts: 13
Joined: Tuesday 18 February 2014 11:51
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: France
Contact:

Re: Help for script dzvent

Post by bricololo »

thank you so much. :D :D :D
I've been trying to do this for days.
It's great.
rrozema
Posts: 470
Joined: Thursday 26 October 2017 13:37
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Delft
Contact:

Re: Help for script dzvent

Post by rrozema »

What does it do? I mean, why do you need to know in which order the devices were last triggered?
bricololo
Posts: 13
Joined: Tuesday 18 February 2014 11:51
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: France
Contact:

Re: Help for script dzvent

Post by bricololo »

It's for a OLED display alarm.
It's send me the activate device in the ordrer.
rrozema
Posts: 470
Joined: Thursday 26 October 2017 13:37
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Delft
Contact:

Re: Help for script dzvent

Post by rrozema »

Ah. Ok.☺️
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest