Page 1 of 1
trigger distiction in a script: physical button or script?
Posted: Wednesday 05 June 2024 19:22
by rugspin
Hi all,
I would like to combine a shelly 1 and a physical switch (inside the house) and a light+PIR (outside the house). The light should be switched on/off by the PIR. But when I use the physical switch inside, the light should stay on until I switch it off with physical switch.
For this, I would need to get the info in a dzvents script, what triggered the switch (the physical switch or a dzvents script from the PIR).
I looked through the dzvents wiki, but could not exactly figure out which object or attribute would have this info.
I would be looking for a statement to replace the parts
'triggered by physical button'
'triggered by the PIR'
in the script below. I would be glade about any help,
best regards
Code: Select all
return
{
on =
{
devices = { 'switch.test' }
},
execute = function(domoticz, switch)
if (switch.state == 'On' ) then
domoticz.log('Hey! Switch was set to on!')
if( 'triggered by physical button' ) then
-- stay switched on and ignore the PIR
elseif( 'triggered by the PIR' ) then
-- stay on for just 3 min
elseif (switch.state == 'Off' ) then
domoticz.log('Hey! Switch was set to off!')
-- recognize PIR again
else
end
end
}
Re: trigger distiction in a script: physical button or script?
Posted: Thursday 06 June 2024 5:02
by rugspin
Now, I figured out a little how to check the event triggers by the isCustomEvent, isDevice, isGroup, ... functions of the item object.
If I click in the domoticz gui on a switch_01 it would tell: isDevice is True
If I use a dzvent script from a second switch_02 and use domoticz.devices('switch_01').switchON() than also: isDevice is True
But I never got any info from the item object:
item.trigger was always NIL
So it looks to me as if it is not really possible within dzvents to figure out exactly which device triggered another, but maybe I did not fully understand the event handling in dzvents?
Re: trigger distiction in a script: physical button or script?
Posted: Thursday 06 June 2024 8:31
by Doler
The way I solved this is to capture the status as changed by the script in dzVents data (data = { savedState = { initial = 'Off' }}). Whenever the script changes the state, change the savedState accordingly. So when you use a physical button this savedState remains unchanged. Then in your script you can test the current state versus the savedState. If unequal it was changed by the physical button.
Re: trigger distiction in a script: physical button or script?
Posted: Thursday 06 June 2024 8:56
by habahabahaba
I think you can use device.name method
Smth like
Code: Select all
execute = function(domoticz, device)
if (device.name =='name of your physical switch' and device.state == 'On' ) then
-- doing smth
elseif (device.name =='light+PIR sensonr' and device.state == 'On') then
-- doing smth else
end
end
}
Re: trigger distiction in a script: physical button or script?
Posted: Thursday 06 June 2024 11:13
by FlyingDomotic
If I correctly remember, change originator is kept in device log (at least for switches). Yo ucan see it clicking on "log" on a switch widget.
Problem with scripts is the fact that you'll get only "EventSystem//home/xxxx/domoticz/dzVents/runtime/dzVents.lua" instead of name of effectively executed script. But in your case, you probably can make the difference between "User (IP)" written here when this a is user initialed command.
That said, I don't known how to extract this information from database log relation (if someone knowns, I'm interrested).
Re: trigger distiction in a script: physical button or script?
Posted: Thursday 06 June 2024 11:20
by habahabahaba
FlyingDomotic wrote: ↑Thursday 06 June 2024 11:13
If I correctly remember, change originator is kept in device log (at least for switches). Yo ucan see it clicking on "log" on a switch widget.
Problem with scripts is the fact that you'll get only "EventSystem//home/xxxx/domoticz/dzVents/runtime/dzVents.lua" instead of name of effectively executed script. But in your case, you probably can make the difference between "User (IP)" written here when this a is user initialed command.
That said, I don't known how to extract this information from database log relation (if someone knowns, I'm interrested).
I'm on Win10 and that logs are empty

May be that is only my problem but this solution will not work in my case/
Re: trigger distiction in a script: physical button or script?
Posted: Thursday 06 June 2024 11:26
by FlyingDomotic
I don't use Windows, I don't known ...
Re: trigger distiction in a script: physical button or script?
Posted: Thursday 06 June 2024 12:29
by rugspin
Thanks for the suggestions, I will try if I can make use of the above.
What I thought was to set a boolean variable 'controlled_by_physical_switch' in a dzvents script whenever the physical button switches on.
Than the a second dzvents script for the PIR checks the 'controlled_by_physical' variable and can only act if it is False. Now the PIR script needs to use .silent() to switch the light switch on. In this case the first script is not triggered and does not change the 'controlled_by_physical_switch' variable.
Re: trigger distiction in a script: physical button or script?
Posted: Thursday 06 June 2024 17:22
by habahabahaba
Code: Select all
-- TEST
return {
on = {
timer = {
'every 1 minutes' -- just an example set as often as you need
},
httpResponses = {
'GettingDevInfo' -- must match with the callback passed to the openURL command
}
},
execute = function(domoticz, item)
local devID = 90 -- ID of your switch
local dzURL = 'http://127.0.0.1:8081'
local APIURL = dzURL..'/json.htm?type=command¶m=getlightlog&idx='..devID -- for text device URL: /json.htm?type=command¶m=gettextlog&idx=IDX
if (item.isTimer) then
domoticz.openURL({
url = APIURL,
method = 'GET',
callback = 'GettingDevInfo', -- see httpResponses above.
}).afterSec(1)
end
if (item.isHTTPResponse) then
if (item.ok) then
if (item.isJSON) then
--domoticz.utils.dumpTable(item.json) -- необязательный элемент.
local userInfo = item.json.result[1].User
local lastDate = item.json.result[1].Date
local pattern = "%d+%.%d+%.%d+%.%d+"
local ipAddress = string.match(userInfo, pattern)
if ipAddress == nil then
ipAddress = 'no info'
end
domoticz.log('Device info: '.. domoticz.devices(devID).name, domoticz.LOG_ERROR)
domoticz.log('Date: '.. lastDate, domoticz.LOG_ERROR)
domoticz.log('User: '.. userInfo, domoticz.LOG_ERROR)
domoticz.log('ipAddress: '.. ipAddress, domoticz.LOG_ERROR)
end
else
domoticz.log('LOG_ERROR', domoticz.LOG_ERROR)
end
end
end
}