Device.changed not working as expected

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

Moderator: leecollings

Post Reply
delcara
Posts: 11
Joined: Wednesday 07 November 2018 10:48
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10171
Location: Netherlands
Contact:

Device.changed not working as expected

Post by delcara »

Hi all,

I've a dummy switch which is called "Beweging_Woonkamer". It's updated by 2 PIR's and is automatically switched off after 20 minutes. Now I have a light script for the dining room and sitting room and want to only execute the script if the status from "Beweging_Woonkamer" is changed (In my logic On > On is no status change). Because of the PIR's multiple On commands are sent if there is movement. I thought this could be achieved with the device.changed but this is not working. If I write the status of the device.change to the domzoticz log then its true even if I have multiple On commands and no change to off. Is my thinking wrong or does device.changed not work this way. Is there another solution to solve this?

Code: Select all

return {
    active = true,
	on = {	
	    devices =   {   'Beweging_Woonkamer',
	                    'Beweging_Eetkamer'     }
        },
	                
	execute = function(dz, device)
		if (device.name == 'Beweging_Woonkamer') and device.changed then
		    if (device.state == 'Off') then
		    dz.groups('Lampen_Woonkamer').switchOff()
	        elseif (dz.time.matchesRule('at nighttime') and device.state == 'On') then
	            if dz.devices('Harmony-PowerOff').state == 'On' then
	                dz.scenes('Avond').switchOn()
	                dz.log('Avond is ingeschakeld')
                else
                    dz.scenes('TV_verlichting').switchOn()
                    dz.log ('TV verlichting is ingeschakeld')
                    dz.log (device.changed)
                end
            end
        end
    
        if (device.name == 'Beweging_Eetkamer') and device.changed then
		    if (device.state == 'Off') then
		    dz.groups('Lampen_Eetkamer').switchOff()
	        elseif (dz.time.matchesRule('at nighttime') and device.state == 'On') then
	            dz.groups('Lampen_Eetkamer').switchOn()
            end
        end
	end
}
Raspberry pi 3, Domoticz Beta, Nginx, dzVents 2.7.4, RFXtrx433e, P1 smart meter, Harmony, Hue, Zwave, Amazon echo, Sonos, Synology DS413, HomeBridge, HA Bridge, Ubiquiti.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Device.changed not working as expected

Post by waaren »

delcara wrote: Monday 19 November 2018 21:16 I've a dummy switch which is called "Beweging_Woonkamer". It's updated by 2 PIR's and is automatically switched off after 20 minutes. Now I have a light script for the dining room and sitting room and want to only execute the script if the status from "Beweging_Woonkamer" is changed (In my logic On > On is no status change). Because of the PIR's multiple On commands are sent if there is movement. I thought this could be achieved with the device.changed but this is not working. If I write the status of the device.change to the domzoticz log then its true even if I have multiple On commands and no change to off. Is my thinking wrong or does device.changed not work this way. Is there another solution to solve this?
Nothing wrong with your thinking but the dzVents changed method does not work as you expect. If you need to remember the state of a PIR or switch device you have to use persistent data. Below is an example of how that could look like .
Please note that the script has to run once to initialize the persistent data before it will become useful.

Code: Select all

return {

    on = { devices =   {   1157, 1158 }},
        
    data = { beweging = { initial = {} }},    
                    
    execute = function(dz, device)
        PIR_Woonkamer = dz.devices(1157)
        PIR_Eetkamer  = dz.devices(1158)
        
        if next(dz.data.beweging) == nil then -- Empty table so initialize
            dz.data.beweging[PIR_Woonkamer.name] = PIR_Woonkamer.state  
            dz.data.beweging[PIR_Eetkamer.name]  = PIR_Eetkamer.state  
        end
        
        if device.state ~= dz.data.beweging[device.name] then
            dz.log("device " .. device.name .. "(".. device.id .. ") was updated and did change state")
            dz.data.beweging[device.name] = device.state
        else
            dz.log("device " .. device.name .. "(".. device.id .. ") was updated but did not change state")
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
delcara
Posts: 11
Joined: Wednesday 07 November 2018 10:48
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10171
Location: Netherlands
Contact:

Re: Device.changed not working as expected

Post by delcara »

Thnx again, fixed it with a variable. I wanted to implement this to sent unnecessary commands (a lot due to the PIR's) in my zwave network. For people interested below is de code.

Code: Select all

return {
    active = true,
	on = {	
	    devices =   {   'Beweging_Woonkamer',
	                    'Beweging_Eetkamer'     }},
        
        data =      { beweging = { initial = {} }},    
	                
	execute = function(dz, device)
	    
	    PIR_Woonkamer = dz.devices('Beweging_Woonkamer')
        PIR_Eetkamer  = dz.devices('Beweging_Eetkamer')
        
        if next(dz.data.beweging) == nil then -- Empty table so initialize
            dz.data.beweging[PIR_Woonkamer.name] = PIR_Woonkamer.state  
            dz.data.beweging[PIR_Eetkamer.name]  = PIR_Eetkamer.state  
        end
	    
		if (device.name == 'Beweging_Woonkamer') and device.state ~= dz.data.beweging[device.name] then
		    dz.log("device " .. device.name .. "(".. device.id .. ") was updated and did change state")
            dz.data.beweging[device.name] = device.state
		    if (device.state == 'Off') then
		    dz.groups('Lampen_Woonkamer').switchOff()
	        elseif (dz.time.matchesRule('at nighttime') and device.state == 'On') then
	            if dz.devices('Harmony-PowerOff').state == 'On' then
	                dz.scenes('Avond').switchOn()
	                dz.log('Avond is ingeschakeld')
                else
                    dz.scenes('TV_verlichting').switchOn()
                    dz.log ('TV verlichting is ingeschakeld')
                end
            end
        else
            dz.log("device " .. device.name .. "(".. device.id .. ") was updated but did not change state")
        end
    
        if (device.name == 'Beweging_Eetkamer') and device.state ~= dz.data.beweging[device.name] then
            dz.log("device " .. device.name .. "(".. device.id .. ") was updated and did change state")
            dz.data.beweging[device.name] = device.state
		    if (device.state == 'Off') then
		    dz.groups('Lampen_Eetkamer').switchOff()
	        elseif (dz.time.matchesRule('at nighttime') and device.state == 'On') then
	            dz.groups('Lampen_Eetkamer').switchOn()
            end
        else
            dz.log("device " .. device.name .. "(".. device.id .. ") was updated but did not change state")
        end
	end
}
Raspberry pi 3, Domoticz Beta, Nginx, dzVents 2.7.4, RFXtrx433e, P1 smart meter, Harmony, Hue, Zwave, Amazon echo, Sonos, Synology DS413, HomeBridge, HA Bridge, Ubiquiti.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest