Page 1 of 1

One script to match multiple devices

Posted: Monday 15 June 2020 12:45
by shketiev
Hello,

I have about 200 panic buttons connected to my domoticz with mysensors gateways. These are push buttons , but I added every button as a door contact , because of automatic release. I want to send notification when button is pressed and I can make it from notification section of every single one , but I want to make a script that matches all of them and send notifications when button is pressed , 1 script for all. I set the names for example: PANIC - 1 , PANIC - 2 , PANIC - 3 and so on ... And the script have to be something like:

commandArray = {}

if (devicechanged['PANIC ???something*'] == 'Closed') then
commandArray['SendNotification']='.. devicename button pressed'
end

return commandArray

Please anyone help with that.....

Re: One script to match multiple devices

Posted: Monday 15 June 2020 13:19
by jvdz
Something like this?:

Code: Select all

commandArray = {}
for dev, status in pairs(devicechanged) do
	-- Check Panicbutton pressed
	if (dev:sub(1, 6) == 'PANIC '] and status == 'Closed') then
		commandArray['SendNotification']= dev ..' button pressed'
	end
end
return commandArray
Jos

Re: One script to match multiple devices

Posted: Monday 15 June 2020 13:49
by shketiev
jvdz wrote: Monday 15 June 2020 13:19 Something like this?:

Code: Select all

commandArray = {}
for dev, status in pairs(devicechanged) do
	-- Check Panicbutton pressed
	if (dev:sub(1, 6) == 'PANIC '] and status == 'Closed') then
		commandArray['SendNotification']= dev ..' button pressed'
	end
end
return commandArray
Jos
Thank you very much ! Yes , exactly like this , but:

It returns error for some reason , Domoticz 2020.2
Error: EventSystem: in PANIC Button: [string "commandArray = {} ..."]:4: ')' expected near ']'

Re: One script to match multiple devices

Posted: Tuesday 16 June 2020 7:21
by jake
shketiev wrote:
jvdz wrote: Monday 15 June 2020 13:19 Something like this?:

Code: Select all

commandArray = {}
for dev, status in pairs(devicechanged) do
	-- Check Panicbutton pressed
	if (dev:sub(1, 6) == 'PANIC '] and status == 'Closed') then
		commandArray['SendNotification']= dev ..' button pressed'
	end
end
return commandArray
Jos
Thank you very much ! Yes , exactly like this , but:

It returns error for some reason , Domoticz 2020.2
Error: EventSystem: in PANIC Button: [string "commandArray = {} ..."]:4: ')' expected near ']'
Yes, because of the ] behind the word panic? Please remove.

Re: One script to match multiple devices

Posted: Tuesday 16 June 2020 11:08
by shketiev
I noticed that , and I actually see that I have posted previous error. After I remove ] after PANIC ' the error is on line 2.

Error: EventSystem: in PANIC Button: [string "commandArray = {} ..."]:2: bad argument #1 to 'pairs' (table expected, got nil)

I don't why this error appears (every 60 seconds), but the script actually works and notifications is received correctly.

Re: One script to match multiple devices

Posted: Tuesday 16 June 2020 11:57
by waaren
shketiev wrote: Tuesday 16 June 2020 11:08 Error: EventSystem: in PANIC Button: [string "commandArray = {} ..."]:2: bad argument #1 to 'pairs' (table expected, got nil)
I don't why this error appears (every 60 seconds), but the script actually works and notifications is received correctly.
You should save the Lua script as triggered on Device. You now probably saved it to be triggered on 'All'

top right corner of the edit window..
trigger.png
trigger.png (5.12 KiB) Viewed 951 times

Re: One script to match multiple devices

Posted: Tuesday 16 June 2020 12:33
by shketiev
waaren wrote: Tuesday 16 June 2020 11:57
shketiev wrote: Tuesday 16 June 2020 11:08 Error: EventSystem: in PANIC Button: [string "commandArray = {} ..."]:2: bad argument #1 to 'pairs' (table expected, got nil)
I don't why this error appears (every 60 seconds), but the script actually works and notifications is received correctly.
You should save the Lua script as triggered on Device. You now probably saved it to be triggered on 'All'

top right corner of the edit window..

trigger.png
YEAH , that works , no more errors. Thank you very much !

Re: One script to match multiple devices

Posted: Saturday 25 July 2020 15:36
by shketiev
Just wondering , how can I add some limiting option for example:

If button is pressed , send notification , but once a minute , or every X time.
Now when someone press the button multiple times , it floods the system :(

Thanks !

Re: One script to match multiple devices

Posted: Saturday 25 July 2020 17:01
by waaren
shketiev wrote: Saturday 25 July 2020 15:36 If button is pressed , send notification , but once a minute , or every X time.
Now when someone press the button multiple times , it floods the system :(
Could look like

Code: Select all

commandArray = {}

    local function string2Epoch(dateString, pattern)
        local pattern = pattern or '(%d+)-(%d+)-(%d+) (%d+):(%d+):(%d+)'
        local year, month, day, hour, minute, seconds = dateString:match(pattern)
        return os.time({year = year, month = month, day = day , hour = hour, min = minute, sec = seconds })
    end

    for dev, status in pairs(devicechanged) do

        if dev:sub(1, 5) == 'PANIC' and status == 'Closed' then
            local secondsAgo = os.time() - string2Epoch(otherdevices_lastupdate[dev])
            if secondsAgo > 60 then
                commandArray['SendNotification']= dev ..' button was pressed before ' .. secondsAgo .. ' seconds Ago'
            else
                 print('No notification send. Last update of ' .. dev .. ' was ' .. secondsAgo .. ' seconds Ago')
            end
            -- commandArray[#commandArray+1] = {[dev] = 'Open NOTRIGGER'} -- Don't know if this is needed and if so it works. Just try after confirming the other part does work
        end
    end

return commandArray