One script to match multiple devices Topic is solved

Moderator: leecollings

Post Reply
shketiev
Posts: 5
Joined: Friday 12 June 2020 9:40
Target OS: Linux
Domoticz version:
Contact:

One script to match multiple devices

Post 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.....
User avatar
jvdz
Posts: 2335
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: One script to match multiple devices

Post 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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
shketiev
Posts: 5
Joined: Friday 12 June 2020 9:40
Target OS: Linux
Domoticz version:
Contact:

Re: One script to match multiple devices

Post 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 ']'
jake
Posts: 744
Joined: Saturday 30 May 2015 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

Re: One script to match multiple devices

Post 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.
shketiev
Posts: 5
Joined: Friday 12 June 2020 9:40
Target OS: Linux
Domoticz version:
Contact:

Re: One script to match multiple devices

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

Re: One script to match multiple devices

Post 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 942 times
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
shketiev
Posts: 5
Joined: Friday 12 June 2020 9:40
Target OS: Linux
Domoticz version:
Contact:

Re: One script to match multiple devices

Post 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 !
shketiev
Posts: 5
Joined: Friday 12 June 2020 9:40
Target OS: Linux
Domoticz version:
Contact:

Re: One script to match multiple devices

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

Re: One script to match multiple devices

Post 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

Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest