trigger distiction in a script: physical button or script?

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

Moderator: leecollings

Post Reply
rugspin
Posts: 12
Joined: Tuesday 04 February 2020 23:59
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

trigger distiction in a script: physical button or script?

Post 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

}
rugspin
Posts: 12
Joined: Tuesday 04 February 2020 23:59
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: trigger distiction in a script: physical button or script?

Post 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?
Doler
Posts: 145
Joined: Friday 31 July 2015 21:02
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Sint-Oedenrode, Netherlands
Contact:

Re: trigger distiction in a script: physical button or script?

Post 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.
Mark: Domoticz Beta on Raspberry Pi 4 running Debian Bookworm - Z-Stick 7 - RFXCom - P1 - MySensors - SolarEdge - Dahua - Philips Hue - Docker - Zigbee2mqtt (plugin) - Zwave-js-ui - dzVents - Nodered
User avatar
habahabahaba
Posts: 232
Joined: Saturday 18 March 2023 14:44
Target OS: Windows
Domoticz version: 2024.4
Contact:

Re: trigger distiction in a script: physical button or script?

Post 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
}
FlyingDomotic
Posts: 349
Joined: Saturday 27 February 2016 0:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: trigger distiction in a script: physical button or script?

Post 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).
User avatar
habahabahaba
Posts: 232
Joined: Saturday 18 March 2023 14:44
Target OS: Windows
Domoticz version: 2024.4
Contact:

Re: trigger distiction in a script: physical button or script?

Post 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/
FlyingDomotic
Posts: 349
Joined: Saturday 27 February 2016 0:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: trigger distiction in a script: physical button or script?

Post by FlyingDomotic »

I don't use Windows, I don't known ...
rugspin
Posts: 12
Joined: Tuesday 04 February 2020 23:59
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: trigger distiction in a script: physical button or script?

Post 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.
User avatar
habahabahaba
Posts: 232
Joined: Saturday 18 March 2023 14:44
Target OS: Windows
Domoticz version: 2024.4
Contact:

Re: trigger distiction in a script: physical button or script?

Post 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&param=getlightlog&idx='..devID -- for text device URL: /json.htm?type=command&param=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
}
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest